Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Elasticsearch query : how to use terms query with Range?

im new to Elasticsearch, how to use terms query with range? Or how to modify if this is not possible

here is my query

 {
 "size": 0,
 "query": {
"terms": {
  "action": [
    "created",
    "updated",
    "deleted"
  ]
}
},
 "aggs": {
   "2": {
  "terms": {
    "field": "action",
    "order": {
      "_count": "desc"
    },
    "size": 100
  },
  "aggs": {
    "3": {
      "date_histogram": {
        "field": "timestamp",
        "fixed_interval": "30m",,
        "min_doc_count": 1
      }
    }
  }
}
}
}

here is the time range which i want to add in it,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  {
"range": {
"timestamp": {
"gte": "now-5y",
  "lte": "now",
  "format": "epoch_millis"
 }

}

>Solution :

You need to combine both terms and range constraints using a bool/filter query, like this:

{
  "size": 0,
  "query": {
    "bool": {
      "filter": [
        {
          "terms": {
            "action": [
              "created",
              "updated",
              "deleted"
            ]
          }
        },
        {
          "range": {
            "timestamp": {
              "gte": "now-5y",
              "lte": "now",
              "format": "epoch_millis"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "2": {
      "terms": {
        "field": "action",
        "order": {
          "_count": "desc"
        },
        "size": 100
      },
      "aggs": {
        "3": {
          "date_histogram": {
            "field": "timestamp",
            "fixed_interval": "30m",
            "min_doc_count": 1
          }
        }
      }
    }
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading