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

0kjbasz6  于 8个月前  发布在  ElasticSearch
关注(0)|答案(1)|浏览(94)

我有一个ES索引,嵌套数据Map如下所示

"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不支持[null]];
如何创建一个匹配多个路径中的字段的过滤器?

lnxxn5zx

lnxxn5zx1#

这个怎么样:

{
  "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_filter,因为你的例子中没有任何聚合。我把内部的bool过滤器替换成了他们拥有的单个术语过滤器,并在附加的键(term)过滤器上标记。但是您可以继续使用您的查询结构,并根据需要扩展它,主要修复解决如何将两个嵌套过滤器放在一起。

相关问题