# Need some help for data structuring

**URL:** https://discuss.elastic.co/t/need-some-help-for-data-structuring/33604
**Category:** Elasticsearch
**Created:** [November 3, 2015, 8:24am UTC](https://discuss.elastic.co/t/need-some-help-for-data-structuring/33604 "2015-11-03T08:24:30Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Stefan\_Kruse\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stefan_kruse_2/32/12005_2.png) [@Stefan\_Kruse\_2](https://discuss.elastic.co/u/Stefan_Kruse_2)
#### Post date: [November 3, 2015, 8:24am UTC](https://discuss.elastic.co/t/need-some-help-for-data-structuring/33604/1 "2015-11-03T08:24:30Z")

</div>

Hi, i have a mySQL Database with the following tables. Company, maincategory, subcategory and listing. Subcategory has a field with a one to one relation to maincategory. And listing has 3 fields with relations to company,maincategory subcategory. In words. Every Company could have many listings. I need to search for a subcategory name like medicine and must retrieve all companies who are listet for this subcategory. My question is now: How can i model this with elasticsearch? Which way would be the right? Thanks a lot for some hints and some help.

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [November 3, 2015, 10:02am UTC](https://discuss.elastic.co/t/need-some-help-for-data-structuring/33604/2 "2015-11-03T10:02:38Z")

</div>

The best bet would be to flatten it as much as possible.

---

<div class="post-metadata">

### Author: ![paol](https://avatars.discourse-cdn.com/v4/letter/p/c2a13f/32.png) [@paol](https://discuss.elastic.co/u/paol)
#### Post date: [November 3, 2015, 11:02am UTC](https://discuss.elastic.co/t/need-some-help-for-data-structuring/33604/3 "2015-11-03T11:02:15Z")

</div>

You have to denormalize the data. To make the search you want you would declare a Subcategory type with 2 fields:

- name (for the search)
- companies (could be just an array of company ids, or it could be an array of objects with whatever company fields you need to display in the search results)

---

<div class="post-metadata">

### Author: ![mainec](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mainec/32/5557_2.png) [@mainec](https://discuss.elastic.co/u/mainec)
#### Post date: [November 3, 2015, 12:59pm UTC](https://discuss.elastic.co/t/need-some-help-for-data-structuring/33604/4 "2015-11-03T12:59:46Z")

</div>

For some more background the following blog post might be interesting for you:

> **[Managing Relations Inside Elasticsearch
	  	 | Elastic](https://www.elastic.co/blog/managing-relations-inside-elasticsearch)**
>
> Data in the real world is rarely simple - often times it is a jumble of interlocking relations. How do you represent relational data in Elasticsearch? There are a few mechanisms that can be used to pr...

---

<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 5, 2017, 11:40pm UTC](https://discuss.elastic.co/t/need-some-help-for-data-structuring/33604/5 "2017-07-05T23:40:54Z")

</div>


