zoukankan      html  css  js  c++  java
  • Lucene和Solr学习总结(2)

    创建完索引接下来当然要查一查啦,

     

    步骤先理好

    1,当然要先知道索引存放的位置

    2,通过IndexSearcher查找

    3,创建IndexSearcher需要提供IndexReader

    4,创建IndexReader 需要提供DirectoryDirectory对象指定索引位置

    后面就是添加查询条件和执行查询了

     

    public class QueryIndex {

     

    //搜索索引

    @Test

    public void testName() throws Exception {

    //1,创建Directory对象,获取索引存放的位置

    Directory directory = FSDirectory.open(new File("D:\lucene\index"));

    //2,创建IndexReader对象,指定Directory对象

    IndexReader indexReader = DirectoryReader.open(directory);

    //3,创建一个IndexSearcher对象,指定IndexReader

    IndexSearcher indexSearcher = new IndexSearcher(indexReader);

    //4,创建一个TermQuery对象,指定查询的域和关键字

    Query query = new TermQuery(new Term("fielName", "lucene"));

    //5,执行查询

    TopDocs search = indexSearcher.search(query, 2);

     //topDocs.scoreDocs存储了document对象的id

    ScoreDoc[] scoreDocsId = search.scoreDocs;

    //6,遍历结果,获得得到的索引的id

    for (ScoreDoc scoreDoc : scoreDocsId) {

    int doc = scoreDoc.doc;  //scoreDoc.doc属性就是document对象的id

    Document document = indexSearcher.doc(doc); //根据document的id找到document对象

    //从document对象中把域取出来

     //文件名称

                System.out.println(document.get("fielName"));

                //文件内容

              //  System.out.println(document.get("file_content"));

                //文件大小

                System.out.println(document.get("fileSize"));

                //文件路径

                System.out.println(document.get("filePath"));

                System.out.println("=============");

    }

    //7,关流

      indexReader.close();

    }

    }

  • 相关阅读:
    对象池使用时要注意几点
    Flash3D学习计划(一)——3D渲染的一般管线流程
    714. Best Time to Buy and Sell Stock with Transaction Fee
    712. Minimum ASCII Delete Sum for Two Strings
    647. Palindromic Substrings(马拉车算法)
    413. Arithmetic Slices
    877. Stone Game
    338. Counting Bits
    303. Range Sum Query
    198. House Robber
  • 原文地址:https://www.cnblogs.com/a-small-lyf/p/10008825.html
Copyright © 2011-2022 走看看