# Nested Filters returns no results on specific attribute

**URL:** <https://discuss.elastic.co/t/nested-filters-returns-no-results-on-specific-attribute/1390>\
**Category:** Elasticsearch\
**Created:** [May 27, 2015, 1:39pm UTC](https://discuss.elastic.co/t/nested-filters-returns-no-results-on-specific-attribute/1390 "2015-05-27T13:39:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SpinBoldak](https://avatars.discourse-cdn.com/v4/letter/s/67e7ee/32.png) [@SpinBoldak](https://discuss.elastic.co/u/SpinBoldak)\
**Post date:** [May 27, 2015, 1:39pm UTC](https://discuss.elastic.co/t/nested-filters-returns-no-results-on-specific-attribute/1390/1 "2015-05-27T13:39:52Z")

</div>

Hi,

I'm trying to apply a nested filter on a document but, for when I filter specifically by one of the nested attributes, I always get 0 results. For example, with a mapping like:

```
"PARTNER": {
   "properties": {
        "Addresses": {
             "properties": {
                    "CountyCode": {
                             "type": "string"
                    },
                    "EntityCode": {
                        "type": "string"
                    }, [...]
               }
        }, "type": "nested"
     }, [...]
}

```

I'm able to perform a filter on the CountyCode attribute, but if I filter on the EntityCode, i always get zero results:

```
{
  "from": 0,
  "size": 10,
  "query": {
    "filtered": {
      "query": {
        "bool": {
          "must": {
            "match_all": {}
          }
        }
      },
      "filter": {
        "nested": {
          "filter": {
            "bool": {
              "must": {
                "query": {
                  "query_string": {
                    "query": "*",
                    "fields": [
                      "EntityCode"
                    ]
                  }
                }
              }
            }
          },
          "path": "Addresses"
        }
      }
    }
  }
}

```

That query gets zero results, if I replace the field with CountyCode, it gives me proper results.  
I'm performing the queries on the web-based console, on ES 1.4.2,.

The nested documents have the format:

```
{
    "CountyCode": "516",
    "EntityCode": "203",
    [...]
}

```

Any idea on why that one field returns nothing ?

Thanks!

---

<div class="post-metadata">

**Author:** ![Camilo\_Sierra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/camilo_sierra/32/14397_2.png) [@Camilo\_Sierra](https://discuss.elastic.co/u/Camilo_Sierra)\
**Post date:** [May 28, 2015, 8:02am UTC](https://discuss.elastic.co/t/nested-filters-returns-no-results-on-specific-attribute/1390/2 "2015-05-28T08:02:29Z")

</div>

ok they are some problems in you query! if the entityCode is a number you dont need to use a query you can just use the [term filter](https://www.elastic.co/guide/en/elasticsearch/reference/1.5/query-dsl-nested-filter.html#query-dsl-nested-filter) it will be faster! query is used if you need to make text search and if you need a score between documents.  
what is your use case ? what you wait as reponse for elasticsearch ?  
filtered is also only used if you need to mix query and filter, but if your query is match\_all you don't need the query ni filtered level.  
if you need all the docs that have entitycode 203 the query will be something like that :

```
{
    "from": 0,
    "size": 10,
    "filter": {
        "nested": {
            "path": "Addresses",
            "filter": {
                "bool": {
                    "must": [
                        {
                            "term": {
                                "Addresses.EntityCode": "203"
                            }
                        }
                    ]
                }
            }
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![SpinBoldak](https://avatars.discourse-cdn.com/v4/letter/s/67e7ee/32.png) [@SpinBoldak](https://discuss.elastic.co/u/SpinBoldak)\
**Post date:** [May 28, 2015, 8:25am UTC](https://discuss.elastic.co/t/nested-filters-returns-no-results-on-specific-attribute/1390/3 "2015-05-28T08:25:11Z")

</div>

Hi Camilo,

Thanks for the input, that query was just a very simplified one that I was using to debug the problem, but yes, for plain numbers/code I should use the term filter.

Anyway, the missing piece was the full path on the actual filter ("Addresses.EntityCode" vs. "EntityCode"). I actually had another EntityCode attribute on a different path. As of now, that solved it!

Thanks

---

<div class="post-metadata">

**Author:** ![Camilo\_Sierra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/camilo_sierra/32/14397_2.png) [@Camilo\_Sierra](https://discuss.elastic.co/u/Camilo_Sierra)\
**Post date:** [May 28, 2015, 8:30am UTC](https://discuss.elastic.co/t/nested-filters-returns-no-results-on-specific-attribute/1390/4 "2015-05-28T08:30:09Z")

</div>

good news! with your new terms filter you will use the [bitsets](https://www.elastic.co/blog/all-about-elasticsearch-filter-bitsets) that is faster than the query 😉

---

<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 6, 2017, 12:11am UTC](https://discuss.elastic.co/t/nested-filters-returns-no-results-on-specific-attribute/1390/5 "2017-07-06T00:11:25Z")

</div>


