我想使用lucene進行增量搜索。我在兩個詞之間有空格。 說「今天的印度」。我的搜索查詢返回使用Lucene的增量搜索
- 印度今天
- 如今,印度時間
- 今天時間印度
我希望搜索導致諸如「今天的印度%」在SQL中。 但是這沒有發生。我試過使用短語查詢,但它適用於精確search.My存儲的數據NOT_ANALYZED,以便我可以搜索空間。
KeywordAnalyzer analyzer = new KeywordAnalyzer();
PhraseQuery pq = new PhraseQuery();
pq.add(new Term("name", "MR DANIEL KELLEHER"));
int hitsPerPage = 1000;
IndexReader reader = IndexReader.open(index);
IndexSearcher searcher = new IndexSearcher(reader);
TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
searcher.search(pq, collector);
我不能夠得到這樣的查詢中還有空間inbetween.I都提到淨許多文章和計算器,以及,但沒有得到解決。
package org.lucenesample;
import org.apache.lucene.search.Query;
import org.apache.lucene.*;
import org.apache.lucene.analysis.*;
import org.apache.lucene.analysis.standard.*;
import org.apache.lucene.analysis.standard.std31.*;
import org.apache.lucene.analysis.tokenattributes.*;
import org.apache.lucene.collation.*;
import org.apache.lucene.document.*;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.*;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.messages.*;
import org.apache.lucene.queryParser.*;
import org.apache.lucene.search.*;
import org.apache.lucene.search.function.*;
import org.apache.lucene.search.payloads.*;
import org.apache.lucene.search.spans.*;
import org.apache.lucene.store.*;
import org.apache.lucene.util.*;
import org.apache.lucene.util.fst.*;
import org.apache.lucene.util.packed.*;
import java.io.File;
import java.sql.*;
import java.util.HashMap;
public class ExactPhrasesearchUsingStandardAnalyser {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
Directory directory = new RAMDirectory();
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);
MaxFieldLength mlf = MaxFieldLength.UNLIMITED;
IndexWriter writer = new IndexWriter(directory, analyzer, true, mlf);
writer.addDocument(createDocument1("1", "foo bar baz blue"));
writer.addDocument(createDocument1("2", "red green blue"));
writer.addDocument(createDocument1("3", "test panda foo & bar testt"));
writer.addDocument(createDocument1("4", " bar test test foo in panda red blue "));
writer.addDocument(createDocument1("4", "test"));
writer.close();
IndexSearcher searcher = new IndexSearcher(directory);
PhraseQuery query = new PhraseQuery();
QueryParser qp2 = new QueryParser(Version.LUCENE_35, "contents", analyzer);
//qp.setDefaultOperator(QueryParser.Operator.AND);
Query queryx2 =qp2.parse("test foo in panda re*");//contains query
Query queryx23 =qp2.parse("+red +green +blu*" );//exact phrase match query.Make last word as followed by star
Query queryx234 =qp2.parse("(+red +green +blu*)& (\"red* green\") " );
/*String term = "new york";
// id and location are the fields in which i want to search the "term"
MultiFieldQueryParser queryParser = new MultiFieldQueryParser(
Version.LUCENE_35,
{ "contents"},
new KeywordAnalyzer());
Query query = queryParser.parse(term);
System.out.println(query.toString());*/
QueryParser qp = new QueryParser(Version.LUCENE_35, "contents", analyzer);
//qp.setDefaultOperator(QueryParser.Operator.AND);
Query queryx =qp.parse("\"air quality\"~10");
System.out.println("******************Searching Code starts******************");
TopDocs topDocs = searcher.search(queryx2, 10);
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
Document doc = searcher.doc(scoreDoc.doc);
System.out.println(doc+"testtttttttt");
}
}
private static Document createDocument1(String id, String content) {
Document doc = new Document();
doc.add(new Field("id", id, Store.YES, Index.NOT_ANALYZED));
doc.add(new Field("contents", content, Store.YES, Index.ANALYZED,
Field.TermVector.WITH_POSITIONS_OFFSETS));
System.out.println(content);
return doc;
}
}
我想這way.I可以搜索包含格式。但我不能夠與選項得到啓動,這樣當用戶按下「印度」,然後「印度的明天」和「今日印度」的結果也出現了。當我做「+ india * + to *」時,我能夠接近它。但是這導致「今天的印度人」也是如此。我無法獲得搜索結果,直到用戶類型完成「今天」。基本上,我。希望短語查詢「\」今日印度\」上班
是前綴查詢你想要什麼? – Kevin
前綴查詢也不支持兩者之間的空間。我也嘗試過。 –