Search children documents from parent

I have a parent - child relationship in my Elastic Search mappings, and they look like follows. There is an "abstract" entity of type user and both driver and passenger inherit from it.

I've created user index like this.

PUT /user/

{
	"mappings": {
		"user": {
			"properties": {
				"firstName": {"type": "text"},
				"lastName": {"type": "text"},
				"dateOfBirth": {"type": "date"},
				"gender": {"type": "keyword"},
				"profileType": {"type": "keyword"},
				"dateJoined": {"type": "date"}
			}
		}
	}
}

I've created driver index like this.

PUT /driver/

{
	"mappings": {
		"driver": {
			"_parent": {"type": "user"},
			"properties": {
				"car": {"type": "text"},
				"license": {"type": "text"}
			}
		}
	}
}

I've created passenger index like this.

PUT /passenger/

{
	"mappings": {
		"passenger": {
			"_parent": {"type": "user"},
			"properties": {
				"record": {"type": "text"},
				"table": {"type": "text"}
			}
		}
	}
}

Now I am able to search within any of those indices, for example I can search the driver index like this: GET /driver/.

However, if I search GET /user/, I would expect to see records from both the driver and the passenger indices (since they inherit from the user), but I am getting an empty result. Although the records in the derived indices do exist.

Is there any way I could get all the indices that inherit from the parent?

There is nothing like inheritance in elasticsearch. You need to implement that by yourself.
Probably by duplicating data. Why would you need to use parent/child by the way?

@dadoonet
Probably I misunderstood the concept there, but my requirement is to be able to search through different types in a single query. For example, I have a type Driver and a type Passenger, and I want to query for all Users (Drivers or Passengers) that have been registered after some specific date. Additionally, I need to be able to query within a single type - for example query all Drivers whose car is BMW. What would be the correct design for such requirements then?

Multiple choices but here is one:

POST user/_doc
{
  "firstName":"foo",
  "type": "driver",
  "driver": {
    "car": "Ford",
    "license": "XYZ"
  }
}
POST user/_doc
{
  "firstName":"bar",
  "type": "passenger",
  "passenger": {
    "record": "bla",
    "table": "bla"
  }
}

@dadoonet
Yeah, that's completely fine, however querying across multiple indices is what is bothering me. What would be the way to query both of those indices per "firstName"?... which would then retrieve me both the Drivers and Passengers whose name is Bob. That's the question.

As you might have noticed, I'm using one single index name here user so I don't have the problem you wrote.

But anyway you can search across multiple indices at the same time:

GET /drivers,passengers/_search
{
// Your query here
}

@dadoonet
Which one of the two approaches would yield better performances?
Additionally, what would the query for multiple indices look like? I tried to search the docs, but couldn't find the example that does something similar.

The part where you wrote "// Your query here". Example if we search for users with name Bob.

The less shards you hit the better.
So if you can fit all your data within one single index with one shard, it will be faster than 2 indices with 1 shard each.

About the query, try a match query.

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.