Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out which fields matched in a multi match query

I am using a typical multi match query on three fields: name, city, state. The multi match query is also using a Java function score script. Is there any way to know in the score script what fields matched my multi match query? If not, is there any way to figure this out from the SearchResponse object?

I am currently on Elasticsearch 1.2.1 but I can easily upgrade if it's necessary.

like image 403
Commander Avatar asked Aug 27 '14 21:08

Commander


People also ask

What is multi match query in Elasticsearch?

The multi_match query provides a convenient shorthand way of running the same query against multiple fields.

How does Elasticsearch match query work?

The match query analyzes any provided text before performing a search. This means the match query can search text fields for analyzed tokens rather than an exact term. (Optional, string) Analyzer used to convert the text in the query value into tokens. Defaults to the index-time analyzer mapped for the <field> .

Should minimum should match?

Minimum Should Match is another search technique that allows you to conduct a more controlled search on related or co-occurring topics by specifying the number of search terms or phrases in the query that should occur within the records returned.

What is term query in Elasticsearch?

Term queryedit. Returns documents that contain an exact term in a provided field. You can use the term query to find documents based on a precise value such as a price, a product ID, or a username. Avoid using the term query for text fields.


2 Answers

I don't believe you can do this directly with just multi match, but if you add highlighting you should get a response showing which fields matched:

http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/highlighting-intro.html

In the example from this page:

    GET /megacorp/employee/_search
    {
        "query" : {
            "match_phrase" : {
                "about" : "rock climbing"
            }
        },
        "highlight": {
            "fields" : {
                "about" : {}
            }
        }

}

you would change the match_phrase to a multi_match and add the field list:

    GET /megacorp/employee/_search
    {
        "query" : {
            "multi_match" : {
                "query" : "rock climbing",
                "fields": ["about", "otherfield"]
            }
        },
        "highlight": {
            "fields" : {
                "about" : {},
                 "otherfield": {}
            }
        }

}

and that should get you a highlight response which includes the highlight around the matching text and the field that was matched.

like image 90
John Petrone Avatar answered Sep 21 '22 22:09

John Petrone


There is another exact way to find out which field is matched in the query

Because the highlight is post highlight process, it is not accurate because of the way it did

Just use named query to do it instead of multi-match

such as

  {
    "multi_match" : {
      "query" : "query phrase here",
      "fields" : [ "name", "tag", "categorys" ],
      "operator" : "AND"
  }

translate it into bool query with name

    "should": [
        {
            "match": {
               "name": {
                    "query": "query phrase here",
                    "_name":"name_field"
               }
            }
        },{
            "match": {
               "tag":{
                    "query": "query phrase here",
                    "_name":"tag_field"
               }
            }   
        },{
            "match": {
               "categorys":{
                    "query": "query phrase here",
                    "_name":"cat_field"
               }
            }
        }
     ]

it will return the result like that

     {
        "_index": "indexName",
        "_type": "type",
        "_id": "id",
        "_score": 0.27836448,
        "matched_queries": [
           "tag_field"
        ]
     },
     {
        "_index": "indexName",
        "_type": "type",
        "_id": "id",
        "_score": 0.27836448,
        "matched_queries": [
           "name_field",
           "tag_field"
        ]
     }
like image 43
Neo Slixurd Avatar answered Sep 19 '22 22:09

Neo Slixurd