hanleyhansen
hanleyhansen

Reputation: 6452

Elasticsearch AND Query

I'm trying to do something like this:

SELECT * FROM x WHERE __account_id = 5 AND __import_id IN [1,2,3]

Here is my ES query:

body = {
    "query": {
        "filtered" : { 
            "filter" : {
                "term" : { 
                    "__account_id" : account.id,
                    "__import_id": [x['id'] for x in ingested_imports]
                }
            }
        }
    }
}

Results seem to filter only on __account_id but not on __import_id. What am I doing wrong?

Upvotes: 0

Views: 128

Answers (1)

Dan Tuffery
Dan Tuffery

Reputation: 5924

You need to wrap a term and terms filter in a bool query because both clauses must match.

{
  "query":{
    "filtered":{
      "filter":{
        "bool":{
          "must":[
            {
              "term":{
                "__account_id":5
              }
            },
            {
              "terms":{
                "__import_id":[1, 2, 3]
              }
            }
          ]
        }
      }
    }
  }
}

Upvotes: 1

Related Questions