当前位置: 首页 > news >正文

elasticsearch常用操作命令

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

  <REST Verb>:REST风格的语法谓词
  <Node>:节点ip
  <port>:节点端口号,默认9200
  <Index>:索引名
  <Type>:索引类型
  <ID>:操作对象的ID号

curl localhost:9200/_cat

=^.^=
/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/master
/_cat/nodes
/_cat/tasks
/_cat/indices
/_cat/indices/{index}
/_cat/segments
/_cat/segments/{index}
/_cat/count
/_cat/count/{index}
/_cat/recovery
/_cat/recovery/{index}
/_cat/health
/_cat/pending_tasks
/_cat/aliases
/_cat/aliases/{alias}
/_cat/thread_pool
/_cat/thread_pool/{thread_pools}
/_cat/plugins
/_cat/fielddata
/_cat/fielddata/{fields}
/_cat/nodeattrs
/_cat/repositories
/_cat/snapshots/{repository}
/_cat/templates

(1)elasticsearch 查看集群统计信息

 curl -XGET 'http://localhost:9200/_cluster/stats?pretty'   

(2)elasticsearch 查看所有索引

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

(3)elasticsearch 查看集群的节点列表

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

(4)elasticsearch 检测集群是否健康

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

(5)elasticsearch 创建索引

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

(6)elasticsearch 插入数据

 curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '

  {
   "name": "John Doe"
  }'

(7)elasticsearch 获取数据

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

 获取customer索引下类型为external,id为1的数据,pretty参数表示返回结果格式美观。

(8)elasticsearch 删除索引

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

(9)elasticsearch 修改数据

  curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
  {
   "name": "John Doe"
  }'
  curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
  {
   "name": "Jane Doe"
  }'
先新增id为1,name为John Doe的数据,然后将id为1的name修改为Jane Doe。

(10)elasticsearch 更新数据

  curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '

  {
   "doc": { "name": "Jane Doe" }
  }'

  curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '

  {
   "doc": { "name": "Jane Doe", "age": 20 }
  }'

  curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '

  {
   "script" : "ctx._source.age += 5"
  }'

(11)elasticsearch 删除数据

  curl -XDELETE 'localhost:9200/customer/external/2?pretty'
  将执行删除Customer中ID为2的数据

  curl -XPUT 'localhost:9200/customer'             //创建索引

  curl -XPUT 'localhost:9200/customer/external/1'-d '    //插入数据

{
"name": "John Doe"
}'

curl 'localhost:9200/customer/external/1'//查询数据

curl -XDELETE 'localhost:9200/customer'//删除索引

(12)elasticsearch 批处理

批量操作中执行创建索引:

  curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d '
  {"index":{"_id":"1"}}
  {"name": "John Doe" }
  {"index":{"_id":"2"}}
  {"name": "Jane Doe" }
  '
  下面语句批处理执行更新id为1的数据然后执行删除id为2的数据

  curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d '
  {"update":{"_id":"1"}}
  {"doc": { "name": "John Doe becomes Jane Doe" } }
  {"delete":{"_id":"2"}}

(13)elasticsearch 导入数据集

 curl -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary "@accounts.json"

   curl 'localhost:9200/_cat/indices?v' 查看

(14)elasticsearch 查询数据

 curl 'localhost:9200/bank/_search?q=*&pretty'

 返回所有bank中的索引数据。其中 q=*  表示匹配索引中所有的数据。

 等价于:

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

 匹配所有数据,但只返回1个:

  curl -XPOST 'localhost:9200/bank/_search?pretty' -d '

  {
  "query": { "match_all": {} },
   "size": 1
  }'
  注意:如果siez不指定,则默认返回10条数据。

  curl -XPOST 'localhost:9200/bank/_search?pretty' -d '

  {
  "query": { "match_all": {} },
   "from": 10,
   "size": 10
  }'

  返回从11到20的数据。(索引下标从0开始)
  curl -XPOST 'localhost:9200/bank/_search?pretty' -d '

  {
   "query": { "match_all": {} },
  "sort": { "balance": { "order": "desc" } }
  }'

  上述示例匹配所有的索引中的数据,按照balance字段降序排序,并且返回前10条(如果不指定size,默认最多返回10条)。

(15)elasticsearch 搜索

 返回两个字段(account_number balance)

  curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
  {
   "query": { "match_all": {} },
   "_source": ["account_number", "balance"]
  }'

 返回account_number 为20 的数据:

  curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
  {
   "query": { "match": { "account_number": 20 } }
  }'

  返回address中包含mill的所有数据:

  curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
  {
   "query": { "match": { "address": "mill" } }
  }'

  返回地址中包含mill或者lane的所有数据:

  curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
  {
  "query": { "match": { "address": "mill lane" } }
  }'

和上面匹配单个词语不同,下面这个例子是多匹配(match_phrase短语匹配),返回地址中包含短语 “mill lane”的所有数据:

  curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
  {
   "query": { "match_phrase": { "address": "mill lane" } }
  }'

 以下是布尔查询,布尔查询允许我们将多个简单的查询组合成一个更复杂的布尔逻辑查询。

  这个例子将两个查询组合,返回地址中含有mill和lane的所有记录数据:

  curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
  {
   "query": {
   "bool": {
   "must": [
   { "match": { "address": "mill" } },
   { "match": { "address": "lane" } }
   ]
  }
  }
  }'
  上述例子中,must表示所有查询必须都为真才被认为匹配。

  相反, 这个例子组合两个查询,返回地址中含有mill或者lane的所有记录数据:

  curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
  {
  "query": {
   "bool": {
   "should": [
   { "match": { "address": "mill" } },
   { "match": { "address": "lane" } }
   ]
   }
  }
  }'

  上述例子中,bool表示查询列表中只要有任何一个为真则认为匹配。

  下面例子组合两个查询,返回地址中既没有mill也没有lane的所有数据:

  curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
  {
   "query": {
   "bool": {
   "must_not": [
   { "match": { "address": "mill" } },
   { "match": { "address": "lane" } }
  ]
  }
  }
  }'
  上述例子中,must_not表示查询列表中没有为真的(也就是全为假)时则认为匹配。

  我们可以组合must、should、must_not来实现更加复杂的多级逻辑查询。

下面这个例子返回年龄大于40岁、不居住在ID的所有数据:

  curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
  {
   "query": {
   "bool": {
   "must": [
   { "match": { "age": "40" } }
   ],
   "must_not": [
   { "match": { "state": "ID" } }
  ]
  }
   }
  }'

(16) elasticsearch 过滤filter(查询条件设置)

  下面这个例子使用了布尔查询返回balance在20000到30000之间的所有数据。
  curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
  {
     "query": {
     "bool": {
     "must": { "match_all": {} },
     "filter": {
    "range": {
  "balance": {
   "gte": 20000,
   "lte": 30000
   }
   }
   }
   }
  }
  }'

(17) elasticsearch 聚合 Aggregations

  下面这个例子: 将所有的数据按照state分组(group),然后按照分组记录数从大到小排序,返回前十条(默认):

  curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
  {
  "size": 0,
   "aggs": {
   "group_by_state": {
   "terms": {
    "field": "state"
   }
   }
  }
  }'

下面这个实例按照state分组,降序排序,返回balance的平均值:

  curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
  {
  "size": 0,
  "aggs": {
   "group_by_state": {
   "terms": {
   "field": "state"
   },
   "aggs": {
   "average_balance": {
   "avg": {
   "field": "balance"
   }
   }
   }
  }
  }
  }'

(18)elasticsearch 取得某个索引中某个字段中的所有出现过的值

这种操作类似于使用SQL的SELECT UNIQUE语句。当需要获取某个字段上的所有可用值时,可以使用terms聚合查询完成:

GET /index_streets/_search?search_type=count
{
"aggs": {
"street_values": {
"terms": {
"field": "name.raw",
"size": 0
}
}
}
}

因为目标是得到name字段上的所有出现过的值,因此search_type被设置为了count,这样在返回的响应中不会出现冗长的hits部分。另外,查询的目标字段的索引类型需要设置为not_analyzed。所以上面的field指定的是name.raw。

得到的响应如下所示:

{
"took": 23,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 7445,
"max_score": 0,
"hits": []
},
"aggregations": {
"street_values": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "江苏路",
"doc_count": 29
},
{
"key": "南京东路",
"doc_count": 28
},
...
...
...

(19)elasticsearch 取得某个索引/类型下某个字段中出现的不同值的个数

这种操作类似于使用SQL的select count( ) from (select distinct from table)语句。当需要获取某个字段上的出现的不同值的个数时,可以使用cardinality聚合查询完成:

GET /index_streets/_search?search_type=count
{
"aggs": {
"uniq_streets": {
"cardinality": {
"field": "name.raw"
}
}
}
}

因为目标是得到name字段上的所有出现过的值,因此search_type被设置为了count,这样在返回的响应中不会出现冗长的hits部分。另外,查询的目标字段如果是字符串类型的,那么其索引类型需要设置为not_analyzed。所以上面的field指定的是name.raw。

得到的响应如下所示:

{
"took": 96,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 4136543,
"max_score": 0,
"hits": []
},
"aggregations": {
"uniq_streets": {
"value": 1951
}
}
}

(20)elasticsearch 每个命令都支持使用?v参数,来显示详细的信息:

  curl localhost:9200/_cat/master?v 

id host ip node
yBet3cYzQbC68FRzLZDmFg 127.0.0.1 127.0.0.1 lihao

(21)elasticsearch 每个命令都支持使用help参数,来输出可以显示的列:

  curl localhost:9200/_cat/master?help 

id | | node id
host | h | host name
ip | | ip address
node | n | node name

(22)elasticsearch 通过h参数,可以指定输出的字段:

$ curl localhost:9200/_cat/master?v
id host ip node
yBet3cYzQbC68FRzLZDmFg 127.0.0.1 127.0.0.1 lihao

$ curl localhost:9200/_cat/master?h=ip,node
127.0.0.1 lihao

(23) elasticsearch 很多的命令都支持返回可读性的大小数字,比如使用mb或者kb来表示。

$ curl localhost:9200/_cat/indices?v

health status index pri rep docs.count docs.deleted store.size pri.store.size
yellow open aaa 5 1 2 0 7.2kb 7.2kb
yellow open logstash-eos-2016.09.01 5 1 297 0 202.3kb 202.3kb
yellow open bank 5 1 1001 1 451.6kb 451.6kb
yellow open website 5 1 2 0 7.8kb 7.8kb
yellow open .kibana 1 1 5 1 26.6kb 26.6kb
yellow open logstash-eos-2016.09.02 5 1 11 0 33.9kb 33.9kb
yellow open test-2016.09.01 5 1 1 0 3.9kb 3.9kb
yellow open testst_index 5 1 0 0 795b 795b

转载于:https://blog.51cto.com/6226001001/2104383

相关文章:

  • 【实习记】2014-09-04浏览代码查middle资料+总结我折腾过的源码浏览器
  • 查找字符串中出现最多的字符
  • xshell 4 中文乱码问题解决
  • Html的空格显示
  • Java基础-Date类常用方法介绍
  • 910
  • 区块链研习 | DAG网络:Blockless无区块概念将解决区块链交易性能问题
  • jQuery 自制上传头像插件-附带Demo实例(ajaxfileupload.js第三弹)
  • 干货 | 手把手教你用python实现新浪微博模拟登陆,超详细
  • 百度编辑器ueditor 在vs2008中的使用方法
  • cordova 建立工程生成apk
  • 回流、重绘及其优化
  • JMeter学习参数化User Defined Variables与User Parameters
  • LAMP架构介绍、MySQL和MariaDB介绍、MySQL安装
  • PL/SQL之--变量
  • CSS相对定位
  • C语言笔记(第一章:C语言编程)
  • egg(89)--egg之redis的发布和订阅
  • es的写入过程
  • in typeof instanceof ===这些运算符有什么作用
  • Java面向对象及其三大特征
  • springboot_database项目介绍
  • Three.js 再探 - 写一个跳一跳极简版游戏
  • Tornado学习笔记(1)
  • ucore操作系统实验笔记 - 重新理解中断
  • 分享一个自己写的基于canvas的原生js图片爆炸插件
  • 服务器之间,相同帐号,实现免密钥登录
  • 前端技术周刊 2018-12-10:前端自动化测试
  • 字符串匹配基础上
  • 数据库巡检项
  • #define用法
  • #我与虚拟机的故事#连载20:周志明虚拟机第 3 版:到底值不值得买?
  • #在线报价接单​再坚持一下 明天是真的周六.出现货 实单来谈
  • (2020)Java后端开发----(面试题和笔试题)
  • (day 12)JavaScript学习笔记(数组3)
  • (Matlab)基于蝙蝠算法实现电力系统经济调度
  • (动手学习深度学习)第13章 计算机视觉---图像增广与微调
  • (附源码)apringboot计算机专业大学生就业指南 毕业设计061355
  • (附源码)python房屋租赁管理系统 毕业设计 745613
  • (附源码)springboot工单管理系统 毕业设计 964158
  • (三)docker:Dockerfile构建容器运行jar包
  • (五) 一起学 Unix 环境高级编程 (APUE) 之 进程环境
  • (一)为什么要选择C++
  • (已更新)关于Visual Studio 2019安装时VS installer无法下载文件,进度条为0,显示网络有问题的解决办法
  • .NET 8.0 发布到 IIS
  • .NET BackgroundWorker
  • .NET Core中的去虚
  • .net wcf memory gates checking failed
  • .NET/MSBuild 中的发布路径在哪里呢?如何在扩展编译的时候修改发布路径中的文件呢?
  • .NET分布式缓存Memcached从入门到实战
  • @Import注解详解
  • @ModelAttribute使用详解
  • [ C++ ] STL---string类的使用指南
  • [ element-ui:table ] 设置table中某些行数据禁止被选中,通过selectable 定义方法解决
  • [AutoSar]BSW_Memory_Stack_004 创建一个简单NV block并调试