ElasticSearch入门

基础概念

  • 集群和节点
    一个集群包含多个节点,节点通过集群名称加入节点。就和我们安装es集群的时候,每一个节点都需要配置cluster.name 属性。
  • 索引
    含有相同属性文档的集合,索引格式:小写不能有中画线

  • 类型
    索引可以定义一个或多个类型,文档必须属于一个类型

  • 文档
    文档是可以被索引的基本单位

上面的概念可以类比于,索引就是database数据库,类型就是table表,文档就是record一行记录

  • 分片
    每个索引都有多个分片,每个分片是Lucene索引。分片的好处,当一个索引的数据量很大时,通过分片,可以缓解磁盘的压力
  • 备份
    拷贝一份分片就完成了分片的备份。备份的好处,提高可用性

es默认创建5个分片一份备份,分片的数量只能在创建索引的时候修改,备份的数量可以后期修改。

ES RESTFul API详解

  • API基本格式
    http://<ip>:<port>/<索引>/<类型>/<文档id> 这些都是名词,通过HTTP请求方式来表示动作 GETPUTPOSTDELETE

索引创建

非结构化索引创建

通过es-head插件创建索引,如下:
这里写图片描述
这里写图片描述
输入索引名称
这里写图片描述
点击 ok 按钮,完成索引的创建,弹出成功提示:
这里写图片描述
进入概述页面,查看到索引:
这里写图片描述
可以看到es创建了5个分片,从0到4,重复出现了两次。和我们创建时指定的一样。其中数字框是粗线框的表示是主分片,细框表示分片的备份
辨别一个索引是结构化的还是非结构化的,通过如下操作查看:
这里写图片描述
点击索引信息按钮
这里写图片描述
查看 mapping 属性,如果为空则是非结构化索引

结构化索引创建

进入复合查询tab页,输入如图信息:
这里写图片描述

{
  "novel": {
    "properties": {
      "title": {
        "type": "text"
      }
    }
  }
}

在查看索引的信息:
这里写图片描述
现在的mapping有值了。这里使用es-head插件的请求工具并不是很好用。这里推荐使用postman工具。这个工具请自行下载。具体使用如下

postman工具的使用

下面以新建一个新建的索引为示例:
请求方式 PUT 请求url:http://192.168.137.8:9200/people
这里写图片描述
请求数据解释:

{
    "settings": {// 索引设置
        "number_of_shards":3, //设置分片数
        "number_of_replicas":1 //设置备份数
    },
    "mappings":{ //映射关系
        "man":{ //映射具体实例
            "properties":{ //定义属性
                "name":{ //名称属性
                    "type":"text" //类型是text },
                "country":{// 国家属性
                    "type":"keyword" //类型是keyword 表示不可分割 },
                "age":{ //年龄属性
                    "type":"integer" //类型是 integer },
                "date":{//生日属性
                    "type":"date", //类型是date
                    "format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" //定义日期的格式,年-月-日 时:分:秒 或 年-月-日 或 时间戳 }
            }
        },
        "woman":{

        }
    }
}

响应结果如下,表示创建成功

{
    "acknowledged": true,
    "shards_acknowledged": true
}

通过es-head插件查看概述也能看出:
这里写图片描述

es插入

指定文档id插入

这里写图片描述
请求方式是:PUT
http://192.168.137.8:9200/people/man/1 表示索引是people,类型是man,文档id是1
请求参数,和我们定义类型man的mappings一样:

{
    "name":"瓦力", 
    "county":"China",
    "age":30,
    "date":"1987-03-07"
}

响应结果是:

{
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "created": true
}

_index 表示索引
_type 表示类型
_id 表示文档id
和我们请求的数据一样。
通过es-head插件查看
这里写图片描述
docs变成了1,原来是0
通过数据浏览tab也能看到
这里写图片描述

自动产生文档id插入

这里写图片描述
这里使用POST方式请求,请求url http://192.168.137.8:9200/people/man/ 也没有了id
响应结果:

{
    "_index": "people",
    "_type": "man",
    "_id": "AWQIAYlX8cmy4qoECslO",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "created": true
}

和指定id插入类似,只是这里的_id 值是es自动生成的。通过数据浏览tab也能看到
这里写图片描述

修改

直接修改

这里写图片描述
请求方式post
http://192.168.137.8:9200/people/man/1/_update 索引/类型/文档id/操作类型
请求参数:

{
    "doc":{ //修改的文档
        "name":"谁是瓦力"
    }
}

响应结果:

{
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    }
}

脚本修改

这里写图片描述
请求接口还是一样 ,参数如下:

{
    "script":{ 
        "lang":"painless", //指定脚本语言的种类
        "inline":"ctx._source.age+=10" //修改的字段,ctx表示上下文
    }
}

响应结果如下,和上面请求一样

{
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 3,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    }
}

脚本修改还有另外一种请求方式:

{
    "script":{
        "lang":"painless",
        "inline":"ctx._source.age=params.age",
        "params":{
            "age":100
        }
    }
}

通过params 指定修改参数,请求url不变

删除

删除文档

这里写图片描述
请求方式 DELETE 请求url: http://192.168.137.8:9200/people/man/1 索引、类型、文档id
没有请求参数
响应参数如下:

{
    "found": true, //是否找到
    "_index": "people", //索引
    "_type": "man", // 类型
    "_id": "1", // 文档id
    "_version": 5,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    }
}

删除索引

通过es-head插件删除索引:
进入es-head界面,点击概述tab,点击动作、点击删除。如下图
这里写图片描述
弹出二次确认提示框,输入确认删除,点击确认按钮
这里写图片描述
提示删除成功:
这里写图片描述

通过postman工具删除
这里写图片描述
请求方式DELETE
请求url:http://192.168.137.8:9200/people 索引
请求参数无
响应结果:

{
    "acknowledged": true
}

这里写图片描述
通过es-head查看索引都被删除

查询

通过如下语句创建book索引

请求方式PUT,请求url http://192.168.137.8:9200/book

{
    "settings":{
        "number_of_shards":3,
        "number_of_replicas":1
    },
    "mappings":{
        "novel":{
            "properties":{
                "wordcount":{
                    "type":"integer" },
                "author":{
                    "type":"keyword" },
                "title":{
                    "type":"text" },
                "publish_date":{
                    "format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis",
                    "type":"date" }
            }
        }
    }
}

添加如下数据:
这里写图片描述

简单查询

请求方式GET
请求url: http://192.168.137.8:9200/book/novel/1 索引/类型/文档id
响应结果:

{
    "_index": "book", //索引
    "_type": "novel", //类型
    "_id": "1", //id
    "_version": 1, //版本
    "found": true,
    "_source": { //数据
        "author": "张三",
        "title": "移魂大法",
        "wordcount": 1000,
        "publish_date": "2000-10-01"
    }
}

条件查询

请求方式 post 请求url http://192.168.137.8:9200/book/_search

  • 查询所有数据
    这里写图片描述
    注意这里条件查询都是以query 关键字开始的
    请求参数
{
    "query":{ //查询
        "match_all":{} //匹配条件
    }
}

响应数据:

{
    "took": 6, //花费时间 6毫秒
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "hits": {
        "total": 11, //总记录数
        "max_score": 1,
        "hits": [ //记录
            {
                "_index": "book",
                "_type": "novel",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "author": "李三",
                    "title": "Java入门",
                    "wordcount": 2000,
                    "publish_date": "2010-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "4",
                "_score": 1,
                "_source": {
                    "author": "李四",
                    "title": "ElasticSearch大法好",
                    "wordcount": 1000,
                    "publish_date": "2010-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "5",
                "_score": 1,
                "_source": {
                    "author": "王五",
                    "title": "菜谱",
                    "wordcount": 5000,
                    "publish_date": "2002-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "6",
                "_score": 1,
                "_source": {
                    "author": "赵六",
                    "title": "剑谱",
                    "wordcount": 10000,
                    "publish_date": "1997-01-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "7",
                "_score": 1,
                "_source": {
                    "author": "张三丰",
                    "title": "太极拳",
                    "wordcount": 1000,
                    "publish_date": "1997-01-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "11",
                "_score": 1,
                "_source": {
                    "author": "孙悟空",
                    "title": "七十二变",
                    "wordcount": 1000,
                    "publish_date": "2000-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "8",
                "_score": 1,
                "_source": {
                    "author": "瓦力",
                    "title": "ElasticSearch入门",
                    "wordcount": 3000,
                    "publish_date": "2017-08-20"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "author": "张三",
                    "title": "移魂大法",
                    "wordcount": 1000,
                    "publish_date": "2000-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "3",
                "_score": 1,
                "_source": {
                    "author": "张三",
                    "title": "Python入门",
                    "wordcount": 2000,
                    "publish_date": "2005-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "9",
                "_score": 1,
                "_source": {
                    "author": "很胖的瓦力",
                    "title": "ElasticSearch精通",
                    "wordcount": 3000,
                    "publish_date": "2017-08-15"
                }
            }
        ]
    }
}

查询指定数量的记录数
请求方式和请求地址不变,请求参数如下:

{
    "query":{
        "match_all":{}
    },
    "from":1, //从第几个开始
    "size":1 //显示记录数
}
  • 关键字查询
    请求方式和请求地址不变,请求参数如下:
{
    "query":{
        "match":{"title":"ElasticSearch"} //这个改为match匹配title包含ElasticSearch的记录
    }
}

响应结果如下:

{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 1.3555917,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "4",
                "_score": 1.3555917, //得分表示匹配的匹配度,默认是按照这个字段倒序排列
                "_source": {
                    "author": "李四",
                    "title": "ElasticSearch大法好",
                    "wordcount": 1000,
                    "publish_date": "2010-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "9",
                "_score": 1.1001158,
                "_source": {
                    "author": "很胖的瓦力",
                    "title": "ElasticSearch精通",
                    "wordcount": 3000,
                    "publish_date": "2017-08-15"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "8",
                "_score": 0.25316024,
                "_source": {
                    "author": "瓦力",
                    "title": "ElasticSearch入门",
                    "wordcount": 3000,
                    "publish_date": "2017-08-20"
                }
            }
        ]
    }
}
  • 排序
    基于上面的查询条件,我们按照出版时间倒序排列
    请求方式和地址不变,请求参数如下:
{
    "query": {
        "match": {
            "title": "ElasticSearch"
        }
    },
    "sort": [ //排序
        {
            "publish_date": { //排序的字段
                "order": "desc" //排序方式
            }
        }
    ]
}

响应结果:

{
    "took": 8,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": null,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "8",
                "_score": null, //现在_score字段为空
                "_source": {
                    "author": "瓦力",
                    "title": "ElasticSearch入门",
                    "wordcount": 3000,
                    "publish_date": "2017-08-20"//按照时间倒序排列
                },
                "sort": [
                    1503187200000
                ]
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "9",
                "_score": null,
                "_source": {
                    "author": "很胖的瓦力",
                    "title": "ElasticSearch精通",
                    "wordcount": 3000,
                    "publish_date": "2017-08-15"
                },
                "sort": [
                    1502755200000
                ]
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "4",
                "_score": null,
                "_source": {
                    "author": "李四",
                    "title": "ElasticSearch大法好",
                    "wordcount": 1000,
                    "publish_date": "2010-10-01"
                },
                "sort": [
                    1285891200000
                ]
            }
        ]
    }
}

聚合查询

这里写图片描述
请求方式和请求地址和普通查询一致,响应参数如下:

{
    "aggs" :{ //聚合查询关键字
        "group_by_word_count":{ //给聚合查询取名一个名称,可以随机
            "terms":{ //聚合关键字
                "field":"wordcount" //需要聚合的字段
            }
        }
    }
}

响应结果:

{
    "took": 21,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "hits": {
        "total": 11,
        "max_score": 1,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "author": "李三",
                    "title": "Java入门",
                    "wordcount": 2000,
                    "publish_date": "2010-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "4",
                "_score": 1,
                "_source": {
                    "author": "李四",
                    "title": "ElasticSearch大法好",
                    "wordcount": 1000,
                    "publish_date": "2010-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "5",
                "_score": 1,
                "_source": {
                    "author": "王五",
                    "title": "菜谱",
                    "wordcount": 5000,
                    "publish_date": "2002-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "6",
                "_score": 1,
                "_source": {
                    "author": "赵六",
                    "title": "剑谱",
                    "wordcount": 10000,
                    "publish_date": "1997-01-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "7",
                "_score": 1,
                "_source": {
                    "author": "张三丰",
                    "title": "太极拳",
                    "wordcount": 1000,
                    "publish_date": "1997-01-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "11",
                "_score": 1,
                "_source": {
                    "author": "孙悟空",
                    "title": "七十二变",
                    "wordcount": 1000,
                    "publish_date": "2000-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "8",
                "_score": 1,
                "_source": {
                    "author": "瓦力",
                    "title": "ElasticSearch入门",
                    "wordcount": 3000,
                    "publish_date": "2017-08-20"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "author": "张三",
                    "title": "移魂大法",
                    "wordcount": 1000,
                    "publish_date": "2000-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "3",
                "_score": 1,
                "_source": {
                    "author": "张三",
                    "title": "Python入门",
                    "wordcount": 2000,
                    "publish_date": "2005-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "9",
                "_score": 1,
                "_source": {
                    "author": "很胖的瓦力",
                    "title": "ElasticSearch精通",
                    "wordcount": 3000,
                    "publish_date": "2017-08-15"
                }
            }
        ]
    },
    "aggregations": { //这里是聚合结果
        "group_by_word_count": { //上面定义的聚合名称
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 1000, //wordcount为1000为5个
                    "doc_count": 5
                },
                {
                    "key": 2000,
                    "doc_count": 2
                },
                {
                    "key": 3000,
                    "doc_count": 2
                },
                {
                    "key": 5000,
                    "doc_count": 1
                },
                {
                    "key": 10000,
                    "doc_count": 1
                }
            ]
        }
    }
}
  • 多字段聚合
    请求方式和请求地址和上面一致,请求参数如下:
{
    "aggs" :{
        "group_by_word_count":{
            "terms":{
                "field":"wordcount"
            }
        },
        "group_by_publish_date":{ //这里再增加了一个通过出版日期分组的
            "terms":{
                "field":"publish_date"
            }
        }
    }
}

响应结果如下:

{
    "took": 14,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "hits": {
        "total": 11,
        "max_score": 1,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "author": "李三",
                    "title": "Java入门",
                    "wordcount": 2000,
                    "publish_date": "2010-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "4",
                "_score": 1,
                "_source": {
                    "author": "李四",
                    "title": "ElasticSearch大法好",
                    "wordcount": 1000,
                    "publish_date": "2010-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "5",
                "_score": 1,
                "_source": {
                    "author": "王五",
                    "title": "菜谱",
                    "wordcount": 5000,
                    "publish_date": "2002-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "6",
                "_score": 1,
                "_source": {
                    "author": "赵六",
                    "title": "剑谱",
                    "wordcount": 10000,
                    "publish_date": "1997-01-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "7",
                "_score": 1,
                "_source": {
                    "author": "张三丰",
                    "title": "太极拳",
                    "wordcount": 1000,
                    "publish_date": "1997-01-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "11",
                "_score": 1,
                "_source": {
                    "author": "孙悟空",
                    "title": "七十二变",
                    "wordcount": 1000,
                    "publish_date": "2000-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "8",
                "_score": 1,
                "_source": {
                    "author": "瓦力",
                    "title": "ElasticSearch入门",
                    "wordcount": 3000,
                    "publish_date": "2017-08-20"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "author": "张三",
                    "title": "移魂大法",
                    "wordcount": 1000,
                    "publish_date": "2000-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "3",
                "_score": 1,
                "_source": {
                    "author": "张三",
                    "title": "Python入门",
                    "wordcount": 2000,
                    "publish_date": "2005-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "9",
                "_score": 1,
                "_source": {
                    "author": "很胖的瓦力",
                    "title": "ElasticSearch精通",
                    "wordcount": 3000,
                    "publish_date": "2017-08-15"
                }
            }
        ]
    },
    "aggregations": {
        "group_by_publish_date": { //按照出版日期分组
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 970358400000,
                    "key_as_string": "2000-10-01 00:00:00",
                    "doc_count": 3
                },
                {
                    "key": 852076800000,
                    "key_as_string": "1997-01-01 00:00:00",
                    "doc_count": 2
                },
                {
                    "key": 1285891200000,
                    "key_as_string": "2010-10-01 00:00:00",
                    "doc_count": 2
                },
                {
                    "key": 1033430400000,
                    "key_as_string": "2002-10-01 00:00:00",
                    "doc_count": 1
                },
                {
                    "key": 1128124800000,
                    "key_as_string": "2005-10-01 00:00:00",
                    "doc_count": 1
                },
                {
                    "key": 1502755200000,
                    "key_as_string": "2017-08-15 00:00:00",
                    "doc_count": 1
                },
                {
                    "key": 1503187200000,
                    "key_as_string": "2017-08-20 00:00:00",
                    "doc_count": 1
                }
            ]
        },
        "group_by_word_count": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 1000,
                    "doc_count": 5
                },
                {
                    "key": 2000,
                    "doc_count": 2
                },
                {
                    "key": 3000,
                    "doc_count": 2
                },
                {
                    "key": 5000,
                    "doc_count": 1
                },
                {
                    "key": 10000,
                    "doc_count": 1
                }
            ]
        }
    }
}
  • 对字段统计
    请求方式和请求地址不变,请求你参数如下:
{
    "aggs" :{
        "grades_word_count":{ //给统计结果取名
            "stats":{ //统计关键字
                "field":"wordcount" //统计的字段
            }
        }
    }
}

响应结果:

{
    "took": 8,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "hits": {
        "total": 11,
        "max_score": 1,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "author": "李三",
                    "title": "Java入门",
                    "wordcount": 2000,
                    "publish_date": "2010-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "4",
                "_score": 1,
                "_source": {
                    "author": "李四",
                    "title": "ElasticSearch大法好",
                    "wordcount": 1000,
                    "publish_date": "2010-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "5",
                "_score": 1,
                "_source": {
                    "author": "王五",
                    "title": "菜谱",
                    "wordcount": 5000,
                    "publish_date": "2002-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "6",
                "_score": 1,
                "_source": {
                    "author": "赵六",
                    "title": "剑谱",
                    "wordcount": 10000,
                    "publish_date": "1997-01-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "7",
                "_score": 1,
                "_source": {
                    "author": "张三丰",
                    "title": "太极拳",
                    "wordcount": 1000,
                    "publish_date": "1997-01-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "11",
                "_score": 1,
                "_source": {
                    "author": "孙悟空",
                    "title": "七十二变",
                    "wordcount": 1000,
                    "publish_date": "2000-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "8",
                "_score": 1,
                "_source": {
                    "author": "瓦力",
                    "title": "ElasticSearch入门",
                    "wordcount": 3000,
                    "publish_date": "2017-08-20"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "author": "张三",
                    "title": "移魂大法",
                    "wordcount": 1000,
                    "publish_date": "2000-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "3",
                "_score": 1,
                "_source": {
                    "author": "张三",
                    "title": "Python入门",
                    "wordcount": 2000,
                    "publish_date": "2005-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "9",
                "_score": 1,
                "_source": {
                    "author": "很胖的瓦力",
                    "title": "ElasticSearch精通",
                    "wordcount": 3000,
                    "publish_date": "2017-08-15"
                }
            }
        ]
    },
    "aggregations": {
        "grades_word_count": { //统计结果
            "count": 11, //统计的数量
            "min": 1000, //最小值
            "max": 10000, //最大值
            "avg": 2727.2727272727275, //平均值
            "sum": 30000 //总数
        }
    }
}

也可以进行单个统计
请求地址和请求地址不变,请求参数如下:

{
    "aggs" :{
        "grades_word_count_sum":{
            "sum":{
                "field":"wordcount"
            }
        }
    }
}

响应结果如下:

{
    "took": 45,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "hits": {
        "total": 11,
        "max_score": 1,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "author": "李三",
                    "title": "Java入门",
                    "wordcount": 2000,
                    "publish_date": "2010-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "4",
                "_score": 1,
                "_source": {
                    "author": "李四",
                    "title": "ElasticSearch大法好",
                    "wordcount": 1000,
                    "publish_date": "2010-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "5",
                "_score": 1,
                "_source": {
                    "author": "王五",
                    "title": "菜谱",
                    "wordcount": 5000,
                    "publish_date": "2002-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "6",
                "_score": 1,
                "_source": {
                    "author": "赵六",
                    "title": "剑谱",
                    "wordcount": 10000,
                    "publish_date": "1997-01-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "7",
                "_score": 1,
                "_source": {
                    "author": "张三丰",
                    "title": "太极拳",
                    "wordcount": 1000,
                    "publish_date": "1997-01-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "11",
                "_score": 1,
                "_source": {
                    "author": "孙悟空",
                    "title": "七十二变",
                    "wordcount": 1000,
                    "publish_date": "2000-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "8",
                "_score": 1,
                "_source": {
                    "author": "瓦力",
                    "title": "ElasticSearch入门",
                    "wordcount": 3000,
                    "publish_date": "2017-08-20"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "author": "张三",
                    "title": "移魂大法",
                    "wordcount": 1000,
                    "publish_date": "2000-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "3",
                "_score": 1,
                "_source": {
                    "author": "张三",
                    "title": "Python入门",
                    "wordcount": 2000,
                    "publish_date": "2005-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "9",
                "_score": 1,
                "_source": {
                    "author": "很胖的瓦力",
                    "title": "ElasticSearch精通",
                    "wordcount": 3000,
                    "publish_date": "2017-08-15"
                }
            }
        ]
    },
    "aggregations": {
        "grades_word_count_sum": { //统计结果的别名
            "value": 30000
        }
    }
}
ElasticSearch是一个开源的分布式搜索引擎,基于Lucene库。它可以快速地存储、搜索和分析大量的数据ElasticSearch被广泛用于日志分析、全文搜索、安全分析和商业智能等领域。 以下是ElasticSearch入门指南: 1. 安装ElasticSearch:你可以从ElasticSearch官网下载并安装ElasticSearch。根据你的操作系统选择相应的版本。 2. 启动ElasticSearch:启动ElasticSearch非常简单。只需在终端中运行elasticsearch命令即可。 3. 探索ElasticSearch:通过在浏览器中输入http://localhost:9200/,可以访问ElasticSearch的REST API,并能看到ElasticSearch的基本信息。 4. 创建索引:在ElasticSearch中,数据被存储在索引中。你可以通过发送PUT请求来创建一个新的索引。例如,你可以使用以下命令来创建一个名为“my_index”的新索引: ``` PUT /my_index { "settings": { "number_of_shards": 1, "number_of_replicas": 0 } } ``` 5. 添加文档:在ElasticSearch中,文档是指一个JSON对象。你可以使用以下命令将文档添加到“my_index”索引中: ``` PUT /my_index/_doc/1 { "title": "Elasticsearch入门", "author": "John", "content": "Elasticsearch是一个开源的分布式搜索引擎" } ``` 6. 搜索文档:你可以使用以下命令来搜索“my_index”索引中的所有文档: ``` GET /my_index/_search ``` 7. 进行查询:你可以使用查询语句来搜索“my_index”索引。例如,你可以使用以下命令来搜索标题包含“Elasticsearch”的所有文档: ``` GET /my_index/_search { "query": { "match": { "title": "Elasticsearch" } } } ``` 这就是ElasticSearch入门指南。对于更深入的学习,你可以查看ElasticSearch官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值