目录
创建索引
查询指定索引
查询所有索引
删除索引
创建文档
查询文档
聚合搜索
创建索引
PUT test
{
"mappings": {
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "keyword"
},
"age": {
"type": "integer"
},
"phone": {
"type": "text",
"fields": {
"keyword":{
"type":"keyword"
}
}
},
"date": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
},
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
}
}
注意:phone会被同时映射成text 和keyword 类型,既能模糊查询也能聚合查询。
类型为keyword 的字段使用like, 和mysql 数据库中的like 使用一样。
类型为text 的字段使用match。
POST _sql?format=txt
{
"query": """
describe "test"
"""
}
批量插入测试数据
PUT test/_bulk
{"index":{"_index":"test","_id":1}}
{"id":1,"name":"专业测试","age":10,"phone":"专业测试","date":"2022-08-01 08:00:00"}
{"index":{"_index":"test","_id":2}}
{"id":2,"name":"专业测试","age":11,"phone":"专业测试","date":"2022-08-02 08:00:00"}
{"index":{"_index":"test","_id":3}}
{"id":3,"name":"测试区别","age":11,"phone":"测试区别","date":"2022-08-02 08:00:00"}
因为phone有keyword类型,所以like能查到
POST _sql?format=txt
{
"query": """
SELECT id, name, age, phone, DATETIME_FORMAT(date,'yyyy-MM-dd HH:mm:ss') FROM "test" where phone like '%测%'
"""
}
POST _sql?format=txt
{
"query": """
SELECT id, name, age, phone, DATETIME_FORMAT(date,'yyyy-MM-dd HH:mm:ss') FROM "test" where name like '%测%'
"""
}
因为name是keyword类型,所以不能用match查到
POST _sql?format=txt
{
"query": """
SELECT id, name, age, phone, DATETIME_FORMAT(date,'yyyy-MM-dd HH:mm:ss') FROM "test" where match(name, '测')
"""
}
POST _sql?format=txt
{
"query": """
SELECT id, name, age, phone, DATETIME_FORMAT(date,'yyyy-MM-dd HH:mm:ss') FROM "test" where match(phone, '测')
"""
}
查询指定索引
GET test
查询所有索引
GET _cat/indices
这里的查询结果表示索引的状态信息,按顺序数据表示结果如下:
内容
|
含义
|
具体描述
|
green
|
health
|
当前服务器健康状态:
green
(
集群完整
)
yellow
(
单点正常、集群不完整
)
red
(
单点不正常
)
|
open
|
status
|
索引打开、关闭状态
| test |
index
|
索引名
|
Swx2xWHLR6yv23kTrK3sAg
|
uuid
|
索引统一编号
| 1 |
pri
|
主分片数量
| 1 |
rep
|
副本数量
| 0 |
docs.count
|
可用文档数量
| 0 |
docs.deleted
|
文档删除状态(逻辑删除)
|
450b
|
store.size
|
主分片和副分片整体占空间大小
|
225b
|
pri.store.size
|
主分片占空间大小
|
删除索引
DELETE test
创建文档
此处因为没有指定数据唯一性标识,所以无法使用
PUT
请求,只能使用
POST
请求,且对
数据会生成随机的唯一性标识。否则会返回错误信息
PUT test/_doc
{
"id":1,
"name":"zhangsan",
"age":18,
"date":"2022-08-01 08:00:00"
}
不指定唯一标识用put报错
用post不报错但是生成随机的唯一性标识
?如果在创建数据时,指定唯一性标识,那么请求范式 POST,PUT 都可以
PUT test/_doc/1
{
"id":1,
"name":"zhangsan",
"age":18,
"date":"2022-08-01 08:00:00"
}
批量创建
PUT test/_bulk
{"index":{"_index":"test","_id":1}}
{"id":1,"name":"zhangsan","age":10,"date":"2022-08-01 08:00:00"}
{"index":{"_index":"test","_id":2}}
{"id":2,"name":"lisi","age":11,"date":"2022-08-02 08:00:00"}
{"index":{"_index":"test","_id":3}}
{"id":3,"name":"wangwu","age":12,"date":"2022-08-03 08:00:00"}
{"index":{"_index":"test","_id":4}}
{"id":4,"name":"zhaoliu","age":13,"date":"2022-08-04 08:00:00"}
{"index":{"_index":"test","_id":5}}
{"id":5,"name":"zhangsan","age":10,"date":"2022-08-01 08:00:00"}
查询文档
GET test/_doc/1
修改文档
本质上和新增文档是一样的,如果存在就修改,如果不存在就新增
匹配查询字段,注意match是分词查询,term是关键词整个词查询
GET test/_search
{
"query": {
"match": {
"name": "zhangsan"
}
}
}
GET test/_search
{
"query": {
"term": {
"name": "zhangsan"
}
}
}
默认情况下,
Elasticsearch
在搜索的结果中,会把文档中保存在
_source
的所有字段都返回。
如果我们只想获取其中的部分字段,我们可以添加
_source
的过滤
GET test/_search
{
"_source": ["name","age"],
"query": {
"terms": {
"name": [
"zhangsan",
"lisi"
]
}
}
}
多条件查询
GET test/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "zhangsan"
}
},{
"match": {
"age": "11"
}
}
]
}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
分页查询,注意:from = (pageNum - 1) * pageSize
GET test/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"id": {
"order": "asc"
}
}
],
"from": 4,
"size": 2
}
聚合搜索
聚合允许使用者对
es
文档进行统计分析,类似与关系型数据库中的
group by
,当然还有很
多其他的聚合,例如取最大值、平均值等等。
分组
GET test/_search
{
"size": 0,
"aggs": {
"ageGroup": {
"terms": {
"field": "age"
}
}
}
}
平均值
GET test/_search
{
"aggs": {
"avgAge": {
"avg": {
"field": "age"
}
}
}
}
加上"size": 0, 只会返回结果而不会返回数据列表
GET test/_search
{
"size": 0,
"aggs": {
"avgAge": {
"avg": {
"field": "age"
}
}
}
}
求和
GET test/_search
{
"size": 0,
"query": {
"constant_score": {
"filter": {
"match":{
"name":"zhangsan"
}
},
"boost": 1.2
}
},
"aggs": {
"sumAage": {
"sum": {
"field": "age"
}
}
}
}
分组求和
GET test/_search
{
"size": 0,
"aggs": {
"ageGroup": {
"terms": {
"field": "age"
},
"aggs": {
"sumAge": {
"sum": {
"field": "age"
}
}
}
}
}
}
按日期分组查询最近100天发布的文档
GET /test/_search
{
"size" : 0,
"query": {
"range": {
"date": {
"gte": "now-100d"
}
}
},
"aggs": {
"dateGroup": {
"date_histogram": {
"field": "date",
"calendar_interval": "day",
"format": "yyyy-MM-dd HH:mm:ss",
"min_doc_count" : 1,
"extended_bounds" : {
"min" : "2021-01-01 00:00:00",
"max" : "2022-12-31 00:00:00"
}
}
}
}
}
参考文档:
ElasticSearch 底层原理与分组查询(下)-阿里云开发者社区
Elasticsearch——Date Math在索引中的用法详解-阿里云开发者社区
最大值
GET test/_search
{
"size": 0,
"aggs": {
"maxAge": {
"max": {
"field": "age"
}
}
}
}
TopN
GET test/_search
{
"size": 0,
"aggs": {
"top3": {
"top_hits": {
"sort": [
{
"age": {
"order": "desc"
}
}
],
"size": 3
}
}
}
}
在
ES
中使用
SQL
查询的语法与在数据库中使用基本一致,format可以是txt,json,csv
POST _sql?format=txt
{
"query": """
SELECT * FROM "test"
"""
}
POST _sql?format=txt
{
"query": """
SELECT id, name, age, DATETIME_FORMAT(date,'yyyy-MM-dd HH:mm:ss') FROM "test" order by id asc
"""
}
条件查询
POST _sql?format=txt
{
"query": """
SELECT id, name, age, DATETIME_FORMAT(date,'yyyy-MM-dd HH:mm:ss') FROM "test" where age > 10 order by id asc
"""
}
查询所有索引
POST _sql?format=txt
{
"query": """
show tables
"""
}
模糊查询索引
POST _sql?format=txt
{
"query": """
show tables like 'ai%'
"""
}
查询索引信息
POST _sql?format=txt
{
"query": """
describe "test"
"""
}
分组查询
POST _sql?format=txt
{
"query": """
SELECT count(id), DATETIME_FORMAT(date,'yyyy-MM-dd') as date FROM "test" group by DATETIME_FORMAT(date,'yyyy-MM-dd') having count(id) > 0
"""
}
|