Lucene查询索引

Query子类:
           1.TermQuery:根据域和关键词进行搜索

private IndexSearcher indexSearcher;
    private IndexReader indexReader;
    @Before
    public void getSearcher() throws IOException {
        //1.创建Directory对象,指定索引库位置
        Directory directory = FSDirectory.open(new File("E:\Y2学习\Lucene\索引库\Index").toPath());
        //2.创建IndexReader对象,读取索引库内容
        indexReader= DirectoryReader.open(directory);
        //3.创建IndexSearcher对象
        indexSearcher=new IndexSearcher(indexReader);
    }
    /**
     * termQuery根据域和关键词进行搜索
     * @throws IOException
     */
    @Test
    public void termQuery() throws IOException {
        //创建查询条件
        Query query=new TermQuery(new Term("fieldName","new"));
        //执行查询
        TopDocs topDocs = indexSearcher.search(query, 10);
        System.out.println("返回的文档个数:"+topDocs.totalHits);

        //获取到文档集合
        ScoreDoc [] scoreDocs=topDocs.scoreDocs;
        for (ScoreDoc doc:scoreDocs) {
            //获取到文档
            Document document = indexSearcher.doc(doc.doc);
            //获取到文档域中数据
            System.out.println("fieldName:"+document.get("fieldName"));
            System.out.println("fieldPath:"+document.get("fieldPath"));
            System.out.println("fieldSize:"+document.get("fieldSize"));
            System.out.println("fieldContent:"+document.get("fieldContent"));
            System.out.println("==============================================================");
        }
        //关闭
        indexReader.close();
    }

2.RangeQuery:范围搜索

前提:创建文档时保存范围
            例:

 document.add(new LongPoint("fieldSize",456));
 document.add(new StoredField("fieldSize",456));
/**
                 * RangeQuery范围搜素
                 * @throws IOException
                 */
                @Test
                public void RangeQuery() throws IOException {
                    //设置范围搜索的条件 参数一范围所在的域
                    Query query=LongPoint.newRangeQuery("fieldSize",0,50);
                    //查询
                    TopDocs topDocs = indexSearcher.search(query, 10);
                    System.out.println("返回的文档个数:"+topDocs.totalHits);

                    //获取到文档集合
                    ScoreDoc [] scoreDocs=topDocs.scoreDocs;
                    for (ScoreDoc doc:scoreDocs) {
                        //获取到文档
                        Document document = indexSearcher.doc(doc.doc);
                        //获取到文档域中数据
                        System.out.println("fieldName:"+document.get("fieldName"));
                        System.out.println("fieldPath:"+document.get("fieldPath"));
                        System.out.println("fieldSize:"+document.get("fieldSize"));
                        System.out.println("fieldContent:"+document.get("fieldContent"));
                        System.out.println("==============================================================");
                    }

                    //关闭
                    indexReader.close();
                }

3.QueryParser:匹配一行数据,这一行数据会自动进行分词

例如查询:Lucene是一个开源的基于Java的搜索库  Lucene    一个   开源   基于......

会先将搜索内容分词

再根据分词查询
            使用方案:

    1.导入依赖,如果当前没有QueryParser依赖则需要导入依赖

    

<!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-queryparser -->
                    <dependency>
                        <groupId>org.apache.lucene</groupId>
                        <artifactId>lucene-queryparser</artifactId>
                        <version>7.4.0</version>
                    </dependency>

    2.测试编码:

/**
     * queryparser搜素,会将搜索条件分词
     * @throws IOException
     */
    @Test
    public void queryparser() throws IOException, ParseException {
        //创建一个QueryParser对象 参数一:查询的域  参数二:使用哪种分析器
        QueryParser parser=new QueryParser("fieldContent",new IKAnalyzer());
        //设置匹配的数据条件
        Query query = parser.parse("Lucene是一个开源的基于Java的搜索库");
        //查询
        TopDocs topDocs = indexSearcher.search(query, 10);
        System.out.println("返回的文档个数:"+topDocs.totalHits);

        //获取到文档集合
        ScoreDoc [] scoreDocs=topDocs.scoreDocs;
        for (ScoreDoc doc:scoreDocs) {
            //获取到文档
            Document document = indexSearcher.doc(doc.doc);
            //获取到文档域中数据
            System.out.println("fieldName:"+document.get("fieldName"));
            System.out.println("fieldPath:"+document.get("fieldPath"));
            System.out.println("fieldSize:"+document.get("fieldSize"));
            System.out.println("fieldContent:"+document.get("fieldContent"));
            System.out.println("==============================================================");
        }


        //关闭
        indexReader.close();
    }
}

原文地址:https://www.cnblogs.com/chx9832/p/12363449.html