多个嵌套路径上的 Elasticsearch 过滤器

Elasticsearch filter on mulitple nested paths

我有一个带有嵌套数据的 ES 索引,它是这样映射的

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

我想创建一个过滤两个(原始)值的查询。我能够创建一个过滤器来过滤这些值之一,如下所示:

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

然而,我需要的是这样的:

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

第一个查询有效,第二个查询出现错误:

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

我将如何着手创建一个过滤器来匹配多个路径中的字段?

这个怎么样:

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

我认为您遇到的问题是嵌套过滤器使用不当造成的,尽管从异常消息中找出确切的问题有点困难。本质上,如果您想组合两个嵌套过滤器,则必须使用 booland 过滤器对它们进行分组。

为了更小 post,我已经大大减少了查询。我没有 post_filter 因为您的示例中没有任何聚合。我只用它们拥有的单个术语过滤器替换了内部 bool 过滤器,并在该附加键 (term) 过滤器上标记。但是您可以继续使用您的查询结构并根据需要扩展它,主要修复解决两个嵌套过滤器的组合方式。