Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
    }
  }
}
'
like image 620
drum Avatar asked Feb 14 '26 01:02

drum


1 Answers

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.

like image 196
Strnm Avatar answered Feb 16 '26 02:02

Strnm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!