ElasticSearch—基础命令

原文作者:牛麦康纳

原文地址:ElasticSearch 命令-(基础篇)

curl是利用URL语法在命令行方式下工作的开源文件传输工具,使用curl可以简单实现常见的get/post请求简单的认为是可以在命令行下面访问url的一个工具。curl常用参数: 

  • -X 指定http的请求方法 有HEAD GET POST PUT DELETE 
  • -d 指定要传输的数据 
  • -H 指定http请求头信息 

ElasticSearch的命令调用是基于http的,提供了丰富的RESTFul API,通过curl 操作ElasticSearch常用命令。从功能上来分ElasticSearch的命令可以分为4类:

  1. 检查集群、节点、索引等状态信息;
  2. 管理集群、节点、索引数据及元数据;
  3. 执行CRUD操作及搜索操作;
  4. 执行高级搜索操作,例如paging、filtering、scripting、faceting、aggregations及其它操作;

一、API命令

1、集群、节点、索引等状态信息的查看命令:

1. 查看健康情况

curl -XGET 'localhost:9200/_cat/health?v'

2. 查看索引

curl -XGET 'localhost:9200/_cat/indices?v'

3. 查看结点

curl -XGET 'localhost:9200/_cat/nodes?v'

小结:此类命令都是GET请求

curl -XGET 'ip:port/_cat/XXX?v'

2、增删改查命令:

命令遵循的格式:

curl -X<REST Verb> '<Node>:<Port>/<Index>/<Type>/<ID>'

新增修改是PUT请求,删除是DELETE请求,查询是GET

1. 创建索引

pretty非必要,为了使返回信息更美丽

curl -XPUT 'localhost:9200/customer?pretty'

2. 插入文档

curl -XPUT 'localhost:9200/customer/car/1?pretty' -d 
        '{
          "name": "YeJingtao"
         }'
  • 插入文档时类型不可省略,必须指定。
  • 索引的创建可以是隐式的,如果不指定,Elasticsearch将产生一个唯一的ID来索引这个文档。从ES设计来讲,我们更关心的是对文档内容的索引,而不去关心ID是多少,所以实际应用中可以将ID交给ES维护。

3. 查看记录

curl -XGET 'localhost:9200/customer/car/1?pretty'

4. 修改记录

curl -XPUT 'localhost:9200/customer/car/1?pretty' -d 
       '{
          "name": "YeJingtao-V2"
        }'

(PUT、POST效果一样,PUT是幂等的)

5. 删除记录

curl -XDELETE 'localhost:9200/customer/car/1?pretty'

6. 删除索引

curl -XDELETE 'localhost:9200/customer'

索引删除时,索引内的类型和记录全部清除。

二、Bulk批量操作命令

除上面介绍的单个操作外,ES还供了一个叫bulk的API可以进行批量操作,进行大批量文档和索引的更新或删除操作,极大的提升了操作效率。API命令格式是:

ip:port/index/type/_bulk–data-binary @jsonfile

Jsonfile也有格式要求,必须是一行描述信息一行文档信息。描述信息:

{"index":
    {
        "_index":"stuff_orders",
        "_type":"order_list",
        "_id":903713
    }
}

其中index和type不是必须的,与上面API命令中的index和type相呼应,二者选择填其一,如果都存在以json文件中的为准。

文档信息只要是标准的json格式就好,没有内容要求,例如:

{
    "account_number":1,
    "balance":39225,
    "firstname":"Amber",
    "lastname":"Duke",
    "age":32,
    "gender":"M",
    "address":"880HolmesLane",
    "employer":"Pyrami",
    "email":"amberduke@pyrami.com",
    "city":"Brogan",
    "state":"IL"
}

文档样例:https://github.com/yejingtao/forblog/tree/master/elasticsearch

命令:

curl -XPOST 192.168.226.133:9200/my_index/customer/_bulk?pretty --data-binary @accounts.json

再回头看下索引的状态:

有了这1000条记录,我们就可以开始ES的核心“搜索之旅”了。

三、查询分类:

从命令格式上来划分有两种方式:

  • 通过RESTfulrequest API传递查询参数,也称“query-string”;
  • 通过发送REST request body,也称作JSON格式。

先从直观上感觉下两种风格的差异,以下两条命令是等价的。

风格1:

curl -XGET '192.168.226.133:9200/my_index/_search?pretty'

风格2:

curl -XGET 'localhost:9200/my_index/_search?pretty' -d
           '{
              "query": {
                    "match_all": {} 
                }
            }'

在搜索时url中可以指定索引名和类型名以减少搜索的范围

  • /_search:搜索所有索引的所有类型;
  • /my_index/_search:搜索my_index索引的所有类型;
  • /students, my_index /_search:搜索students和my_index索引的所有类型;
  • / my_*/_search:搜索名称以my_开头的所有索引的所有类型;
  • / my_index/customer/_search:搜索my_index索引的customer类型;
  • /_all/customer/_search:搜索所有索引的customer类型

PS:一旦你取回了你的搜索结果,Elasticsearch就完成了使命,它不会维护任何服务器端的资源或者在你的结果中打开游标,这也是RESTFul风格的一个特性,也注定了ES每次返回值默认是有限制的。

由于REST request body的JSON格式从可读性和灵活性来说都给开发者带来太多的美感,所以实际应用中重点使用JSON格式,这种查询语句又被称为DSL。而DSL又因原理不同被划分为查询DSL(query DSL)和过滤DSL(filter DSL)

DSL格式如果再使用linux的curl命令来书写那真是太累了,这里我们使用ELK组合中的Kibana来帮助我们查询ES。

下载地址:https://www.elastic.co/downloads/kibana

按照官网教程:

  1. 下载解压,
  2. 配置config/kibana.yml,
  3. 执行bin/kibana,
  4. 浏览器访问http://localhost:5601

 

 

二、Kibana命令行

集群相关查询

# 查询集群健康状态
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

1、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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值