ElasticSearch _parent查询

时间:2017-01-09 20:07:55

标签: elasticsearch

弹性文档声明可以在查询中使用_parent字段(请参阅https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-parent-field.html)。

但是,我无法让它发挥作用。这是简单的测试:

PUT /company
{
  "mappings": {
    "branch": {},
    "employee": {
      "_parent": {
        "type": "branch" 
      }
    }
  }
}

POST /company/branch/_bulk
{ "index": { "_id": "london" }}
{ "name": "London Westminster", "city": "London", "country": "UK" }
{ "index": { "_id": "liverpool" }}
{ "name": "Liverpool Central", "city": "Liverpool", "country": "UK" }
{ "index": { "_id": "paris" }}
{ "name": "Champs Élysées", "city": "Paris", "country": "France" }

PUT /company/employee/1?parent=london 
{
  "name":  "Alice Smith",
  "dob":   "1970-10-24",
  "hobby": "hiking"
}

验证员工是否有_parent字段:

GET /company/employee/_search
{
  "query": {
    "match_all": {}
  }
}

返回

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "company",
        "_type": "employee",
        "_id": "1",
        "_score": 1,
        "_routing": "london",
        "_parent": "london",
        "_source": {
          "name": "Alice Smith",
          "dob": "1970-10-24",
          "hobby": "hiking"
        }
      }
    ]
  }
}

但是以下内容:

GET /company/employee/_search
{
  "query": {
    "term": {
      "_parent":"london"
    }
  }
}

返回:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

使用“has_parent”有效,但为什么不使用_parent工作,如文档中所述。

以下是使用has_parent的查询:

GET /company/employee/_search
{
  "query": {
    "has_parent": {
      "parent_type":"branch",
      "query":{
        "match_all": {}
      }
    }
  }
}

我错过了什么?使用ElasticSearch 5.0.2。

1 个答案:

答案 0 :(得分:0)

这是一个文档错误。根据{{​​3}},_parent字段不再编入索引,因此无法在该字段上运行term查询。您需要使用has_parent查询或新的parent_id查询来查找该子文档:

POST /company/employee/_search
{
  "query": {
    "parent_id": {
      "type": "employee",
      "id": "london"
    }
  }
}

对于那些想要关注的人,我breaking changes in 5.0报告此问题并得到修复。很快就会有更新的文档。