title: What is the purpose of BoolQuery's "filter" in ElasticSearch?
body: <p>I read the documentation of <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html" rel="nofollow noreferrer">BoolQuery</a> and according to it, this the purpose, </p>
  
  <blockquote>
    <p><strong>filter</strong></p>
    
    <p>The clause (query) must appear in matching documents. However unlike
    must the score of the query will be ignored. Filter clauses are
    executed in filter context, meaning that scoring is ignored and
    clauses are considered for caching.</p>
  </blockquote>
  
  <p>Also from BoolQueryBuilder class: </p>
  
  <pre><code>   /**
       * Adds a query that &lt;b&gt;must&lt;/b&gt; appear in the matching documents but will
       * not contribute to scoring. No {@code null} value allowed.
       */
      public BoolQueryBuilder filter(QueryBuilder queryBuilder) {
          if (queryBuilder == null) {
              throw new IllegalArgumentException("inner bool query clause cannot be null");
          }
          filterClauses.add(queryBuilder);
          return this;
      }
  </code></pre>
  
  <p>but I can't get my head around, this. When should I use filter vs (should or must)</p>
  
  <p>Here is the example I am working on :</p>
  
  <blockquote>
    <p>I want to filter out some records based on the following assumptions :</p>
    
    <p>Fetch All </p>
    
    <p>1) Records where <code>deleted=0</code> and <code>isPrivate=true</code></p>
    
    <p><strong>AND</strong></p>
    
    <p>2) Records where <code>(isPrivate=false or [isPrivate=true and
    createdBy=loggedInUser])</code></p>
  </blockquote>
  
  <p>Here are the 2 queries which give the same result, I want to know what <code>filter</code> query signifies</p>
  
  <p><strong>Result without Filter using just must and should clause.</strong></p>
  
  <pre><code>"query": {
      "bool": {
        "must": [
          {
            "term": {
              "deleted": {
                "value": "0",
                "boost": 1
              }
            }
          },
          {
            "match": {
              "isPrivate": {
                "query": true
              }
            }
          },
          {
            "bool": {
              "should": [
                {
                  "term": {
                    "isPrivate": {
                      "value": "false",
                      "boost": 1
                    }
                  }
                },
                {
                  "bool": {
                    "must": [
                      {
                        "term": {
                          "createdBy": {
                            "value": "1742991596",
                            "boost": 1
                          }
                        }
                      },
                      {
                        "term": {
                          "isPrivate": {
                            "value": "true",
                            "boost": 1
                          }
                        }
                      }
                    ],
                    "adjust_pure_negative": true,
                    "boost": 1
                  }
                }
              ]
            }
          }
        ]
      }
    },
  </code></pre>
  
  <p><strong>Query with using filter</strong></p>
  
  <pre><code>"query": {
      "bool": {
        "adjust_pure_negative": true,
        "boost": 1,
        "filter": [
          {
            "bool": {
              "must": [
                {
                  "term": {
                    "deleted": {
                      "value": "0",
                      "boost": 1
                    }
                  }
                },
                {
                  "match": {
                    "isPrivate": {
                      "query": true
                    }
                  }
                }
              ],
              "should": [
                {
                  "term": {
                    "isPrivate": {
                      "value": "false",
                      "boost": 1
                    }
                  }
                },
                {
                  "bool": {
                    "must": [
                      {
                        "term": {
                          "createdBy": {
                            "value": "1742991596",
                            "boost": 1
                          }
                        }
                      },
                      {
                        "term": {
                          "isPrivate": {
                            "value": "true",
                            "boost": 1
                          }
                        }
                      }
                    ],
                    "adjust_pure_negative": true,
                    "boost": 1
                  }
                }
              ],
              "adjust_pure_negative": true,
              "boost": 1
            }
          }
        ]
      }
    }
  </code></pre>