ElasticSearch——常用命令


 

集群相關

--查詢集群健康狀態
GET _cluster/health

--查詢所有節點
GET _cat/nodes

--查詢索引及分片的分布
GET _cat/shards

--查詢指定索引分片的分布
GET _cat/shards/order_stpprdinf_2019-12?v
--查詢所有插件 GET _cat/plugins

 

索引相關查詢

--查詢所有索引及容量
GET _cat/indices

--查詢索引映射結構
GET my_index/_mapping

--查詢所有索引映射結構    
GET _all

--查詢所有的相同前綴索引
GET my-*/_search

--查詢所有索引模板   
GET _template

--查詢具體索引模板
GET _template/my_template

 

索引相關操作

1、創建索引模板

# 創建模板
PUT _template/test_hot_cold_template { "index_patterns": "test_*", "settings": { "number_of_shards" : 3, "index.number_of_replicas": 1 }, "mappings": { "order": { "dynamic": false, "properties": { "id": {"type": "long"}, "name": {"type": "keyword"} } } }, "aliases": { "test": {} } } # 根據模板創建索引並寫入數據 POST test_hot_cold-2019-12-01/order { "id":1, "name":"cwx" }

# 刪除模板
DELETE _template/order_stpprdinf_template

  

2、直接創建索引

PUT my_index
{
  "mappings": {
    "doc": {
      "properties": {
        "name": {
          "type": "text"
        },
        "blob": {
          "type": "binary"
        }
      }
    }
  }
}
-- 增加字段
put my_index/_mapping/doc { "properties": { "cwxtest": {"type": "keyword"} } }

-- 數據冷備份
PUT _snapshot/my_backup  # my_backup 備份的名稱
{
    "type": "order", 
    "settings": {
        "location": "/mount/backups/my_backup" 
    }
}

3、寫入索引

PUT my_index/doc/1
{
  "name": "Some binary blob",
  "blob": "U29tZSBiaW5hcnkgYmxvYg==" 
}

4、刪除索引

DELETE my-index

5、修改索引setting

PUT /test_hot_cold-2019-12-01/_settings 
{ 
  "settings": { 
    "index.routing.allocation.require.hotwarm_type": "cold"
  } 
}

  

 

DSL query查詢

--1.查詢所有
GET _search
{
  "query": {
    "match_all": {}
  }
}

--2.查詢單個索引 的 固定屬性
--精確匹配
GET _search
{
  "query": {
    "term": { "name" : "you" }
  }
}

--模糊匹配
GET _search
{
  "query": {
    "match": { "name" : "you" }
  }
}
--范圍查找 GET _search { "query": { "range": { "age":{ "gte" : 15 , "lte" : 25 } } } }

 GET indexName/_search
 {
   "query": {
     "wildcard":{"relateId":"*672499460503*"}
   }
 }


--3.功能性查詢
--過濾
GET my_index/_search
{
  "query": {
    "bool": {
      "filter": {
        "term":{"age":1095}
      }
    }
  }
}

--或 or
GET my - test / _search 
{   
"query": {     "bool": {       "should": [{         "term": {           "name": "you"         }         }, {         "match": {           "age": 20         }       }]     }   } } --與 AND GET my-test/_search { "query": { "bool": { "must" : [{ "match" : { "name" : "you" } },{ "range":{ "age":{ "from" : 10 , "to" : 20 } } }] } } } --必須 = GET my_index/_search { "query": { "bool": { "must" : { "range" : { "age" : { "from" : 10, "to" : 20 } } } } } } --必須不 not GET my_index/_search { "query": { "bool": { "must_not" : { "term" : { "name" : "you" } } } } } --復合查找 GET my_index/_search { "query": { "bool": { "should": [{ "match": { "age": 40 } }, { "match": { "age": 20 } }], "filter": { "match":{ "name":"you" } } } } } --4.索引遷移 --場景 從A索引 復制到B索引 POST _reindex { "source": { "index": "my_index" }, "dest": { "index": "new_my_index" } } --5.基於查詢的刪除 POST test-index/_delete_by_query { "query":{ "term": { "cameraId":"00000000002" } } } --查詢 GET test-index/_search { "query":{ "term": { "cameraId":"00000000002" } } }

按時間范圍查詢:

GET order_stpprdinf_2019-12/_search
{
  "size":10,
  "query":{
    "range":{
      "order_time":{
        "gte":"2019-12-11T00:00:00+08:00",
        "lte":"2019-12-11T23:59:59+08:00"
      } 
    }
  }
}

按條件刪除:

GET order_stpprdinf_2019-12/_delete_by_query?conflicts=proceed
{
  "query":{
    "range":{
      "order_time":{
        "gte":"2019-12-11T00:00:00+08:00",
        "lte":"2019-12-11T23:59:59+08:00"
      } 
    }
  }
}

 

 

可在kibana的Dev Tools控制板上執行以上命令:

 

 

 

參考鏈接:https://blog.csdn.net/ailice001/article/details/79541816


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM