How to work with elasticsearch and associations between index objects?

I use the Tire pearl for Rails, and I have a couple of questions about model associations. How do you work with them? Say you have a relationship between a Person and a car. Each person has a lot of cars. Now, if you also want to index car objects, how do you do it? How can you get a person by searching for car.make for example?

In general, I see that elasticsearch, which is document-oriented, does not have the same concepts as RDBMS. One to one, one to many and many to many.

If you have a many-to-many relationship, for example, and you want objects with the other end of the relationship to be impossible? Is elastic search better suited for a NoSQL database like MongoDB?

+4
source share
1 answer

There are many possible strategies for modeling your data in Elasticsearch, including relationships. There are at least three strategies for related data in Elasticsearch:

  • Just use JSON and its ability to freely express hierarchies,
  • for a situation where Car is actually a list of cars, use a nested type,
  • use parent / child support for situations where you need to index both entities individually.

With Tire, first check and try this code in your code: Elasticsearch, Tire, and nested queries / associations with ActiveRecord . It should contain all the information necessary for your scenario. The code is also available separately .

Literature:

+4
source

All Articles