elasticsearch 即使没有与查询匹配的子记录,也返回父记录

liwlm1x9  于 2022-11-02  发布在  ElasticSearch
关注(0)|答案(1)|浏览(172)

(this实际上是AWS OpenSearch,我相信它是Elastic Search 7.x的一个分支)
所以在这个人造的例子中,我在制造商和产品之间有一个父子关系。我想返回“acme”信息和所有的产品。一些产品可能是禁运的(还没有准备好向公众上市)。对于一个新公司,像acme,它只有新的禁运产品,所以当我运行这个查询时,我没有拿回公司信息。我试着使用"min_children": 0,,但我仍然没有拿回制造商。
对于此查询,如果其他制造商至少有一个产品未被禁运,则会返回这些制造商,因此它与has_child命中数有关,但不返回任何产品。

{
    "track_total_hits": true,
    "query": {
        "bool": {
            "must": [
                {
                    "has_child": {
                        "inner_hits": {
                            "name": "manf_products",
                            "size": 100
                        },
                        "min_children": 0,
                        "query": {
                            "bool": {
                                "should": [
                                    {
                                        "range": {
                                            "embargo_date": {
                                                "lt": "now/s"
                                            }
                                        }
                                    }
                                ]
                            }
                        },
                        "type": "product"
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "term": {
                                    "manuf": {
                                        "value": "acme"
                                    }
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}
wz8daaqr

wz8daaqr1#

min_children的最小值为1。This可以提供有关此问题的更多信息。
以下查询将返回未禁运的父文档和子文档。如果不存在未禁运的子文档,则将返回父文档

{
  "track_total_hits": true,
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "has_child": {
            "inner_hits": {
              "name": "manf_products",
              "size": 100
            },
            "query": {
              "bool": {
                "should": [
                  {
                    "range": {
                      "embargo_date": {
                        "lt": "now/s"
                      }
                    }
                  }
                ]
              }
            },
            "type": "product"
          }
        },
        {
          "bool": {
            "must_not": [
              {
                "has_child": {
                  "inner_hits": {
                    "name": "manf_products",
                    "size": 100
                  },
                  "query": {
                    "bool": {
                      "should": [
                        {
                          "range": {
                            "embargo_date": {
                              "lt": "now/s"
                            }
                          }
                        }
                      ]
                    }
                  },
                  "type": "product"
                }
              }
            ]
          }
        }
      ],
      "filter": [
        {
          "term": {
            "manf": {
              "value": "acme"
            }
          }
        }
      ]
    }
  }
}

相关问题