赞
踩
比如在mysql中我有两张表,movies和ratings,这两张表依赖movie_id实现关联。
那么我需要在elasticsearch中实现一下两个任务
When Will I Be Loved
的电影评分的均值是多少定义索引结构以及定义父子关系
DELETE /movies_ratings_inde
PUT /movies_ratings_index
{
"mappings": {
"properties": {
"movie_id": {"type": "keyword"},
"movie_title": {"type": "keyword"}
}
}
}
PUT /movies_ratings_index/_mapping
{
"properties": {
"rating_score": {"type": "float"},
"movie_id": {"type": "keyword"}
}
}
# ratings就是表连接,其中movie是父,rating是子
PUT /movies_ratings_index/_mapping
{
"properties": {
"movie_id": {"type": "keyword"},
"movie_title": {"type": "keyword"},
"ratings": {
"type": "join",
"relations": {
"movie": "rating"
}
}
}
}
导入数据,我这里父数据定义了两条,movie_id分别是1和2,子数据定义了多个。
POST /movies_ratings_index/_doc/1
{
"movie_id": "1",
"movie_title": "When Will I Be Loved",
"ratings": {
"name": "movie"
}
}
POST /movies_ratings_index/_doc/2
{
"movie_id": "2",
"movie_title": "When Will I Be Disdained",
"ratings": {
"name": "movie"
}
}
POST /movies_ratings_index/_doc/3?routing=1
{
"rating_score": 4.5,
"movie_id": "1",
"ratings": {
"name": "rating",
"parent": "1"
}
}
POST /movies_ratings_index/_doc/4?routing=1
{
"rating_score": 6.5,
"movie_id": "1",
"ratings": {
"name": "rating",
"parent": "1"
}
}
POST /movies_ratings_index/_doc/5?routing=1
{
"rating_score": 36.5,
"movie_id": "1",
"ratings": {
"name": "rating",
"parent": "1"
}
}
POST /movies_ratings_index/_doc/6?routing=1
{
"rating_score": 26.5,
"movie_id": "1",
"ratings": {
"name": "rating",
"parent": "1"
}
}
POST /movies_ratings_index/_doc/7?routing=1
{
"rating_score": 16.5,
"movie_id": "1",
"ratings": {
"name": "rating",
"parent": "1"
}
}
POST /movies_ratings_index/_doc/8?routing=2
{
"rating_score": 50,
"movie_id": "2",
"ratings": {
"name": "rating",
"parent": "2"
}
}
问题1:使用has_parent,因为我们这里是对父数据的movie_title字段进行筛选数据。
#使用has_parent查询
GET /movies_ratings_index/_search
{
"query": {
"has_parent": {
"parent_type": "movie",
"query": {
"match": {
"movie_title": "When Will I Be Loved"
}
}
}
},
"aggs": {
"avg_rating_score": {
"avg": {
"field": "rating_score"
}
}
}
}
问题2:使用has_child,因为我们这里是对孩子数据进行筛选。
#使用has_child查询
GET /movies_ratings_index/_search
{
"query": {
"has_child": {
"type": "rating",
"query": {
"range": {
"rating_score": {
"gt": 6
}
}
}
}
},
"aggs": {
"movies_with_high_ratings": {
"terms": {
"field": "movie_title.keyword",
"size": 10 // 返回前10个最频繁出现的电影标题
}
}
}
}
到这里能发现,就是说要想在elasticsearch中实现mysql的表连接操作必须要事先定义好父子关系,除此之外还要我这里只提到了两张表之间的关系,那么更多表的连接需要如何操作呢
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。