One search on different index VS nested object

Hi everyone,

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

In "customer" Index:

{
    "_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 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:

{
   "_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

Bonjour Paris :wink:

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

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:

{
   "_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.

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.

:wink:

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