# Type Self-Referential Relationships

**URL:** <https://discuss.elastic.co/t/type-self-referential-relationships/132526>\
**Category:** Elasticsearch\
**Created:** [May 18, 2018, 6:11pm UTC](https://discuss.elastic.co/t/type-self-referential-relationships/132526 "2018-05-18T18:11:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ehorodyski](https://avatars.discourse-cdn.com/v4/letter/e/a698b9/32.png) [@ehorodyski](https://discuss.elastic.co/u/ehorodyski)\
**Post date:** [May 18, 2018, 6:11pm UTC](https://discuss.elastic.co/t/type-self-referential-relationships/132526/1 "2018-05-18T18:11:25Z")

</div>

I'm very confused with self-referencial types in Elasticsearch 6.2. Something simple, I want to create a family tree where I map myself to all my brothers. We're all of `person` type, so how can I connect this relationship?

Using the sample from the documentation, I have a mapping like below, but it doesn't work:

```auto
PUT people
{
  "mappings": {
    "person": {
      "properties": {
        "brothers": { 
          "type": "join",
          "relations": {
            "brother": "brother" 
          }
        }
      }
    }
  }
}

```

Maybe this isn't the best way to approach this? Essentially what I'd like to do is store the IDs of my brothers in _my_ document, and when I search for my ID, they come back with my record, either through aggregation or part of my `_source`.

Any help would be appreciated...

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [May 25, 2018, 2:01pm UTC](https://discuss.elastic.co/t/type-self-referential-relationships/132526/2 "2018-05-25T14:01:28Z")

</div>

The only way to create the relationship between brothers is by linking them with the same parent. Brother-to-brother relationship is N-to-N, and the join datatype supports only 1-to-N relationships.

---

<div class="post-metadata">

**Author:** ![ehorodyski](https://avatars.discourse-cdn.com/v4/letter/e/a698b9/32.png) [@ehorodyski](https://discuss.elastic.co/u/ehorodyski)\
**Post date:** [May 25, 2018, 2:24pm UTC](https://discuss.elastic.co/t/type-self-referential-relationships/132526/3 "2018-05-25T14:24:02Z")

</div>

Thanks Igor, what would you suggest in this scenario? Is there a way I can store an array of type string which would be the IDs of my brothers, but then return those records in my query?

Since they all live in the same index, is it possible?

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [May 25, 2018, 2:39pm UTC](https://discuss.elastic.co/t/type-self-referential-relationships/132526/4 "2018-05-25T14:39:08Z")

</div>

I am not really sure what you are trying to achieve. What types of queries are you planning to run with this data?

---

<div class="post-metadata">

**Author:** ![ehorodyski](https://avatars.discourse-cdn.com/v4/letter/e/a698b9/32.png) [@ehorodyski](https://discuss.elastic.co/u/ehorodyski)\
**Post date:** [June 12, 2018, 9:23pm UTC](https://discuss.elastic.co/t/type-self-referential-relationships/132526/5 "2018-06-12T21:23:46Z")

</div>

In reality, it's products. If I sell a Raspberry Pi bundle that is composed of 3 different products, I'd like to get the parent products as well.

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [June 13, 2018, 1:38pm UTC](https://discuss.elastic.co/t/type-self-referential-relationships/132526/6 "2018-06-13T13:38:14Z")

</div>

Can the same part be part of 2 different bundles? What kind of queries are you planning to run?

---

<div class="post-metadata">

**Author:** ![ehorodyski](https://avatars.discourse-cdn.com/v4/letter/e/a698b9/32.png) [@ehorodyski](https://discuss.elastic.co/u/ehorodyski)\
**Post date:** [June 17, 2018, 7:12pm UTC](https://discuss.elastic.co/t/type-self-referential-relationships/132526/7 "2018-06-17T19:12:21Z")

</div>

Yes, the same part could be part of 2 different bundles.

Here's another example: I run a pharmacy. All of my products have the same schema/type and live in the same index. On my pharmacy's website, a user searches for `Tylenol`, and clicks `Tylenol PM`. In this detail view, I would like to show the user metadata on the other products in the Tylenol family -- `Tylenol Soft-Gel`, `Tylenol for Kids`, `Liquid Tylenol`. Those would be the sibling products I would like to store in `Tylenol PM`'s entry.

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [June 18, 2018, 2:15am UTC](https://discuss.elastic.co/t/type-self-referential-relationships/132526/8 "2018-06-18T02:15:13Z")

</div>

> [@ehorodyski](#):
>
> In this detail view, I would like to show the user metadata on the other products in the Tylenol family

In this case, it sounds like just storing the family in a keyword field and then executing the second search using term filter to populate the detailed view would be the way to go.

---

<div class="post-metadata">

**Author:** ![ehorodyski](https://avatars.discourse-cdn.com/v4/letter/e/a698b9/32.png) [@ehorodyski](https://discuss.elastic.co/u/ehorodyski)\
**Post date:** [June 18, 2018, 9:15pm UTC](https://discuss.elastic.co/t/type-self-referential-relationships/132526/9 "2018-06-18T21:15:42Z")

</div>

That's what I thought, no way to combine it into one query though?

---

<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 16, 2018, 9:15pm UTC](https://discuss.elastic.co/t/type-self-referential-relationships/132526/10 "2018-07-16T21:15:50Z")

</div>

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