Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which field matched query in multi_match search in Elasticsearch?

I have query with multi_match in Elasticsearch:

{
  "query": {
    "multi_match": {
      "query": "luk",
      "fields": [
        "xml_string.autocomplete",
        "state"
      ]
    }
  },
  "size": 10,
  "fields": [
    "xml_string",
    "state"
  ]
}

It works great, result returns expected value:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.41179964,
    "hits": [
      {
        "_index": "documents",
        "_type": "document",
        "_id": "11",
        "_score": 0.41179964,
        "fields": {
          "xml_string": "Lukas bla bla bla",
          "state": "new"
        }
      }
    ]
  }
}

I've searched a lot, but I am not able to find out which field matched the query(if it was xml_string OR state)

like image 805
gertruda Avatar asked Sep 02 '13 15:09

gertruda


1 Answers

I have found solution: I have used highlight feature and it's working great

This is how my curl looks like:

 curl -X GET 'http://xxxxx.com:9200/documents/document/_search?load=false&size=10&pretty' -d '{
    "query": {
        "multi_match": {
            "query": "123",
            "fields": ["some_field", "another_field"]
        }
    },
    "highlight": {
        "fields": {
            "some_field": {},
            "another_field": {}
        }
    },
    "size": 10,
    "fields": ["field","another_field"]
}'
like image 184
gertruda Avatar answered Dec 07 '22 19:12

gertruda