# Elastic join over type in same index

**URL:** <https://discuss.elastic.co/t/elastic-join-over-type-in-same-index/124034>\
**Category:** Elasticsearch\
**Created:** [March 15, 2018, 6:42am UTC](https://discuss.elastic.co/t/elastic-join-over-type-in-same-index/124034 "2018-03-15T06:42:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ramshad](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@ramshad](https://discuss.elastic.co/u/ramshad)\
**Post date:** [March 15, 2018, 6:42am UTC](https://discuss.elastic.co/t/elastic-join-over-type-in-same-index/124034/1 "2018-03-15T06:42:11Z")

</div>

I have 3 different type 'user','school','user\_preferences' in one index 'elastic' .Its structure would be as follows:  
User Type:  
| **id** | **name** |  
|1 |User A|  
|2 |User B|

School Type:  
| **id** | **school\_name** |  
|1 |School A |  
|2 |School AB |  
|3 |School AB |

User preferences:  
| **user\_id** | **type** | **prefered\_id** |  
|1 |user |2 |  
|2 |school |3 |

if my user id is '1' and am searching for a school 'AB' ,then i need to get the school which my friend is preferred and then other matched schools.here user\_id '2' is my friend(see user\_preference type) and he preferred school 'AB'(school id 3).So in search result i need to get that school in first result and then remaining matched items.How can i achieve this using Elasticsearch?Is it possible using Elasticsearch?

---

<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:** [March 15, 2018, 7:23am UTC](https://discuss.elastic.co/t/elastic-join-over-type-in-same-index/124034/2 "2018-03-15T07:23:31Z")

</div>

1. this is a relational database model not a NoSql document oriented model
2. in elasticsearch 6.0, multiple types are allowed
3. joins are not possible (unless you use parent/child feature which I don't recommend here)

With that said, just insert one single type of document which contains all the fields from your 2 tables: do join at index time.

---

<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:** [April 12, 2018, 7:23am UTC](https://discuss.elastic.co/t/elastic-join-over-type-in-same-index/124034/3 "2018-04-12T07:23:46Z")

</div>

Closed on April 12, 2018, 7:23am UTC

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