Multiple phrase search

Source: Internet
Author: User

/*
* This method is based on phrase search
* The ability to search for multiple phrases
* */
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.Hits;
Import Org.apache.lucene.search.IndexSearcher;
Import Org.apache.lucene.search.MultiPhraseQuery;
Import Org.apache.lucene.search.PhraseQuery;


public class Multipharse_query {


Public Multipharse_query (String Index_store_path) {
try{
IndexWriter writer = new IndexWriter (Index_store_path, 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_path);

Create a Multipharsequery
Multiphrasequery query = new Multiphrasequery ();

First add the phrase prefix you want to find
Query.add (New term ("BookName", "Steel"));

Construct three term as a suffix of a phrase
term T1 = new term ("BookName", "Iron");
Term t2 = new Term ("BookName", "and");
term t3 = new term ("BookName", "want");

Then add all the suffixes to query China, together with the prefixes, they will form 3 phrases
Query.add (New term[]{t1, T2, T3});


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 ("Multiple phrases start searching------------>>>>>");
Multipharse_query MQ = new Multipharse_query ("E:\\lucene project \ \ Index file");
System.out.println ("Search completed------------------>>>>>");
System.out.println ("Stop Search-----| | | | | | | | | | | | | |");
}


}

Multiple phrase search

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.