# Terms query routing optimization

**URL:** <https://discuss.elastic.co/t/terms-query-routing-optimization/286390>\
**Category:** Elasticsearch\
**Created:** [October 11, 2021, 4:21pm UTC](https://discuss.elastic.co/t/terms-query-routing-optimization/286390 "2021-10-11T16:21:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ragav\_Kb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ragav_kb/32/46637_2.png) [@Ragav\_Kb](https://discuss.elastic.co/u/Ragav_Kb)\
**Post date:** [October 11, 2021, 4:21pm UTC](https://discuss.elastic.co/t/terms-query-routing-optimization/286390/1 "2021-10-11T16:21:39Z")

</div>

My search is going to look something as below. For most cases the number of items in userId is expected to be between 100 to 200 atmost, although on some cases it may go up to 1000.

Is there anything to consider in general (either at the time of storage or the way i can define the mapping) that can help improve the performance of such query ?

I'm anticipating up to 20 million documents in the `userdocs` index.

```auto
GET userdocs/_search?pretty
{
  "query": {
    "terms": {
      "userId": [100, 200 ..... so on]
    }
  },
  "sort": [
    {
      "timestamp": {
        "order": "desc"
      }
    }
  ]
}

```

---

<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:** [November 8, 2021, 4:21pm UTC](https://discuss.elastic.co/t/terms-query-routing-optimization/286390/2 "2021-11-08T16:21:43Z")

</div>

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