springboot集成Lucene详细使用

以下是 Spring Boot 集成 Lucene 的详细步骤:

添加依赖

在 Spring Boot 项目的 pom.xml 文件中添加 Lucene 的依赖,常用的核心依赖和中文分词器依赖如下:

XML 复制代码
<dependency>
    <groupId>org.apache.lucene</groupId>
    <artifactId>lucene-core</artifactId>
    <version>8.11.0</version>
</dependency>
<dependency>
    <groupId>org.apache.lucene</groupId>
    <artifactId>lucene-analyzers-common</artifactId>
    <version>8.11.0</version>
</dependency>
<dependency>
    <groupId>org.wltea</groupId>
    <artifactId>ik-analyzer</artifactId>
    <version>20200623</version>
</dependency>

创建配置类

创建一个配置类,对 Lucene 的相关组件进行配置,如索引目录、分词器等:

java 复制代码
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.nio.file.Paths;

@Configuration
public class LuceneConfig {

    private final String indexPath = "indexDir"; // 索引存储路径

    @Bean
    public Directory directory() throws Exception {
        return FSDirectory.open(Paths.get(indexPath));
    }

    @Bean
    public Analyzer analyzer() {
        return new StandardAnalyzer(); // 可替换为其他分词器,如 IKAnalyzer
    }
}

创建实体类

根据实际需求创建一个实体类,用于表示要索引的文档对象,例如:

java 复制代码
public class Book {
    private String id;
    private String title;
    private String author;
    private String content;
    // 省略getter、setter等方法
}

创建索引服务类

创建一个服务类,用于处理索引相关的操作,如创建索引、添加文档、删除文档等:

java 复制代码
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class LuceneIndexService {

    @Autowired
    private Directory directory;

    @Autowired
    private Analyzer analyzer;

    // 创建索引
    public void createIndex(List<Book> bookList) throws IOException {
        IndexWriterConfig config = new IndexWriterConfig(analyzer);
        IndexWriter writer = new IndexWriter(directory, config);
        for (Book book : bookList) {
            Document doc = new Document();
            doc.add(new TextField("id", book.getId(), Field.Store.YES));
            doc.add(new TextField("title", book.getTitle(), Field.Store.YES));
            doc.add(new TextField("author", book.getAuthor(), Field.Store.YES));
            doc.add(new TextField("content", book.getContent(), Field.Store.YES));
            writer.addDocument(doc);
        }
        writer.close();
    }

    // 添加文档到索引
    public void addDocument(Book book) throws IOException {
        IndexWriterConfig config = new IndexWriterConfig(analyzer);
        IndexWriter writer = new IndexWriter(directory, config);
        Document doc = new Document();
        doc.add(new TextField("id", book.getId(), Field.Store.YES));
        doc.add(new TextField("title", book.getTitle(), Field.Store.YES));
        doc.add(new TextField("author", book.getAuthor(), Field.Store.YES));
        doc.add(new TextField("content", book.getContent(), Field.Store.YES));
        writer.addDocument(doc);
        writer.close();
    }

    // 删除文档
    public void deleteDocument(String id) throws IOException {
        IndexWriterConfig config = new IndexWriterConfig(analyzer);
        IndexWriter writer = new IndexWriter(directory, config);
        writer.deleteDocuments(new Term("id", id));
        writer.forceMergeDeletes();
        writer.close();
    }
}

创建搜索服务类

创建一个服务类,用于处理搜索相关的操作,如简单搜索、高亮搜索等:

java 复制代码
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.search.highlight.*;
import org.apache.lucene.store.Directory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class LuceneSearchService {

    @Autowired
    private Directory directory;

    @Autowired
    private Analyzer analyzer;

    // 简单搜索
    public List<Document> search(String queryStr) throws Exception {
        DirectoryReader reader = DirectoryReader.open(directory);
        IndexSearcher searcher = new IndexSearcher(reader);
        QueryParser parser = new QueryParser("content", analyzer);
        Query query = parser.parse(queryStr);
        TopDocs results = searcher.search(query, 10);
        List<Document> docs = new ArrayList<>();
        for (ScoreDoc scoreDoc : results.scoreDocs) {
            docs.add(searcher.doc(scoreDoc.doc));
        }
        reader.close();
        return docs;
    }

    // 高亮搜索
    public List<Map<String, String>> searchWithHighlight(String queryStr) throws Exception {
        DirectoryReader reader = DirectoryReader.open(directory);
        IndexSearcher searcher = new IndexSearcher(reader);
        QueryParser parser = new QueryParser("content", analyzer);
        Query query = parser.parse(queryStr);
        TopDocs results = searcher.search(query, 10);
        List<Map<String, String>> docs = new ArrayList<>();

        SimpleHTMLFormatter htmlFormatter = new SimpleHTMLFormatter("<span style='color:red'>", "</span>");
        Highlighter highlighter = new Highlighter(htmlFormatter, new QueryScorer(query));

        for (ScoreDoc scoreDoc : results.scoreDocs) {
            Document doc = searcher.doc(scoreDoc.doc);
            String content = doc.get("content");
            TokenStream tokenStream = analyzer.tokenStream("content", new StringReader(content));
            String highlightedText = highlighter.getBestFragment(tokenStream, content);

            Map<String, String> docMap = new HashMap<>();
            docMap.put("id", doc.get("id"));
            docMap.put("title", doc.get("title"));
            docMap.put("author", doc.get("author"));
            docMap.put("content", highlightedText != null ? highlightedText : content);
            docs.add(docMap);
        }
        reader.close();
        return docs;
    }
}

创建控制器类

创建一个控制器类,用于处理 HTTP 请求,并调用相应的服务类方法:

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private LuceneIndexService luceneIndexService;

    @Autowired
    private LuceneSearchService luceneSearchService;

    // 创建索引
    @PostMapping("/index")
    public String createIndex(@RequestBody List<Book> bookList) {
        try {
            luceneIndexService.createIndex(bookList);
            return "索引创建成功";
        } catch (IOException e) {
            e.printStackTrace();
            return "索引创建失败";
        }
    }

    // 搜索结果
    @GetMapping
    public List<Document> search(@RequestParam String query) {
        try {
            return luceneSearchService.search(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

    // 高亮搜索
    @GetMapping("/highlight")
    public List<Map<String, String>> searchWithHighlight(@RequestParam String query) {
        try {
            return luceneSearchService.searchWithHighlight(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }
}

使用示例

此外,还可以根据实际需求对上述代码进行扩展和优化,例如添加更复杂的查询条件、实现分页功能、优化索引的性能等。

  • 创建索引 :启动 Spring Boot 应用后,发送一个 POST 请求到http://localhost:8080/search/index,请求体中包含要索引的图书列表,如:

    bash 复制代码
    [
        {
            "id": "1",
            "title": " Lucene in Action ",
            "author": "Robert Muir",
            "content": "Lucene is a search library from Apache"
        },
        {
            "id": "2",
            "title": " Java编程思想 ",
            "author": "Bruce Eckel",
            "content": "Java is a programming language"
        }
    ]
  • 简单搜索 :发送一个 GET 请求到http://localhost:8080/search/?query=Java,即可搜索出与"Java"相关的文档。

  • 高亮搜索 :发送一个 GET 请求到http://localhost:8080/search/highlight/?query=Java,即可搜索出与"Java"相关的文档,并且搜索结果中的"Java"会以高亮显示。

相关推荐
代码老y38 分钟前
Spring Boot + MyBatis + Vue:全栈开发中的最佳实践
vue.js·spring boot·mybatis
Q_Q19632884751 小时前
python+uniapp基于微信小程序的高校二手商品交易系统
spring boot·python·微信小程序·django·flask·uni-app·node.js
萌新小码农‍2 小时前
SpringBoot新闻项目学习day3--后台权限的增删改查以及权限管理分配
spring boot·后端·学习
Luffe船长3 小时前
springboot将文件插入到指定路径文件夹,判断文件是否存在以及根据名称删除
java·spring boot·后端·spring
武昌库里写JAVA5 小时前
VUE vuex深入浅出
vue.js·spring boot·毕业设计·layui·课程设计
代码老y5 小时前
Spring Boot + MyBatis + Vue:从零到一构建全栈应用
vue.js·spring boot·mybatis
罗政5 小时前
小区物业管理系统源码+SpringBoot + Vue (前后端分离)
vue.js·spring boot·后端
vx Biye_Design5 小时前
SSM学生社团管理系统-计算机毕业设计源码75136
spring boot·sql·mysql·ajax·bootstrap·mybatis
篱笆院的狗6 小时前
Spring Boot 工程启动以后,我希望将数据库中已有的固定内容,打入到 Redis 缓存中,请问如何处理?
数据库·spring boot·缓存
飞翔的佩奇7 小时前
基于Spring+MyBatis+MySQL实现的监考安排与查询系统设计与实现(附源码+数据库)推荐!
java·数据库·mysql·spring·毕业设计·mybatis·监考安排与查询