码迷,mamicode.com
首页 > 其他好文 > 详细

记录一次ES工作过程

时间:2018-01-17 01:01:35      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:arch   ppi   字符串   segment   开启   this   data   统计   rms   

1、新建index

curl -X PUT "localhost:9200/test"

2、生成 mapping

curl -XPUT "localhost:9200/test/order/_mapping?pretty" -d ‘ 
{
    "order": {
        "properties": {
            "product_code": {
                "type": "string",
                "store": "yes",
                "fielddata": true, #es5.6之后,字符串做聚合必须开启此字段
                "fields": {
                  "raw" : { #为了下面的此字段聚合不至于将字符串拆开计数
                    "type": "string",
                    "index": "not_analyzed"
                  }
              }
            },
            "price": {
                "type": "double"
            },
            "num": {
                "type": "integer"
            },
            "pay_time": {
                "type": "date",
                "format": "yyyy-MM-dd HH:mm:ss"
            }
        }
    }
}

3、查看mapping

curl -X GET "localhost:9200/test/_mapping?pretty"

4、插入数据

curl -X POST ‘localhost:9200/test/order/‘ -d ‘
{
  "product_code": "G-4-6",
  "price": 43,
  "num":2,
  "pay_time": "2018-01-16 00:00:13"
}
curl -X POST ‘localhost:9200/test/order/‘ -d ‘
{
  "product_code": "G-4-5",
  "price": 43,
  "num":4,
  "pay_time": "2018-01-17 00:00:13"
}
curl -X POST ‘localhost:9200/test/order/‘ -d ‘
{
  "product_code": "G-4-6",
  "price": 43,
  "num":40,
  "pay_time": "2017-12-30 01:12:13"
}

5、按月统计售卖数量,并排序

curl ‘localhost:9200/test/order/_search?pretty‘ -d ‘
{
   "size" : 0,
   "aggs": {
      "num": {
         "date_histogram": {
            "field": "pay_time",
            "interval": "month", 
            "format": "yyyy-MM-dd",
            "order": {"sum_this_month": "desc"} 
         },
         "aggs": {
            "sum_this_month": {
              "sum": {
                "field": "num"
              }
            }
          }
      }
   }
}

6、按商品统计售卖数量

curl ‘localhost:9200/test/order/_search?pretty‘ -d ‘
{
    "size" : 0,
    "aggs" : {
        "num" : {
            "terms" : {
                "field" : "product_code.raw"
            },
            "aggs": {
                "sum_this_product": {
                  "sum": {
                    "field": "num"
                  }
                }
            }
        }
    }
}

 感谢大神的文章

https://segmentfault.com/a/1190000004433446 系列文章

记录一次ES工作过程

标签:arch   ppi   字符串   segment   开启   this   data   统计   rms   

原文地址:https://www.cnblogs.com/leedaily/p/8297551.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!