欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Lucene其他查询方式

程序员文章站 2022-03-23 14:26:41
...
1. 指定项范围查询TermRangeQuery

2. 指定数字范围查询NumericRangeQuery

3. 指定字符串开头搜索PrefixQuery

4. 组合查询BooleanQuery

New maven project ->
Create a simple project ->
    Group Id: com.andrew.lucene
    Artifact Id: Lucene04
    Version: 0.0.1-SNAPSHOT
    Packaging: jar


pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.andrew.lucene</groupId>
  <artifactId>Lucene04</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
      <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-core</artifactId>
        <version>5.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-queryparser</artifactId>
        <version>5.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-analyzers-common</artifactId>
        <version>5.3.1</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
  </dependencies>
</project>


Indexer.java代码

package com.andrew.lucene;

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.document.Field;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class Indexer {
    private Integer ids[] = { 1, 2, 3 };
    private String citys[] = { "aingdao", "banjing", "changhai" };
    private String descs[] = { "Qingdao is b beautiful city.", "Nanjing is c city of culture.",
            "Shanghai is d bustling city." };
    private Directory dir;
    // 获取IndexWriter实例
    private IndexWriter getWriter() throws Exception {
        Analyzer analyzer = new StandardAnalyzer(); // 标准分词器
        IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
        IndexWriter writer = new IndexWriter(dir, iwc);
        return writer;
    }
    // 生成索引
    private void index(String indexDir) throws Exception {
        dir = FSDirectory.open(Paths.get(indexDir));
        IndexWriter writer = getWriter();
        for (int i = 0; i < ids.length; i++) {
            Document doc = new Document();
            doc.add(new IntField("id", ids[i], Field.Store.YES));
            doc.add(new StringField("city", citys[i], Field.Store.YES));
            doc.add(new TextField("desc", descs[i], Field.Store.YES));
            writer.addDocument(doc); // 添加文档
        }
        writer.close();
    }
    public static void main(String[] args) throws Exception {
        new Indexer().index("E:\\lucene5");
    }
}


SearchTest.java代码

package com.andrew.lucene;

import java.nio.file.Paths;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermRangeQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.BytesRef;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class SearchTest {
    private Directory dir;
    private IndexReader reader;
    private IndexSearcher is;
    @Before
    public void setUp() throws Exception {
        dir = FSDirectory.open(Paths.get("E:\\lucene5"));
        reader = DirectoryReader.open(dir);
        is = new IndexSearcher(reader);
    }
    @After
    public void tearDown() throws Exception {
        reader.close();
    }
    // 指定项范围搜索
    @Test
    public void testTermRangeQuery() throws Exception {
        TermRangeQuery query = new TermRangeQuery("desc", new BytesRef("b".getBytes()), new BytesRef("c".getBytes()), true, true);
        TopDocs hits = is.search(query, 10);
        for (ScoreDoc scoreDoc : hits.scoreDocs) {
            Document doc = is.doc(scoreDoc.doc);
            System.out.println(doc.get("id"));
            System.out.println(doc.get("city"));
            System.out.println(doc.get("desc"));
        }
    }
    // 指定数字范围
    @Test
    public void testNumericRangeQuery() throws Exception {
        NumericRangeQuery<Integer> query = NumericRangeQuery.newIntRange("id", 1, 2, true, true);
        TopDocs hits = is.search(query, 10);
        for (ScoreDoc scoreDoc : hits.scoreDocs) {
            Document doc = is.doc(scoreDoc.doc);
            System.out.println(doc.get("id"));
            System.out.println(doc.get("city"));
            System.out.println(doc.get("desc"));
        }
    }
    // 指定字符串开头搜索
    @Test
    public void testPrefixQuery() throws Exception {
        PrefixQuery query = new PrefixQuery(new Term("city", "a"));
        TopDocs hits = is.search(query, 10);
        for (ScoreDoc scoreDoc : hits.scoreDocs) {
            Document doc = is.doc(scoreDoc.doc);
            System.out.println(doc.get("id"));
            System.out.println(doc.get("city"));
            System.out.println(doc.get("desc"));
        }
    }
    // 多条件查询
    @Test
    public void testBooleanQuery() throws Exception {
        NumericRangeQuery<Integer> query1 = NumericRangeQuery.newIntRange("id", 1, 2, true, true);
        PrefixQuery query2 = new PrefixQuery(new Term("city", "a"));
        BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
        booleanQuery.add(query1, BooleanClause.Occur.MUST);
        booleanQuery.add(query2, BooleanClause.Occur.MUST);
        TopDocs hits = is.search(booleanQuery.build(), 10);
        for (ScoreDoc scoreDoc : hits.scoreDocs) {
            Document doc = is.doc(scoreDoc.doc);
            System.out.println(doc.get("id"));
            System.out.println(doc.get("city"));
            System.out.println(doc.get("desc"));
        }
    }
}

运行结果:

1
aingdao
Qingdao is b beautiful city.
2
banjing
Nanjing is c city of culture.
3
changhai
Shanghai is d bustling city.

1
aingdao
Qingdao is b beautiful city.
2
banjing
Nanjing is c city of culture.

1
aingdao
Qingdao is b beautiful city.

1
aingdao
Qingdao is b beautiful city.
相关标签: lucene