# Indexing m:n relations

**URL:** <https://discuss.elastic.co/t/indexing-m-n-relations/16526>\
**Category:** Elasticsearch\
**Created:** [March 21, 2014, 1:42pm UTC](https://discuss.elastic.co/t/indexing-m-n-relations/16526 "2014-03-21T13:42:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pat\_4](https://avatars.discourse-cdn.com/v4/letter/p/f05b48/32.png) [@Pat\_4](https://discuss.elastic.co/u/Pat_4)\
**Post date:** [March 21, 2014, 1:42pm UTC](https://discuss.elastic.co/t/indexing-m-n-relations/16526/1 "2014-03-21T13:42:03Z")

</div>

Hi,

I would like to index documents of type A and B that have an m:n relation  
between them (indexing 1:n is pretty straightforward using parent/child  
relationships or nested documents).  
For example, I would like to find all documents of type A that have a  
related document B which itself has a property x = 123. Currently I nest B  
within A, but every time a specific B object is referenced by an A object,  
it gets stored in the index again as a seperate document (plus all  
documents that B references itself). Since the data model contains many  
relations, the index gets very large and building queries spanning the  
nested structures is rather complex and error-prone.  
I really would like to index A and B as separate documents which contain a  
reference to each other. But this makes something like a filtered query  
across different document types necessary.  
Something like "Search for all Bs that have x = 123 and use the results as  
a filter on A". I could do that within my application, but I would need to  
retrieve ALL hits for the first query on B (since the results need to be  
sorted on some property of A) and use the results to build a filter for the  
second query on A. Two queries and probably lots of data exchanged between  
my application and Elasticsearch.  
Is there a simpler solution for this?

Regards,  
Pat

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/0d9877f0-ed40-4606-ba37-85b2998ee907%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0d9877f0-ed40-4606-ba37-85b2998ee907%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:41am UTC](https://discuss.elastic.co/t/indexing-m-n-relations/16526/2 "2017-07-06T01:41:34Z")

</div>


