代码如下 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)); } } } |