文章目录

  • kibana安装与配置
  • 常用接口
  • _cat
  • /_cat查询所有指令
  • /_cat/indices:查询索引
  • /_cat/nodes:查询节点
  • /_cat/health:查看集群
  • /_cat/plugins:查询插件,用得不多
  • 索引(index)以及映射(mapping)
  • 创建索引
  • 查看索引
  • 删除索引
  • 增删改查
  • 数据保存 - 新增
  • 简单查询
  • 数据保存 - 指定ID - 新增或者编辑
  • 数据查询 - 指定ID
  • 数据删除
  • 批量操作
  • 查询条件
  • match_all
  • match 模糊查询
  • match_phrase精确查询
  • match_phrase_prefix 以?开始
  • term 字符串或数字
  • terms 字符串或者数字
  • query_string
  • range 时间或数字范围
  • geo_polygon 自定义范围
  • geo_bounding_box 框选
  • geo_distance 聚合查询
  • bool 多条件
  • 聚合条件
  • sum 求和
  • avg 求平均
  • max 最大值
  • min 最小值
  • terms 分组
  • value_count 总条数
  • stats 求和 + 平均 + 最大 + 最小 + count
  • range 自定义时间或者数字聚合
  • date_histogram 时间间隔
  • geohash 网格、热力


这里说的常用指令、接口其实都是一个意思,就是通过ES提供的客户端,去操作(数据增删改查、集群查看等)ES集群。

首先简单说一下,操作ES的几种方式,就是ES提供的几种客户端

  1. Java客户端,RestClient 和 RestHighLevelClient
    从名字基本上就可以看出来,一个是普通的rest客户端,一个是高级的rest客户端
    使用起来专业性太强,非技术人员可以直接忽略。
  2. python elasticsearch
    这是elasticsearch提供的Python语言的客户端
    专业性不算太强,用起来相对java客户端简单, 不过需要懂ES的DSL,因为是脚本,在不联网环境下操作es集群回相对简单一点
  3. kibana - dev-tools

    ①是执行指令,②可以复制为curl指令,然后复制后Linux shell执行
    这种方式应该是最简单的,这里写指令有提示,简化难度
  4. Linux curl命令

    不做更多解释
  5. http请求
    使用postman工具或者其他工具发送http请求

    GET类型的请求也可以直接放浏览器

    其实,可以这么理解,所有的方式方式,最后都是发送的HTTP请求
    后面的所有的内容,都是基于kibana dev-tools写的

kibana安装与配置

很简单,不多说了,启动之后浏览器直接访问

http://127.0.0.1:5601/app/dev_tools#/console

ES type有哪些 es接口_agg


在左侧编译相关指令、DSL等, 执行,右侧执行结果展示

常用接口

_cat

/_cat查询所有指令

ES type有哪些 es接口_elasticsearch_02


常用的有下面几个

/_cat/indices:查询索引

ES type有哪些 es接口_query_03

  • ?v表示输出标题行,heath status index…
  • index:表示索引名称
  • docs.count:表示索引条数
  • store.size:表示磁盘大小

/_cat/nodes:查询节点

ES type有哪些 es接口_elasticsearch_04

  • ip:由于是一台机器上搭建的集群,所有这里的IP都是一样的
  • master → *:表示当前节点为主节点

/_cat/health:查看集群

ES type有哪些 es接口_query_05

/_cat/plugins:查询插件,用得不多

ES type有哪些 es接口_ES type有哪些_06

索引(index)以及映射(mapping)

基本数据如下:user_info

id

name

sex

age

birthday

location

1

张三

0

18

2021-11-11 11:11:11

[132.8763458712, 32.08437892108]

2

李四

0

18

2021-11-11 12:11:11

[132.5748398377, 32.59873342747]

3

王二

1

20

2021-11-11 11:10:00

[132.5874367473, 32.49857343987]

4

麻子

0

24

2021-11-12 11:11:11

[132.4987303938, 32.47839808876]

创建索引

PUT /user_info
{
  "mappings": {
    "properties": {
      "birthday": {
        "type": "date"
      },
      "location": {
        "type": "geo_point"
      }
    }
  },
  "settings": {
    "number_of_replicas": 1,
    "number_of_shards": 1
  }
}

ES type有哪些 es接口_DSL_07

  1. PUT请求
  2. user_info:索引名称
  3. birthday、loccation…:字段名以及字段类型,这里值写了两个,ES添加数据的时候es会字段推断字段类型
  4. number_of_replicas:备份,可以不用设置
  5. number_of_shards:分片,可以不用设置

查看索引

# 查看索引
GET /user_info
# 查询映射
GET /user_info/_mapping
# 查看设置
GET /user_info/_settings

ES type有哪些 es接口_agg_08

  1. 索引名称
  2. 映射关系
  3. 设置

删除索引

DELETE /user_info

ES type有哪些 es接口_agg_09

  1. DELETE请求
  2. user_info:索引名称

增删改查

数据保存 - 新增

POST /user_info/_doc
{
  "id": 1,
  "name": "张三",
  "sex": 0,
  "age": 18,
  "birthday": 1636600271000,
  "location": {
    "lon": 132.8763458712,
    "lat": 32.08437892108
  }
}

ES type有哪些 es接口_agg_10

  1. POST请求
  2. 索引名称
  3. type:es7弃用这个概念,_doc写死就行
  4. 这是的时间放的不是已经格式化的字符串
  5. 空间点的其中一种形式

简单查询

GET /user_info/_search

ES type有哪些 es接口_elasticsearch_11

  1. GET请求
  2. user_info:字段名称
  3. _search:关键字,表示查询
  4. 总条数:es7数据条数大于10000的时候获取总条数使用聚合去统计
  5. es数据,包括索引、版本、id、真实数据(自己保存的数据)
  6. 表示这条数据的ID,这个ID是es自动生成的
  7. 真实数据

浏览器访问,发现返回结果是没有格式化的

ES type有哪些 es接口_DSL_12


加上pretty参数格式化返回结果

ES type有哪些 es接口_elasticsearch_13

数据保存 - 指定ID - 新增或者编辑

POST /user_info/_doc/1
{
  "id": 1,
  "name": "张三",
  "sex": 0,
  "age": 18,
  "birthday": 1636600271000,
  "location": {
    "lon": 132.8763458712,
    "lat": 32.08437892108
  }
}

ES type有哪些 es接口_agg_14


跟保存的时候一样,不过在url地址里面指定ID

数据查询 - 指定ID

GET /user_info/_doc/1

ES type有哪些 es接口_elasticsearch_15

数据删除

DELETE /user_info/_doc/1

ES type有哪些 es接口_elasticsearch_16


改成DELETE请求就可以了

批量操作

POST /_bulk
{"index": {"_index": "user_info", "_id": "1"}}
{"id":1,"name":"张三","sex":0,"age":18,"birthday":1636600271000,"location":{"lon":132.8763458712,"lat":32.08437892108}}
{"index": {"_index": "user_info", "_id": "2"}}
{"id":2,"name":"李四","sex":0,"age":18,"birthday":1636603871000,"location":{"lon":132.5748398377,"lat":32.59873342747}}
{"index": {"_index": "user_info", "_id": "3"}}
{"id":3,"name":"王二","sex":1,"age":20,"birthday":1636600200000,"location":{"lon":132.5874367473,"lat":32.49857343987}}
{"index": {"_index": "user_info", "_id": "4"}}
{"id":4,"name":"麻子","sex":0,"age":24,"birthday":1636686671000,"location":{"lon":132.4987303938,"lat":32.47839808876}}

ES type有哪些 es接口_elasticsearch_17

行为

解释

create

当文档不存在时创建之

index

创建新文档或替换已有文档

update

局部更新文档

delete

删除一个文档

这里都是的行为都是index

查询条件

match_all

ES type有哪些 es接口_DSL_18


相当于where 1 = 1

match 模糊查询

GET /user_info/_search
{
  "query": {
    "match": {
      "name": "张"
    }
  }
}

ES type有哪些 es接口_elasticsearch_19

match_phrase精确查询

GET /user_info/_search
{
  "query": {
    "match_phrase": {
      "name.keyword": "张三"
    }
  }
}

ES type有哪些 es接口_ES type有哪些_20


由于name的字段类型为text,索引需要.keyword转成keyword类型,match_phrase只能查询keyword

match_phrase_prefix 以?开始

GET /user_info/_search
{
  "query": {
    "match_phrase_prefix": {
      "name": "张"
    }
  }
}

ES type有哪些 es接口_agg_21


类似,startWith(),字段类型不能是keyword

term 字符串或数字

GET /user_info/_search
{
  "query": {
    "term": {
      "name": {
        "value": "张"
      }
    }
  }
}

ES type有哪些 es接口_agg_22

GET /user_info/_search
{
  "query": {
    "term": {
      "sex": { 
        "value": 0
      }
    }
  }
}

ES type有哪些 es接口_DSL_23

terms 字符串或者数字

GET /user_info/_search
{
  "query": {
    "terms": {
      "name": [
        "张三",
        "李"
      ]
    }
  }
}

ES type有哪些 es接口_ES type有哪些_24

GET /user_info/_search
{
  "query": {
    "terms": {
      "sex": [
        0,
        1
      ]
    }
  }
}

ES type有哪些 es接口_query_25

query_string

GET /user_info/_search
{
  "query": {
    "query_string": {
      "default_field": "name",
      "query": "张三 OR 李四"
    }
  }
}

ES type有哪些 es接口_agg_26


用OR或者AND拼接条件

range 时间或数字范围

GET /user_info/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 18,
        "lte": 20
      }
    }
  }
}

ES type有哪些 es接口_elasticsearch_27


gte、from:大于等于

gt:大于

lte、to:小于等于

lt:小于

可以单独出现

GET /user_info/_search
{
  "query": {
    "range": {
      "birthday": {
        "gte": 1636686000000,
        "lte": 1636689600000
      }
    }
  }
}

ES type有哪些 es接口_ES type有哪些_28


这里的时间是毫秒或者已经格式化的字符串

GET /user_info/_search
{
  "query": {
    "range": {
      "birthday": {
        "gte": "2021-11-12 11:00:00",
        "lte": "2021-11-12 12:00:00",
        "format": "yyyy-MM-dd HH:mm:ss",
        "time_zone": "+08:00"
      }
    }
  }
}

ES type有哪些 es接口_DSL_29


需要设置时间格式,需要设置偏移量

geo_polygon 自定义范围

ES type有哪些 es接口_elasticsearch_30

GET /user_info/_search
{
  "query": {
    "geo_polygon" : {
      "location" : {
        "points" : [
          [
            93.91379,
            40.91869
          ],
          [
            86.57241,
            31.16073
          ],
          [
            106.11494,
            23.54678
          ],
          [
            120.33697,
            42.43072
          ],
          [
            93.91379,
            40.91869
          ]
        ]
      },
      "ignore_unmapped" : false
    }
  }
}

ES type有哪些 es接口_DSL_31


经度在前,维度在后,我这里是因为这个面里面没有数据

geo_bounding_box 框选

ES type有哪些 es接口_agg_32

GET /user_info/_search
{
  "query": {
    "geo_bounding_box": {
      "location": {
        "top_left": {
          "lat": 44.136538,
          "lon": 81.287368
        },
        "bottom_right": {
          "lat": 15.8475738,
          "lon": 127.873428
        }
      }
    }
  }
}

ES type有哪些 es接口_elasticsearch_33


左上角和右下角两个点确定一个矩形

geo_distance 聚合查询

ES type有哪些 es接口_query_34

GET /user_info/_search
{
  "query": {
    "geo_distance": {
      "distance": "2780km",
      "location": [
        101.92,
        34.95
      ]
    }
  }
}

ES type有哪些 es接口_elasticsearch_35

bool 多条件

GET /user_info/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "张"
          }
        },
        {
          "term": {
            "sex": {
              "value": 0
            }
          }
        }
      ],
      "should": [
        {
          "match_all": {}
        }
      ]
    }
  }
}

ES type有哪些 es接口_agg_36


多条件查询

must:相当于and

should:相当于or

must_not:and取反

聚合条件

sum 求和

GET /user_info/_search
{
  "size": 0,
  "aggs": {
    "_agg": {
      "sum": {
        "field": "age"
      }
    }
  }
}

ES type有哪些 es接口_DSL_37


_agg:自定义名称,java客户端需要用这个名称取获取聚合结果

avg 求平均

GET /user_info/_search
{
  "size": 0,
  "aggs": {
    "_agg": {
      "avg": {
        "field": "age"
      }
    }
  }
}

ES type有哪些 es接口_query_38

max 最大值

GET /user_info/_search
{
  "size": 0,
  "aggs": {
    "_agg": {
      "max": {
        "field": "age"
      }
    }
  }
}

ES type有哪些 es接口_DSL_39

min 最小值

GET /user_info/_search
{
  "size": 0,
  "aggs": {
    "_agg": {
      "min": {
        "field": "age"
      }
    }
  }
}

ES type有哪些 es接口_agg_40

terms 分组

GET /user_info/_search
{
  "size": 0,
  "aggs": {
    "_agg": {
      "terms": {
        "field": "sex",
        "size": 10
      }
    }
  }
}

ES type有哪些 es接口_elasticsearch_41


这里需要设置返回条数,防止聚合结果太大

value_count 总条数

GET /user_info/_search
{
  "size": 0,
  "aggs": {
    "_agg": {
      "value_count": {
        "field": "age"
      }
    }
  }
}

ES type有哪些 es接口_DSL_42

stats 求和 + 平均 + 最大 + 最小 + count

GET /user_info/_search
{
  "size": 0,
  "aggs": {
    "_agg": {
      "stats": {
        "field": "age"
      }
    }
  }
}

ES type有哪些 es接口_elasticsearch_43

range 自定义时间或者数字聚合

GET /user_info/_search
{
  "size": 0,
  "aggs": {
    "_agg": {
      "range": {
        "field": "age",
        "ranges": [
          {
            "to": 20
          },
          {
            "from": 20,
            "to": 25
          },
          {
            "from": 25
          }
        ]
      }
    }
  }
}

ES type有哪些 es接口_elasticsearch_44


自定义时间范围如下

GET /user_info/_search
{
  "size": 0,
  "aggs": {
    "_agg": {
      "range": {
        "field": "birthday",
        "format": "yyyy-MM-dd HH:mm:ss",
        "ranges": [
          {
            "to": "2021-11-11 11:00:00"
          },
          {
            "from": "2021-11-11 11:00:00",
            "to": "2021-11-11 12:00:00"
          },
          {
            "from": "2021-11-11 12:00:00"
          }
        ]
      }
    }
  }
}

ES type有哪些 es接口_query_45

date_histogram 时间间隔

GET /user_info/_search
{
  "size": 0,
  "aggs": {
    "_agg": {
      "date_histogram": {
        "field": "birthday",
        "calendar_interval": "hour",
        "format": "yyyy-MM-dd HH:mm:ss",
        "time_zone": "+08:00",
        "extended_bounds": {
          "min": 1636599600000,
          "max": 1636606800000
        }
      }
    }
  }
}

ES type有哪些 es接口_query_46

  1. 单位:year、month、day、week、hour…
  2. format:时间格式
  3. time_zone:+8个小时
  4. extended_bounds:这个边界要与query - range一起使用才能生效

geohash 网格、热力

GET /user_info/_search
{
  "size": 0,
  "aggs": {
    "_agg": {
      "geohash_grid": {
        "field": "location",
        "precision": 3
      }
    }
  }
}

ES type有哪些 es接口_query_47


precision精度,取值1-12