# Implementing many to many relations with elastic

**URL:** https://discuss.elastic.co/t/implementing-many-to-many-relations-with-elastic/123423
**Category:** Elasticsearch
**Created:** [March 11, 2018, 12:23pm UTC](https://discuss.elastic.co/t/implementing-many-to-many-relations-with-elastic/123423 "2018-03-11T12:23:52Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![sepide\_mahmoodi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sepide_mahmoodi/32/28651_2.png) [@sepide\_mahmoodi](https://discuss.elastic.co/u/sepide_mahmoodi)
#### Post date: [March 11, 2018, 12:23pm UTC](https://discuss.elastic.co/t/implementing-many-to-many-relations-with-elastic/123423/1 "2018-03-11T12:23:52Z")

</div>

Hi.  
i have `news`, `category` and `news_category_relation` tables in mysql and i use logstash to import these data to elastic.  
i want to get relational data of news and its categoies in one query that have output like below:

```
news {
"title" : "test",
"createdAt" : "45666677868",
"category" : {
{"id" : 1,
"title" : "news_category1"
},
{"id" : 2,
"title" : "news_category2"
}
}
}

```

my data for news and category and those relation store in separate indexes.  
i tried parent/child approach but this approach use different type in one index and latest version of elastic does'nt support this feature.  
please help me, how to do this?

---

<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 11, 2018, 12:46pm UTC](https://discuss.elastic.co/t/implementing-many-to-many-relations-with-elastic/123423/2 "2018-03-11T12:46:24Z")

</div>

The first thing you need to think about is the use case.

What people want to search for? News? Categories?

Let's say that they want to search for news. Then I'd index documents like:

```auto
{
  "title" : "test",
  "createdAt" : "45666677868",
  "category" : [
    { 
      "id" : 1,
      "title" : "news_category1"
    },
    { 
      "id" : 2,
      "title" : "news_category2"
    }
  ]
}

```

I'd even simplify more with:

```auto
{
  "title" : "test",
  "createdAt" : "45666677868",
  "category" : ["news_category1", "news_category2"]
}

```

But it depends on the use case.

---

<div class="post-metadata">

### Author: ![sepide\_mahmoodi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sepide_mahmoodi/32/28651_2.png) [@sepide\_mahmoodi](https://discuss.elastic.co/u/sepide_mahmoodi)
#### Post date: [March 12, 2018, 7:30am UTC](https://discuss.elastic.co/t/implementing-many-to-many-relations-with-elastic/123423/3 "2018-03-12T07:30:50Z")

</div>

i want when choose a specific category showing all related news,  
then i want filter and search categories.

---

<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 12, 2018, 7:48am UTC](https://discuss.elastic.co/t/implementing-many-to-many-relations-with-elastic/123423/4 "2018-03-12T07:48:47Z")

</div>

What prevents doing so with the model I proposed?

---

<div class="post-metadata">

### Author: ![sepide\_mahmoodi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sepide_mahmoodi/32/28651_2.png) [@sepide\_mahmoodi](https://discuss.elastic.co/u/sepide_mahmoodi)
#### Post date: [March 12, 2018, 8:55am UTC](https://discuss.elastic.co/t/implementing-many-to-many-relations-with-elastic/123423/5 "2018-03-12T08:55:03Z")

</div>

you doing this with parent/child approach?  
i don`t know how implement this with logstash statemenets, and kibana queries.

---

<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 12, 2018, 9:31am UTC](https://discuss.elastic.co/t/implementing-many-to-many-relations-with-elastic/123423/6 "2018-03-12T09:31:00Z")

</div>

> you doing this with parent/child approach?

No. It's not needed.

---

<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 9, 2018, 9:31am UTC](https://discuss.elastic.co/t/implementing-many-to-many-relations-with-elastic/123423/7 "2018-04-09T09:31:34Z")

</div>

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