Elasticsearch with multiple parent / child relationships

I am building an application with a complex model, says Book, User and Review.

The overview contains the identifier of the book and user. In order to be able to search for Books containing at least one review, I set the parent record "Book as a review" and had routing as such. However, I also need to find users who wrote reviews containing specific phrases.

Is it possible to have a parent Book and User as Review? Is there a better way to handle this situation?

Please note that I cannot change the way the data is modeled / not wanting this, because the data is transferred to Elasticsearch from the save database.

+5
source share
3 answers

As far as I know, you do not have a document with two parents.

My suggestion, based on the Applied Part of the Elasticsearch Chapter for a definitive guide :

  • Create Parent / Child Relationship Book / Review
  • Make sure you have the user_id property in the Review display, which contains the identifier of the user who wrote this review.

I think that in both cases the cases described below are used:

  • Books that contain at least one review It can be resolved with a child filter / query
  • Users who wrote reviews that contain certain phrases This can be resolved by referring to the reviews with the phrase you want to perform and perform power aggregation in the user_id field. If you need user information, you should query your database (or another elasticsearch search index ) with extracted identifiers.

Edit: "give me the books that have reviews this month written by user whose name started with John"

I recommend that you collect all of these advanced use cases and denormalize the data necessary to achieve them. In this particular case, it is enough to denormalizing the username in Review . In any case, people working with elasticsearch wrote about relationship management on their blog or elasticsearch the final guide

+5
source

You have two options.

Elasticsearch Nested Objects

Source Parent & Parent

both are compared and rated beautifully here

0
source

Somths like (just create the Books type as the parent for user types and reviews)

 .../index/users/_search?pretty" -d ' { "query": { "filtered": { "filter": { "and": [ { "has_parent": { "parent_type": "books", "filter": { "has_child": { "type": "Reviews", "query": { "term": { "text_review": "some word" } } } } } } ] } } } } ' 
0
source

All Articles