# Elasticsearch and a complex relationship

**URL:** https://discuss.elastic.co/t/elasticsearch-and-a-complex-relationship/179625
**Category:** Elasticsearch
**Created:** [May 4, 2019, 5:11pm UTC](https://discuss.elastic.co/t/elasticsearch-and-a-complex-relationship/179625 "2019-05-04T17:11:36Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![hiroshi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hiroshi/32/45547_2.png) [@hiroshi](https://discuss.elastic.co/u/hiroshi)
#### Post date: [May 4, 2019, 5:11pm UTC](https://discuss.elastic.co/t/elasticsearch-and-a-complex-relationship/179625/1 "2019-05-04T17:11:37Z")

</div>

For design reasons, the relationship between my entities is as follows: Having entities A, B, C and AB where AB is the relationship many to many. I want to index the values of the entity C that has a many to many relationship with the B, starting from the relationship with A. I have a postgresql relational database and I am trying to map to elasticsearch but I can not find a way to do this and handle the relations may to many. Thanks in advance.

```
@Entity
public class A {
     ...

     @OneToMany(mappedBy = "a", cascade = CascadeType.ALL)
      private List<AB> b;
}

@Entity
 public class AB {
      ...

     @ManyToOne
     B b;
 }

@Entity
public class B {
    ...

   @ManyToMany
   private List<C> c;
}

@Entity
public class C {
    ... // I want this indexes!!!

   @ManyToMany(mappedBy = "c")
   private List<B> b;
}
```

---

<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: [May 6, 2019, 9:27am UTC](https://discuss.elastic.co/t/elasticsearch-and-a-complex-relationship/179625/2 "2019-05-06T09:27:30Z")

</div>

I'd totally forget about the existing model and would just think about the usage to build the right "search" objects for my use case.  
Here I can't really answer as I don't know the use case and because it's not real objects.

Basically I'd recommend to ask yourself 2 questions:

- What kind of objects my users want to get back as a response? If the answer is `C` then index a `C` object.
- What typical attributes my users want to search for? `B`? Then store in the `C` object the content of the `B` object.

HTH

---

<div class="post-metadata">

### Author: ![hiroshi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hiroshi/32/45547_2.png) [@hiroshi](https://discuss.elastic.co/u/hiroshi)
#### Post date: [May 6, 2019, 1:26pm UTC](https://discuss.elastic.co/t/elasticsearch-and-a-complex-relationship/179625/3 "2019-05-06T13:26:01Z")

</div>

Thank you very much for answering, I will work based on your advice.

---

<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: [June 3, 2019, 1:26pm UTC](https://discuss.elastic.co/t/elasticsearch-and-a-complex-relationship/179625/4 "2019-06-03T13:26:08Z")

</div>

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

---

<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: [June 3, 2019, 1:26pm UTC](https://discuss.elastic.co/t/elasticsearch-and-a-complex-relationship/179625/5 "2019-06-03T13:26:09Z")

</div>



---

<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: [November 4, 2022, 7:06am UTC](https://discuss.elastic.co/t/elasticsearch-and-a-complex-relationship/179625/6 "2022-11-04T07:06:32Z")

</div>


