title: Elasticsearch filtering by array field size
body: <p>I have a collection in MongoDB with the following structure:</p>
  
  <pre><code>[
      {
          "Name" : "A",
          "CustomerInfo" : [ 
              {
                  "CustomerCompany" : "abc",
                  "CustomerEmail" : "user1@abc.com",
                  "SubmissionDate" : 1415070090
              }, 
              {
                  "CustomerCompany" : "xyz",
                  "CustomerEmail" : "user2@xyz.com",
                  "SubmissionDate" : 1438820000
              }
          ]
      },
      {
          "Name" : "B",
      },
      {
          "Name" : "C",
          "CustomerInfo" : [ 
              {
                  "CustomerCompany" : "mno",
                  "CustomerEmail" : "user3@mno.com",
                  "SubmissionDate" : 1412590000
              }
          ]
      }
      ...
  ]
  </code></pre>
  
  <p>I created a river for this collection in elasticsearch using <a href="https://github.com/richardwilly98/elasticsearch-river-mongodb" rel="nofollow noreferrer">Elasticsearch-river-plugin</a></p>
  
  <pre><code>curl -XPUT 'http://localhost:9200/_river/product_custinfo/_meta' -d '{
    "index": {
      "name": "CustomerInfo",
      "type": "CustomerInfo"
    },
    "mongodb": {
      "db": "MyDB",
      "collection": "CustInfo"
    },
    "type": "mongodb"
  }'
  </code></pre>
  
  <p>Mapping created by river:</p>
  
  <pre><code>"CustomerInfo" : {
      "properties" : {
          "CustomerCompany" : {
              "type" : "string"
          },
          "CustomerEmail" : {
              "type" : "string"
          },
          "SubmissionDate" : {
              "type" : "long"
          }
      }
  }
  </code></pre>
  
  <p>Now I want to query documents that have more than 2 customers. (Note: It can be any value).
  I was able to get the results in MongoDB using the following query:</p>
  
  <pre><code>db.CustInfo.find({'$where' : 'this.CustomerInfo.length &gt; 2'})
  </code></pre>
  
  <p>Based on <a href="https://stackoverflow.com/questions/15543308/elasticsearch-filtering-by-the-size-of-a-field-that-is-an-array">this</a> question, I tried this elasticsearch query:</p>
  
  <pre><code>{
      "query": {
          "filtered": {
              "filter": {
                  "bool": {
                      "must": [
                          {"exists" : {
                              "field" : "CustomerInfo"
                              }
                          },
                          {"script" : {
                              "script" : "doc['CustomerInfo'].size() &gt; 2"
                              }
                          }
                      ]
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>which resulted in this error:</p>
  
  <blockquote>
    <p>nested: GroovyScriptExecutionException[ElasticsearchIllegalArgumentException[No field found for [CustomerInfo] in mapping with types []]];</p>
  </blockquote>
  
  <p>Using <code>_source</code> instead of <code>doc</code> gives the correct result but is very slow.</p>
  
  <pre><code>"script" : {
      "script" : "_source.CustomerInfo.size() &gt; 2"
  }
  </code></pre>
  
  <p>Is there another way to filter the result based on array field size?
  I don't want to create a new field with number of customers.</p>
  
  <hr>
  
  <p>MongoDB : 3.0.7</p>
  
  <p>ElasticSearch : 1.4.2</p>
  
  <p>Elasticsearch-River-Plugin : 2.0.9</p>