Boolean search, laser printer
/*
* This code is written in BooleanQuery Boolean search.
* As the name implies, it is a Boolean query.
* A boolean query is composed of the Boolean logic between multiple clauses and clauses.
**/
Package query;
Import java. io. IOException;
Import org. apache. lucene. analysis. standard. StandardAnalyzer;
Import org.apache.e.doc ument. Document;
Import org.apache.e.doc ument. Field;
Import org. apache. lucene. index. IndexWriter;
Import org. apache. lucene. index. Term;
Import org. apache. lucene. search. BooleanClause;
Import org. apache. lucene. search. BooleanQuery;
Import org. apache. lucene. search. Hits;
Import org. apache. lucene. search. IndexSearcher;
Import org. apache. lucene. search. TermQuery;
Public class Query_Boolean {
Public Query_Boolean (String INDEX_STORE ){
Try {
IndexWriter writer = new IndexWriter (INDEX_STORE, new StandardAnalyzer (), true );
Writer. setUseCompoundFile (false );
// Create 3 Documents
Document doc1 = new Document ();
Document doc2 = new Document ();
Document doc3 = new Document ();
Field f1 = new Field ("bookname", "How steel is made", Field. Store. YES, Field. Index. TOKENIZED );
Field f2 = new Field ("bookname", "Heroes and children", Field. Store. YES, Field. Index. TOKENIZED );
Field f3 = new Field ("bookname", "Awesome women and dogs", Field. Store. YES, Field. Index. TOKENIZED );
Doc1.add (f1 );
Doc2.add (f2 );
Doc3.add (f3 );
Writer. addDocument (doc1 );
Writer. addDocument (doc2 );
Writer. addDocument (doc3 );
Writer. close ();
IndexSearcher searcher = new IndexSearcher (INDEX_STORE );
// Create two entry objects
Term t1 = new Term ("bookname", "female ");
Term t2 = new Term ("bookname", "dog ");
// Create two TermQuery objects
TermQuery q1 = new TermQuery (t1 );
TermQuery q2 = new TermQuery (t2 );
// Construct a Boolean object
BooleanQuery query = new BooleanQuery ();
// Add two termqueries to the Boolea clause, and the relationship must satisfy
Query. add (q1, BooleanClause. Occur. MUST );
Query. add (q2, BooleanClause. Occur. MUST );
// Print the query Structure
Hits hits = searcher. search (query );
For (int I = 0; I System.out.println(hits.doc (I ));
}
} Catch (IOException e ){
E. printStackTrace ();
}
}
Public static void main (String [] args ){
// TODO Auto-generated method stub
Query_Boolean qb = new Query_Boolean ("E: \ Lucene project \ index file ");
}
}