Lucas Oliveira
Lucas Oliveira

Reputation: 113

Combine elasticsearch queries

I recently started learning Elasticsearch and am looking to combine two queries.

{
    'query': {
        'match': {
            'title': 'phrase'
        }
    }
}

And

{
    'query': {
        'range': {
           '@timestamp': {
              'gte': 'now-30d',
              'lte': 'now'
            }
        }
    }
}

Reading the documentation I saw that it is possible to perform this combination with bool, but I don't know exactly how I structure it.

Upvotes: 1

Views: 53

Answers (1)

jaspreet chahal
jaspreet chahal

Reputation: 9109

bool is a container which provides four options

  1. Must- It works like AND

  2. Should - It works like OR

  3. Must_not - It works like NOT

  4. Filter - It works as AND but doesn't calculate score.

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "phrase"
          }
        }
      ],
      "filter": [
        {
          "range": {
            "@timestamp": {
              "gte": "now-30d",
              "lte": "now"
            }
          }
        }
      ]
    }
  }
}

Upvotes: 2

Related Questions