Lucene search-search from multiple index files

Source: Internet
Author: User

Import java. io. StringReader;
Import java. util. Date;

Import org.apache.e.analysis.cn. ChineseAnalyzer;
Import org.apache.e.doc ument. Document;
Import org. apache. lucene. index. IndexReader;
Import org. apache. lucene. queryParser. QueryParser;
Import org. apache. lucene. search. Hits;
Import org. apache. lucene. search. IndexSearcher;
Import org. apache. lucene. search. MultiSearcher;
Import org. apache. lucene. search. Query;

Import org. apache. lucene. search. highlight. Highlighter;

Public class Search {
Public static void main (String [] args) throws Exception {
Search test = new Search ();
//
Hits h = null;
H = test. search ("show ");
Test. printResult (h );

 
}
Public Search (){
Try {
// Searcher = new IndexSearcher (IndexReader. open ("E: // lucene // test4 // index "));
Searchers = new IndexSearcher [2];
Searchers [0] = new IndexSearcher (IndexReader. open ("E: // lucene // test4 // index "));
Searchers [1] = new IndexSearcher (IndexReader. open ("E: // lucene // test5 // index "));

} Catch (Exception e ){
E. printStackTrace ();
}
}
IndexSearcher [] searchers = null;
// Declare an IndexSearcher object

// Declare a Query object
Private Query query = null;
ChineseAnalyzer analyzer = new ChineseAnalyzer ();
Highlighter highlighter = null;
Public final Hits search (String keyword ){
System. out. println ("retrieving keywords:" + keyword );
Try {
Date start = new Date ();


/***** A keyword used to query a field *****/
QueryParser qp = new QueryParser ("content", analyzer );
Query = qp. parse (keyword );

 



MultiSearcher multiSearcher = new MultiSearcher (searchers );
Hits hits = multiSearcher. search (query );


Date end = new Date ();
System. out. println ("retrieval completed, time used" + (end. getTime ()-start. getTime () + "millisecond ");
Return hits;
} Catch (Exception e ){
E. printStackTrace ();
Return null;
}

}
 
 
Public void printResult (Hits h ){
If (h. length () = 0 ){
System. out. println ("sorry, the result is not found .");
} Else {
For (int I = 0; I Try {
Document doc = h.doc (I );
// System. out. println ("result" + (I + 1) + ":" + doc. get ("id") + "createTime:" + doc. get ("createTime") + "title:" + doc. get ("title") + "content:" + doc. get ("content "));

System. out. println ("result" + (I + 1) + ":" + doc. get ("id") + "createTime:" + doc. get ("createTime") + "title:" + doc. get ("title") + "content:" + doc. get ("content "));


// System. out. println (doc. get ("path "));
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
System. out. println ("--------------------------------------");
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.