Elasticsearch with multiple parent/child relations

2020-03-16 07:28发布

I'm building an application with complicated model, says Book, User and Review.

A Review contains both Book and User id. To be able to search for Books that contain at least one review, I've set the Book as Review's parent and have routing as such. However I also need to find Users who wrote reviews that contain certain phrases.

Is it possible to have both the Book and User as Review's parent? Is there a better way to handle such situation?

Note that I'm not able to change the way data is modeled/not willing to do so because the data is transfered to Elasticsearch from a persistence database.

3条回答
家丑人穷心不美
2楼-- · 2020-03-16 07:39

As far as I know you can't have a document with two parents.

My suggestion based on Application-side join chapter of Elasticsearch the definitive guide:

  • Create a parent/child relationship Book/Review
  • Be sure you have user_id property in Review mapping which contain the user id who wrote that review.

I think that covers both uses cases you described as follows:

  • Books that contain at least one review It can be solved with has child filter/query
  • Users who wrote reviews that contain certain phrases It can be solved by querying to reviews with the phrase you want to search and perform a cardinality aggregation on field user_id. If you need users information you have to query your database (or another elasticsearch index) with the ids retrieved.

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

I recommend you to collect all those advanced uses cases and denormalize the data you need to achieve them. In this particular case it's enough with denormalizing the user name into Review. In any case elasticsearch people has written about managing relations in their blog or elasticsearch the definitive guide

查看更多
Juvenile、少年°
3楼-- · 2020-03-16 07:47

You have two options

Elasticsearch Nested Objects

Elasticsearch parent&child

both are compared and evaluated nicely here

查看更多
Fickle 薄情
4楼-- · 2020-03-16 07:54

Somths like (just make Books type as parent for Users and Reviews types)

.../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"
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    ]
                }
            }
        }
    }
    '
查看更多
登录 后发表回答