我想使用lucene进行增量搜索。我的词之间有空格。 说“今日印度”。我的搜索查询返回
我希望在sql中搜索结果,例如“India today%”。 但这不会发生。我尝试使用短语查询,但这适用于精确搜索。我存储的数据是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);
我无法获得具有空间的查询。我已经在net和stackoverflow上引用了很多文章,但没有得到解决方案。
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;
}
}
我试过这种方式。我可以搜索包含格式。但是我无法获得选项的开头,这样当用户按下“印度到”然后“明天印度”和“今日印度”结果也会出现。我当我做“+ india * + to *”时能够接近它但是这会导致“今天的印度人”。我无法获得搜索结果,直到用户类型完成“今天”。基本上我想要短语查询“ “今日印度”上班。
答案 0 :(得分:1)
对于已分析的字段,一种方法是使用已MultiPhraseQuery枚举的prefix terms:
<MultiPhraseQuery: field:"india (today todays)">
或者可以使用SpanQuery,其优点是可以处理术语扩展。
<SpanNearQuery: spanNear([field:india, SpanMultiTermQueryWrapper(field:today*)], 0, true)>