Chris Jones
Chris Jones

Reputation: 151

ElasticSearch _parent query

Elastic documentation states that one can use the _parent field in a query (see https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-parent-field.html).

However, I have been unable to get it to work. Here's the simple test:

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

Verifying that the employees have a _parent field:

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

returns

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

But the following:

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

returns:

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

Using "has_parent" works, but why doesn't using _parent work, as stated in the docs.

Here's the query using has_parent that works:

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

What am I missing? Using ElasticSearch 5.0.2.

Upvotes: 1

Views: 716

Answers (1)

Val
Val

Reputation: 217334

It's a documentation bug. According to the breaking changes in 5.0, the _parent field is no longer indexed and hence it is not possible to run a term query on that field. You either need to use the has_parent query or the new parent_id query to find that child document:

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

For those who want to follow, I've filed an issue to report this and it got fixed. The updated documentation will soon be available.

Upvotes: 1

Related Questions