es的restful api在学习和理解es的所欲知识点和功能点:
1、es最重要的api,让我们进行各种尝试、学习甚至在某些环境下进行使用的api,就是restful api。如果你学习不用es restful api,比如我上来就用java api来学习es,也是可以的,但是你根本就漏掉了es知识的一大块,你都不知道它最重要的restful api是怎么用的
2、学习用esrestful api,更加方便,快捷,不用每次都写大量的java代码,能加快关注es本身的知识和功能的学习
计算每个tag下的商品数量
GET/ecommerce/product/_search
{
"aggs": {
"group_by_tags": {
"terms": { "field":"tags" }
}
}
}
会报这个错误
将文本field的fielddata属性设置为true
PUT/ecommerce/_mapping/product
{
"properties": {
"tags": {
"type": "text",
"fielddata": true
}
}
}
设置后显示
{
"acknowledged": true
}
GET/ecommerce/product/_search
{
"size": 0,
"aggs": {
"all_tags": {
"terms": { "field":"tags" }
}
}
}
{
"took": 20,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 0,
"hits": []
},
"aggregations": {
"group_by_tags": {
"doc_count_error_upper_bound":0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "fangzhu",
"doc_count": 2
},
{
"key": "meibai",
"doc_count": 2
},
{
"key": "qingxin",
"doc_count": 1
}
]
}
}
}
对名称中包含yagao的商品,计算每个tag下的商品数量
GET/ecommerce/product/_search
{
"size": 0,
"query": {
"match": {
"name": "yagao"
}
},
"aggs": {
"all_tags": {
"terms": {
"field": "tags"
}
}
}
}
先分组,再算每组的平均值,计算每个tag下的商品的平均价格
GET/ecommerce/product/_search
{
"size": 0,
"aggs" : {
"group_by_tags" : {
"terms" : {"field" : "tags" },
"aggs" : {
"avg_price" : {
"avg" : {"field" : "price" }
}
}
}
}
}
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 0,
"hits": []
},
"aggregations": {
"group_by_tags": {
"doc_count_error_upper_bound":0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "fangzhu",
"doc_count": 2,
"avg_price": {
"value": 27.5
}
},
{
"key": "meibai",
"doc_count": 2,
"avg_price": {
"value": 40
}
},
{
"key": "qingxin",
"doc_count": 1,
"avg_price": {
"value": 40
}
}
]
}
}
}
计算每个tag下的商品的平均价格,并且按照平均价格降序排序
GET/ecommerce/product/_search
{
"size": 0,
"aggs" : {
"all_tags" : {
"terms" : {"field" : "tags", "order": {"avg_price": "desc" } },
"aggs" : {
"avg_price" : {
"avg" : {"field" : "price" }
}
}
}
}
}
console输出显示:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 0,
"hits": []
},
"aggregations": {
"group_by_tags": {
"doc_count_error_upper_bound":0,
"sum_other_doc_count": 0,
"buckets": [
{
"key":"hanghaishi",
"doc_count": 1,
"avg_price": {
"value": 2200000000
}
},
{
"key": "qingxin",
"doc_count": 1,
"avg_price": {
"value": 40
}
},
{
"key": "meibai",
"doc_count": 1,
"avg_price": {
"value": 30
}
},
{
"key": "fangzhu",
"doc_count": 2,
"avg_price": {
"value": 27.5
}
}
]
}
}
}
按照指定的价格范围区间进行分组,然后在每组内再按照tag进行分组,最后再计算每组的平均价格
GET/ecommerce/product/_search
{
"size": 0,
"aggs": {
"group_by_price": {
"range": {
"field": "price",
"ranges": [
{
"from": 0,
"to": 20
},
{"from": 20,
"to": 40
},{
"from": 40,
"to": 50
}
]
},"aggs":{
"group_by_tags": {
"terms": {
"field": "tags"
} ,"aggs": {
"avg_price": {
"avg": {
"field":"price"
}
}
}
}
}
}
}
}
console输出显示:
{
"took": 9,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 0,
"hits": []
},
"aggregations": {
"group_by_price": {
"buckets": [
{
"key":"0.0-20.0",
"from": 0,
"to": 20,
"doc_count": 0,
"group_by_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": []
}
},
{
"key":"20.0-40.0",
"from": 20,
"to": 40,
"doc_count": 2,
"group_by_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key":"fangzhu",
"doc_count": 2,
"avg_price": {
"value": 27.5
}
},
{
"key":"meibai",
"doc_count": 1,
"avg_price": {
"value": 30
}
}
]
}
},
{
"key":"40.0-50.0",
"from": 40,
"to": 50,
"doc_count": 1,
"group_by_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key":"qingxin",
"doc_count": 1,
"avg_price": {
"value": 40
}
}
]
}
}
]
}
}
}