Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

python - Identify which query matched in bool query 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"
                                        }}
                        ]
             }
        }
    }

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...