# Query referenced documents

**URL:** <https://discuss.elastic.co/t/query-referenced-documents/13721>\
**Category:** Elasticsearch\
**Created:** [September 23, 2013, 10:02am UTC](https://discuss.elastic.co/t/query-referenced-documents/13721 "2013-09-23T10:02:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Liron\_Cohen](https://avatars.discourse-cdn.com/v4/letter/l/65b543/32.png) [@Liron\_Cohen](https://discuss.elastic.co/u/Liron_Cohen)\
**Post date:** [September 23, 2013, 10:02am UTC](https://discuss.elastic.co/t/query-referenced-documents/13721/1 "2013-09-23T10:02:00Z")

</div>

Hello,

This is my first post here and I'm quite new to ES.

After doing A LOT of digging I still couldn't find a good way to implement  
what I need.  
Let's say I have these documents:  
curl -XGET [http://localhost:9200/test/customers/1](http://localhost:9200/test/customers/1)  
[http://localhost:9200/test/customers/1/\_source?pretty=true](http://localhost:9200/test/customers/1/_source?pretty=true){"\_index":  
"test","\_type": "customers","\_id": "1","\_version": 1,"exists": true,  
"\_source": {"Name": "MyCust1"}}

curl -XGET [http://localhost:9200/test/invoices/1](http://localhost:9200/test/invoices/1)  
{"\_index": "test","\_type": "invoices","\_id": "1","\_version": 1,"exists":  
true,"\_source": {"custId": "1"}}  
(these are examples of course)

As you can see, the invoice document has a field (named: custId, with value  
of 1) pointing to a custoer document with \_id = 1.

Of course customer in not a child document of invoices and it'll be used as  
a reference in other documents as well.

How can I query the invoices with a certain customer name?  
What is the mapping I need to do?  
Is it all possible?

Thanks

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [September 23, 2013, 10:07am UTC](https://discuss.elastic.co/t/query-referenced-documents/13721/2 "2013-09-23T10:07:21Z")

</div>

Index invoice with all customer data.  
It really makes sense here to build it as a single document as you want to take a 'snapshot' of the invoice when it's generated, right?

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 23 sept. 2013 à 12:02, Kido [cliron1@gmail.com](mailto:cliron1@gmail.com) a écrit :

Hello,

This is my first post here and I'm quite new to ES.

After doing A LOT of digging I still couldn't find a good way to implement what I need.  
Let's say I have these documents:  
curl -XGET [http://localhost:9200/test/customers/1](http://localhost:9200/test/customers/1)  
{"\_index": "test","\_type": "customers","\_id": "1","\_version": 1,"exists": true,"\_source": {"Name": "MyCust1"}}

curl -XGET [http://localhost:9200/test/invoices/1](http://localhost:9200/test/invoices/1)  
{"\_index": "test","\_type": "invoices","\_id": "1","\_version": 1,"exists": true,"\_source": {"custId": "1"}}  
(these are examples of course)

As you can see, the invoice document has a field (named: custId, with value of 1) pointing to a custoer document with \_id = 1.

Of course customer in not a child document of invoices and it'll be used as a reference in other documents as well.

How can I query the invoices with a certain customer name?  
What is the mapping I need to do?  
Is it all possible?

## Thanks

You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Liron\_Cohen](https://avatars.discourse-cdn.com/v4/letter/l/65b543/32.png) [@Liron\_Cohen](https://discuss.elastic.co/u/Liron_Cohen)\
**Post date:** [September 23, 2013, 12:41pm UTC](https://discuss.elastic.co/t/query-referenced-documents/13721/3 "2013-09-23T12:41:28Z")

</div>

Hey David,

Visited your site earlier 🙂  
Thank you for answering.

As I mentioned: "customer in not a child document of invoices and it'll be  
used as a reference in other documents as well."  
But even if so, customer has many invoices and once the customer's address  
is change, I'll need to sync all referencing docs (like invoices) with the  
new details.  
If it helps, I can give another example: a tourist index; Locations and  
Hotels docs. A location (doc) is used in hotel (doc), but also for  
Reservation (doc), BusRoute (doc) etc.  
What I'm looking for (if it's possible) is a way to reference docs in other  
docs and then be able to query by them.  
Something like the "parent field" (  
[http://www.elasticsearch.org/guide/reference/mapping/parent-field/](http://www.elasticsearch.org/guide/reference/mapping/parent-field/)).  
Actually, after reading about it immediately searched for "child field"  
(which would be a solution for my problem), but couldn't find it.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Liron\_Cohen](https://avatars.discourse-cdn.com/v4/letter/l/65b543/32.png) [@Liron\_Cohen](https://discuss.elastic.co/u/Liron_Cohen)\
**Post date:** [September 23, 2013, 3:13pm UTC](https://discuss.elastic.co/t/query-referenced-documents/13721/4 "2013-09-23T15:13:38Z")

</div>

Okay... after doing some _more_ research I reached the conclusion that it's  
not possible in ES (which is a shame, I still think the ES team should  
consider adding a \_child field).  
So I read this[http://www.elasticsearch.org/blog/managing-relations-inside-elasticsearch/](http://www.elasticsearch.org/blog/managing-relations-inside-elasticsearch/) and  
reached the "Denormalization" section and it got me thinking.  
Most of the referenced docs are pretty small (relatively) and rarely change  
so why not include their source in the referring doc and use \_update[http://www.elasticsearch.org/guide/reference/api/update/](http://www.elasticsearch.org/guide/reference/api/update/) (with  
partial doc) to denormalize them when changes occur?  
Tested it out and it works great.

Now, the question is how will the \_update perform on massive (well, I'm  
exaggerating) scale.  
Let's say, the referred doc is referenced 1,000 times in the index, in  
different types.  
Has anyone tried this sort of thing? Any issues I should consider?

Is there another way of denormalize data?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [September 25, 2013, 11:11am UTC](https://discuss.elastic.co/t/query-referenced-documents/13721/5 "2013-09-25T11:11:52Z")

</div>

Hi Kido,

De-normalizing your data is a valid approach for this use case. The  
downside if something is referenced N times you will need to update /  
reindex N times.  
You can also use the parent / child support the comes with elasticsearch,  
which will eliminate to issue mentioned in the previous sentence. Multiple  
child types can point to the same parent type.

If you use the p/c queries you either get back the parent hits or the child  
hits. Including the top child hits per parent hit or parent hit per child  
hit is on the road map. In the mean time you can use a work around. In  
addition to your regular request (with a has\_child or has\_parent query) you  
can use the msearch api to retrieve the top children or parent for each  
hit. For each hit you will add an search request to the msearch api, that  
will for example fetch the top children for that particular parent hit.  
This way with just one additional request to your cluster you can get the  
job done.

Martijn

On 23 September 2013 17:13, Kido [cliron1@gmail.com](mailto:cliron1@gmail.com) wrote:

> Okay... after doing some _more_ research I reached the conclusion that  
> it's not possible in ES (which is a shame, I still think the ES team should  
> consider adding a \_child field).  
> So I read this[http://www.elasticsearch.org/blog/managing-relations-inside-elasticsearch/](http://www.elasticsearch.org/blog/managing-relations-inside-elasticsearch/) and  
> reached the "Denormalization" section and it got me thinking.  
> Most of the referenced docs are pretty small (relatively) and rarely  
> change so why not include their source in the referring doc and use  
> \_update [http://www.elasticsearch.org/guide/reference/api/update/](http://www.elasticsearch.org/guide/reference/api/update/) (with  
> partial doc) to denormalize them when changes occur?  
> Tested it out and it works great.
> 
> Now, the question is how will the \_update perform on massive (well, I'm  
> exaggerating) scale.  
> Let's say, the referred doc is referenced 1,000 times in the index, in  
> different types.  
> Has anyone tried this sort of thing? Any issues I should consider?
> 
> Is there another way of denormalize data?
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
Met vriendelijke groet,

Martijn van Groningen

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Amit\_Soni](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@Amit\_Soni](https://discuss.elastic.co/u/Amit_Soni)\
**Post date:** [October 7, 2013, 6:36am UTC](https://discuss.elastic.co/t/query-referenced-documents/13721/6 "2013-10-07T06:36:28Z")

</div>

I have a similar use case and I am feeling reluctant to de-normalize the  
data since I might have hundreds of thousands of documents that would need  
an update as a result of an update to a dependent document.

I am still exploring what would be the right way to solve this use case and  
whether parent-child functionality is the right way to go.

-Amit.

On Wed, Sep 25, 2013 at 4:11 AM, Martijn v Groningen \<  
[martijn.v.groningen@gmail.com](mailto:martijn.v.groningen@gmail.com)\> wrote:

> Hi Kido,
> 
> De-normalizing your data is a valid approach for this use case. The  
> downside if something is referenced N times you will need to update /  
> reindex N times.  
> You can also use the parent / child support the comes with elasticsearch,  
> which will eliminate to issue mentioned in the previous sentence. Multiple  
> child types can point to the same parent type.
> 
> If you use the p/c queries you either get back the parent hits or the  
> child hits. Including the top child hits per parent hit or parent hit per  
> child hit is on the road map. In the mean time you can use a work around.  
> In addition to your regular request (with a has\_child or has\_parent query)  
> you can use the msearch api to retrieve the top children or parent for each  
> hit. For each hit you will add an search request to the msearch api, that  
> will for example fetch the top children for that particular parent hit.  
> This way with just one additional request to your cluster you can get the  
> job done.
> 
> Martijn
> 
> On 23 September 2013 17:13, Kido [cliron1@gmail.com](mailto:cliron1@gmail.com) wrote:
> 
> > Okay... after doing some _more_ research I reached the conclusion that  
> > it's not possible in ES (which is a shame, I still think the ES team should  
> > consider adding a \_child field).  
> > So I read this[http://www.elasticsearch.org/blog/managing-relations-inside-elasticsearch/](http://www.elasticsearch.org/blog/managing-relations-inside-elasticsearch/) and  
> > reached the "Denormalization" section and it got me thinking.  
> > Most of the referenced docs are pretty small (relatively) and rarely  
> > change so why not include their source in the referring doc and use  
> > \_update [http://www.elasticsearch.org/guide/reference/api/update/](http://www.elasticsearch.org/guide/reference/api/update/) (with  
> > partial doc) to denormalize them when changes occur?  
> > Tested it out and it works great.
> > 
> > Now, the question is how will the \_update perform on massive (well, I'm  
> > exaggerating) scale.  
> > Let's say, the referred doc is referenced 1,000 times in the index, in  
> > different types.  
> > Has anyone tried this sort of thing? Any issues I should consider?
> > 
> > Is there another way of denormalize data?
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> Met vriendelijke groet,
> 
> Martijn van Groningen
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:13am UTC](https://discuss.elastic.co/t/query-referenced-documents/13721/7 "2017-07-06T02:13:28Z")

</div>


