elasticsearch - 在 Elasticsearch 中的一个查询中搜索父级及其所有子级

标签 elasticsearch elasticsearch-6 aws-elasticsearch

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

该链接提供了问答连接关系,其中问题是父类型,答案是子类型。

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

假设他们在 json 文档中有键值对,如下所示,我们搜索与问题中的文本匹配的父项和与 answer-text 中的值匹配的子项。

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

我们不希望结果中只包含父项或子项,而是所有父项及其与查询匹配的关联子项。

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

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

我还尝试了此处提到的父子内部命中示例:

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

这给了我所有 parent 的匹配 child 。如何向父级添加过滤器并仅查询同一查询中的特定父级(以及子级过滤器)?是否可以在同一查询中的特定字段上过滤父记录?

像这样的东西——
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"}
                }
            ]
        }
    }
}

关于elasticsearch - 在 Elasticsearch 中的一个查询中搜索父级及其所有子级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49147218/

相关文章:

elasticsearch - 如何在 Python 中对 Elasticsearch DSL 的结果进行分页

ElasticSearch 按嵌套文档的总和过滤

Elasticsearch 索引在磁盘上占用的空间比显示的要大

elasticsearch - 使用Java API索引文档时未知主机异常(RestHighLevelClient)Elasticsearch

elasticsearch - Elasticsearch中的重新索引需要很长时间

amazon-web-services - 使用 key 凭证连接到 AWS 中的 elasticsearch

python - Django elasticsearch-dsl在pre_save上更新M2M

elasticsearch - ElasticSearch查询可一次过滤单个值和值列表

javascript - 使用Express JS部署Elasticsearch

amazon-web-services - 将 AWS CloudWatch 日志组流式传输到多个 AWS Elasticsearch 服务