【问题标题】:Elasticsearch parent child shout return parent instead of childElasticsearch父子喊返回父而不是子
【发布时间】:2017-01-20 23:19:48
【问题描述】:

我想我需要一些关于ElasticSearch 的帮助。

我有一个parent child 映射,我想在子文档的父中搜索包含该单词的文档。想一个 SO 问题和答案。有1:n 的问题需要回答。我想知道SeachWord 在问题(父母)中的问题,如果SearchWord 在任何子答案中,即使SearchWord 不在问题中,我也想知道问题。

这是我的查询,结果是子文档,但我希望得到父文档。

{
    "query": {
        "bool": {
            "should": [{
                "query_string": {
                    "query": "SearchWord"
                }
            },
            {
                "has_child": {
                    "type": "answers",
                    "query": {
                        "match": {
                            "_all": "SearchWord"
                        }
                    }
                }
            }]
        }
    },
    "size": 1
}

感谢您的帮助!

更新

查询完全正确,我唯一的错误是针对索引运行查询。

错误: cUrl /index/_search

正确: cUrl /index/parenttype/_search

我希望这可以帮助任何在父子搜索方面遇到问题的人 ;-)

问候
菲利普

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    你应该查询 child 和 filter on parent

    cUrl /indexName/childType

    {
      "query": {
        "bool": {
          "should": [
            {
              "has_parent": {
                "type": "questions",
                "query": {
                  "query_string": {
                    "query": "SearchWord"
                  }
                }
              }
            },
            {
              "match": {
                "_all": "SearchWord"
              }
            }
          ]
        }
      },
      "size": 1
    }
    

    【讨论】:

      【解决方案2】:

      你的答案不是我想要的,但我给了我正确的方向。
      我必须查询父类型而不是整个索引。所以这是我的解决方案,它按预期工作。

      {
        "query": {
          "bool": {
            "should": [
              {
                "has_child": {
                  "type": "answers",
                  "query": {
                    "query_string": {
                      "query": "SearchWord"
                    }
                  }
                }
              },
              { 
                  "query_string" : {
                      "query": "SearchWord" 
                  } 
              }
            ]
          }
        },
        "size": 1
      }
      

      非常感谢!

      【讨论】:

      • 我只是好奇我的查询出了什么问题 :) 什么不起作用?
      • 您的答案是查询子类型并查找父文档。我只是想让它反过来。当我第一次测试它时,我仍然使用索引而不是类型,所以我收到了子文档。这就是为什么我认为您的答案不太合适的原因。但正如我所说,你的回答引领了方向 ;-) 干杯
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-23
      • 2018-08-30
      • 2016-02-13
      • 2014-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多