Nested Filters returns no results on specific attribute


#1

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!


(Camilo Sierra) #2

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 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"
                            }
                        }
                    ]
                }
            }
        }
    }
}

#3

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


(Camilo Sierra) #4

good news! with your new terms filter you will use the bitsets that is faster than the query :wink:


(system) #5