【问题标题】:Neo4j\Lucene multiterm wildcard at the end of query查询结束时的 Neo4j\Lucene 多项通配符
【发布时间】:2015-03-14 23:18:34
【问题描述】:
我正在尝试基于 Lucene 全文索引创建自动建议。
主要问题是如何基于多词短语创建自动建议(自动完成),例如 -
nosql dat*
结果可以
nosql database
nosql data
但不是
perfect nosql database
Lucene 查询的正确语法是什么,以便根据多词查询中的第一个词创建自动建议,并在末尾带有通配符?
【问题讨论】:
标签:
lucene
neo4j
full-text-search
autosuggest
fulltext-index
【解决方案1】:
我也有类似的要求,
Lucene 具有 Span 查询,允许您在查询中使用文本中单词的位置。
我已经使用 FirstSpanQuery 在 Lucene 中实现了它。 (在docs 中了解它)
这里我使用 SpanNearQuery 强制所有单词彼此相邻并且
SpanFirstQuery 强制所有这些都在文本的开头。
if (querystr.contains(" ")) // more than one word?
{
String[] words = querystr.split(" ");
SpanQuery[] clausesWildCard = new SpanQuery[words.length];
for (int i = 0; i < words.length; i++) {
if (i == words.length - 1) //last word, add wildcard clause
{
PrefixQuery pq = new PrefixQuery(new Term(VALUE, words[i]));
clausesWildCard[i] = new SpanMultiTermQueryWrapper<PrefixQuery>(pq);
}
else
{
Term clause = new Term(VALUE, words[i]);
clausesWildCard[i] = new SpanTermQuery(clause);
}
}
SpanQuery allTheWordsNear = new SpanNearQuery(clausesWildCard, 0, true);
prefixquery = new SpanFirstQuery(allTheWordsNear, words.length);
}