# Does terms-lookup-filter aggregation order in elasticsearch query affect the execution time (efficiency)?

**URL:** <https://discuss.elastic.co/t/does-terms-lookup-filter-aggregation-order-in-elasticsearch-query-affect-the-execution-time-efficiency/88351>\
**Category:** Elasticsearch\
**Created:** [June 6, 2017, 5:11am UTC](https://discuss.elastic.co/t/does-terms-lookup-filter-aggregation-order-in-elasticsearch-query-affect-the-execution-time-efficiency/88351 "2017-06-06T05:11:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bhesh\_Raj\_Pokhrel](https://avatars.discourse-cdn.com/v4/letter/b/6f9a4e/32.png) [@Bhesh\_Raj\_Pokhrel](https://discuss.elastic.co/u/Bhesh_Raj_Pokhrel)\
**Post date:** [June 6, 2017, 5:11am UTC](https://discuss.elastic.co/t/does-terms-lookup-filter-aggregation-order-in-elasticsearch-query-affect-the-execution-time-efficiency/88351/1 "2017-06-06T05:11:20Z")

</div>

I have following elasticsearch query with terms-lookup-filter aggregation. Both queries will give the same result. Both queries have same filter type only the difference is order (place) of terms-lookup-filter.

1.Here terms-lookup filter is in 2nd/last position of **and** filter aggregation.

```auto
{
   "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.Here terms-lookup filter is in 1st position of **and** filter aggregation.

```auto
{
   "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, 1st query executes more efficiently (7 to 10 times faster) than the second one. Now my question is, Does terms-lookup-filter aggregation order in elasticsearch query affect the execution time (efficiency)? How will the filter place order affect execution time?

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [June 9, 2017, 12:46pm UTC](https://discuss.elastic.co/t/does-terms-lookup-filter-aggregation-order-in-elasticsearch-query-affect-the-execution-time-efficiency/88351/2 "2017-06-09T12:46:42Z")

</div>

If you upgrade to 5.4, then filter order won't affect query execution. Elasticsearch will execute your query in the optimal order

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 7, 2017, 12:46pm UTC](https://discuss.elastic.co/t/does-terms-lookup-filter-aggregation-order-in-elasticsearch-query-affect-the-execution-time-efficiency/88351/3 "2017-07-07T12:46:53Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
