Elasticsearch has_child 未返回任何结果

Elasticsearch has_child returning no results

我正在尝试获取父文档的所有子文档,但 has_child 查询没有得到任何结果,

{
    "index": "index_x",
    "include_type_name": true,
    "body": {
        "mappings": {
            "agents": {
                "properties": {
                    "id": {
                        "type": "keyword"
                    },
                    "listings": {
                        "type": "join",
                        "eager_global_ordinals": true,
                        "relations": {
                            "agent": "listing"
                        }
                    },
                    "name": {
                        "type": "object"
                    }
                }
            }
        }
    }
}

这是我的查询

{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "_id": <id>
                    }
                },
                {
                    "has_child": {
                        "type": "listing",
                        "query": {
                            "match_all": {}
                        },
                        "inner_hits": {}
                    }
                }
            ]
        }
    }
}

但是,当我 运行 这个查询时,我得到的子结果很好

{
    "query": {
        "bool": {
            "must": [
                {
                    "parent_id": {
                        "type":"listing",
                        "id": <id>
                    }
                }
            ]
        }
    }
}

与 has_parent 查询相同,未获得任何结果。 我正在使用 Elasticsearch 7.7

听起来您想使用 has_parent 查询。这是它如何在 ESv7.7 上工作的最小示例:

PUT /so
{
    "mappings": {
        "properties" : {
            "my-join-field" : {
                "type" : "join",
                "relations": {
                    "parent": "child"
                }
            },
            "tag" : {
                "type" : "keyword"
            }
        }
    }
}

POST /so/_doc/1
{
  "my-join-field": "parent",
  "tag": "Adult"
}

POST /so/_doc/2?routing=1
{
  "my-join-field": {
    "name": "child",
    "parent": "1"
  },
  "tag": "Youth"
}

POST /so/_doc/3?routing=1
{
  "my-join-field": {
    "name": "child",
    "parent": "1"
  },
  "tag": "Youth2"
}

GET /so/_search
{
  "query": {
    "has_parent": {
      "parent_type": "parent",
      "query": {
        "match": {
          "tag": "Adult"
        }
      }
    }
  }
}