Phraseprefixquery and phrase are somewhat similar. In phrasequery, if the user wants to find the phrase "David Robert", he wants to find the phrase "Mary Robert". Then, he can only build two phrasequery, and then use booleanquery as the clause, and use the "or" operator to connect, so as to achieve the desired effect. Phraseprefixquery can make it easy for users to implement this need.
PackageCh11;ImportOrg.apache.lucene.analysis.standard.StandardAnalyzer;Importorg.apache.lucene.document.Document;ImportOrg.apache.lucene.document.Field;ImportOrg.apache.lucene.index.IndexWriter;Importorg.apache.lucene.index.Term;Importorg.apache.lucene.search.Hits;ImportOrg.apache.lucene.search.IndexSearcher;ImportOrg.apache.lucene.search.PhrasePrefixQuery;ImportOrg.apache.lucene.search.PhraseQuery;ImportOrg.apache.lucene.search.RangeQuery; Public classPhraseprefixquerytest { Public Static voidMain (string[] args)throwsException {//Generate Document ObjectDocument Doc1=NewDocument (); //Add the contents of the "Content" fieldDoc1.add (Field.text ("Content", "David Mary Smith Robert")); //Add the contents of the "title" FieldDoc1.add (Field.keyword ("title", "Doc1")); //building an index writer objectIndexWriter writer=NewIndexWriter ("C://index", NewStandardAnalyzer (),true); //to add a document to the indexwriter.adddocument (Doc1); //Close the index writerWriter.close (); //Build Index RetrieverIndexsearcher Searcher=NewIndexsearcher ("C://index"); //Construct EntryTerm word1=NewTerm ("content", "David"); Term Word2=NewTerm ("content", "Mary"); Term Word3=NewTerm ("content", "Smith"); Term Word4=NewTerm ("content", "Robert"); //to save the results of a searchHits Hits=NULL; //generates a Phraseprefixquery object, initialized to nullphraseprefixquery Query=NULL; Query=NewPhraseprefixquery (); //to add all the possible uncertain wordsQuery.add (Newterm[]{word1, word2}); //Add a definite wordQuery.add (WORD4); //Set SlopeQuery.setslop (2); //start the search and return the results of the searchhits=searcher.search (query); //output information about the results of the searchPrintresult (hits,"Existence of the phrase ' David Robert ' or ' Mary Robert ' 's Documentation"); } Public Static voidPrintresult (Hits Hits, String key)throwsException {System.out.println ("Find/" "+ Key +"/":"); if(hits! =NULL) { if(hits.length () = = 0) {System.out.println ("No results found"); System.out.println (); } Else{System.out.print (Find); for(inti = 0; I < hits.length (); i++) { //Get Document ObjectDocument D=Hits.doc (i); //get the "title" Field contentString dname= D.get ("title"); System.out.print (Dname+ " "); } System.out.println (); System.out.println (); } } }}
In the code above, a Document was first built , and its "content" field contains 4 keywords. Next, a Phraseprefixquery object is constructed, and its add (term []) method is called to set the first keyword that appears in the phrase. Since the parameter type of this method is an array of term type, it can set multiple terms, that is, the first word appearing in the phrase is selected in this array. Then, use the Add (term) method to set the last word that appears in the phrase.
lucene-query query->phraseprefixquery using phrase-prefix search