PUT /shop/product/1
{
"name": "Charcoal Toothpaste",
"desc": "Travel-Friendly Daily Use Teeth Whitening Cleaning Activated Organic Charcoal Toothpaste",
"price": 30,
"producer": "Charcoal Producer",
"tags": ["Whitening", "Refreshing"]
}
PUT /shop/product/2
{
"name": "Netural Toothpaste",
"desc": "128g Rock salt whitening anti sensitive Toothpaste natural oral deeping cleaning",
"price": 25,
"producer": "Netural Producer",
"tags": ["Whitening", "Refreshing", "Cleaning"]
}
PUT /shop/product/3
{
"name": "Bamboo Toothpaste",
"desc": "Bamboo Charcoal Toothpaste Fluoride Free 4 oz",
"price": 40,
"producer": "Bamboo Producer",
"tags": ["Anti-Cavity", "Sensitive"]
}
PUT /shop/_mapping/product
{
"properties": {
"tags": {
"type": "text",
"fielddata": true
}
}
}
GET /shop/product/_search
{
"query": {
"match": {
"name": "Toothpaste"
}
},
"sort": [
{
"price": {
"order": "desc"
}
}
]
}
{
"took" : 55,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : null,
"hits" : [
{
"_index" : "shop",
"_type" : "product",
"_id" : "3",
"_score" : null,
"_source" : {
"name" : "Bamboo Toothpaste",
"desc" : "Bamboo Charcoal Toothpaste Fluoride Free 4 oz",
"price" : 40,
"producer" : "Bamboo Producer",
"tags" : [
"Anti-Cavity",
"Sensitive"
]
},
"sort" : [
40
]
},
{
"_index" : "shop",
"_type" : "product",
"_id" : "1",
"_score" : null,
"_source" : {
"name" : "Charcoal Toothpaste",
"desc" : "Travel-Friendly Daily Use Teeth Whitening Cleaning Activated Organic Charcoal Toothpaste",
"price" : 30,
"producer" : "Charcoal Producer",
"tags" : [
"Whitening",
"Refreshing"
]
},
"sort" : [
30
]
},
{
"_index" : "shop",
"_type" : "product",
"_id" : "2",
"_score" : null,
"_source" : {
"name" : "Netural Toothpaste",
"desc" : "128g Rock salt whitening anti sensitive Toothpaste natural oral deeping cleaning",
"price" : 25,
"producer" : "Netural Producer",
"tags" : [
"Whitening",
"Refreshing",
"Cleaning"
]
},
"sort" : [
25
]
}
]
}
}
# 分页下标从0开始,document并非按照id排序
GET /shop/product/_search
{
"query": {
"match_all": {}
},
"from": 1,
"size": 1,
"_source": ["name", "price"]
}
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 1.0,
"hits" : [
{
"_index" : "shop",
"_type" : "product",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"price" : 30,
"name" : "Charcoal Toothpaste"
}
}
]
}
}
GET /shop/product/_search
{
"query": {
"bool": {
"must": {
"match": {
"name": "Toothpaste"
}
},
"filter": {
"range": {
"price": {
"gt": 30
}
}
}
}
}
}
{
"took" : 15,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "shop",
"_type" : "product",
"_id" : "3",
"_score" : 0.2876821,
"_source" : {
"name" : "Bamboo Toothpaste",
"desc" : "Bamboo Charcoal Toothpaste Fluoride Free 4 oz",
"price" : 40,
"producer" : "Bamboo Producer",
"tags" : [
"Anti-Cavity",
"Sensitive"
]
}
}
]
}
}
GET /shop/product/_search
{
"query": {
"match": {
"name": "Bamboo"
}
},
"highlight": {
"fields": {
"name": {}
}
}
}
{
"took" : 51,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "shop",
"_type" : "product",
"_id" : "3",
"_score" : 0.2876821,
"_source" : {
"name" : "Bamboo Toothpaste",
"desc" : "Bamboo Charcoal Toothpaste Fluoride Free 4 oz",
"price" : 40,
"producer" : "Bamboo Producer",
"tags" : [
"Anti-Cavity",
"Sensitive"
]
},
"highlight" : {
"name" : [
"<em>Bamboo</em> Toothpaste"
]
}
}
]
}
}
GET /shop/product/_search
{
"aggs": {
"products_per_tag": {
"terms": {
"field": "tags"
}
}
},
"size": 0 # 这个代表只返回聚合结果而不返回每个Document记录
}
{
"took" : 38,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"products_per_tag" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "refreshing",
"doc_count" : 2
},
{
"key" : "whitening",
"doc_count" : 2
},
{
"key" : "anti",
"doc_count" : 1
},
{
"key" : "cavity",
"doc_count" : 1
},
{
"key" : "cleaning",
"doc_count" : 1
},
{
"key" : "sensitive",
"doc_count" : 1
}
]
}
}
}
GET /shop/product/_search
{
"query": {
"match": {
"name": "Toothpaste"
}
},
"aggs": {
"products_per_tag": {
"terms": {
"field": "tags"
}
}
},
"size": 0
}
# 结果与上述结果是一样的
GET /shop/product/_search
{
"aggs": {
"group_by_tag": {
"terms": {
"field": "tags"
},
"aggs": {
"avg_price_per_tag": {
"avg": {
"field": "price"
}
}
}
}
},
"size": 0
}
{
"took" : 11,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"group_by_tag" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "refreshing",
"doc_count" : 2,
"avg_price_per_tag" : {
"value" : 27.5
}
},
{
"key" : "whitening",
"doc_count" : 2,
"avg_price_per_tag" : {
"value" : 27.5
}
},
{
"key" : "anti",
"doc_count" : 1,
"avg_price_per_tag" : {
"value" : 40.0
}
},
{
"key" : "cavity",
"doc_count" : 1,
"avg_price_per_tag" : {
"value" : 40.0
}
},
{
"key" : "cleaning",
"doc_count" : 1,
"avg_price_per_tag" : {
"value" : 25.0
}
},
{
"key" : "sensitive",
"doc_count" : 1,
"avg_price_per_tag" : {
"value" : 40.0
}
}
]
}
}
}
GET /shop/product/_search
{
"aggs": {
"group_by_tag": {
"terms": {
"field": "tags",
"order": {
"avg_price_per_tag": "desc"
}
},
"aggs": {
"avg_price_per_tag": {
"avg": {
"field": "price"
}
}
}
}
},
"size": 0
}
{
"took" : 14,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"group_by_tag" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "anti",
"doc_count" : 1,
"avg_price_per_tag" : {
"value" : 40.0
}
},
{
"key" : "cavity",
"doc_count" : 1,
"avg_price_per_tag" : {
"value" : 40.0
}
},
{
"key" : "sensitive",
"doc_count" : 1,
"avg_price_per_tag" : {
"value" : 40.0
}
},
{
"key" : "refreshing",
"doc_count" : 2,
"avg_price_per_tag" : {
"value" : 27.5
}
},
{
"key" : "whitening",
"doc_count" : 2,
"avg_price_per_tag" : {
"value" : 27.5
}
},
{
"key" : "cleaning",
"doc_count" : 1,
"avg_price_per_tag" : {
"value" : 25.0
}
}
]
}
}
}
GET /shop/product/_search
{
"aggs": {
"group_by_price_range": {
"range": {
"field": "price",
"ranges": [
{
"from": 0,
"to": 30
},
{
"from": 30,
"to": 60
}
]
},
"aggs": {
"group_by_tag": {
"terms": {
"field": "tags"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
},
"size": 0
}
{
"took" : 13,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"group_by_price_range" : {
"buckets" : [
{
"key" : "0.0-30.0",
"from" : 0.0,
"to" : 30.0,
"doc_count" : 1,
"group_by_tag" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "cleaning",
"doc_count" : 1,
"avg_price" : {
"value" : 25.0
}
},
{
"key" : "refreshing",
"doc_count" : 1,
"avg_price" : {
"value" : 25.0
}
},
{
"key" : "whitening",
"doc_count" : 1,
"avg_price" : {
"value" : 25.0
}
}
]
}
},
{
"key" : "30.0-60.0",
"from" : 30.0,
"to" : 60.0,
"doc_count" : 2,
"group_by_tag" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "anti",
"doc_count" : 1,
"avg_price" : {
"value" : 40.0
}
},
{
"key" : "cavity",
"doc_count" : 1,
"avg_price" : {
"value" : 40.0
}
},
{
"key" : "refreshing",
"doc_count" : 1,
"avg_price" : {
"value" : 30.0
}
},
{
"key" : "sensitive",
"doc_count" : 1,
"avg_price" : {
"value" : 40.0
}
},
{
"key" : "whitening",
"doc_count" : 1,
"avg_price" : {
"value" : 30.0
}
}
]
}
}
]
}
}
}