# ElasticSearch 2.0, Bool Query with Inner Hits and Parent Query

**URL:** <https://discuss.elastic.co/t/elasticsearch-2-0-bool-query-with-inner-hits-and-parent-query/31706>\
**Category:** Elasticsearch\
**Created:** [October 6, 2015, 3:02pm UTC](https://discuss.elastic.co/t/elasticsearch-2-0-bool-query-with-inner-hits-and-parent-query/31706 "2015-10-06T15:02:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bevans88](https://avatars.discourse-cdn.com/v4/letter/b/a698b9/32.png) [@bevans88](https://discuss.elastic.co/u/bevans88)\
**Post date:** [October 6, 2015, 3:02pm UTC](https://discuss.elastic.co/t/elasticsearch-2-0-bool-query-with-inner-hits-and-parent-query/31706/1 "2015-10-06T15:02:16Z")

</div>

I'm currently working on the ElasticSearch 2.0 beta (though this may also happen in previous versions as well), and was wondering if anyone can tell me if the following is intended functionality or a bug;

Create test index:

```
curl -XPUT 'http://localhost:9200/test/'

```

Create mapping for child document:

```
curl -XPUT 'http://localhost:9200/test/_mapping/child' -d '
{
    "child" : {
	"_parent":{
		"type": "parent"
	},
        "properties" : {
            "message" : {"type" : "string"}
        }
    }
}
'

```

Create mapping for parent document:

```
curl -XPUT 'http://localhost:9200/test/_mapping/parent' -d '
{
    "parent" : {
        "properties" : {
            "message" : {"type" : "string"}
        }
    }
}
'

```

Index a parent document:

```
curl -XPUT 'http://localhost:9200/test/parent/parent01
{
   "message":"Parent01"
}
'

```

Index two child documents, one of which has a parent set:

```
curl -XPUT 'http://localhost:9200/test/child/child01?parent=parent01
{
   "message":"Child01"
}
'

curl -XPUT 'http://localhost:9200/test/child/child02?routing=child02
{
   "message":"Child02"
}
'

```

Run a search to get all 'child' documents that have either a value of 'Child02' in the 'message' field, or that have a parent of type 'parent', with inner\_hits being returned on this has\_parent query.

```
curl -XPOST 'http://localhost:9200/test/child/_search?pretty
{
  "query" : {
	"bool" : {
	  "should" : [ {
		"match" : {
		  "message" : {
			"query" : "Child02",
			"type" : "boolean"
		  }
		}
	  }, {
		"bool" : {
		  "must" : [ {
			"match_all" : { }
		  }, {
			"has_parent" : {
			  "query" : {
				"bool" : {
				  "must" : {
					"exists" : {
					  "field" : "message"
					}
				  }
				}
			  },
			  "parent_type" : "parent",
			  "inner_hits" : {
				"_source" : false
			  }
			}
		  } ]
		}
	  } ],
	  "minimum_should_match" : "1"
	}
   }
}
'

```

And the result:

```
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 4,
    "failed" : 1,
    "failures" : [ {
      "shard" : 0,
      "index" : "test",
      "node" : "ZP6ZIhMyQrK9ZEpH_bo3Rg",
      "reason" : {
        "type" : "illegal_state_exception",
        "reason" : "All children must have a _parent"
      }
    } ]
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.45329466,
    "hits" : [ {
      "_index" : "test",
      "_type" : "child",
      "_id" : "child01",
      "_score" : 0.45329466,
      "_parent" : "parent01",
      "_routing" : "parent01",
      "_source":{"message":"Child01"},
      "inner_hits" : {
        "parent" : {
          "hits" : {
            "total" : 1,
            "max_score" : 1.7320508,
            "hits" : [ {
              "_index" : "test",
              "_type" : "parent",
              "_id" : "parent01",
              "_score" : 1.7320508
            } ]
          }
        }
      }
    } ]
  }
}

```

Now at this point I'd expect two documents to be returned, child01 and child02. Instead only a single document is returned, child01, with child02 not being returned due to an illegal\_sate\_exception with the reason of 'All children must have a \_parent". If I run the same query without the inner\_hits being set then both documents are returned.

Is this a bug or just the current limitations on inner\_hits?

Thanks,

Brent

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [October 6, 2015, 7:51pm UTC](https://discuss.elastic.co/t/elasticsearch-2-0-bool-query-with-inner-hits-and-parent-query/31706/2 "2015-10-06T19:51:40Z")

</div>

> [@bevans88](#):
>
> curl -XPUT '[http://localhost:9200/test/child/child02?routing=child02](http://localhost:9200/test/child/child02?routing=child02)  
> {  
> "message":"Child02"  
> }  
> '

I don't think this is a child document, because `parent` parameter is missing, so the error `All children must have a _parent` makes sense to me.

---

<div class="post-metadata">

**Author:** ![bevans88](https://avatars.discourse-cdn.com/v4/letter/b/a698b9/32.png) [@bevans88](https://discuss.elastic.co/u/bevans88)\
**Post date:** [October 7, 2015, 7:59am UTC](https://discuss.elastic.co/t/elasticsearch-2-0-bool-query-with-inner-hits-and-parent-query/31706/3 "2015-10-07T07:59:10Z")

</div>

So it is not possible to have child documents with no associated parent? Would setting a non-existent parent id be a way around this?

Thanks

---

<div class="post-metadata">

**Author:** ![bevans88](https://avatars.discourse-cdn.com/v4/letter/b/a698b9/32.png) [@bevans88](https://discuss.elastic.co/u/bevans88)\
**Post date:** [October 20, 2015, 4:36pm UTC](https://discuss.elastic.co/t/elasticsearch-2-0-bool-query-with-inner-hits-and-parent-query/31706/4 "2015-10-20T16:36:40Z")

</div>

Is anyone able to confirm if having child documents with no associated parent is 'abnormal', or if the search should return the result with no inner hit set for parents fields?

Thanks

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [October 20, 2015, 10:35pm UTC](https://discuss.elastic.co/t/elasticsearch-2-0-bool-query-with-inner-hits-and-parent-query/31706/5 "2015-10-20T22:35:35Z")

</div>

I agree this behaviour is not right: I opened an issue: [https://github.com/elastic/elasticsearch/issues/14218](https://github.com/elastic/elasticsearch/issues/14218)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:43pm UTC](https://discuss.elastic.co/t/elasticsearch-2-0-bool-query-with-inner-hits-and-parent-query/31706/6 "2017-07-05T23:43:46Z")

</div>


