Elasticsearch 5.4.3 聚合分組實戰演練
阿新 • • 發佈:2019-02-10
GET /my_index/my_type/_search
{
"query": {
"fuzzy": {
"text":{
"value": "surprize",
"fuzziness": 6
}
}
}
}
GET /my_index/my_type/_search
{
"query": {
"fuzzy": {
"text":{
"value": "surprize",
"fuzziness": 5
}
}
}
}
GET /my_index/my_type/_search
{
"query": {
"match": {
"text": {
"query": "SURPIZE ME",
"fuzziness": "AUTO",
"operator": "and"
}
}
}
}
GET /my_index/my_type/_search
{
"query": {
"match": {
"text": {
"query": "SURPIZE ME",
"fuzziness": "AUTO",
"operator": "and"
}
}
}
}
PUT my_index
{
"mappings": {
"my_type":{
"properties": {
"text": {
"type":"text",
"analyzer":"ik_max_word"
}
}
}
}
}
POST /my_index/my_type/_bulk
{ "index": { "_id": "1"} }
{ "text": "男子偷上萬元發紅包求交女友 被抓獲時仍然單身" }
{ "index": { "_id": "2"} }
{ "text": "16歲少女為結婚“變”22歲 7年後想離婚被法院拒絕" }
{ "index": { "_id": "3"} }
{ "text": "深圳女孩騎車逆行撞賓士 遭索賠被嚇哭(圖)" }
{ "index": { "_id": "4"} }
{ "text": "女人對護膚品比對男票好?網友神懟" }
{ "index": { "_id": "5"} }
{ "text": "為什麼國內的街道招牌用的都是紅黃配?" }
GET /my_index/my_type/_search
{
"query": {
"match": {
"text": "16歲少女結婚好還是單身好"
}
}
}
GET /my_index/_analyze
{
"text": "男子偷上萬元發紅包求交女友 被抓獲時仍然單身",
"analyzer": "ik_max_word"
}
GET /my_index/_analyze
{
"text":"深圳女孩騎車逆行撞賓士 遭索賠被嚇哭(圖)",
"analyzer": "ik_max_word"
}
GET _analyze
{
"text": "啥藍瘦香菇",
"analyzer": "ik_max_word"
}
PUT /tvs
{
"mappings": {
"sales": {
"properties": {
"price": {
"type": "long"
},
"color": {
"type": "keyword"
},
"brand": {
"type": "keyword"
},
"sold_date": {
"type": "date"
}
}
}
}
}
POST /tvs/sales/_bulk
{ "index": {}}
{ "price" : 1000, "color" : "紅色", "brand" : "長虹", "sold_date" : "2016-10-28" }
{ "index": {}}
{ "price" : 2000, "color" : "紅色", "brand" : "長虹", "sold_date" : "2016-11-05" }
{ "index": {}}
{ "price" : 3000, "color" : "綠色", "brand" : "小米", "sold_date" : "2016-05-18" }
{ "index": {}}
{ "price" : 1500, "color" : "藍色", "brand" : "TCL", "sold_date" : "2016-07-02" }
{ "index": {}}
{ "price" : 1200, "color" : "綠色", "brand" : "TCL", "sold_date" : "2016-08-19" }
{ "index": {}}
{ "price" : 2000, "color" : "紅色", "brand" : "長虹", "sold_date" : "2016-11-05" }
{ "index": {}}
{ "price" : 8000, "color" : "紅色", "brand" : "三星", "sold_date" : "2017-01-01" }
{ "index": {}}
{ "price" : 2500, "color" : "藍色", "brand" : "小米", "sold_date" : "2017-02-12" }
GET /tvs/sales/_search
{
"size": 0,
"aggs": {
"colour": {
"terms": {
"field": "color"
}
}
}
}
GET /tvs/sales/_search
{
"size" : 0,
"aggs": {
"colors": {
"terms": {
"field": "color"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
GET /tvs/sales/_search
{
"size": 0,
"aggs": {
"color": {
"terms": {
"field": "color"
},
"aggs": {
"price": {
"avg": {
"field": "price"
}
}
}
}
}
}
GET /tvs/sales/_search
{
"size": 0,
"aggs": {
"color": {
"terms": {
"field": "color"
},
"aggs": {
"price": {
"avg": {
"field": "price"
}
},
"terms": {
"field": "brand"
},
"aggs": {
"price": {
"avg": {
"field": "price"
}
}
}
}
}
}
}
GET /tvs/sales/_search
{
"size": 0,
"aggs": {
"group_by_color": {
"terms": {
"field": "color"
},
"aggs": {
"color_avg_price": {
"avg": {
"field": "price"
}
},
"group_by_brand": {
"terms": {
"field": "brand"
},
"aggs": {
"brand_avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
}
}
GET /tvs/sales/_search
{
"size": 0,
"aggs": {
"color": {
"terms": {
"field": "color"
},
"aggs": {
"price": {
"avg": {
"field": "price"
}
},
"group_brand":{
"terms": {
"field": "brand"
},
"aggs": {
"brand_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
}
}
GET /tvs/sales/_search
{
"size": 0,
"aggs": {
"colors": {
"terms": {
"field": "color"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
},
"min_price": {
"min": {
"field": "price"
}
},
"max_price": {
"max": {
"field": "price"
}
},
"sum_price": {
"sum": {
"field": "price"
}
}
}
}
}
}
GET /tvs/sales/_search
{
"size": 0,
"aggs": {
"color": {
"terms": {
"field": "color"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
},
"min_price":{
"min": {
"field": "price"
}
},
"max_price":{
"max": {
"field": "price"
}
},
"sum_price":{
"sum": {
"field": "price"
}
}
}
}
}
}
GET /tvs/sales/_search
{
"size": 0,
"aggs": {
"group_by_price": {
"histogram": {
"field": "price",
"interval": 2000
},
"aggs": {
"sum_price": {
"sum": {
"field": "price"
}
}
}
}
}
}
GET /tvs/sales/_search
{
"size": 0,
"aggs": {
"sale": {
"date_histogram": {
"field": "sold_date",
"interval": "month",
"format": "yyyy-MM-dd",
"min_doc_count": 0,
"extended_bounds" : {
"min" : "2016-01-01",
"max" : "2017-12-31"
}
}
}
}
}
GET /tvs/sales/_search
{
"size": 0,
"aggs": {
"group_by_sold_date": {
"date_histogram": {
"field": "sold_date",
"interval": "quarter",
"format": "yyyy-MM-dd",
"min_doc_count": 0,
"extended_bounds": {
"min": "2016-01-01",
"max": "2017-12-31"
}
},
"aggs": {
"group_by_brand": {
"terms": {
"field": "brand"
},
"aggs": {
"sum_price": {
"sum": {
"field": "price"
}
}
}
},
"total_sum_price": {
"sum": {
"field": "price"
}
}
}
}
}
}
GET /tvs/sales/_search
{
"size": 0,
"query": {
"term": {
"brand":{
"value":"小米"
}
}
},
"aggs": {
"color": {
"terms": {
"field": "color"
}
}
}
}
GET /tvs/sales/_search
{
"size": 0,
"query": {
"term": {
"brand": {
"value": "長虹"
}
}
},
"aggs": {
"group_by_price": {
"avg": {
"field": "price"
}
},
"all":{
"global": {},
"aggs": {
"price": {
"avg": {
"field": "price"
}
}
}
}
}
}
GET /tvs/sales/_search
{
"size": 0,
"query": {
"constant_score": {
"filter": {
"range": {
"price": {
"gte": 1200
}
}
}
}
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
GET /tvs/sales/_search
{
"size": 0,
"query": {
"constant_score": {
"filter": {
"range": {
"price": {
"gte": 1200
}
}
}
}
},
"aggs": {
"price": {
"avg": {
"field": "price"
}
}
}
}
GET /tvs/sales/_search
{
"size": 0,
"query": {
"term": {
"brand": {
"value": "長虹"
}
}
},
"aggs": {
"recent_150d": {
"filter": {
"range": {
"sold_date": {
"gte": "now-150d"
}
}
},
"aggs": {
"recent_150d_avg_price": {
"avg": {
"field": "price"
}
}
}
},
"recent_140d": {
"filter": {
"range": {
"sold_date": {
"gte": "now-140d"
}
}
},
"aggs": {
"recent_140d_avg_price": {
"avg": {
"field": "price"
}
}
}
},
"recent_130d": {
"filter": {
"range": {
"sold_date": {
"gte": "now-130d"
}
}
},
"aggs": {
"recent_130d_avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}