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"
  } 
}

 

创建文档

在Kibana创建索引,插入数据,查询数据

PUT b-ups-app-test

PUT b-ups-app-test/_doc/1
{
  "logTimestamp": "2022-12-12T14:32:43+08",
  "logLevel": "ERROR",
  "logContent": "com.zat.umpg.exception.BusinessException: 交易不存在交易不存在交易不存在交易不存在交易不存在"
}

get b-ups-app-test/_doc/1
也可以自动生成 _id 值:
POST /products/_doc
{
"id":"10027",
"title":"vivo X9前置双摄全网通4G美颜自拍超薄智能手机大屏vivox9",
"category":"手机会场",
"price":"2798.00",
"city":"广东东莞",
"barcode":"541396973568"
}

 

查看文档

GET /products/_doc/10037

查看指定文档title字段的分词结果:

GET /products/_doc/10037/_termvectors?fields=title

修改文档

底层索引数据无法修改,修改数据实际上是先删除再重新添加。两种修改方式:

  • PUT:对文档进行完整的替换
  • POST:可以修改一部分字段
# 修改文档 - 替换
PUT /products/_doc/10037
{
  "id":"10037",
  "title":"SONOS PLAY:1无线智能音响系统 美国原创WiFi连接 家庭桌面音箱",
  "category":"潮酷数码会场",
  "price":"9999.99",
  "city":"上海",
  "barcode":"527783392239"
}
# 修改文档 - 更新部分字段
POST /products/_update/10037
{
  "doc": {
    "price":"8888.88",
    "city":"深圳"
  }
}

 

删除文档

#删除索引指定文档
DELETE /products/_doc/10037


#清空索引所有文档
POST /products/_delete_by_query
{
  "query": {
    "match_all": {}
  }
}

 

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

posted on 2019-11-04 14:52  曹伟雄  阅读(4297)  评论(0编辑  收藏  举报

导航