熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Lucene在多個索引上進行搜索

2013-11-23 19:25:20  來源: Java核心技術 

  代碼如下

  view plaincopy to clipboardprint?
package comlucenesearch;

  import orgapacheluceneanalysisstandardStandardAnalyzer;
import orgapachelucenedocumentDocument;
import orgapachelucenedocumentField;
import orgapacheluceneindexIndexWriter;
import orgapachelucenequeryParserMultiFieldQueryParser;
import orgapachelucenesearchBooleanClause;
import orgapachelucenesearchHits;
import orgapachelucenesearchIndexSearcher;
import orgapachelucenesearchMultiSearcher;
import orgapachelucenesearchQuery;

  public class Multisearcher {

  private static String INDEX_STORE_PATH = C:\\multi\\;
    private static String INDEX_STORE_PATH = C:\\multi\\;

  public static void main(String[] args) throws Exception {
        Multisearchermultisearcher();
    }

  public static void multisearcher() throws Exception {
        IndexWriter writer = new IndexWriter(INDEX_STORE_PATH new StandardAnalyzer() true);
        writersetUseCompoundFile(false);

  Document doc = new Document();
        Field f = new Field(bookname 鋼鐵是怎樣煉成的 FieldStoreYES FieldIndexTOKENIZED);
        Field f = new Field(price FieldStoreYES FieldIndexUN_TOKENIZED);

  docadd(f);
        docadd(f);

  Document doc = new Document();
        Field f = new Field(bookname 鋼鐵戰士 FieldStoreYES FieldIndexTOKENIZED);
        Field f = new Field(price FieldStoreYES FieldIndexUN_TOKENIZED);

  docadd(f);
        docadd(f);

  Document doc = new Document();
        Field f = new Field(bookname 鋼和鐵是兩種不同的元素 FieldStoreYES FieldIndexTOKENIZED);
        Field f = new Field(price FieldStoreYES FieldIndexUN_TOKENIZED);

  docadd(f);
        docadd(f);

  writeraddDocument(doc);
        writeraddDocument(doc);
        writeraddDocument(doc);

  writerclose();

  //創建第二個索引器
        IndexWriter writer = new IndexWriter(INDEX_STORE_PATH new StandardAnalyzer() true);
        writersetUseCompoundFile(false);

  Document doc = new Document();
        Field f = new Field(bookname 鋼要比鐵有更多的元素 FieldStoreYES FieldIndexTOKENIZED);
        Field f = new Field(price FieldStoreYES FieldIndexUN_TOKENIZED);

  docadd(f);
        docadd(f);

  Document doc = new Document();
        Field f = new Field(bookname 鋼和鐵是兩種重要的金屬 FieldStoreYES FieldIndexTOKENIZED);
        Field f = new Field(price FieldStoreYES FieldIndexUN_TOKENIZED);

  docadd(f);
        docadd(f);

  Document doc = new Document();
        Field f = new Field(bookname 鋼鐵是兩種重要的金屬 FieldStoreYES FieldIndexTOKENIZED);
        Field f = new Field(price FieldStoreYES FieldIndexUN_TOKENIZED);

  docadd(f);
        docadd(f);

  writeraddDocument(doc);
        writeraddDocument(doc);
        writeraddDocument(doc);

  writerclose();

  String query = ;
        String query = [ TO ];//注意格式中括號還有關鍵字TO是大寫的
        String[] queries = { query query };

  //指定兩個域Field
        String field = bookname;
        String field = price;
        String[] fields = { field field };

  //指定查詢字句之間的關系
        BooleanClauseOccur[] clauses = { BooleanClauseOccurMUST BooleanClauseOccurMUST };

  //轉成多域查詢MultiFieldQuery
        Query q = MultiFieldQueryParserparse(queries fields clauses new StandardAnalyzer());

  //打印Query的內容
        Systemoutprintln(qtoString());

  //創建兩個IndexSearcher以實現在多個索引目錄進行查詢
        IndexSearcher searcher = new IndexSearcher(INDEX_STORE_PATH);
        IndexSearcher searcher = new IndexSearcher(INDEX_STORE_PATH);

  IndexSearcher[] searchers = { searcher searcher };

  //使用MultiSearcher進行多域搜索
        MultiSearcher searcher = new MultiSearcher(searchers);
        Hits hits = searchersearch(q);
        for (int i = ; i < hitslength(); i++) {
            Systemoutprintln(hitsdoc(i));
        }
    }
}


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26839.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.