Shail Patel
Shail Patel

Reputation: 1814

Elasticsearch multiple fields OR query

Here is an example record that I have stored in ES:

  "taskCurateStatus": true,
  "taskMigrateStatus": true,
  "verifiedFields": 7,
  "taskId": "abcdef123",
  "operatorEmail": "[email protected]"

Example Query I'm making via /_search:
{
  "sort": [
    {
      "@timestamp": {
        "order": "desc"
      }
    }
  ],
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "msg.operator_email": "[email protected]"
          }
        }
        {
          "range": {
            "@timestamp": {
              "gte": "2017-03-05",
              "lte": "2017-03-12"
            }
          }
        }
      ]
    }
  },
  "from": 0,
  "size": 50
}

Basically I want to also filter by documents that have EITHER taskCurateStatus or taskMigrateStatus be true. Some messages have only one of them defined. I was thinking of using a should query but not sure how that would work with the match query. Any help would be appreciated. Thanks

Upvotes: 0

Views: 61

Answers (1)

user3775217
user3775217

Reputation: 4803

you can add another boolean filter inside your must filter. This boolean filter can implemenet the should clause where you can compare the boolean flags with a should filter combining both the boolean check filters

{
    "sort": [{
        "@timestamp": {
            "order": "desc"
        }
    }],
    "query": {
        "bool": {
            "must": [{
                "match": {
                    "msg.operator_email": "[email protected]"
                }
            }, {
                "range": {
                    "@timestamp": {
                        "gte": "2017-03-05",
                        "lte": "2017-03-12"
                    }
                }
            }, {
                "bool": {
                    "should": [{
                        "term": {
                            "taskCurateStatus": {
                                "value": true
                            }
                        }
                    }, {
                        "term": {
                            "taskMigrateStatus": {
                                "value": true
                            }
                        }
                    }]
                }
            }]
        }
    },
    "from": 0,
    "size": 50
}

Take a look at the above query and see if the helps Thanks

Upvotes: 2

Related Questions