title: Retrieving documents by ids in a static list using ElasticSearch
body: <p>I am having a hard time trying to solve a problem. The problem is something like this -</p>
  
  <p>I have millions of documents in a type called <code>user</code> in ElasticSearch. There are two types of lists we create with these users. </p>
  
  <ol>
  <li>Dynamic - Depending on the conditions the list members changes (for eg users which were created in last 30 days)<br></li>
  <li>Static - The list of users is hardcoded with CSV of user id</li>
  </ol>
  
  <p>We query both types of the list from ElasticSearch. A dynamic list is easier to query as it has fewer conditions to evaluate and ES does that pretty well. But the problem is retrieving data from the static list as the query is a <code>terms</code> query which has performance issues when the number of user ids are larger. </p>
  
  <p>Following is my ES query</p>
  
  <pre><code>{
      "query": {
          "terms" : { "userId" : ["userId1", "userId2"]}
      }
  }
  </code></pre>
  
  <p>If I pass multiple userId (of the order of 50k) ES CPU spikes up and starts giving 504 error. </p>
  
  <p>Is there any alternative to storing static list where the data could be retrieved quickly from ES?</p>