Is it possible to combine filters and nested filters?


(gmarz) #1

I have a document that contains nested documents. I know it is possible to
combine multiple filters on fields in the root document, but I haven't
figured out how to combine those filters with nested filters.

Take this query for example:

{
"from": 0,
"size": 20,
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must": [
{
"term": {
"company_id": 123
}
},
{
"nested": {
"filter": {
"term": {
"categories.name": {
"value": "Engineering"
}
}
},
"path": "categories"
}
}
]
}
}
}
}
}

company_id is a field at the root of the document. categories.name is a
field at the root of a nested category document. The following query
returns 0 results, although there are documents that match this criteria.
Is it not possible to combine filters like this, or am I just doing it
wrong?

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/39143c3b-f989-4f60-bce9-a59a3b857924%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(Binh Ly-2) #2

Should work fine. Since you are using a term filter which is an exact
case-sensitive match, is it possible that your categories.name field is
indexed differently (like standard) that would make term filter not match?

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/052801c4-7b6d-42a3-a40d-f841d09fc94b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(gmarz) #3

Hey Binh,

The field is indexed the same way I am searching on it, and should be an
exact match. The term filter matches when I remove the company_id filter,
and the company_id filter works when I remove the categories.name nested
filter. However, when combined, it does not return results.

On Wednesday, March 26, 2014 4:49:52 PM UTC-4, Binh Ly wrote:

Should work fine. Since you are using a term filter which is an exact
case-sensitive match, is it possible that your categories.name field is
indexed differently (like standard) that would make term filter not match?

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/fd63952d-ac7d-4985-91d4-e42728be6b62%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(Binh Ly-2) #4

Hmmm, not sure. I just tried this and it works for me (ES 1.1):

  1. PUT http://localhost:9200/n
    {
    "mappings": {
    "doc": {
    "properties": {
    "n1": {
    "type": "nested",
    "properties": {
    }
    }
    }
    }
    }
    }
  1. POST http://localhost:9200/n/doc
    { "company_id": 123, "n1": { "name": "engineering" } }

  2. POST http://localhost:9200/n/_search
    {
    "query": {
    "filtered": {
    "query": {
    "match_all": {}
    },
    "filter": {
    "bool": {
    "must": [
    {
    "term": {
    "company_id": 123
    }
    },
    {
    "nested": {
    "filter": {
    "term": {
    "n1.name": {
    "value": "engineering"
    }
    }
    },
    "path": "n1"
    }
    }
    ]
    }
    }
    }
    }
    }

And it returned the document fine. Maybe you can duplicate my steps and see
if it works for you.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/dcad6e92-e343-4d17-97b6-478332e5c808%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(gmarz) #5

Hey Binh,

I tried your example and it does in fact work. I realized my query was
just wrong...really stupid mistake. Thanks for your help!

On Wednesday, March 26, 2014 5:22:44 PM UTC-4, Binh Ly wrote:

Hmmm, not sure. I just tried this and it works for me (ES 1.1):

  1. PUT http://localhost:9200/n
    {
    "mappings": {
    "doc": {
    "properties": {
    "n1": {
    "type": "nested",
    "properties": {
    }
    }
    }
    }
    }
    }
  1. POST http://localhost:9200/n/doc
    { "company_id": 123, "n1": { "name": "engineering" } }

  2. POST http://localhost:9200/n/_search
    {
    "query": {
    "filtered": {
    "query": {
    "match_all": {}
    },
    "filter": {
    "bool": {
    "must": [
    {
    "term": {
    "company_id": 123
    }
    },
    {
    "nested": {
    "filter": {
    "term": {
    "n1.name": {
    "value": "engineering"
    }
    }
    },
    "path": "n1"
    }
    }
    ]
    }
    }
    }
    }
    }

And it returned the document fine. Maybe you can duplicate my steps and
see if it works for you.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/087014f1-0344-4cbd-8d3d-af370ae79ee6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(system) #6