Does the aggregation-search-filter order in elasticsearch query indicate runtime (efficiency)?
I have an elasticsearch query with filter-search-terms aggregation. Both queries will give the same result. Both queries have the same filter type, only the difference is the order (place) of the search-criteria filter.
1.The search filter interface is in the 2nd / last place of the filter aggregation and .
{
"size": 0,
"aggs": {
"filterAggs": {
"filter": {
"and": {
"filters": [
{
"range": {
"eligibleDates": {
"include_lower": true,
"include_upper": true,
"from": <fromDate>,
"to": <toDate>
}
}
},
{
"terms": {
"rollNo": {
"path": "student.rollNo",
"index": "<index_name>",
"id": "<record_id>",
"type": "<es Type>"
}
}
}
]
}
}
}
}
}
2.Then the search filter term is in the 1st position of the filter aggregation and .
{
"size": 0,
"aggs": {
"filterAggs": {
"filter": {
"and": {
"filters": [
{
"terms": {
"rollNo": {
"path": "student.rollNo",
"index": "<index_name>",
"id": "<record_id>",
"type": "<es Type>"
}
}
},
{
"range": {
"eligibleDates": {
"include_lower": true,
"include_upper": true,
"from": <fromDate>,
"to": <toDate>
}
}
}
]
}
}
}
}
}
In my experiment / test the 1st query is executed more efficiently (7-10 times faster) than the 2nd. Now my question is, does the order of aggregation-search-filter-filter in elasticsearch query affect runtime (efficiency)? How does the filter order affect runtime?
No It doesn't matter, because elasticsearch will find an optimized execution order for certain algorithms.
I saw in some articles they say that you must order filters / queries by some criteria (for example, one of them more filtering should go first), but after reading a very detailed and accurate article, it turned out that they are incorrect . This is a great article on how to execute filter / query in elasticsearch:
What order are my Elasticsearch queries / filters executed in?
If you want to optimize your query, I suggest you read the following:
Elasticsearch search optimization
For example, you have range filter
and it can be optimized with a part Cache Granularity and Acceleration Filters
.