这里说的常用指令、接口其实都是一个意思,就是通过ES提供的客户端,去操作(数据增删改查、集群查看等)ES集群。
首先简单说一下,操作ES的几种方式,就是ES提供的几种客户端
很简单,不多说了,启动之后浏览器直接访问
http://127.0.0.1:5601/app/dev_tools#/console
在左侧编译相关指令、DSL等, 执行,右侧执行结果展示
基本数据如下: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
}
}
# 查看索引
GET /user_info
# 查询映射
GET /user_info/_mapping
# 查看设置
GET /user_info/_settings
DELETE /user_info
POST /user_info/_doc
{
"id": 1,
"name": "张三",
"sex": 0,
"age": 18,
"birthday": 1636600271000,
"location": {
"lon": 132.8763458712,
"lat": 32.08437892108
}
}
_doc
写死就行GET /user_info/_search
浏览器访问,发现返回结果是没有格式化的
加上pretty
参数格式化返回结果
POST /user_info/_doc/1
{
"id": 1,
"name": "张三",
"sex": 0,
"age": 18,
"birthday": 1636600271000,
"location": {
"lon": 132.8763458712,
"lat": 32.08437892108
}
}
GET /user_info/_doc/1
DELETE /user_info/_doc/1
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}}
行为 | 解释 |
---|---|
create | 当文档不存在时创建之 |
index | 创建新文档或替换已有文档 |
update | 局部更新文档 |
delete | 删除一个文档 |
这里都是的行为都是index
GET /user_info/_search
{
"query": {
"match": {
"name": "张"
}
}
}
GET /user_info/_search
{
"query": {
"match_phrase": {
"name.keyword": "张三"
}
}
}
由于name
的字段类型为text,索引需要.keyword
转成keyword类型,match_phrase
只能查询keyword
GET /user_info/_search
{
"query": {
"match_phrase_prefix": {
"name": "张"
}
}
}
GET /user_info/_search
{
"query": {
"term": {
"name": {
"value": "张"
}
}
}
}
GET /user_info/_search
{
"query": {
"term": {
"sex": {
"value": 0
}
}
}
}
GET /user_info/_search
{
"query": {
"terms": {
"name": [
"张三",
"李"
]
}
}
}
GET /user_info/_search
{
"query": {
"terms": {
"sex": [
0,
1
]
}
}
}
GET /user_info/_search
{
"query": {
"query_string": {
"default_field": "name",
"query": "张三 OR 李四"
}
}
}
GET /user_info/_search
{
"query": {
"range": {
"age": {
"gte": 18,
"lte": 20
}
}
}
}
gte、from:大于等于
gt:大于
lte、to:小于等于
lt:小于
可以单独出现
GET /user_info/_search
{
"query": {
"range": {
"birthday": {
"gte": 1636686000000,
"lte": 1636689600000
}
}
}
}
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"
}
}
}
}
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
}
}
}
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
}
}
}
}
}
GET /user_info/_search
{
"query": {
"geo_distance": {
"distance": "2780km",
"location": [
101.92,
34.95
]
}
}
}
GET /user_info/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "张"
}
},
{
"term": {
"sex": {
"value": 0
}
}
}
],
"should": [
{
"match_all": {}
}
]
}
}
}
多条件查询
must:相当于and
should:相当于or
must_not:and取反
GET /user_info/_search
{
"size": 0,
"aggs": {
"_agg": {
"sum": {
"field": "age"
}
}
}
}
_agg:自定义名称,java客户端需要用这个名称取获取聚合结果
GET /user_info/_search
{
"size": 0,
"aggs": {
"_agg": {
"avg": {
"field": "age"
}
}
}
}
GET /user_info/_search
{
"size": 0,
"aggs": {
"_agg": {
"max": {
"field": "age"
}
}
}
}
GET /user_info/_search
{
"size": 0,
"aggs": {
"_agg": {
"min": {
"field": "age"
}
}
}
}
GET /user_info/_search
{
"size": 0,
"aggs": {
"_agg": {
"terms": {
"field": "sex",
"size": 10
}
}
}
}
GET /user_info/_search
{
"size": 0,
"aggs": {
"_agg": {
"value_count": {
"field": "age"
}
}
}
}
GET /user_info/_search
{
"size": 0,
"aggs": {
"_agg": {
"stats": {
"field": "age"
}
}
}
}
GET /user_info/_search
{
"size": 0,
"aggs": {
"_agg": {
"range": {
"field": "age",
"ranges": [
{
"to": 20
},
{
"from": 20,
"to": 25
},
{
"from": 25
}
]
}
}
}
}
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"
}
]
}
}
}
}
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
}
}
}
}
}
GET /user_info/_search
{
"size": 0,
"aggs": {
"_agg": {
"geohash_grid": {
"field": "location",
"precision": 3
}
}
}
}