推荐学习阮一鸣《 Elasticsearch 核心技术与实战》
集群信息
- 查看欢迎信息
# url
http://112.xx.xx.xx:9200/
- 查看集群是否健康
# 查看集群健康状态
# url
http://112.xx.xx.xx:9200/_cluster/health
# Kibana
GET /_cluster/health
# url
http://112.xx.xx.xx:9200/_cluster/settings?include_defaults
# Kibana
GET /_cluster/settings?include_defaults
- 查看节点列表
# 查看节点列表
# url
http://112.xx.xx.xx:9200/_cat/nodes?v
# Kibana
GET /_cat/nodes?v
索引
查看索引
- 查看所有索引
# 查看所有索引
GET /_cat/indices
- 查看某个索引的状态
# 查看某个索引的状态
GET /_cat/indices/my_index
- 查看某个索引的 mapping
# 查看某个索引的 mapping
GET /my_index/_mapping
- 查看某个索引的 settings
# 查看某个索引的 settings
GET /my_index/_settings
- 如果 index 的状态为 yellow,可能是因为副本分片未分配出去
# 查看 shard 未分配(unassigned)出去的原因
GET /_cat/shards?v&h=index,shard,prirep,state,unassigned.reason
创建索引
- 用明确的 mapping 创建索引
PUT /my_index
{
"mappings": {
"dynamic": false,
"properties": {
"age": { "type": "integer" },
"email": { "type": "keyword" },
"name": { "type": "text" }
}
}
}
删除索引
- 删除 my_index 索引
DELETE /my_index
重建索引
- 重建 my_index 到 my_index_new
POST /_reindex
{
"source": {
"index": "my_index", # 原有索引
"size": 5000 # 一个批次处理的数据量
},
"dest": {
"index": "my_index_new" # 新索引
}
}
- 查看重建索引的进度
GET /_tasks?detailed=true&actions=*reindex
索引模板
- 查看有哪些模板
GET _cat/templates
- 查看一个具体的模板
GET _template/my_index_tpl
- 创建模板
PUT /_template/my_index_tpl
{
"order": 0,
"index_patterns": [
"my_index_*"
],
"settings": {
"index": {
"number_of_shards": 12,
"number_of_replicas": 1
}
},
"mappings": {
"_source": {
"enabled": false
},
"dynamic": false,
"properties": {
"name_en": {
"analyzer": "english",
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"name_cn": {
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart",
"type": "text"
},
"country": {
"type": "keyword"
},
"full_field": {
"index": false,
"store": true,
"type": "text"
}
}
},
"aliases": {}
}
- 更多索引模板知识查看: Put index template API
索引别名
- 查看有哪些别名
GET _cat/aliases
- 添加别名
# 多对一
POST /_aliases
{
"actions": [
{
"add": {
"indices": [
"my_index_1",
"my_index_2"
],
"alias": "my_index_alias"
}
}
]
}
# 支持通配符(*)
POST /_aliases
{
"actions": [
{
"add": {
"indices": [
"my_index_*"
],
"alias": "my_index_alias"
}
}
]
}
- 移除别名
POST /_aliases
{
"actions": [
{
"remove": {
"index": "my_index_1",
"alias": "my_index_alias"
}
},
{
"remove": {
"index": "my_index_2",
"alias": "my_index_alias"
}
}
]
}
- 替换(移除和添加组合使用)
POST /_aliases
{
"actions": [
{
"remove": {
"indices": [
"my_index_1_20201011",
"my_index_2_20201011"
],
"alias": "my_index_alias"
}
},
{
"add": {
"indices": [
"my_index_1_20201022",
"my_index_2_20201022"
],
"alias": "my_index_alias"
}
}
]
}
- 更多索引别名知识查看: Update index alias API
settings
分片(shard)
- 初始化分片数
PUT /my_temp_index
{
"settings": {
"number_of_shards" : 1, # 主分片数,不可动态修改
"number_of_replicas" : 0 # 副本分片数,可以动态修改
}
}
- 动态修改副本分片数
PUT /my_index/_settings
{
"number_of_replicas": 0
}
- 查看分片情况
# 查看所有索引的分片情况
GET /_cat/shards?v
# 查看 my_index 的分片情况
GET /_cat/shards/my_index?v
mapping
新增索引字段
# 无需 reindex
PUT /my_index/_mapping
{
"properties": {
"employee-id": {
"type": "keyword"
}
}
}
# 当给已有无索引字段添加索引后,
# 该字段的新增数据可以被检索到,
# 该字段的历史数据不能被检索到,
#此时可以用
POST /my_index/_update_by_query
# 语句刷新索引
# 增加子字段也可以用类似方法
文档的增删改查(CRUD)
Elasticsearch | 类比MySQL | 说明 |
---|---|---|
Index | replcae into | Index在索引不存在时会创建索引, replace into 并不会创建库或表 |
Create | insert into | 增加 |
Read | select | 读取 |
Update | update | 更新 |
Delete | delete | 删除 |
Index(增加 or 更新)
- 指定 ID
POST /my_index/_doc/1
{"user":"walker"}
- 系统自动生成 ID
POST /my_index/_doc
{"user":"walker"}
Create(增加)
- 指定 ID
POST /my_index/_create/2
{"user":"walker"}
Read(读取)
- Query DSL
- 查看某个索引的文档总数
# 查看某个索引的文档总数
GET /_cat/count/my_index?v
# OR
GET /my_index/_count
- 返回索引的所有文档
# 返回索引的所有文档
GET /kibana_sample_data_ecommerce/_search
# OR
POST my_index/_search
{
"query": {
"match_all": {}
}
}
- 根据ID查看文档
# 根据ID查看文档
GET /kibana_sample_data_ecommerce/_doc/xPGYeWwBVtEez7y_Ku1U
- term 查询精确匹配
# term 查询精确匹配
GET /_search
{
"query": {
"term": {
"currency": "EUR"
}
}
}
# 通过 Constant Score 将查询转换成一个 Filtering
# 避免算分,并利用缓存,提高性能
GET /_search
{
"query": {
"constant_score": {
"filter": {
"term": {
"currency": "EUR"
}
}
}
}
}
- 通配符模糊查询
# 通配符模糊查询
GET /_search
{
"query": {
"wildcard": {
"currency": "*U*"
}
}
}
# 通过 Constant Score 将查询转换成一个 Filtering
# 避免算分,并利用缓存,提高性能
GET /_search
{
"query": {
"constant_score": {
"filter": {
"wildcard": {
"currency": "*U*"
}
}
}
}
}
- 多条件组合查询
# Boolean query
GET /my_index/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"author": "walker"
}
},
{
"wildcard": {
"title": "*科技*"
}
}
]
}
}
}
- 返回查询结果及数据总量(track_total_hits )
GET my_index/_search
{
"track_total_hits": true,
"query": {
"match_all": {}
}
}
- 仅仅返回查询的数据总量(_count)
GET /my_index/_count
{
"query": {
"wildcard": {
"title": "*科技*"
}
}
}
- 游标查询(深度分页 Scroll,命中数大于10000时,可返回命中总数)
# 游标查询
POST /my_index/_search?scroll=1m
Update(更新)
- 指定 ID 更新
POST /my_index/_update/1
{
"doc": {
"user": "walker",
"age": 99
}
}
Delete(删除)
- 指定 ID 删除
DELETE /my_index/_doc/1
批量操作
上面讲的都是对单文档进行操作,多文档批量操作可自行去翻看官网文档:Document APIs
Elasticsearch SQL
用法示例
POST _sql?format=txt
{
"query": "SELECT Carrier FROM kibana_sample_data_flights LIMIT 100"
}
将 SQL 转化为 DSL
POST _sql/translate
{
"query": "SELECT Carrier FROM kibana_sample_data_flights LIMIT 100"
}
# 转换结果如下
{
"size" : 100,
"_source" : false,
"stored_fields" : "_none_",
"docvalue_fields" : [
{
"field" : "Carrier"
}
],
"sort" : [
{
"_doc" : {
"order" : "asc"
}
}
]
}
本文出自 walker snapshot