<pre><code>lets say records have city field as an array of city names. 
</code></pre>
<p>records ex:</p>
<pre><code>  record 1:
    {
      cities : [
        {name: city1},
        {name : city2},
        {name : city3}
      ]
    }
    record 2:
    {
      cities : [
        {name: city2},
        {name : city3},
        {name : city4}
      ]
    }
    record 3:
    {
      cities : [
        {name: city3},
        {name : city4},
        {name : city5}
      ]
    }
</code></pre>
<p>requirement:
My filter criteria is to fetch the records matches with city1 or city2 or city3 but since the record 1 matches all 3 it should come first and record 2 matches 2 so it should come 2nd and record 3 matches only one so it should come last.</p>

kuna
<p>You don't have to use the <code>nested</code> data-type as you don't have the nested properties or complex object, its very simple and easy to achieve.</p>
<h2>Working example</h2>
<p><strong>Index mapping</strong></p>
<pre><code>{
    &quot;mappings&quot;: {
        &quot;properties&quot;: {
            &quot;cities&quot;: {
                &quot;type&quot;: &quot;text&quot;
            }
        }
    }
}
</code></pre>
<p><strong>Index sample docs</strong></p>
<pre><code>{
    &quot;cities&quot;: [
        &quot;tel-aviv&quot;, &quot;bangalore&quot;, &quot;sf&quot;
    ]
}

{
    &quot;cities&quot;: [
        &quot;tel-aviv&quot;
    ]
}

{
    &quot;cities&quot;: [
        &quot;sf&quot;
    ]
}
</code></pre>
<p><strong>Search query</strong></p>
<pre><code>{
    &quot;query&quot;: {
        &quot;bool&quot;: {
            &quot;should&quot;: [
                {
                    &quot;match&quot;: {
                        &quot;cities&quot;: &quot;tel-aviv&quot;
                    }
                },
                {
                   &quot;match&quot;: {
                        &quot;cities&quot;: &quot;bangalore&quot;
                    }
                },
                 {
                   &quot;match&quot;: {
                        &quot;cities&quot;: &quot;sf&quot;
                    }
                }
            ] 
        }
    }
}
</code></pre>
<p><strong>And search result with proper expected result and score</strong></p>
<pre><code> &quot;hits&quot;: [
            {
                &quot;_index&quot;: &quot;cities&quot;,
                &quot;_type&quot;: &quot;_doc&quot;,
                &quot;_id&quot;: &quot;1&quot;,
                &quot;_score&quot;: 1.850198,
                &quot;_source&quot;: {
                    &quot;cities&quot;: [
                        &quot;tel-aviv&quot;,
                        &quot;bangalore&quot;,
                        &quot;sf&quot;
                    ]
                }
            },
            {
                &quot;_index&quot;: &quot;cities&quot;,
                &quot;_type&quot;: &quot;_doc&quot;,
                &quot;_id&quot;: &quot;2&quot;,
                &quot;_score&quot;: 0.9983525,
                &quot;_source&quot;: {
                    &quot;cities&quot;: [
                        &quot;tel-aviv&quot;
                    ]
                }
            },
            {
                &quot;_index&quot;: &quot;cities&quot;,
                &quot;_type&quot;: &quot;_doc&quot;,
                &quot;_id&quot;: &quot;3&quot;,
                &quot;_score&quot;: 0.6133945,
                &quot;_source&quot;: {
                    &quot;cities&quot;: [
                        &quot;sf&quot;
                    ]
                }
            }
        ]
</code></pre>
