• (二)Lucene之根据关键字搜索文件


    • 前提:在使用lucene进行搜索的时候,必须先生成索引文件,即必须先进行上一章节的案例,生成索引文件如下:

    • 该索引文件为"segments"开头,如果没有该文件则说明没有索引文件则报错:org.apache.lucene.index.IndexNotFoundException: no segments* file found in SimpleFSDirectory@E:luceneindex lockFactory=org.apache.lucene.store.NativeFSLockFactory@87aac27: files: [_0.cfe, _0.cfs, _0.si, write.lock]
    • 搜索

    package com.shyroke.lucene;
    
    import java.io.IOException;
    import java.nio.file.Paths;
    
    import org.apache.lucene.analysis.Analyzer;
    import org.apache.lucene.analysis.standard.StandardAnalyzer;
    import org.apache.lucene.document.Document;
    import org.apache.lucene.index.DirectoryReader;
    import org.apache.lucene.index.IndexReader;
    import org.apache.lucene.index.StandardDirectoryReader;
    import org.apache.lucene.queryparser.classic.ParseException;
    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.apache.lucene.store.Directory;
    import org.apache.lucene.store.SimpleFSDirectory;
    
    public class Search {
    
        /**
         * 根据关键字检索文件
         * 
         * @param indexDir
         *            存放索引的目录
         * @param key
         *            关键字
         * @throws IOException
         * @throws ParseException 
         */
        public static void search(String indexDir, String key) throws IOException, ParseException {
            Directory directory = new SimpleFSDirectory(Paths.get(indexDir));
            IndexReader reader = DirectoryReader.open(directory);
    
            Analyzer analyzer=new StandardAnalyzer();
            QueryParser queryParser=new QueryParser("fileContents", analyzer);
            Query query=queryParser.parse(key);
            
            
            IndexSearcher searcher = new IndexSearcher(reader);
    
            long startTime=System.currentTimeMillis();
            TopDocs topDocs=searcher.search(query, 10);
            long endTime=System.currentTimeMillis();
            System.out.println("匹配    "+key+"   总共花费:"+(endTime-startTime)+"毫秒,查询到"+topDocs.totalHits+"条记录");
            
            for(ScoreDoc scoreDoc:topDocs.scoreDocs) {
                Document document=searcher.doc(scoreDoc.doc);
                System.out.println(document.get("filePath"));
            }
            
        }
        
        
        public static void main(String[] args) {
            String indexDir="E:\lucene\index";
            String key="Zygmunt#Saloni";
            try {
                Search.search(indexDir, key);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }

    结果:

    • 此时的关键字是“Zygmunt#Saloni” ,查询结果是在LICENSE.txt中,但是该文件中并没有这个内容,但是有

    这样也会匹配到,这是分词器StandardAnalyzer在起作用。

  • 相关阅读:
    Vmware 添加虚拟磁盘
    2019-2020-2 《网络对抗技术》 Exp3 免杀原理与实践
    Docker 容器更换软件源
    Docker 查看容器 Linux 版本
    OpenMediaVault 5 进阶配置(四) Portainer 管理 Docker
    Portainer 中文文档:部署
    树莓派 部署 Docker 数据库容器
    通过Cookie统计上次网页访问时间
    用JavaMail通过QQ邮箱来发送邮件(第一篇博客,备忘)
    删掉双系统
  • 原文地址:https://www.cnblogs.com/shyroke/p/7905266.html
Copyright © 2020-2023  润新知