确定在bool查询elasticsearch中匹配的查询

时间:2016-04-25 14:34:04

标签: python 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"
                                        }}
                        ]
             }
        }
    }

我希望能够确定所有这些查询中的哪一个是匹配结果的一个(一个)。是否有内置的弹性搜索方法允许或者我必须手动执行此操作?

1 个答案:

答案 0 :(得分:2)

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

{
        "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"
],