ElasticSearch 2.0, Bool Query with Inner Hits and Parent Query

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

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.

1 Like

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

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

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