【问题标题】:How does a has_parent query work with the join datatype in Elasticsearch?has_parent 查询如何与 Elasticsearch 中的连接数据类型一起使用?
【发布时间】:2019-07-26 03:16:31
【问题描述】:

在查询使用join 数据类型设置的索引时,我无法让has_parent 查询返回任何结果。我完全按照Elasticsearch's join datatype documentation page 上的示例进行操作:

添加一个索引,其中包含question(父)和answer(子)之间的连接:

PUT my_index
{
  "settings": {
    "mapping.single_type": true
  },
  "mappings": {
    "doc": {
      "properties": {
        "my_join_field": { 
          "type": "join",
          "relations": {
            "question": "answer" 
          }
        }
      }
    }
  }
}

添加两个问题:

PUT my_index/doc/1?refresh
{
  "text": "This is a question",
  "my_join_field": {
    "name": "question" 
  }
}

PUT my_index/doc/2?refresh
{
  "text": "This is a another question",
  "my_join_field": {
    "name": "question"
  }
}

添加两个答案:

PUT my_index/doc/3?routing=1&refresh 
{
  "text": "This is an answer",
  "my_join_field": {
    "name": "answer", 
    "parent": "1" 
  }
}

PUT my_index/doc/4?routing=1&refresh
{
  "text": "This is another answer",
  "my_join_field": {
    "name": "answer",
    "parent": "1"
  }
}

该文档没有示例说明如何使用has_parent 查询来查询。这是我的尝试,基于the has_parent doc,但它没有返回任何结果:

GET my_index/_search
{
  "query": {
    "has_parent": { 
      "parent_type": "question",
      "query" : {
        "match" : { "text" : "another" }
      }
    }
  }
}

即使这会返回一个问题和一个答案,正如预期的那样:

GET my_index/_search
{
  "query": {
    "match" : { "text" : "another" }
  }
}

我使用的是 Elasticsearch 5.6.2。

【问题讨论】:

    标签: elasticsearch elasticsearch-5


    【解决方案1】:

    这就是 has_parent 和 has_child 查询如何使用新的连接数据类型。为了回答您的问题,我使用了相同的数据作为索引。从您的示例来看,您似乎希望使用 has_child 查询。

    PUT my_index
    {
      "settings": {
        "mapping.single_type": true
      },
      "mappings": {
        "doc": {
          "properties": {
            "my_join_field": {
              "type": "join",
              "relations": {
                "question": "answer"
              }
            }
          }
        }
      }
    }
    
    PUT my_index/doc/1?refresh
    {
      "text": "This is a question",
      "my_join_field": {
        "name": "question" 
      }
    }
    
    PUT my_index/doc/2?refresh
    {
      "text": "This is a another question",
      "my_join_field": {
        "name": "question"
      }
    }
    
    PUT my_index/doc/3?routing=1&refresh 
    {
      "text": "This is an answer",
      "my_join_field": {
        "name": "answer", 
        "parent": "1" 
      }
    }
    
    PUT my_index/doc/4?routing=1&refresh
    {
      "text": "This is another answer",
      "my_join_field": {
        "name": "answer",
        "parent": "1"
      }
    }
    
    # has_parent query 
    # added inner_hits to see what matched
    GET my_index/_search
    {
      "query": {
        "has_parent": {
          "parent_type": "question",
          "query": {
            "match": {
              "text": "This is a question"
            }
          },
          "inner_hits": {}
        }
      }
    }
    
    # has_child query
    GET my_index/_search
    {
      "query": {
        "has_child": {
          "type": "answer",
          "query": {
            "match": {
              "text": "another"
            }
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-03
      • 2017-08-08
      • 1970-01-01
      • 2016-02-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多