Identify which query matched in bool query elasticsearch

后端 未结 1 647
梦毁少年i
梦毁少年i 2020-12-04 03:38

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

 {
            \"query\": {
                    


        
相关标签:
1条回答
  • 2020-12-04 04:04

    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"
                                        }}
                        ]
             }
        }
    }
    

    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"
    ],
    
    0 讨论(0)
提交回复
热议问题