一尘不染

确定布尔查询elasticsearch中匹配的查询

elasticsearch

我正在用查询查询我的elasticsearch索引bool。查询本身的结构与此类似

 {
            "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方法允许这样做,还是我必须手动进行?


阅读 200

收藏
2020-06-22

共1个答案

一尘不染

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

{
        "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数组,其中包含与文档匹配的查询的名称。

"_source": {
    ...
},
"matched_queries": [
    "title_query"
],
2020-06-22