我正在用查询查询我的elasticsearch索引bool。查询本身的结构与此类似
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方法允许这样做,还是我必须手动进行?
您可以使用命名查询,然后在结果中获得匹配的查询的名称。
{ "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数组,其中包含与文档匹配的查询的名称。
matched_queries
"_source": { ... }, "matched_queries": [ "title_query" ],