Lucene在多个索引上进行搜索

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MultiSearcher;
import org.apache.lucene.search.Query;

public class Multisearcher {

    private static String INDEX_STORE_PATH1 = "C:\\multi\\1";
    private static String INDEX_STORE_PATH2 = "C:\\multi\\2";

    public static void main(String[] args) throws Exception {
        Multisearcher.multisearcher();
    }

    public static void multisearcher() throws Exception {
        IndexWriter writer = new IndexWriter(INDEX_STORE_PATH1, new StandardAnalyzer(), true);
        writer.setUseCompoundFile(false);

        Document doc1 = new Document();
        Field f1 = new Field("bookname", "钢铁是怎样炼成的", Field.Store.YES, Field.Index.TOKENIZED);
        Field f11 = new Field("price", "20.5", Field.Store.YES, Field.Index.UN_TOKENIZED);

        doc1.add(f1);
        doc1.add(f11);

        Document doc2 = new Document();
        Field f2 = new Field("bookname", "钢铁战士", Field.Store.YES, Field.Index.TOKENIZED);
        Field f22 = new Field("price", "18.4", Field.Store.YES, Field.Index.UN_TOKENIZED);

        doc2.add(f2);
        doc2.add(f22);

        Document doc3 = new Document();
        Field f3 = new Field("bookname", "钢和铁是两种不同的元素", Field.Store.YES, Field.Index.TOKENIZED);
        Field f33 = new Field("price", "7.6", Field.Store.YES, Field.Index.UN_TOKENIZED);

        doc3.add(f3);
        doc3.add(f33);

        writer.addDocument(doc1);
        writer.addDocument(doc2);
        writer.addDocument(doc3);

        writer.close();

        //创建第二个索引器;
        IndexWriter writer2 = new IndexWriter(INDEX_STORE_PATH2, new StandardAnalyzer(), true);
        writer2.setUseCompoundFile(false);

        Document doc4 = new Document();
        Field f4 = new Field("bookname", "钢要比铁有更多的元素", Field.Store.YES, Field.Index.TOKENIZED);
        Field f44 = new Field("price", "22.5", Field.Store.YES, Field.Index.UN_TOKENIZED);

        doc4.add(f4);
        doc4.add(f44);

        Document doc5 = new Document();
        Field f5 = new Field("bookname", "钢和铁是两种重要的金属", Field.Store.YES, Field.Index.TOKENIZED);
        Field f55 = new Field("price", "15.9", Field.Store.YES, Field.Index.UN_TOKENIZED);

        doc5.add(f5);
        doc5.add(f55);

        Document doc6 = new Document();
        Field f6 = new Field("bookname", "钢铁是两种重要的金属", Field.Store.YES, Field.Index.TOKENIZED);
        Field f66 = new Field("price", "19.00", Field.Store.YES, Field.Index.UN_TOKENIZED);

        doc6.add(f6);
        doc6.add(f66);

        writer2.addDocument(doc4);
        writer2.addDocument(doc5);
        writer2.addDocument(doc6);

        writer2.close();

        String query1 = "钢";
        String query2 = "[10 TO 20]";//注意格式:中括号还有关键字TO是大写的
        String[] queries = { query1, query2 };

        //指定两个域Field
        String field1 = "bookname";
        String field2 = "price";
        String[] fields = { field1, field2 };

        //指定查询字句之间的关系
        BooleanClause.Occur[] clauses = { BooleanClause.Occur.MUST, BooleanClause.Occur.MUST };

        //转成多域查询MultiFieldQuery
        Query q = MultiFieldQueryParser.parse(queries, fields, clauses, new StandardAnalyzer());

        //打印Query的内容
        System.out.println(q.toString());

        //创建两个IndexSearcher,以实现在多个索引目录进行查询
        IndexSearcher searcher1 = new IndexSearcher(INDEX_STORE_PATH1);
        IndexSearcher searcher2 = new IndexSearcher(INDEX_STORE_PATH2);

        IndexSearcher[] searchers = { searcher1, searcher2 };

        //使用MultiSearcher进行多域搜索
        MultiSearcher searcher = new MultiSearcher(searchers);
        Hits hits = searcher.search(q);
        for (int i = 0; i < hits.length(); i++) {
            System.out.println(hits.doc(i));
        }
    }
}

发表评论

返回顶部