在弹性搜索的一个查询中搜索 parent 及其全部 children

Search for a parent and all it's children in one query in elastic search

参考官方文档中提到的建立parent-child关系的例子- https://www.elastic.co/guide/en/elasticsearch/reference/current/parent-join.html

link 提供了 question-answer 连接关系,其中问题是 parent,答案是 child 类型。

如果您需要在一个查询中搜索所有匹配某些文本的 parents 以及它们匹配某些特定于 children 的文本的 children,您会怎么做?

假设他们在 json 文档中有 key-value 对,如下所示,我们搜索 parents 匹配问题中的文本,children 匹配answer-text.

中的值
Parent --> question --> "question-text" : "Geography: Where is Mt. Everest?"
Child --> answer --> "answer-text" : "Nepal?"

我们不希望结果中只有 parents 或 children,而是所有 parents 及其关联的 children 都匹配查询。

我知道内部命中是一个选项,但无法弄清楚用法 - https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html

我希望问题很清楚,如果需要可以添加更多细节。

我还尝试了此处提到的示例以获得 parent-child 内部命中率:

示例:

POST test_index/_search
    {
      "query": {
        "has_child": {
          "type": "child",
          "query": {
            "match": {
              "number": 2
            }
          },
          "inner_hits": {}    
        }
      }
    }

这为我提供了所有 parent 的匹配 children。如何向 parent 添加过滤器并仅查询同一查询中的特定 parent(以及 child 过滤器)?是否可以过滤同一查询中特定字段的 parent 条记录?

像这样 -

POST test_index/_search
    {
      "query": {
        <match parents first on number:1 and then match the children below>
        "has_child": {
          "type": "child",
          "query": {
            "match": {
              "number": 2
            }
          },
          "inner_hits": {}    
        }
      }
    }

这对我有用。唯一的缺点是 kibana Discover 仪表板不支持 has_child 或 has_parent,因此我们无法构建可视化。

POST test_index/_search
{
    "query": {
        "bool": {
            "must": [{
                "has_child": {
                    "type": "childname",
                    "query": {
                        "match": {
                            "properties.name": "hello"
                        }
                    },
                     "inner_hits": {}    
                }
            },
            {
                "match": {
                    "account.name": "random",
                    "version": {"2.0"}
                }
            },{
                "match": {
                    "account.name": "random",
                    "version": {"2.0"}
                }
            ]
        }
    }
}