Skip to content
鼓励作者:欢迎打赏犒劳

02-springBoot整合lucene

application.properties

yaml
# application.properties
spring.lucene.index.path=/path/to/your/index-directory

LuceneConfig

java
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.nio.file.Paths;

@Configuration
public class LuceneConfig {

    @Value("${spring.lucene.index.path}")
    private String luceneIndexPath;

    @Bean
    public Directory luceneDirectory() throws IOException {
        return FSDirectory.open(Paths.get(luceneIndexPath));
    }

    @Bean
    public IndexWriter luceneIndexWriter(Directory directory) throws IOException {
        IndexWriterConfig config = new IndexWriterConfig(new StandardAnalyzer());
        return new IndexWriter(directory, config);
    }
    
    @Bean
    public IndexReader luceneIndexReader(Directory directory) throws IOException {
        return DirectoryReader.open(directory);
    }
    
    @Bean
    public IndexSearcher luceneIndexSearcher(IndexReader reader) {
        return new IndexSearcher(reader);
    }
}

LuceneIndexService

java
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class LuceneIndexService {

    @Autowired
    private IndexWriter indexWriter;

    public void indexDocument(String id, String title, String content) throws IOException {
        Document doc = new Document();
        doc.add(new StringField("id", id, Field.Store.YES));
        doc.add(new TextField("title", title, Field.Store.YES));
        doc.add(new TextField("content", content, Field.Store.YES));
        indexWriter.addDocument(doc);
        indexWriter.commit();
    }
}

LuceneSearchService

java
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LuceneSearchService {

    @Autowired
    private IndexSearcher indexSearcher;

    public void search(String queryStr) throws Exception {
        QueryParser parser = new QueryParser("content", new StandardAnalyzer());
        Query query = parser.parse(queryStr);
        TopDocs results = indexSearcher.search(query, 10);
        for (ScoreDoc scoreDoc : results.scoreDocs) {
            Document doc = indexSearcher.doc(scoreDoc.doc);
            // 处理搜索结果
            System.out.println("Found: " + doc.get("title"));
        }
    }
}

如有转载或 CV 的请标注本站原文地址