title: Elasticsearch terms look up using query
body: <p>I am using the bool filter for doing cardinality as well as value count aggregation. My term filter has almost 2000 terms and so it is taking a lot of time to load. Also for me it is not possible to test it manually by specifying the filter terms.</p>
  
  <p>I just read about 'Terms Lookup' feature. Is there any possibility that I can specify an array or another query in the terms lookup, so I an avoid fetching up the terms first uaing PHP and then using them in the aggregation query. </p>
  
  <p>My PHP code for the above metioned operation is like this :</p>
  
  <pre><code>$reportParams['body']['query'] = array(
      "filtered" =&gt; array(
          "filter" =&gt; array(
              "bool" =&gt; array(
                  "must" =&gt; array(
                      "range" =&gt; array(
                          "time" =&gt; array(
                              "gt" =&gt; $start,
                              "lt" =&gt; $end
                          )
                      )
                  ),
                  "should" =&gt;$termFilter
              )
          )
      )
      );
  
  $reportParams['body']['aggs'] = array(
  "distinct_user_count" =&gt; array(
      "cardinality" =&gt; array(
          "field" =&gt; $details-&gt;agg_field
      )
  ),
  "daily_event_count" =&gt; array(
      "value_count" =&gt; array(
          "field" =&gt; $details-&gt;agg_field
      )
  )
  );
  </code></pre>
  
  <p>and the termsFilter is an array like this :</p>
  
  <pre><code>[0] =&gt; Array
          (
              [term] =&gt; Array
                  (
                      [andi] =&gt; 84cc04c6059fb490
                  )
  
          )
  
      [1] =&gt; Array
          (
              [term] =&gt; Array
                  (
                      [andi] =&gt; af9cbd253100f2c7
                  )
  
          )
  
      [2] =&gt; Array
          (
              [term] =&gt; Array
                  (
                      [andi] =&gt; c1a3fcf0fefe7107
                  )
  
          )
  
      [3] =&gt; Array
          (
              [term] =&gt; Array
                  (
                      [andi] =&gt; b3a630084347fb2f
                  )
  
          )
  </code></pre>