【问题标题】:Search for a parent and all it's children in one query in elastic search在弹性搜索中的一个查询中搜索父级及其所有子级
【发布时间】:2018-08-15 06:40:20
【问题描述】:

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

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

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

假设他们在下面的 json 文档中有键值对,我们搜索匹配问题中的文本的父项和匹配答案文本中的值的子项。

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": {}    
        }
      }
    }

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

类似的东西 -

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": {}    
        }
      }
    }

【问题讨论】:

    标签: elasticsearch elasticsearch-6 aws-elasticsearch


    【解决方案1】:

    这对我有用。唯一的缺点是 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"}
                    }
                ]
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-12
      • 1970-01-01
      • 1970-01-01
      • 2023-01-16
      • 2020-09-12
      相关资源
      最近更新 更多