has_child 和 has_parent 未返回结果

has_child and has_parent not returning results

我在粘贴问题之前浏览了以下链接

ElasticSearch 7.3 has_parent/has_child don't return any hits

ES documentation

我创建了一个简单的映射,其中 text_doc 作为父项,flag_doc 作为子项。

{
  "doc_index_ap3" : {
    "mappings" : {
      "properties" : {
        "domain" : {
          "type" : "keyword"
        },
        "email_text" : {
          "type" : "text"
        },
        "id" : {
          "type" : "keyword"
        },
        "my_join_field" : {
          "type" : "join",
          "eager_global_ordinals" : true,
          "relations" : {
            "text_doc" : "flag_doc"
          }
        }
      }
    }
  }
}

使用 parent_id 的查询工作正常 & returns 1 文档如预期

GET doc_index_ap3/_search
{
  "query": {
      "parent_id": {
        "type": "flag_doc",
        "id":"f0d2cb3c-bf4b-11eb-9f67-93a282921115"
      }
  }
}

但是下面的 none 查询 return 任何结果。

GET doc_index_ap3/_search
{
  "query": {
    "has_parent": {
      "parent_type": "text_doc",
      "query": {
        "match_all": {
        }
      }
    }
  }
}

GET doc_index_ap3/_search
{
  "query": {
    "has_child": {
      "type": "flag_doc",
      "query": {
        "match_all": {}
      }
    }
  }
}

您为父文档和子文档编制索引的方式一定有问题。参考这个官方文档,了解更多 parent-child relationship

使用上述问题中给出的相同索引映射添加一个工作示例

text_doc 上下文中的父文档

PUT /index-name/_doc/1

{
  "domain": "ab",
  "email_text": "ab",
  "id": "ab",
  "my_join_field": {
    "name": "text_doc"
  }
}

子文档

PUT /index-name/_doc/2?routing=1&refresh



 {
  "domain": "cs",
  "email_text": "cs",
  "id": "cs",
  "my_join_field": {
    "name": "flag_doc",
    "parent": "1"
  }
}

搜索查询:

{
  "query": {
    "has_parent": {
      "parent_type": "text_doc",
      "query": {
        "match_all": {
        }
      }
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "67731507",
        "_type": "_doc",
        "_id": "2",
        "_score": 1.0,
        "_routing": "1",
        "_source": {
          "domain": "cs",
          "email_text": "cs",
          "id": "cs",
          "my_join_field": {
            "name": "flag_doc",
            "parent": "1"
          }
        }
      }
    ]

搜索查询:

{
  "query": {
    "has_child": {
      "type": "flag_doc",
      "query": {
        "match_all": {}
      }
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "67731507",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.0,
        "_source": {
          "domain": "ab",
          "email_text": "ab",
          "id": "ab",
          "my_join_field": {
            "name": "text_doc"
          }
        }
      }
    ]