具有多个父/子关系的弹性搜索 [英] Elasticsearch with multiple parent/child relationship
问题描述
评论包含Book和User id。
为了能够搜索至少包含一个评论的图书,我已将Book设置为Review,并具有路由。但是,我还需要找到撰写包含某些短语的评论的用户。
是否可以将Book和User作为Review?有没有更好的方法来处理这种情况?
请注意,我无法更改数据建模/不愿意这样做的方式,因为数据被转移来自持久性数据库的弹性搜索。
据我所知,您无法拥有两个父母的文档。 p>
我的建议是基于应用程序连接弹性搜索章节:
- 创建父/子关系
预订
/评论
- 确保你有
user_id
属性在
评论
映射中,其中包含撰写该评论的用户标识。
我认为这两个用例描述如下:
-
至少包含的书籍一个评论
可以用h解决作为儿童过滤器 / 查询 -
撰写包含某些短语的评论的用户
可以通过使用要搜索的短语查询评论并执行<在场上的href =http://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html =noreferrer>基数聚合USER_ID
。如果您需要用户信息,您必须查询您的数据库(或其他弹性搜索索引),并检索ids。
编辑:给我本月由John写的用户评论的书籍
我建议您收集所有那些高级用例和非标准化您需要实现的数据。在这种特殊情况下,将用户名转换为 Review
就足够了。无论如何,弹性人物已经在管理关系他们的博客或< a href =http://www.elastic.co/guide/en/elasticsearch/guide/master/relations.html =noreferrer>弹性搜索最终指南
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.
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 inReview
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/queryUsers 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 fielduser_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
这篇关于具有多个父/子关系的弹性搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!