使用lucene实现简单的全文检索

本文是一个使用lucene为文本创建并管理索引,根据索引检索文本的简单全文搜索例子。

Manven依赖

<dependency>
  <groupId>org.apache.lucene</groupId>
  <artifactId>lucene-core</artifactId>
  <version>6.0.0</version>
</dependency>
<dependency>
  <groupId>org.apache.lucene</groupId>
  <artifactId>lucene-queryparser</artifactId>
  <version>6.0.0</version>
</dependency>
<dependency>
  <groupId>org.apache.lucene</groupId>
  <artifactId>lucene-analyzers-common</artifactId>
  <version>6.0.0</version>
</dependency>
<dependency>
  <groupId>commons-io</groupId>
  <artifactId>commons-io</artifactId>
  <version>2.5</version>
</dependency>

索引管理

创建索引

public static void createIndex(FileManage file){
//索引保存路径
    String indexRootPath = getIndexRoot();
    File f = new File(indexRootPath);
    if(!f.exists()){
        f.mkdirs();
    }

    String indexPath = indexRootPath+File.separator+File.separator;
    Directory indexDir = null;
    IndexWriter indexWriter = null;
    try{
        indexDir = FSDirectory.open(new File(indexPath).toPath());
        Analyzer analyzer = new StandardAnalyzer();
        IndexWriterConfig config = new IndexWriterConfig(analyzer);
        indexWriter = new IndexWriter(indexDir, config);

        File sourceFile = new File(sourceRootPath);
        String content = org.apache.commons.io.FileUtils.readFileToString(sourceFile, "utf-8");

        Document doc = new Document();
        doc.add(new TextField("fileName", file.getPath(), Field.Store.YES));
        doc.add(new TextField("content", content, Field.Store.YES));
        indexWriter.addDocument(doc);
        indexWriter.flush();
    }catch(Exception e){
        log.error("create index error|",e);
    }finally{
        if(null != indexWriter){
            try {
                indexWriter.close();
            } catch (IOException e) {
                log.error("",e);
            }
        }
    }
}

删除索引

public static void deleteIndex(FileManage file){
    String indexRootPath = getIndexRoot();
    File f = new File(indexRootPath);
    if(!f.exists()){
        f.mkdirs();
    }

    String indexPath = indexRootPath+File.separator;
    Directory indexDir = null;
    IndexWriter indexWriter = null;
    try{
        indexDir = FSDirectory.open(new File(indexPath).toPath());
        Analyzer analyzer = new StandardAnalyzer();
        IndexWriterConfig config = new IndexWriterConfig(analyzer);
        indexWriter = new IndexWriter(indexDir, config);
        indexWriter.deleteDocuments(new Term("fileName", file.getFileName()));
        indexWriter.flush();
    }catch(Exception e){
        log.error("create index error|",e);
    }finally{
        if(null != indexWriter){
            try {
                indexWriter.close();
            } catch (IOException e) {
                log.error("",e);
            }
        }
    }
}

更新索引

public static void updateIndex(FileManage file){
    String indexRootPath = getIndexRoot();
    String sourceRootPath = getSourceRoot();
    File f = new File(indexRootPath);
    if(!f.exists()){
        f.mkdirs();
    }

    String indexPath = indexRootPath+File.separator;
    Directory indexDir = null;
    IndexWriter indexWriter = null;
    try{
        indexDir = FSDirectory.open(new File(indexPath).toPath());
        Analyzer analyzer = new StandardAnalyzer();
        IndexWriterConfig config = new IndexWriterConfig(analyzer);
        indexWriter = new IndexWriter(indexDir, config);

        File sourceFile = new File(file.getPath());
        String content = org.apache.commons.io.FileUtils.readFileToString(sourceFile, "utf-8");

        Document doc = new Document();
        doc.add(new TextField("fileName", file.getFileName(), Field.Store.YES));
        doc.add(new TextField("content", content, Field.Store.YES));
        indexWriter.updateDocument(new Term("fileName", file.getFileName()),doc);
        indexWriter.flush();
    }catch(Exception e){
        log.error("create index error|",e);
    }finally{
        if(null != indexWriter){
            try {
                indexWriter.close();
            } catch (IOException e) {
                log.error("",e);
            }
        }
    }
}

根据索引检索

public static List<SearchResult> search(List<FileManage> sourcefiles, String param){
    if(XaUtil.isEmpty(sourcefiles)){
        return Collections.EMPTY_LIST;
    }
    List<SearchResult> result = new ArrayList<SearchResult>();
    String indexRootPath = getIndexRoot();
    File f = new File(indexRootPath);
    if(!f.exists()){
        f.mkdirs();
    }
    Directory indexDir = null;
    DirectoryReader ireader = null;
    try{
        Analyzer analyzer = new StandardAnalyzer();
        IndexWriterConfig config = new IndexWriterConfig(analyzer);
        String[] fieldStr = new String[]{"fileName", "content"};
        BooleanClause.Occur[] flags = {BooleanClause.Occur.MUST,
            BooleanClause.Occur.MUST};
        String indexPath = indexRootPath+File.separator;
        indexDir = FSDirectory.open(new File(indexPath).toPath());
        for(FileManage file : sourcefiles){
            ireader = DirectoryReader.open(indexDir);
            IndexSearcher isearcher = new IndexSearcher(ireader);
            String[] queryAry = new String[]{file.getUuidName(), param};
            Query query = MultiFieldQueryParser.parse(queryAry,fieldStr,flags,analyzer);
            ScoreDoc[] hits = isearcher.search(query,1000).scoreDocs;
            Document doc =null;
            for(ScoreDoc hit : hits){
                doc = isearcher.doc(hit.doc);
                SearchResult sr = new SearchResult();
                sr.setScore(hit.score);
                sr.setFileName(doc.get("fileName"));
                result.add(sr);
            }
        }
    }catch (Exception e){
        log.error("",e);
    }finally {
        if(null != indexDir){
            try {
                indexDir.close();
            } catch (IOException e) {
                log.error("",e);
            }
        }
        if(null != ireader){
            try {
                ireader.close();
            } catch (IOException e) {
                log.error("",e);
            }
        }
        return result;
    }
}

(完)



  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值