Maarten00
Maarten00

Reputation: 704

Elasticsearch filter on multiple nested paths

I have an ES index with nested data which is mapped like this

"mappings": {
    "voertuig": {
        "properties": {
            "vestiging": {
                "properties": {
                    "name_dtc": {
                        "type": "string",
                        "fields": {
                            "raw": {
                                "index": "not_analyzed",
                                "type": "string"
                            }
                        }
                    },
                },
                "type": "nested"
            },
            "accessoires": {
                "properties": {
                    "name": {
                        "type": "string",
                        "fields": {
                            "raw": {
                                "index": "not_analyzed",
                                "type": "string"
                            }
                        }
                    }
                },
                "type": "nested"
            }
        }
    }
}

I would like to create a query that filters on both (raw) values. I am able to create a filter that filters on one of these values, like this:

   {
        "body": {
            "post_filter": {
                "nested": {
                    "filter": {
                        "bool": {
                            "must": [
                                {
                                    "term": {
                                        "vestiging.name_dtc.raw": "Location X"
                                    }
                                }
                            ]
                        }
                    },
                    "path": "vestiging"
                }
            },
            "query": {
                "match_all": { }
             }
        },
        "index": "ocm",
        "type": "voertuig"
    }

However, what I need is something like this:

{
    "body": {
        "post_filter": {
            "nested": [
                {
                    "filter": {
                        "bool": {
                            "must": [
                                {
                                    "term": {
                                        "accessoires.name.raw": "Climate Control"
                                    }
                                }
                            ]
                        }
                    },
                    "path": "opties"
                },
                {
                    "filter": {
                        "bool": {
                            "must": [
                                {
                                    "term": {
                                        "vestiging.name_dtc.raw": "Location X"
                                    }
                                }
                            ]
                        }
                    },
                    "path": "vestiging"
                }
            ]
        },
        "query": {
            "filtered": {
                "filter": {
                    "term": {
                        "key": "33e75ff09dd6"
                    }
                },
                "query": []
            }
        }
    },
    "index": "ocm",
    "type": "voertuig"
}

The first query works the second one raises an error:

nested: QueryParsingException[[ocm] [nested] filter does not support [null]];

How would I go about creating a filter that matches fields in multiple paths?

Upvotes: 0

Views: 756

Answers (1)

eemp
eemp

Reputation: 1166

What about this:

{
  "query" : {
    "filtered" : {
      "filter" : {
        "bool" : {
          "must" : [
            {
              "nested": {
                "filter": {
                  "term": {
                    "accessoires.name.raw": "Climate Control"
                  }
                },
                "path": "accessoires"
              }
            },
            {
              "nested": {
                "filter": {
                  "term": {
                    "vestiging.name_dtc.raw": "Location X"
                  }
                },
                "path": "vestiging"
              }
            },
            {
              "term": {
                "key": "33e75ff09dd6"
              }
            }
          ]
        }
      }
    }
  }
}

I think the problem you are having is a result of incorrect usage of the nested filter although it is a bit difficult to get the exact problem from the exception message. Essentially if you want to combine two nested filters, you will have to group them using bool or and filters.

I have reduced the query quite a bit for the sake of a smaller post. I do not have the post_filter since you don't have any aggregations in your example. I replaced the inner bool filters with just the single term filter that they have and tagged on that additional key (term) filter. But you can continue using your query structure and expand it as you need with the main fix addressing how the two nested filters are put together.

Upvotes: 2

Related Questions