title: Filter query by length of nested objects. ie. min_child
body: <p>I'm trying to filter my query by the number of nested objects found. The Elastic Search documentation mentions that using a script is an expensive task, so I've set out to do it with a score, though I can't seem to get the script to work either.</p>
  <p><strong>Here's my mappings:</strong></p>
  <pre class="lang-json prettyprint-override"><code>&quot;mappings&quot;: {
    &quot;properties&quot;: {
      &quot;dates&quot; : {
        &quot;type&quot; : &quot;nested&quot;,
        &quot;properties&quot; : {
          &quot;rooms&quot; : {
            &quot;type&quot; : &quot;integer&quot;
          },
          &quot;timestamp&quot; : {
            &quot;type&quot; : &quot;long&quot;
          }
        }
      },
      &quot;doc_id&quot; : {
        &quot;type&quot; : &quot;text&quot;
      },
      &quot;distance&quot; : {
        &quot;type&quot; : &quot;integer&quot;
      }
      ...
    }
  }
  </code></pre>
  <p><strong>Here's some example data:</strong></p>
  <pre class="lang-json prettyprint-override"><code>PUT /test/_doc/1
  {
    &quot;doc_id&quot;: &quot;1&quot;,
    &quot;distance&quot;: 1,
    &quot;dates&quot;: [
      {
        &quot;rooms&quot;: 1,
        &quot;timestamp&quot;: 1
      },
      {
        &quot;rooms&quot;: 1,
        &quot;timestamp&quot;: 2
      },
      ...
    ]
  }
  </code></pre>
  <p>I'm filtering by the parents <code>distance</code> field, among others, and filtering the nested <code>dates</code> by their <code>timestamps</code>, and <code>rooms</code>. I need to filter all results to an exact number of nest dates found.</p>
  <p>I tried to borrow from <a href="https://gist.github.com/xethorn/290e31176f493814823a20f281e82fd4" rel="nofollow noreferrer">here</a>.</p>
  <p><strong>This is my search query:</strong></p>
  <pre class="lang-json prettyprint-override"><code>GET /test/_search
  {
    &quot;query&quot; : {
      &quot;function_score&quot;: {
        &quot;min_score&quot;: 20,
        &quot;boost&quot;: 1,
        &quot;functions&quot;: [
          {
            &quot;script_score&quot;: {
              &quot;script&quot;: {
                  &quot;source&quot;: &quot;if (_score &gt; 20) { return - 1; } return _score;&quot;
              }
            }
          }
        ],
        &quot;query&quot;: {
          &quot;bool&quot; : {
            &quot;filter&quot;: [
              { &quot;range&quot;: { &quot;distance&quot;: { &quot;lt&quot;: 5 }}},
              {
                &quot;nested&quot;: {
                  &quot;score_mode&quot;: &quot;sum&quot;,
                  &quot;boost&quot;: 10,
                  &quot;path&quot;: &quot;dates&quot;,
                  &quot;query&quot;: {
                    &quot;bool&quot;: {
                      &quot;filter&quot;: [
                        { &quot;range&quot;: { &quot;dates.rooms&quot;: { &quot;gte&quot;: 1 } } },
                        { &quot;range&quot;: { &quot;dates.timestamp&quot;: { &quot;lte&quot;: 2 }}},
                        { &quot;range&quot;: { &quot;dates.timestamp&quot;: { &quot;gte&quot;: 1 }}}
                      ]
                    }
                  }
                }
              }
            ]
          }
        }
      }
    }
  }
  </code></pre>
  <p>This returns all the results that match, yet they all have a score of 0.0 and aren't getting filtered by the number of nested objects found.</p>
  <p>If this is the right solution, how can I get this working? If not, how can I get a script to do it within this search?</p>
  <p>Thanks!</p>