Rakesh
Rakesh

Reputation: 281

Multiple AND and OR condition in elasticsearch

I having trouble with elasticsearch query.

Data Structrue: [{agent : "abc", origin: "US"}, {agent : "abc", origin: "US"}

I'm not able to find multiple agent name (OR condition) and (AND Condition) multiple Origin (OR condition)

Upvotes: 0

Views: 58

Answers (2)

Bhavya
Bhavya

Reputation: 16192

You can use a combination of bool/must/should clause along with the terms query

    {
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "agent": [
              "abc",
              "abc"
            ]
          }
        },
        {
          "terms": {
            "origin": [
              "US",
              "US"
            ]
          }
        }
      ]
    }
  }
}

Upvotes: 2

Val
Val

Reputation: 217544

Since terms already has OR semantics, you don't need to wrap them in bool/should queries. The following query should do what you expect:

{
  "query": {
    "bool": {
      "filter": [
        {
          "terms": {
            "agent": [
              "agent1",
              "agent2"
            ]
          }
        },
        {
          "terms": {
            "origin": [
              "US",
              "CA"
            ]
          }
        }
      ]
    }
  }
}

Upvotes: 1

Related Questions