# How to write Parent-child relationship and has\_child queries in Spark?

**URL:** <https://discuss.elastic.co/t/how-to-write-parent-child-relationship-and-has-child-queries-in-spark/44126>\
**Category:** Elasticsearch\
**Tags:** es-hadoop\
**Created:** [March 11, 2016, 8:45am UTC](https://discuss.elastic.co/t/how-to-write-parent-child-relationship-and-has-child-queries-in-spark/44126 "2016-03-11T08:45:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![yuCompHW](https://avatars.discourse-cdn.com/v4/letter/y/71c47a/32.png) [@yuCompHW](https://discuss.elastic.co/u/yuCompHW)\
**Post date:** [March 11, 2016, 8:45am UTC](https://discuss.elastic.co/t/how-to-write-parent-child-relationship-and-has-child-queries-in-spark/44126/1 "2016-03-11T08:45:51Z")

</div>

Hello

I am a beginner in Elasticsearch-Spark.  
My data is created in a Spark task, and it will be stored into Elasticsearch.  
My problem is that, I have two types, namely "branch" and "employee", which are in parent-child relationship.  
Now I cannot find the way to define their parent-child relationship when transferring data to Elasticsearch from Spark.

In detail, I have the mapping below defined in my Elasticsearch:  
curl -XPUT 'localhost:9200/company?pretty=1' -d'  
{  
"mappings": {  
"branch": {},  
"employee": {  
"\_parent": {  
"type": "branch"  
}  
}  
}  
}'

Then, I put several documents into type branch:  
curl -XPOST 'localhost:9200/company/branch/\_bulk?pretty' -d '  
{ "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" }

Next, I insert a document of type employee into the index company,  
curl -XPUT 'localhost:9200/company/employee/1?parent=london' -d'  
{  
"name": "Alice Smith",  
"dob": "1970-10-24",  
"hobby": "hiking"  
}'

Then, inside Spark-Shell  
scala\> val options = Map("pushdown" -\> "true", "es.read.metadata" -\> "true")  
scala\> val esDf = sqlContext.read.format("org.elasticsearch.spark.sql").options(options).load("company/employee")  
scala\> esDf.printSchema  
root  
|-- dob: timestamp (nullable = true)  
|-- hobby: string (nullable = true)  
|-- name: string (nullable = true)  
|-- \_metadata: map (nullable = true)  
| |-- key: string  
| |-- value: string (valueContainsNull = true)

NO PARENT information shown in the schema.  
Therefore, I wonder how can I specify "parent=london" in Spark when using functions like saveToEs ?  
Also, I wonder whether or not I can conduct has\_child, has\_parent queries to Elasticsearch through Spark? What are the functions or configurations ?

I use Elasticsearch 2.2.0, Spark 1.6.0 and elasticsearch-spark\_2.10-2.2.0.jar

Many Thanks.

---

<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 6, 2017, 1:25pm UTC](https://discuss.elastic.co/t/how-to-write-parent-child-relationship-and-has-child-queries-in-spark/44126/2 "2017-07-06T13:25:50Z")

</div>


