# Filter Query Not Working

**URL:** https://discuss.elastic.co/t/filter-query-not-working/43685
**Category:** Elasticsearch
**Created:** [March 7, 2016, 5:50pm UTC](https://discuss.elastic.co/t/filter-query-not-working/43685 "2016-03-07T17:50:02Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![K\_Samanth\_Kumar\_Redd](https://avatars.discourse-cdn.com/v4/letter/k/e9c0ed/32.png) [@K\_Samanth\_Kumar\_Redd](https://discuss.elastic.co/u/K_Samanth_Kumar_Redd)
#### Post date: [March 7, 2016, 5:50pm UTC](https://discuss.elastic.co/t/filter-query-not-working/43685/1 "2016-03-07T17:50:02Z")

</div>

Hi,

I am newbie to elasticsearch. I have written the following two queries on my existing elasticsearch index.

**Query 1:**  
{  
"size" : 100,  
"from" : 0,

```
"query": {
    "bool": {
        "must": [
            { "match": { "eu_name": "ARTEA AG REG TOSCANA EROGAZ AGRIC" }},
            {
                "nested": {
                    "path": "SUPPORT",

                    "query": {
                        "bool": {
                            "must": [
                                { "match": { "SUPPORT.ticket_id": "77098"}}
                            ]
                        }
                    }                        
                }
            }
        ]
    }
}

```

}

**Query 2:**

{  
"size" : 100,  
"from" : 0,

```
"query": {
    "bool": {
        "must": [
            { "match": { "eu_name": "ARTEA AG REG TOSCANA EROGAZ AGRIC" }},
            {
                "nested": {
                    "path": "SUPPORT",

                    "query": {
                        "bool": {
                            "must": [
                                { "match": { "SUPPORT.ticket_id": "77098"}}
                            ]
                        }
                    },

                    "filter": {
                        "range": {
                            "SUPPORT.closed_Dt": {
                                "gt": "2015-02-13"
                            }
                        }
                    }
                }
            }
        ]
    }
}

```

}

"Query 1" is giving the result having the 1 document. Here Query 1 is formed without applying the filter.  
"Query 2" is giving the result having the 2 documents. Here Query 2 is formed by applying the filter.

I am wondering why the "Query 2" has given 2 document after applying the filter.

I am really wondering!!!

It would be greatly appreciated if any expert can throw some light on this magical problem.

Thank You!

Regards,  
Samanth

---

<div class="post-metadata">

### Author: ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)
#### Post date: [March 8, 2016, 10:01am UTC](https://discuss.elastic.co/t/filter-query-not-working/43685/2 "2016-03-08T10:01:05Z")

</div>

Query 2 is malformed, it cannot provide both a query and a filter. It should look like this:

```auto
{
	"query": {
		"bool": {
			"must": [{
				"match": {
					"eu_name": "ARTEA AG REG TOSCANA EROGAZ AGRIC"
				}
			}, {
				"nested": {
					"path": "SUPPORT",

					"query": {
						"bool": {
							"must": [{
								"match": {
									"SUPPORT.ticket_id": "77098"
								}
							}],
							"filter": [{
								"range": {
									"SUPPORT.closed_Dt": {
										"gt": "2015-02-13"
									}
								}
							}]
						}
					}
				}
			}]
		}
	}
}

```

---

<div class="post-metadata">

### Author: ![K\_Samanth\_Kumar\_Redd](https://avatars.discourse-cdn.com/v4/letter/k/e9c0ed/32.png) [@K\_Samanth\_Kumar\_Redd](https://discuss.elastic.co/u/K_Samanth_Kumar_Redd)
#### Post date: [March 17, 2016, 8:02am UTC](https://discuss.elastic.co/t/filter-query-not-working/43685/3 "2016-03-17T08:02:48Z")

</div>

Thank You!

Even this query did not work with my elasticsearch. Because I am using old version.  
I have changed my elasticsearch version to latest. Now it is working.

Thank you!.

Regards,  
Samanth

---

<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, 11:07pm UTC](https://discuss.elastic.co/t/filter-query-not-working/43685/4 "2017-07-05T23:07:37Z")

</div>


