0

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"

1 Answers1

2

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')])
Val
  • 207,596
  • 13
  • 358
  • 360