zoukankan      html  css  js  c++  java
  • lucene&solr查询索引实例

    1.实现步骤:

     2.代码

        // 搜索索引
        @Test
        public void testSearch() throws Exception {
            // 第一步:创建一个Directory对象,也就是索引库存放的位置。
            Directory directory = FSDirectory.open(new File("D:\temp\index"));// 磁盘
            // 第二步:创建一个indexReader对象,需要指定Directory对象。
            IndexReader indexReader = DirectoryReader.open(directory);
            // 第三步:创建一个indexsearcher对象,需要指定IndexReader对象
            IndexSearcher indexSearcher = new IndexSearcher(indexReader);
            // 第四步:创建一个TermQuery对象,指定查询的域和查询的关键词。
            Query query = new TermQuery(new Term("fileName", "lucene"));
            // 第五步:执行查询。TopDocs对象表示查询到的多条文档的信息
            TopDocs topDocs = indexSearcher.search(query, 10);
            // 第六步:返回查询结果。遍历查询结果并输出。
            ScoreDoc[] scoreDocs = topDocs.scoreDocs;
            for (ScoreDoc scoreDoc : scoreDocs) {
                int doc = scoreDoc.doc;
                Document document = indexSearcher.doc(doc);
                // 文件名称
                String fileName = document.get("fileName");
                System.out.println(fileName);
                // 文件内容
                String fileContent = document.get("fileContent");
                System.out.println(fileContent);
                // 文件大小
                String fileSize = document.get("fileSize");
                System.out.println(fileSize);
                // 文件路径
                String filePath = document.get("filePath");
                System.out.println(filePath);
                System.out.println("------------");
            }
            // 第七步:关闭IndexReader对象
            indexReader.close();
    
        }
  • 相关阅读:
    tensorflow之tf.meshgrid()
    Python中数据的保存和读取
    透视投影推导
    tensorflow之tf.slice()
    tensorflow的tf.train.Saver()模型保存与恢复
    偶数分割求平均值
    母牛的故事
    统计一行的不重复的单词字符个数
    N个顶点构成多边形的面积
    贪心法基本入门
  • 原文地址:https://www.cnblogs.com/ibcdwx/p/13525830.html
Copyright © 2011-2022 走看看