以相同的顺序分析和匹配所有条款 [英] Analyze and match all terms in same order
问题描述
我想要实现的目标:
文件:一二三四
搜索字符串:
- 一四(必须匹配)
- 一个(不能匹配)
我学到了什么:
为了解决订单,应该使用 span_near
查询,但是这假设这些术语已被客户端分析所有条款必须单独提供)。
For order to be accounted for, the span_near
query should be used, but this assumes that the terms are already analyzed by the client (all terms must be supplied separately).
为了分析搜索字符串,应该使用 phrase_match
查询,但是它不考虑订单。
To have the search string analyzed, the phrase_match
query should be used, but it does not take order into account.
可能应该使用一个脚本(谢谢@ ChintanShah25),但是似乎不可能分析脚本中的输入字符串。
It's likely a script should be used (thanks @ChintanShah25), but it seems impossible to analyse the input string inside the script.
如何实现分析和订单需求?
推荐答案
你没有直接的方式来实现这一点,你是coul d使用 _analyze
端点,使用 span query
或使用脚本
和 match_phrase
There is no straightforward way to achieve this, you could do this with either using _analyze
endpoint with span query
or with script
and match_phrase
1)将您的搜索字符串传递给 _分析与
1) You pass your search string to _analyze with
curl -XGET 'localhost:9200/_analyze' -d '
{
"analyzer" : "my_custom_analyzer",
"text" : "one four"
}'
你会得到这样的东西
{
"tokens": [
{
"token": "one",
"start_offset": 0,
"end_offset": 3,
"type": "<ALPHANUM>",
"position": 1
},
{
"token": "four",
"start_offset": 4,
"end_offset": 8,
"type": "<ALPHANUM>",
"position": 2
}
]
}
然后将令牌传递给 span query
you then pass the tokens to the span query
{
"span_near" : {
"clauses" : [
{ "span_term" : { "field" : "token1" } },
{ "span_term" : { "field" : "token2" } }
],
"slop" : 2,
"in_order" : true,
"collect_payloads" : false
}
}
2)另一种方法是使用高级脚本,看看@Andrei Stefan的答案为这个问题,他使用 _POS I
与 match_phrase
以获得有关条款的结果。
2) Another way is to use advanced scripting, have a look at the answer of @Andrei Stefan for this question, He used _POSITIONS
with match_phrase
to get back results with terms in order.
希望这有助于!
这篇关于以相同的顺序分析和匹配所有条款的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!