# Has\_child data retrival

**URL:** <https://discuss.elastic.co/t/has-child-data-retrival/8663>\
**Category:** Elasticsearch\
**Created:** [August 8, 2012, 2:20am UTC](https://discuss.elastic.co/t/has-child-data-retrival/8663 "2012-08-08T02:20:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon48959148](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@anon48959148](https://discuss.elastic.co/u/anon48959148)\
**Post date:** [August 8, 2012, 2:20am UTC](https://discuss.elastic.co/t/has-child-data-retrival/8663/1 "2012-08-08T02:20:54Z")

</div>

I have a few types like so:

First the mapping:

> curl -XPUT localhost:9200/test/account/\_mapping -d '{  
> "name": {  
> "type": "string"  
> }  
> }'

curl -XPUT '[http://localhost:9200/test/email/\_mapping](http://localhost:9200/test/email/_mapping)' -d '{

> ```
> "email" : {
> "_parent" : {"type" : "account"}
> }
> 
> ```
> 
> }'

Some data:

> curl -XPUT [http://localhost:9200/test/account/1](http://localhost:9200/test/account/1) -d '{  
> "name": "Andy Boyce",  
> }'
> 
> curl -XPUT [http://localhost:9200/test/account/2](http://localhost:9200/test/account/2) -d '{  
> "name": "Catherine Davids",  
> }'
> 
> curl -XPUT [http://localhost:9200/test/account/3](http://localhost:9200/test/account/3) -d '{  
> "name": "Elizabeth Faraday",  
> }'

> curl -XPUT [http://localhost:9200/test/email/1?parent=1](http://localhost:9200/test/email/1?parent=1) -d '{  
> "email": "[andy@boyce.com](mailto:andy@boyce.com)"  
> }'

curl -XPUT [http://localhost:9200/test/email/2?parent=1](http://localhost:9200/test/email/2?parent=1) -d '{

> ```
> "email": "andy.boyce@company.com"
> 
> ```
> 
> }'

curl -XPUT [http://localhost:9200/test/email/3?parent=1](http://localhost:9200/test/email/3?parent=1) -d '{

> ```
> "email": "support@corporation.com"
> 
> ```
> 
> }'

curl -XPUT [http://localhost:9200/test/email/4?parent=2](http://localhost:9200/test/email/4?parent=2) -d '{

> ```
> "email": "cat@designgroup.com"
> 
> ```
> 
> }'
> 
> curl -XPUT [http://localhost:9200/test/email/5?parent=3](http://localhost:9200/test/email/5?parent=3) -d '{  
> "email": "[ef@company.com](mailto:ef@company.com)"  
> }'

> curl -XPUT [http://localhost:9200/test/email/6?parent=3](http://localhost:9200/test/email/6?parent=3) -d '{  
> "email": "[liz@personalblog.com](mailto:liz@personalblog.com)"  
> }'

I am then running a simple query to search on`email.email` and  
`account.name`. Essentially, if we get a match on `email.email`, only the  
parent account will be present in the results:

> curl '[http://localhost:9200/test/account/\_search](http://localhost:9200/test/account/_search)' -d '{  
> "query": {  
> "bool": {  
> "should": [  
> {  
> "prefix": {  
> "name": "a"  
> }  
> },  
> {  
> "has\_child": {  
> "type": "email",  
> "query": {  
> "prefix": {  
> "email": "a"  
> }  
> }  
> }  
> }  
> ],  
> "minimum\_number\_should\_match" : 1  
> }  
> }  
> }'

This works fine, but the problem is that once I get the accounts, I need to  
know if the match was on `email.email` or `account.name`. For example, I  
would like to display an email address in the results for each account. If  
the match was on the email address, then I would like to display the  
matched email address. If the match was on the account name, I would like  
to display the first email address ('lowest id') linked to the account.

How can this be done?

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [August 8, 2012, 10:49am UTC](https://discuss.elastic.co/t/has-child-data-retrival/8663/2 "2012-08-08T10:49:58Z")

</div>

Including the top email address (email with lowest id) per account  
isn't possible in one search request.  
What you could do is executing subsequent search requests. For each  
account returned  
in the search result, you'll need to execute a search request.

Or you could change your mapping, so that each account has an email  
address field.

Martijn

---

<div class="post-metadata">

**Author:** ![anon48959148](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@anon48959148](https://discuss.elastic.co/u/anon48959148)\
**Post date:** [August 8, 2012, 10:53am UTC](https://discuss.elastic.co/t/has-child-data-retrival/8663/3 "2012-08-08T10:53:32Z")

</div>

Hi Martin,

Multiple emails can be associated to one account.

Would changing the mapping so that accounts contain emails help deal with  
the issue? Please forgive my ignorance as I am quite new to ES 🙂

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [August 8, 2012, 11:24am UTC](https://discuss.elastic.co/t/has-child-data-retrival/8663/4 "2012-08-08T11:24:13Z")

</div>

I think it would. You can associate multiple email addresses with one account:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elas...

To find out what email addresses have matched with your query you can  
use highlighting on the email field:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elas...

Martijn

---

<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, 3:17am UTC](https://discuss.elastic.co/t/has-child-data-retrival/8663/5 "2017-07-06T03:17:16Z")

</div>


