Lucene的其他搜索(三)

生成索引:

package com.wp.search;

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 dustling dity." };
    // 这里的descs中的Shanghai is d dustling dity这句中我让他们不出现b和c,为等下搜索准备

    private Directory dir;

    /**
     * 获取IndexWriter实例
     * 
     * @return
     * @throws Exception
     */
    private IndexWriter getWriter() throws Exception {
        Analyzer analyzer = new StandardAnalyzer(); // 标准分词器
        IndexWriterConfig iwc = new IndexWriterConfig(analyzer);// 为索引配置分词器
        IndexWriter writer = new IndexWriter(dir, iwc);
        return writer;
    }

    /**
     * 生成索引
     * 
     * @param indexDir
     * @throws Exception
     */
    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("D:\\lucene\\luceneIndex");
    }

}

 其他搜索:

package com.wp.search;

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("D:\\lucene\\luceneIndex"));// FSDirectory为专门处理目录文件的一个类
        reader = DirectoryReader.open(dir);
        is = new IndexSearcher(reader);
    }

    @After
    public void tearDown() throws Exception {
        reader.close();
    }

    /**
     * 指定项范围搜索 之前我将Shanghai is d dustling dity不出现b和c的原因,更好看结果
     * 
     * @throws Exception
     */
    @Test
    public void testTermRangeQuery() throws Exception {
        TermRangeQuery query = new TermRangeQuery("desc", new BytesRef("b"
                .getBytes()), new BytesRef("c".getBytes()), true, true);// 查询满足包含b和c区间的结果
        TopDocs hits = is.search(query, 10);// 显示查询结果的最前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"));
        }
    }

    /**
     * 指定数字范围
     * 
     * @throws Exception
     */
    @Test
    public void testNumericRangeQuery() throws Exception {
        NumericRangeQuery<Integer> query = NumericRangeQuery.newIntRange("id",
                1, 3, true, true);// 查询在id包含1到3以内的结果(包括2)
        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"));
        }
    }

    /**
     * 指定字符串开头搜索
     * 
     * @throws Exception
     */
    @Test
    public void testPrefixQuery() throws Exception {
        PrefixQuery query = new PrefixQuery(new Term("city", "a"));// 查询城市以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"));
        }
    }

    /**
     * 多条件查询
     * 
     * @throws Exception
     */
    @Test
    public void testBooleanQuery() throws Exception {
        NumericRangeQuery<Integer> query1 = NumericRangeQuery.newIntRange("id",
                1, 2, true, true);// 查询id在1和2之间的
        PrefixQuery query2 = new PrefixQuery(new Term("city", "a"));// 城市以a开头的
        BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
        // booleanQuery.add(query1, BooleanClause.Occur.MUST_NOT);//
        // MUST_NOT表示除了
        // booleanQuery.add(query1, BooleanClause.Occur.SHOULD);// SHOULD表示或者
        booleanQuery.add(query1, BooleanClause.Occur.MUST);// 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"));
        }
    }
}

 

posted @ 2016-09-27 20:08  一个java小生的奋斗史  阅读(252)  评论(0编辑  收藏  举报