Lucene短语匹配末尾有通配符
我正在尝试进行预想性文本搜索,以允许用户开始输入文字,并且文本中的结果会作为结果显示出来
I'm trying to make a predictive text search that allows a user to start typing, and results from their text come through as results
例如有了"ca",他们可以获得戴帽子的猫",我的微积分很棒"猫狗老鼠"
E.g. with "ca" they can get "cat in the hat", "my calculus is cool" "cat dog mouse"
但是,如果一个人继续输入空格,我希望将整个短语视为一个术语
However, if a person continues to type with spaces, I want the whole phrase to be considered as one term
例如猫我"应该找到戴帽子的猫"
E.g. "cat i" should find "cat in the hat"
但不是猫狗"或我的微积分很酷"
but NOT "[cat] dog mouse" nor "my calculus [i]s cool"
这是我当前的代码,但是它似乎并没有按照我希望的那样工作:
This is my current code, however it does not seem to be working as I'd hoped:
val mySort = new Sort(SortField.FIELD_SCORE, new SortField("popularity", SortField.Type.INT, true))
val analyzer = new StandardAnalyzer(Version.LUCENE_43)
val parser: QueryParser = new QueryParser(Version.LUCENE_43, "title", analyzer)
val query = parser.parse(queryString+"*")
val titleQuery = new ConstantScoreQuery(query)
titleQuery.setBoost(2)
val synopsisQuery = new QueryParser(Version.LUCENE_43, "synopsis", analyzer).parse(queryString)
val summaryQuery = new ConstantScoreQuery(synopsisQuery)
val finalQuery = new DisjunctionMaxQuery(0)
finalQuery.add(titleQuery)
finalQuery.add(summaryQuery)
val collector = TopFieldCollector.create(mySort,Limit,false,true,true,false)
searcher.search(finalQuery, collector)
collector.topDocs().scoreDocs
基本上有两种方法可以实现此目的.
There are basically two ways to achieve this.
老方法是手动构建 MultiPhraseQuery
-请参见此答案以获取详细信息.
The old way, is to construct a MultiPhraseQuery
manually - see this answer for details.
The new way is simpler though: construct a SpanNearQuery
. Use the following parameters: inOrder = true
and slop = 0
to get an equivalent of PhraseQuery
.
Each clause in the SpanNearQuery
should be a SpanTermQuery
except the last one. These should be the full terms contained in your phrase.
最后一个子句应为 SpanMultiTermQueryWrapper< PrefixQuery>
,包装一个
The last clause should be a SpanMultiTermQueryWrapper<PrefixQuery>
, wrapping a PrefixQuery
. Use the last term of your phrase as the prefix value.
总结一下,对于 cat i
:
SpanNearQuery [inOrder = true, slop = 0]
|
+-- SpanTermQuery [term = "cat"]
|
+-- SpanMultiTermQueryWrapper<PrefixQuery>
|
+-- Prefixquery [prefix = "i"]