弹性搜索中的一对多关系 [英] one-to-many relationships in Elastic Search
问题描述
假设我有2个表名为twitter_user和twitter_comments。
Suppose I have 2 tables called "twitter_user" and "twitter_comments".
twitter_users 有以下字段:username和bio
twitter_comments 包含以下字段:用户名和评论
twitter_users has the fields: username and bio twitter_comments has the fields: username and comment
显然,一个用户在twitter_users中有1个条目,并且可能在twitter_comments中有许多
Obviously, an user has 1 entry in twitter_users and potentially many in twitter_comments
我想在弹性搜索中同时建立twitter_users和twitter_comments,当我查询时,ES会搜索这两个模型,知道一则评论针对一个twitter用户的整体相关性分数。
I want to model both twitter_users and twitter_comments in Elastic Search, have ES search both models when I query, knowing that a comment counts towards the overall relevancy score for a twitter user.
我知道我可以通过创建一个额外的字段(除了用户名和生物)与所有的意见连接,只能模仿这个模型。但是还有另一种更干净的方式吗?
I know I can mimic this with just 1 model, by creating a single extra field (in addition to username and bio) with all the comments concatenated. But is there another "cleaner" way?
推荐答案
这取决于。
如果您只想搜索用户评论,全文和所有字段,只需将用户对象内的所有评论(不需要连接任何内容):
If you just want to be able to search for a users comments ,full-text and over all fields, simply store all comments within the user object (no need to concatenate anything):
{
"user" : {
"username" : "TestUser",
"bio" : "whatever",
"comments" : [
{
"title" : "First comment",
"text" : "My 1st comment"
},
{
"title" : "Second comment",
"text" : "My 2nd comment"
}
]
}
}
如果您需要基于评论的查询,则需要将注释映射为嵌套(在提交任何数据之前),以便每个注释都被视为单个项目。
If you need per-comment-based queries you need to map the comments as nested (before submitting any data), so that every comment gets treated as a single item.
对于您的得分,只需添加另一个字段comment_count,并将其用于提升/评分。
For your scoring, simply add another field "comment_count" and use this for your boost/scoring.
这篇关于弹性搜索中的一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!