【发布时间】:2014-08-13 20:10:18
【问题描述】:
在电影数据库中,我存储用户对每部电影的评分(0 到 5 星)。我在 Elastic Search(版本 1.2.2)中索引了以下文档结构
"_index": "my_index"
"_type": "film",
"_id": "6629",
"_source": {
"id": "6629",
"title": "Fight Club",
"ratings" : [
{ "user_id" : 1234, "rating_value" : 3 },
{ "user_id" : 4567, "rating_value" : 2 },
{ "user_id" : 7890, "rating_value" : 1 }
.....
]
}
"_index": "my_index"
"_type": "film",
"_id": "6630",
"_source": {
"id": "6630",
"title": "Pulp Fiction",
"ratings" : [
{ "user_id" : 1234, "rating_value" : 1 },
{ "user_id" : 7654, "rating_value" : 2 },
{ "user_id" : 4321, "rating_value" : 5 }
.....
]
}
等等……
我的目标是在一次搜索中获得用户(比如用户 1234)评分的所有电影,以及 rating_value
如果我进行以下搜索
GET my_index/film/_search
{
"query": {
"match": {
"ratings.user_id": "1234"
}
}
}
对于所有匹配的电影,我得到整个文档,然后,我必须解析整个评级数组以找出数组中的哪个元素与我的查询匹配,以及与 user_id 1234 关联的 rating_value 是什么。
理想情况下,我希望这个查询的结果是
"hits": [ {
"_index": "my_index"
"_type": "film",
"_id": "6629",
"_source": {
"id": "6629",
"title": "Fight Club",
"ratings" : [
{ "user_id" : 1234, "rating_value" : 3 }, // <= only the row that matches the query
]
},
"_index": "my_index"
"_type": "film",
"_id": "6630",
"_source": {
"id": "6630",
"title": "Pulp Fiction",
"ratings" : [
{ "user_id" : 1234, "rating_value" : 1 }, // <= only the row that matches the query
]
}
} ]
提前致谢
【问题讨论】:
-
您无法获得您想要的理想结果,因为 _source 将始终与您索引的 JSON 完全相同。但是,您可以使用聚合来获取所需的信息。
标签: elasticsearch