title: elasticsearch: decide which query should run first
body: <p>We have a simple web page, where the user can provide some input and query the database. We currently use mongodb but want to migrate to elasticsearch, since the queries are faster. </p>
  
  <p>There are some required search fields, like start and end date, and some optional ones, like a search string to match an entry, or a parent search string, to match parent entries. Parent-child relations are just described through fields containing each entry's ancestors ids.</p>
  
  <p>The question is the following: If both search and parent search string are provided, is there a way to know before executing the queries, which query should be executed first, in order to provide results faster and to be more performant?</p>
  
  <p>For example, it could be that a specific parent search results in only 2 docs/parent entries, and then we can fetch all children matching the search string. In that case we should execute firstly the parent query and then the entry query.</p>
  
  <p>One option would be to get the count of both queries and then execute first the one with the smallest count, but isn't this solution worse, since the queries are going to be executed twice? Once for the count and once for the actual query.</p>
  
  <p>Are there any other options to solve this?</p>
  
  <p>PS. We use elasticsearch v1.7</p>
  
  <p><strong>Example</strong></p>
  
  <p>Let's say the user wants to search for all entries matching the following fields.</p>
  
  <p>searchString: <code>type:BLOCK AND name:test</code></p>
  
  <p>parentSearchString: <code>name:parentTest AND NOT type:BLOCK</code></p>
  
  <p>This means that we either have to </p>
  
  <ol>
  <li>fetch all entries (parents) matching the <code>parentSearchString</code> and store their ids. Then, we have to fetch all entries that match the <code>searchString</code> and also have to contain any of the parent ids in the <code>ancestors</code> field.</li>
  </ol>
  
  <p>OR</p>
  
  <ol start="2">
  <li>fetch all entries that match the <code>searchString</code> and store all <code>ancestors</code> ids. Then fetch all entries that match the <code>parentSearchString</code> and their id is one of the <code>ancestors</code> ids.</li>
  </ol>
  
  <p>Just to clarify, both parent and children entries have the exact same structure and reside in the same index. We cannot have different indices since the pare-child relation can be 10 times nested, so an entry can be both a parent and a child. An entry looks more or less like:</p>
  
  <pre><code>{
    id: "e32452365321",
    name: "name",
    type: "type",
    ancestors: "id1 id2 id3" // stored in node as an array of ids
  }
  </code></pre>