确定在 bool 查询 elasticsearch 中匹配的查询 [英] Identify which query matched in bool query elasticsearch

查看:26
本文介绍了确定在 bool 查询 elasticsearch 中匹配的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 bool 查询查询我的 elasticsearch 索引.查询本身具有与此类似的结构

I'm querying my elasticsearch index with a bool query. The query itself has a structure similar to this

 {
            "query": {
                "bool": {
                    "should": [
                        {"multi_match": {
                            "fields": ["field1", "field2"],
                            "query": self.cleaned_stemmed_phrase,
                            "type": "phrase",
                            "fuzziness":"AUTO"
                                        }},
                        {"multi_match": {
                            "fields": ["field3"],
                            "query": self.cleaned_stemmed_phrase,
                            "fuzziness":"AUTO",
                            "boost": 4
                                        }},
                        {"multi_match": {
                            "fields": ["field4"],
                            "query": self.cleaned_stemmed_phrase,
                            "fuzziness":"AUTO"
                                        }},
                        {"multi_match": {
                            "fields": ["field5", "filed6"],
                            "query": self.spaces_removed,
                            "fuzziness":"AUTO"
                                        }},
                        {"multi_match": {
                            "fields": ["field7", "field8"],
                            "query": self.no_space_stems,
                            "fuzziness":"AUTO"
                                        }}
                        ]
             }
        }
    }

我希望能够确定所有这些查询中的哪一个是匹配结果的一个(一个).是否有内置的 elasticsearch 方法允许这样做,还是我必须手动执行此操作?

I want to be able to identify which of all these queries was the one (ones) that matched results. Is there a built-in method of elasticsearch that allows this or do I have to manually do it?

推荐答案

您可以使用 命名查询 然后在结果中您将获得匹配的查询的名称.

You can use named queries and then in the results you'll get the name of the query that matched.

{
        "query": {
            "bool": {
                "should": [
                    {"multi_match": {
                        "fields": ["field1", "field2"],
                        "query": self.cleaned_stemmed_phrase,
                        "type": "phrase",
                        "fuzziness":"AUTO",
   add name --->        "_name": "query1"
                                    }},
                    {"multi_match": {
                        "fields": ["field3"],
                        "query": self.cleaned_stemmed_phrase,
                        "fuzziness":"AUTO",
                        "boost": 4,
   add name --->        "_name": "query2"
                                    }},
                    {"multi_match": {
                        "fields": ["field4"],
                        "query": self.cleaned_stemmed_phrase,
                        "fuzziness":"AUTO",
   add name --->        "_name": "query3"
                                    }},
                    {"multi_match": {
                        "fields": ["field5", "filed6"],
                        "query": self.spaces_removed,
                        "fuzziness":"AUTO",
   add name --->        "_name": "query4"
                                    }},
                    {"multi_match": {
                        "fields": ["field7", "field8"],
                        "query": self.no_space_stems,
                        "fuzziness":"AUTO",
   add name --->        "_name": "query5"
                                    }}
                    ]
         }
    }
}

然后在结果中,您将获得一个 matched_queries 数组,其中包含与文档匹配的查询的名称.

Then in the results you'll get a matched_queries array with the name of the query/ies that matched the document.

"_source": {
    ...
},
"matched_queries": [
    "title_query"
],

这篇关于确定在 bool 查询 elasticsearch 中匹配的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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