<p dir="auto">I am trying to model a employee hierarchy where every employee can be a manager of a team of employees and, at the same time, she can be part of a team managed by other employee:</p>
<p dir="auto">curl -XPOST '<a href="http://localhost:9200/manager_employee" rel="nofollow">http://localhost:9200/manager_employee</a>' -d '{<br>
"mappings": {<br>
"employee": {<br>
"_parent": {<br>
"type": "employee"<br>
},<br>
"properties": {<br>
"name": {<br>
"type": "string"<br>
}<br>
}<br>
}<br>
}<br>
}'</p>
<p dir="auto">The problem is when modeling a employee which is not managed by anyone (let's think about the CEO). I tried both alternatives bellow:</p>
<p dir="auto">curl -s -XPOST localhost:9200/_bulk --data-binary '<br>
{ "index" : { "_index" : "manager_employee", "_type" : "employee", "_id" : "employee1" , _parent:"employee1"} }<br>
{ "name" : "chris"}<br>
{ "index" : { "_index" : "manager_employee", "_type" : "employee", "_id" : "employee2" , _parent:"employee1"} }<br>
{ "name" : "joseph" }<br>
{ "index" : { "_index" : "manager_employee", "_type" : "employee", "_id" : "employee3" , _parent:"employee1"} }<br>
{ "name" : "john" }'</p>
<p dir="auto">i.e., the CEO is managed by herself and</p>
<p dir="auto">curl -s -XPOST localhost:9200/_bulk --data-binary '<br>
{ "index" : { "_index" : "manager_employee", "_type" : "employee", "_id" : "employee1" , _parent:"dummyID1"} }<br>
{ "name" : "chris"}<br>
{ "index" : { "_index" : "manager_employee", "_type" : "employee", "_id" : "employee2" , _parent:"employee1", _routing:"dummyID1"} }<br>
{ "name" : "joseph" }<br>
{ "index" : { "_index" : "manager_employee", "_type" : "employee", "_id" : "employee3" , _parent:"employee1", _routing:"dummyID1"} }<br>
{ "name" : "john" }'</p>
<p dir="auto">the CEO is managed by some dummy manager</p>
<p dir="auto">In both cases:</p>
<p dir="auto">curl -XGET 'localhost:9200/manager_employee/employee/_search' -d '{<br>
"filter" : {<br>
"has_child" : {<br>
"child_type" : "employee",<br>
"query" : {<br>
"term" : {<br>
"employee.name" : "john"<br>
}<br>
}<br>
}}}'</p>
<p dir="auto">works returning chris, but the other side of the relations fails:</p>
<p dir="auto">curl -XGET 'localhost:9200/manager_employee/employee/_search' -d '{<br>
"filter" : {<br>
"has_parent" : {<br>
"parent_type" : "employee",<br>
"query" : {<br>
"term" : {<br>
"employee.name" : "chris"<br>
}<br>
}<br>
}}}'</p>
<p dir="auto">returns an empty hit set.</p>
<p dir="auto">Is there any other way (without defining different) types of implementing this? Any workaround? Is it a bug that the relation is correctly mapped from children to parent but not from parent to children when using the same type in both sides of the relation?</p>
<p dir="auto">Thank you in advance,</p>
<p dir="auto">We don't explicitly prohibit types from referring to themselves as parents. This functionality worked fine before 0.90 release, but since 0.90 it seems to be broken for the <code class="notranslate">has_parent</code> query. In the following script, both search requests return results in 0.20.x but only the second search request returns results in 0.90.x and above.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="curl -XDELETE localhost:9200/test-idx
curl -XPUT localhost:9200/test-idx -d '{
    &quot;settings&quot;: {
        &quot;index.number_of_shards&quot;: 1,
        &quot;index.number_of_replicas&quot;: 0
    },
    &quot;mappings&quot;: {
        &quot;doc&quot;: {
            &quot;_parent&quot;: {
                &quot;type&quot;: &quot;doc&quot;
            }
        }
    }
}'
curl -XPUT &quot;localhost:9200/test-idx/doc/1?routing=1&amp;pretty&quot; -d '{&quot;name&quot;: &quot;doc_1&quot;}'
curl -XPUT &quot;localhost:9200/test-idx/doc/2?parent=1&amp;pretty&quot; -d '{&quot;name&quot;: &quot;doc_2&quot;}'
curl -XPOST &quot;localhost:9200/test-idx/_refresh?pretty&quot;
echo
curl &quot;localhost:9200/test-idx/doc/_search?pretty&quot; -d '{
    &quot;query&quot;: {
        &quot;has_parent&quot;: {
            &quot;type&quot;: &quot;doc&quot;,
            &quot;query&quot; : {
                &quot;match_all&quot;: {
                }
            }
        }
    }
}'
echo
curl &quot;localhost:9200/test-idx/doc/_search?pretty&quot; -d '{
    &quot;query&quot;: {
        &quot;has_child&quot;: {
            &quot;type&quot;: &quot;doc&quot;,
            &quot;query&quot; : {
                &quot;match_all&quot;: {
                }
            }
        }
    }
}'"><pre class="notranslate"><code class="notranslate">curl -XDELETE localhost:9200/test-idx
curl -XPUT localhost:9200/test-idx -d '{
    "settings": {
        "index.number_of_shards": 1,
        "index.number_of_replicas": 0
    },
    "mappings": {
        "doc": {
            "_parent": {
                "type": "doc"
            }
        }
    }
}'
curl -XPUT "localhost:9200/test-idx/doc/1?routing=1&amp;pretty" -d '{"name": "doc_1"}'
curl -XPUT "localhost:9200/test-idx/doc/2?parent=1&amp;pretty" -d '{"name": "doc_2"}'
curl -XPOST "localhost:9200/test-idx/_refresh?pretty"
echo
curl "localhost:9200/test-idx/doc/_search?pretty" -d '{
    "query": {
        "has_parent": {
            "type": "doc",
            "query" : {
                "match_all": {
                }
            }
        }
    }
}'
echo
curl "localhost:9200/test-idx/doc/_search?pretty" -d '{
    "query": {
        "has_child": {
            "type": "doc",
            "query" : {
                "match_all": {
                }
            }
        }
    }
}'
</code></pre></div>
<p dir="auto">search result in 0.20.6:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="{
  &quot;took&quot; : 54,
  &quot;timed_out&quot; : false,
  &quot;_shards&quot; : {
    &quot;total&quot; : 1,
    &quot;successful&quot; : 1,
    &quot;failed&quot; : 0
  },
  &quot;hits&quot; : {
    &quot;total&quot; : 1,
    &quot;max_score&quot; : 1.0,
    &quot;hits&quot; : [ {
      &quot;_index&quot; : &quot;test-idx&quot;,
      &quot;_type&quot; : &quot;doc&quot;,
      &quot;_id&quot; : &quot;2&quot;,
      &quot;_score&quot; : 1.0, &quot;_source&quot; : {&quot;name&quot;: &quot;doc_2&quot;}
    } ]
  }
}
{
  &quot;took&quot; : 2,
  &quot;timed_out&quot; : false,
  &quot;_shards&quot; : {
    &quot;total&quot; : 1,
    &quot;successful&quot; : 1,
    &quot;failed&quot; : 0
  },
  &quot;hits&quot; : {
    &quot;total&quot; : 1,
    &quot;max_score&quot; : 1.0,
    &quot;hits&quot; : [ {
      &quot;_index&quot; : &quot;test-idx&quot;,
      &quot;_type&quot; : &quot;doc&quot;,
      &quot;_id&quot; : &quot;1&quot;,
      &quot;_score&quot; : 1.0, &quot;_source&quot; : {&quot;name&quot;: &quot;doc_1&quot;}
    } ]
  }
}"><pre class="notranslate"><code class="notranslate">{
  "took" : 54,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "test-idx",
      "_type" : "doc",
      "_id" : "2",
      "_score" : 1.0, "_source" : {"name": "doc_2"}
    } ]
  }
}
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "test-idx",
      "_type" : "doc",
      "_id" : "1",
      "_score" : 1.0, "_source" : {"name": "doc_1"}
    } ]
  }
}
</code></pre></div>
<p dir="auto">search result in the current master:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="{
  &quot;took&quot; : 66,
  &quot;timed_out&quot; : false,
  &quot;_shards&quot; : {
    &quot;total&quot; : 1,
    &quot;successful&quot; : 1,
    &quot;failed&quot; : 0
  },
  &quot;hits&quot; : {
    &quot;total&quot; : 0,
    &quot;max_score&quot; : null,
    &quot;hits&quot; : [ ]
  }
}

{
  &quot;took&quot; : 16,
  &quot;timed_out&quot; : false,
  &quot;_shards&quot; : {
    &quot;total&quot; : 1,
    &quot;successful&quot; : 1,
    &quot;failed&quot; : 0
  },
  &quot;hits&quot; : {
    &quot;total&quot; : 1,
    &quot;max_score&quot; : 1.0,
    &quot;hits&quot; : [ {
      &quot;_index&quot; : &quot;test-idx&quot;,
      &quot;_type&quot; : &quot;doc&quot;,
      &quot;_id&quot; : &quot;1&quot;,
      &quot;_score&quot; : 1.0,
      &quot;_source&quot;:{&quot;name&quot;: &quot;doc_1&quot;}
    } ]
  }
}"><pre class="notranslate"><code class="notranslate">{
  "took" : 66,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
}

{
  "took" : 16,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "test-idx",
      "_type" : "doc",
      "_id" : "1",
      "_score" : 1.0,
      "_source":{"name": "doc_1"}
    } ]
  }
}
</code></pre></div>