# Post\_filter doesn't work on filtered documents - ES 1.0.2

**URL:** https://discuss.elastic.co/t/post-filter-doesnt-work-on-filtered-documents-es-1-0-2/58548
**Category:** Elasticsearch
**Created:** [August 22, 2016, 5:47am UTC](https://discuss.elastic.co/t/post-filter-doesnt-work-on-filtered-documents-es-1-0-2/58548 "2016-08-22T05:47:47Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![bneelima84](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bneelima84/32/28009_2.png) [@bneelima84](https://discuss.elastic.co/u/bneelima84)
#### Post date: [August 22, 2016, 5:47am UTC](https://discuss.elastic.co/t/post-filter-doesnt-work-on-filtered-documents-es-1-0-2/58548/1 "2016-08-22T05:47:47Z")

</div>

Hi,

We have around 1million documents in our index where we are performing search and have defined some script filters. As the script filters work on each document and is costly, we tried using post\_filter (which has script filters defined) in order to reduce the documents on which the script filters apply.

However, when we noticed the post\_filter is working on all documents rather than limiting to the filtered results  
Below is the query snippet:

```
{
  "from" : 0,
  "size" : 20,
  "query" : {
    "filtered" : {
      "query" : {
        "query_string" : {
          "query" : "test*",
          "default_field" : "fullContent",
          "default_operator" : "and",
          "allow_leading_wildcard" : true,
          "analyze_wildcard" : true
        }
      },
      "filter" : {
        "fquery" : {
          "query" : {
            "query_string" : {
              "query" : "firstName:(\"XY-Z\" OR \"A-B-C\")",
              "default_field" : "fullContent",
              "default_operator" : "and",
              "allow_leading_wildcard" : true
            }
          },
          "_cache" : false
        }
      }
    }
  },
  "post_filter" : {
    "or" : {
      "filters" : [ {
        "fquery" : {
          "query" : {
            "query_string" : {
              "query" : "firstName:A-B-C",
              "default_field" : "fullContent",
              "default_operator" : "and",
              "allow_leading_wildcard" : true
            }
          },
          "_cache" : false
        }
      }, {
        "and" : {
          "filters" : [ {
            "and" : {
              "filters" : [ {
                "and" : {
                  "filters" : [ {
                    "fquery" : {
                      "query" : {
                        "query_string" : {
                          "query" : "NOT HeirarchicalProp:10",
                          "default_field" : "fullContent",
                          "default_operator" : "and",
                          "allow_leading_wildcard" : true
                        }
                      },
                      "_cache" : false
                    }
                  }, {
                    "script" : {
                      "script" : "native_sample_script",
                      "params" : {
                        "userCode" : "1,2,3",
                        "recordCode" : "samplecolumn"
                      },
                      "lang" : "native",
                      "_name" : "sample"
                    }
                  } ]
                }
              }, {
                "script" : {
                  "script" : "native_test_script",
                  "params" : {
                    "userCode" : "7,8,9",
                    "recordCode" : "testcolumn"
                  },
                  "lang" : "native",
                  "_name" : "test"
                }
              } ]
            }
          }, {
            "fquery" : {
              "query" : {
                "query_string" : {
                  "query" : "firstName:XY-Z",
                  "default_field" : "fullContent",
                  "default_operator" : "and",
                  "allow_leading_wildcard" : true
                }
              },
              "_cache" : false
            }
          } ]
        }
      } ]
    }
  },
  "fields" : "*"
}

```

Am I missing any thing while using post\_filter in the above snippte ?

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [August 22, 2016, 6:13am UTC](https://discuss.elastic.co/t/post-filter-doesnt-work-on-filtered-documents-es-1-0-2/58548/2 "2016-08-22T06:13:41Z")

</div>

Please format your code with the `</>` button, it’s very hard to read.

---

<div class="post-metadata">

### Author: ![bneelima84](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bneelima84/32/28009_2.png) [@bneelima84](https://discuss.elastic.co/u/bneelima84)
#### Post date: [August 22, 2016, 3:55pm UTC](https://discuss.elastic.co/t/post-filter-doesnt-work-on-filtered-documents-es-1-0-2/58548/4 "2016-08-22T15:55:15Z")

</div>

Any ideas ?

---

<div class="post-metadata">

### Author: ![bneelima84](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bneelima84/32/28009_2.png) [@bneelima84](https://discuss.elastic.co/u/bneelima84)
#### Post date: [September 21, 2016, 4:22am UTC](https://discuss.elastic.co/t/post-filter-doesnt-work-on-filtered-documents-es-1-0-2/58548/5 "2016-09-21T04:22:39Z")

</div>

Understood why this happens.  
As "query" snippet is the last that gets executed, the documents on which the script filter or post filter gets executed is marginally high (depends on the filter conditions); For our case, we pushed the "query" as a "filter" and used match\_all for query; it gave us better performance.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 5, 2017, 10:18pm UTC](https://discuss.elastic.co/t/post-filter-doesnt-work-on-filtered-documents-es-1-0-2/58548/6 "2017-07-05T22:18:38Z")

</div>


