Elasticsearch 7.x 常用命令
集羣信息
- 查看歡迎信息
# url
http://112.xx.xx.xx:9200/
- 查看集羣是否健康
# 查看集羣健康狀態
# url
http://112.xx.xx.xx:9200/_cluster/health
# Kibana
GET /_cluster/health
- 查看節點堆內存狀況
GET _cat/nodes?h=heap*&v
# 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