目录
elasticsearch 更新数据(更新指定字段,其他字段不受影响)
1、常用命令:
以下命令可以在ES Header执行,样例如下:
也可以直接在浏览器地址栏输入,输入格式为:
也可以直接在浏览器地址栏输入,输入格式为:
http://{ES的IP地址}:{端口号}/命令行
如:http://localhost:9200/_cat/allocation?v
#查看磁盘空间
_cat/allocation?v
#查看索引分片分布及分片占用存储空间大小与文档数(会扫描所有分片,慎用!!!)
_cat/shards?v
#查看指定索引分片分布及分片占用存储空间大小与文档数
_cat/shards/{index}?v
#查看主节点是哪台机器
_cat/master?v
#查看主节点CPU,内存情况
_cat/nodes?v
#查看集群任务
_cat/tasks?v
#查看集群所有索引列表,并展示每个索引的状态、分片数量、文档数量、索引占用存储空间大小
_cat/indices?v
#查看集群指定索引信息,并展示每个索引的状态、分片数量、文档数量、索引占用存储空间大小
_cat/indices/{index}?v
#查看索引分片管理
_cat/segments?v
_cat/segments/{index}?v
#查看索引的文档计数
_cat/count?v
_cat/count/{index}?v
#查看索引分片转移后的集群索引恢复情况
_cat/recovery?v
_cat/recovery/{index}?v
#查看集群健康状况
_cat/health?v
#查看集群待处理任务
_cat/pending_tasks?v
#查看索引别名信息
_cat/aliases?v
#查看指定别名对应的所有索引
_cat/aliases/{alias}?v
#查看节点集群线程池统计信息
_cat/thread_pool?v
_cat/thread_pool/{thread_pools}?v
#查看每个节点有多少插件,插件版本
_cat/plugins?v
#查看数据节点上字段使用多少堆内存
_cat/fielddata?v
_cat/fielddata/{fields}?v
#查看自定义节点信息
_cat/nodeattrs?v
#查看集群中注册的快照存储库
_cat/repositories?v
#查看指定快照存储库的快照信息
_cat/snapshots/{repository}?v
#查看集群索引模板
_cat/templates?v
#获取集群统计信息,如:集群名称、节点个数、ES版本号、索引、分片、操作系统、JVM信息
_cluster/stats
2、常用增删改查操作命令(Linux)
符号术语:
{index}:索引,根据需要自己设定,下面例子的索引为:customer
{type}:类型,根据需要自己设定,下面例子的索引为:external
{id}:文档id,根据需要自己设定
elasticsearch 创建索引
#直接创建索引,不指定mapping
curl -XPUT 'localhost:9200/{index}'
例:
curl -XPUT 'localhost:9200/customer'
#设定mapping信息创建索引
curl -XPUT 'localhost:9200/{index}' -d '{}'
例:
curl -XPUT 'localhost:9200/customer' -d '
{
"mappings": {
"external": {
"properties": {
"name": {
"type": "keyword"
}
}
}
}
}'
elasticsearch 插入数据
curl -XPUT 'localhost:9200/{index}/{type}/{id}?pretty' -d '{}'
#在customer索引下类型为external,插入id为1的数据,数据体必须为json格式
curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
{
"name": "John Doe"
}'
elasticsearch 获取数据
curl -XGET 'localhost:9200/{index}/{type}/{id}?pretty'
#获取customer索引下类型为external,id为1的数据,pretty参数表示返回结果格式美观
curl -XGET 'localhost:9200/customer/external/1?pretty'
elasticsearch 修改数据(全覆盖的形式)
curl -XPUT 'localhost:9200/{index}/{type}/{id}?pretty' -d'{}'
#在customer索引下类型为external,插入id为1的数据,数据体必须为json格式,相同id的数据会被覆盖
curl -XPUT 'localhost:9200/customer/external/1?pretty' -d'
{
"name": "John Doe xxxxxxx"
}'
elasticsearch 更新数据(更新指定字段,其他字段不受影响)
#只更新name字段的信息
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d'
{
"doc": {
"name": "Jane Doe"
}
}'
#更新name字段的信息,同时还新增了age字段
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
{
"doc": {
"name": "Jane Doe",
"age": 20
}
}'
#使用脚本方式进行更新,age自增5
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d'
{
"script": "ctx._source.age += 5"
}'
elasticsearch 删除数据
#删除customer索引下类型为external的ID为2的数据
curl -XDELETE 'localhost:9200/customer/external/2?pretty'
elasticsearch 删除索引
curl -XDELETE 'localhost:9200/customer'