# One search on different index VS nested object

**URL:** <https://discuss.elastic.co/t/one-search-on-different-index-vs-nested-object/231566>\
**Category:** Elasticsearch\
**Created:** [May 7, 2020, 2:52pm UTC](https://discuss.elastic.co/t/one-search-on-different-index-vs-nested-object/231566 "2020-05-07T14:52:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![the\_fire\_fades](https://avatars.discourse-cdn.com/v4/letter/t/77aa72/32.png) [@the\_fire\_fades](https://discuss.elastic.co/u/the_fire_fades)\
**Post date:** [May 7, 2020, 2:52pm UTC](https://discuss.elastic.co/t/one-search-on-different-index-vs-nested-object/231566/1 "2020-05-07T14:52:30Z")

</div>

Hi everyone,

I haves 2 index, customer and address. The customer can have many addresses, an addresse belong to one customer.

In "customer" Index:

```auto
{
    "_id": 1,
    "firstname": "john",
   "lastname": "doe"
}
{
   "_id": 2,
   "firstname": "jane",
   "lastname": "done"
}

"addresses":
{
   {"_id":99, id_customer: 2, "street":"12 rue ordener", "city": "Paris"}
}

```

I need to get a list of customer with their address propore address.

After reading this post [How to join two index](https://discuss.elastic.co/t/how-to-join-two-index/57377) of thiago, I think it's maybe better to create one index  
customer and add the addresses inside customer. Something like this:

In customer Index:

```auto
{
   "_id": 1,
   "firstname": "john",
   "lastname": "doe",
  "addresses": [
          {"_id":99, "street":"155 rue ordener", "city": "Paris"},
          {"_id":100, "street":"125 rue championnet", "city": "Paris"}
   ]
}

```

Like a this a respect the concept behind ElasticSearch to have flat documents.  
But If I really want to have two indexes how can I do ?

Is it possible to create a query to get all the address index and then filter the result on customer index ?

Finally I just want to know the best practices to index thoses kind of "relation", is my first solution okay ?

Best regards

---

<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 7, 2020, 3:16pm UTC](https://discuss.elastic.co/t/one-search-on-different-index-vs-nested-object/231566/2 "2020-05-07T15:16:10Z")

</div>

Bonjour Paris 😉

> [@the\_fire\_fades](#):
>
> But If I really want to have two indexes how can I do ?

The question is "what for?" Why do you think you need this?

Basically I'd recommend to ask yourself 2 questions:

- What kind of objects my users want to get back as a response? If it's object `X`, then just index object `X`
- What typical attributes my users want to search for? Let say I need attribute `a`, `b` and `c`, just index those attributes within object `X` whatever the original source of those attributes is.

HTH

---

<div class="post-metadata">

**Author:** ![the\_fire\_fades](https://avatars.discourse-cdn.com/v4/letter/t/77aa72/32.png) [@the\_fire\_fades](https://discuss.elastic.co/u/the_fire_fades)\
**Post date:** [May 8, 2020, 12:26pm UTC](https://discuss.elastic.co/t/one-search-on-different-index-vs-nested-object/231566/3 "2020-05-08T12:26:38Z")

</div>

I want to create two indexes to get a better separation between customers and addresses. But I think it's coming from my sql background. Indexes are not tables, so I don't really get a benefit to separate customers and addresses. What I want is to search customers and get theire addresses, so I think It's maybe better to stick with this solution:

```auto
{
   "_id": 1,
   "firstname": "john",
   "lastname": "doe",
  "addresses": [
          {"_id":99, "street":"155 rue ordener", "city": "Paris"},
          {"_id":100, "street":"125 rue championnet", "city": "Paris"}
   ]
}

```

Like this I can search for X (customers) and get a, b, c (addresses).

Thanks a lot for the response.

---

<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 8, 2020, 12:53pm UTC](https://discuss.elastic.co/t/one-search-on-different-index-vs-nested-object/231566/4 "2020-05-08T12:53:49Z")

</div>

Yes. That's the thing I learned when I discovered search engines in the past. Forget all what your learned and don't think technically first but use case first.

😉

---

<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 5, 2020, 12:54pm UTC](https://discuss.elastic.co/t/one-search-on-different-index-vs-nested-object/231566/5 "2020-06-05T12:54:07Z")

</div>

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