您可以在 elasticsearch 中的 parent-child 连接中查询多个 children 之间的元素吗?

Can you query for elements between multiple children in a parent-child join in elasticsearch?

是否可以在children和return的元素中查询parent。因此,如果一个 child 的性别为男性,年龄为 17 岁,而另一个的性别为女性,年龄为 21 岁。是否有可能 return parent 当且仅当他们有一个男性 child 和一个年龄是 21 岁的 child?

添加包含索引数据、映射、搜索查询和搜索结果的工作示例

索引映射:

{
  "mappings": {
    "properties": {
      "age": {
        "type": "integer"
      },
      "gender": {
        "type": "text"
      },
      "my_join_field": {
        "type": "join",
        "relations": {
          "parents": "children"
        }
      }
    }
  }
}

索引数据:

父文档-

{
  "my_id": "1",
  "text": "This is parent 1",
  "my_join_field": {
    "name": "parents" 
  }
}

子文档- 这两个文档都是同一父项的子项

{
  "my_id": "4",
  "age": 21,
  "gender": "female",
  "text": "This is second child",
  "my_join_field": {
    "name": "children",
    "parent": "1"
  }
}
{
  "my_id": "3",
  "age": 17,
  "gender": "male",
  "text": "This is first child",
  "my_join_field": {
    "name": "children",
    "parent": "1"
  }
}

搜索查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "has_child": {
            "type": "children",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "gender": "male"
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "has_child": {
            "type": "children",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "age": 21
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "65677365",
        "_type": "_doc",
        "_id": "1",
        "_score": 2.0,
        "_source": {
          "my_id": "1",
          "text": "This is parent 1",
          "my_join_field": {
            "name": "parents"
          }
        }
      }
    ]