I have an elastic query as below:
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "my search term",
"fields": [
"title",
"keywords",
"text"
]
}
}
],
"filter": [
{
"terms": {
"catid": [
83
]
}
},
{
"range": {
"updateDate": {
"gte": "now/d-30d",
"lte": "now/d"
}
}
}
]
}
},
"aggs": {
"global_aggregation": {
"global": {},
"aggs": {
"filtered_aggregation": {
"filter": {
"bool": {
"must": [
{
"multi_match": {
"query": "my search term",
"fields": [
"title",
"keywords",
"text"
]
}
},
{
"terms": {
"status": [
2
]
}
}
]
}
},
"aggs": {
"by_time_period": {
"date_histogram": {
"field": "updateDate",
"calendar_interval": "quarter",
"format": "yyyy-MM-dd",
"order": {
"_key": "desc"
}
},
"aggs": {
"top_hits_per_bucket": {
"top_hits": {
"size": 10,
"_source": [
"title",
"msid",
"keywords",
"updateDate"
]
}
}
}
}
}
}
}
}
},
"size": 0,
"_source": [
"title",
"msid",
"keywords",
"updateDate"
]
}
Now the problem is that in the aggregated results I am getting the date_histogram for results only for the last 30 days but the global aggregations should ignore the range query in the main search. What Can I do to fix this query? If I remove the range filter from main query I get the date histograms for all the past years since beginning
Query filters affect both query results (hits) and aggregation results. So in your case, in the aggs results there will be only results from last 30 days. You can use post_filter, it will only affect the search results (hits).
An example query:
GET /shirts/_search
{
"query": {
"bool": {
"filter": {
"term": { "brand": "gucci" }
}
}
},
"aggs": {
"colors": {
"terms": { "field": "color" }
},
"color_red": {
"filter": {
"term": { "color": "red" }
},
"aggs": {
"models": {
"terms": { "field": "model" }
}
}
}
},
"post_filter": {
"term": { "color": "red" }
}
}
- The main query now finds all shirts by Gucci, regardless of color.
- The colors agg returns popular colors for shirts by Gucci.
- The color_red agg limits the models sub-aggregation to red Gucci shirts.
- Finally, the post_filter removes colors other than red from the search hits.