Elasticsearch"match_phrase"查询和“模糊"查询-可以结合使用 [英] Elasticsearch "match_phrase" query and "fuzzy" query - can both be used in conjunction
问题描述
我需要使用match_phrase和模糊匹配的查询.但是我找不到任何文档来构造这样的查询.另外,当我尝试组合查询(一个在另一个)时,它会引发错误.可以构造这样的查询吗?
I need a query using match_phrase along with fuzzy matching. However I'm not able to find any documentation to construct such a query. Also, when I try combining the queries(one within another), it throws errors. Is it possible to construct such a query?
推荐答案
您需要使用以下查询将对champions league
进行词组匹配+模糊查询,例如在类型为text
The below query would perform phrase match+fuzzy query for champions league
say for e.g. on a sample field name
which is of type text
如果您想要多个字段,请添加另一个must
子句.
If you'd want multiple fields, then add another must
clause.
注意我已经提到了slop:0
和in_order:true
,它们可以进行精确的词组匹配,而在match
查询中使用fuzzy
查询可以实现模糊行为.
Notice I've mentioned slop:0
and in_order:true
which would do exact phrase match, while you achieve fuzzy behaviour using fuzzy
queries inside match
query.
POST span-index/mydocs/1
{
"name": "chmpions leage"
}
POST span-index/mydocs/2
{
"name": "champions league"
}
POST span-index/mydocs/3
{
"name": "chompions leugue"
}
跨度查询:
POST span-index/_search
{
"query":{
"bool":{
"must":[
{
"span_near":{
"clauses":[
{
"span_multi":{
"match":{
"fuzzy":{
"testField":"champions"
}
}
}
},
{
"span_multi":{
"match":{
"fuzzy":{
"testField":"league"
}
}
}
}
],
"slop":0,
"in_order":true
}
}
]
}
}
}
响应:
{
"took": 19,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.5753642,
"hits": [
{
"_index": "span-index",
"_type": "mydocs",
"_id": "2",
"_score": 0.5753642,
"_source": {
"name": "champions league"
}
},
{
"_index": "span-index",
"_type": "mydocs",
"_id": "1",
"_score": 0.5753642,
"_source": {
"name": "chmpions leage"
}
},
{
"_index": "span-index",
"_type": "mydocs",
"_id": "3",
"_score": 0.5753642,
"_source": {
"name": "chompions leugue"
}
}
]
}
}
让我知道这是否有帮助!
Let me know if this helps!
这篇关于Elasticsearch"match_phrase"查询和“模糊"查询-可以结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!