我想使用 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);
我无法获得中间有空格的查询。我也参考了很多关于网络和 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”时,“India today”和“india today”结果也会出现。我能够当我做“+india* +to*”时靠近它但是这也会导致“今天的印度人”。在用户输入完成“今天”之前我无法获得搜索结果。基本上我想要短语查询“\”印度今天"上班。