orshachar
orshachar

Reputation: 5037

how to know which keywords matched in elasticsaearch

Say that I query:

POST /story/story/_search
{  
   "query":{  
      "bool":{  
         "should":[  
            {  
               "match":{  
                  "termVariations":{  
                     "query":"not driving",
                     "type":"boolean",
                     "operator":"AND"
                  }
               }
            },
            {  
               "match":{  
                  "termVariations":{  
                     "query":"driving",
                     "type":"boolean",
                     "operator":"AND"
                  }
               }
            }
         ]
      }
   }
}

This query returned by one analyzer or another 3 documents. How do I tell which should clause was matched? Can Elasticsearch return the matched phrase along with the result?

Thanks!

Upvotes: 2

Views: 66

Answers (2)

Vineeth Mohan
Vineeth Mohan

Reputation: 19283

The best option here would be named queries. You can name your query and the name of the queries that matched would be provided per document.

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "name.first": {
              "query": "qbox",
              "_name": "first"
            }
          }
        },
        {
          "match": {
            "name.last": {
              "query": "search",
              "_name": "last"
            }
          }
        }
      ]
    }
  }
}

Upvotes: 2

orshachar
orshachar

Reputation: 5037

Thanks @keety! highlight was exactly what I was looking for!! :-)

Upvotes: 0

Related Questions