Search code examples
elasticsearch-6

ElasticSearch: Exact match for multiple fields


How can I generate a query like this?

select * from topic where field1 = "abc" and field2 = "xyz"

I've tried the following but I can't get the correct syntax:

curl -X POST "localhost:9200/topic/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "terms" : { 
       "field1": "abc",
       "field2": "xyz"
    }
  }
}
'

Solution

  • How about...

    {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "field1": "abc"
              }
            },
            {
              "term": {
                "field2": "xyz"
              }
            }
            ]
          }
      }
    }
    

    The Terms Query doesn't do what you want. Instead it searches for different values within the same key.