Package query;
Import java.io.IOException;
Import Org.apache.lucene.analysis.standard.StandardAnalyzer;
Import org.apache.lucene.document.Document;
Import Org.apache.lucene.document.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.PhraseQuery;
Import Org.apache.lucene.search.TermQuery;
public class Phrase_query {
Public Phrase_query (String index_store) {
try{
IndexWriter writer = new IndexWriter (Index_store, New StandardAnalyzer (), true);
Writer.setusecompoundfile (FALSE);
Create 3 of documents
Document Doc1 = new document ();
Document DOC2 = new document ();
Document DOC3 = new document ();
Field f1 = new Field ("BookName", "How Steel is Tempered", Field.Store.YES, Field.Index.TOKENIZED);
Field F2 = new Field ("BookName", "Children of Heroes", Field.Store.YES, Field.Index.TOKENIZED);
Field F3 = new Field ("BookName", "Fence Woman and Dog", 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 a pharsequery and search for the phrase "steel"
Phrasequery query = new Phrasequery ();
Query.add (New term ("BookName", "Steel"));
Query.add (New term ("BookName", "Iron"));
Print query structure
Hits Hits =searcher.search (query);
for (int i = 0; i < hits.length (); i++) {
System.out.println (Hits.doc (i));
}
}catch (IOException e) {
E.printstacktrace ();
}
}
public static void Main (string[] args) {
TODO auto-generated Method Stub
System.out.println ("Start phrase search----------->>");
Phrase_query HQ = new Phrase_query ("E:\\lucene project \ \ Index file");
SYSTEM.OUT.PRINTLN ("End phrase search----------->>");
}
}
Phrase Search Phrasequery