无法正确使用lucene的关键字分析器,
String term = "new york";
// id and location are the fields in which i want to search the "term"
MultiFieldQueryParser queryParser = new MultiFieldQueryParser(
Version.LUCENE_30,
{"id", "location"},
new KeywordAnalyzer());
Query query = queryParser.parse(term);
System.out.println(query.toString());
OUTCOME:(id:new location:new)(id:york location:york)
预期结果:(id:纽约地点:纽约)(id:纽约地点:纽约)
请帮我辨别我在这里做错了什么?
答案 0 :(得分:4)
你没有做错任何事。这就是QueryParser的工作方式。 由于您使用KeywordAnalyzer将文本索引为单个标记,因此应使用TermQuery。由于您有两个要搜索的字段,因此可以组合两个TermQueries,如:
BooleanQuery bq = new BooleanQuery();
bq.Add(new TermQuery(new Term("id", term)), BooleanClause.Occur.SHOULD );
bq.Add(new TermQuery(new Term("location", term)), BooleanClause.Occur.SHOULD );
string txtQuery = bq.ToString();