title: Elasticsearch query: combine must_not conditions
body: <p>I am creating a query filter for multiple court documents in elasticsearch. In each document, the source contains a JSON object, defining the court that took the decision. To make the question clear, I will add some examples.</p>
  <p>E.g. Court of Appeal in city one:</p>
  <pre><code>&quot;_source&quot;: {
        &quot;CourtInstance&quot;: {
              &quot;CourtInstance&quot;: &quot;&quot;,
              &quot;CourtInstanceType&quot;: &quot;Court of appeal&quot;,
              &quot;CourtInstanceDivission&quot;: &quot;&quot;,
              &quot;CourtInstanceLocation&quot;: &quot;cityOne&quot;
          },
          *** further source omitted***
  }
  </code></pre>
  <p>E.g. court of appeal in city two:</p>
  <pre><code>&quot;_source&quot;: {
        &quot;CourtInstance&quot;: {
              &quot;CourtInstance&quot;: &quot;&quot;,
              &quot;CourtInstanceType&quot;: &quot;Court of appeal&quot;,
              &quot;CourtInstanceDivission&quot;: &quot;&quot;,
              &quot;CourtInstanceLocation&quot;: &quot;cityTwo&quot;
          },
          *** further source omitted***
  }
  </code></pre>
  <p>E.g. <em>tribunal</em> in city two, division one (so the local tribunal and the court of appeal nr. 2 share the same city).</p>
  <pre><code>&quot;_source&quot;: {
        &quot;CourtInstance&quot;: {
              &quot;CourtInstance&quot;: &quot;&quot;,
              &quot;CourtInstanceType&quot;: &quot;Tribunal&quot;,
              &quot;CourtInstanceDivission&quot;: &quot;divissionOne&quot;,
              &quot;CourtInstanceLocation&quot;: &quot;cityTwo&quot;
          },
          *** further source omitted***
  }
  </code></pre>
  <p>Users should be able to filter out a specific court type, e.g. all Courts of appeal, but also just the Court of appeal of cityOne or cityTwo, depending on what is selected by the user.</p>
  <p>From what I understand, in order to combine multiple combinations, a bool query needs to be written within the first query (refer: <a href="https://stackoverflow.com/questions/38640784/combining-must-not-in-elasticsearch-query">Combining must_not in ElasticSearch Query</a> - <a href="https://stackoverflow.com/questions/66849856/combining-two-conditions-in-must-not-elasticsearch">Combining two conditions in must_not ElasticSearch</a> - see especially the changes in versions of and the adaption of the query format).</p>
  <p>I thus wrote the following query:</p>
  <pre><code>{&quot;query&quot;: {
      &quot;bool&quot;: {
          &quot;must_not&quot; : [
              {
                
                      &quot;bool&quot;: {
                          &quot;must&quot;: [
                              {
                                 &quot;match&quot;: {
                                   &quot;CourtInstance.CourtInstanceType&quot;: &quot;Court of appeal&quot;
                                 }
                              },
                              {
                                 &quot;match&quot;: {
                                   &quot;CourtInstance.CourtInstanceLocation&quot;: &quot;cityOne&quot;
                                 }
                              }
                          ]
                      }
                  }
              
          ]
      }
  }
              
  }
  </code></pre>
  <p>This works fine for the Court of Appeal of City one. If the user selects that he/she does not wants to get the results of the court of appeal of city one, the decisions of the court of appeal of city two are still returned.</p>
  <p>However, if I change the CourtInstanceLocation to cityTwo, and keep the &quot;CourtInstanceType&quot; at &quot;Court of appeal&quot;, the tribunal of cityTwo is also filtered out, even though this one should still be there. The &quot;CourtInstance.CourtInstanceType&quot; of the tribunal !== &quot;Court of appeal&quot; but === &quot;Tribunal&quot;</p>
  <p>E.g of query that filters out both the court of appeal of city one and the tribunal of city two:</p>
  <pre><code>{&quot;query&quot;: {
      &quot;bool&quot;: {
          &quot;must_not&quot; : [
              {
                
                      &quot;bool&quot;: {
                          &quot;must&quot;: [
                              {
                                 &quot;match&quot;: {
                                   &quot;CourtInstance.CourtInstanceType&quot;: &quot;Court of appeal&quot;
                                 }
                              },
                              {
                                 &quot;match&quot;: {
                                   &quot;CourtInstance.CourtInstanceLocation&quot;: &quot;cityTwo&quot;
                                 }
                              }
                          ]
                      }
                  }
              
          ]
      }
  }
              
  }
  </code></pre>
  <p>EDIT: the mappings of the index</p>
  <pre><code>&quot;CourtInstance&quot; : {
            &quot;properties&quot; : {
              &quot;CourtInstance&quot; : {
                &quot;type&quot; : &quot;text&quot;,
                &quot;fields&quot; : {
                  &quot;keyword&quot; : {
                    &quot;type&quot; : &quot;keyword&quot;,
                    &quot;ignore_above&quot; : 256
                  }
                }
              },
              &quot;CourtInstanceLocation&quot; : {
                &quot;type&quot; : &quot;text&quot;,
                &quot;fields&quot; : {
                  &quot;keyword&quot; : {
                    &quot;type&quot; : &quot;keyword&quot;,
                    &quot;ignore_above&quot; : 256
                  }
                }
              },
              &quot;CourtInstanceType&quot; : {
                &quot;type&quot; : &quot;text&quot;,
                &quot;fields&quot; : {
                  &quot;keyword&quot; : {
                    &quot;type&quot; : &quot;keyword&quot;,
                    &quot;ignore_above&quot; : 256
                  }
                }
              }
  
  </code></pre>
  <p>Does someone have any idea why this is happening? The condition should be that if both conditions are met (AND) the decisions of this specific court won't be shown.</p>
  <p>Thank you and kind regards.</p>