Boolean search, laser printer

Source: Internet
Author: User

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 ");
}


}

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.