Home > front end >  Elasticsearch query : how to use terms query with Range?
Elasticsearch query : how to use terms query with Range?

Time:12-10

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,

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

}

CodePudding user response:

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
          }
        }
      }
    }
  }
}
  • Related