Lucene短语匹配末尾有通配符 [英] Lucene phrase match with a wildcard at the end

查看:80
本文介绍了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"]

这篇关于Lucene短语匹配末尾有通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆