Kaushal Prajapati
Kaushal Prajapati

Reputation: 73

How do I create an “or” Condition filter using elasticsearch-dsl-py?

The query below is what I would like to construct using elasticsearch-dsl-py, but I do not know how to do it.

GET /my_index/_search
{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
           "or": {
            "filters": [
            {
              "term": {
                "status": "a"
              },
              "term": {
                "x_status": "a"
              },
             
            }
          ]
         }
        }
      }
    }
  }
}

I just want to execute a query like below in SQL format

select * from my_index where status = "a" or x_status="a"

Upvotes: 0

Views: 626

Answers (1)

Val
Val

Reputation: 217424

I'm not sure which version of ES you're running, but just know that filtered has been replaced by bool a long time ago in version 5. So your query can be rewritten like this:

GET /my_index/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "status": "a"
          }
        },
        {
          "term": {
            "x_status": "a"
          }
        }
      ]
    }
  }
}

Using elasticsearch-dsl-py, this translates to:

s = Search()
s = s.query('bool', should=[Q('term', status='a'), Q('term', x_status='a')])

Upvotes: 2

Related Questions