ElasticSearch6.X查询及相关基本操作
基础概念
- RESTFul API
- curl命令操作
使用curl命令操作elasticsearch
- 第一:_cat系列
查看集群是否健康
- 第二:_cluster系列
- 第三:_nodes系列
- 第四:索引操作
基础概念
- 索引:含有相同属性的文档集合
相当于与SQL中database - 类型:索引可以定义一个或多个类型,文档必须属于一个类型
相当于与SQL中table - 文档:文档是可以被索引的基本数据单位
相当于与SQL中row - 分片:每个索引都有多个分片,每个分片是一个Lucene索引
- 备份:拷贝一份分片就完成了分片的备份
RESTFul API
api基本格式
http://[ip]:[port]/[索引]/[类型]/[文档id]
常用HTTP动词
GET/PUT/DELETE/POST
curl命令操作
-X 指定http的请求方法
有 HEAD GET POST PUT DELETE
-d 指定要传输的数据
-H 指定http请求头信息
使用curl命令操作elasticsearch
第一:_cat系列
cat系列提供了一系列查询ES集群状态的接口。
可以通过执行 curl -XGET localhost:9200/_cat 命令,获取所有cat系列的操作:
[root@C20-23U-10 ~]# curl -XGET localhost:9200/_cat
=^.^=
/_cat/allocation 查看节点分配情况。
/_cat/shards 看分片情况。
/_cat/master 查看主节点。
/_cat/nodes 查看所有节点。
/_cat/indices 查看所用索引状态。
/_cat/segments 查看索引的分片信息。
/_cat/count 查看文档个数。
/_cat/health 查看集群健康情况。
.........
可以在下列命令后加上?v格式化输出,也可以加上?help查看命令相关信息。结果如下:
[root@zq-zabbix ~]# curl -XGET 172.17.19.29:9200/_cat/master
BOO9Ur0dRoKZB_9dZIPtSg 172.17.19.29 172.17.19.29 node1-29
[root@zq-zabbix ~]# curl -XGET 172.17.19.29:9200/_cat/master?v
id host ip node
BOO9Ur0dRoKZB_9dZIPtSg 172.17.19.29 172.17.19.29 node1-29
查看集群是否健康
curl -XGET localhost:9200/_cat/health?v
绿色——最健康的状态,代表所有的主分片shard和副本分片replica都可用。
黄色——所有的主分片shard可用,但是部分副本分片replica不可用。
红色——部分主分片shard不可用,此种情况需立即解决。
第二:_cluster系列
1、查询集群健康状态
[root@zq-zabbix ~]# curl -XGET 172.17.19.29:9200/_cluster/health?pretty=true
{
"cluster_name" : "zhangqi-es",
"status" : "yellow",
"timed_out" : false,
"number_of_nodes" : 1,
"number_of_data_nodes" : 1,
"active_primary_shards" : 2012,
"active_shards" : 2012,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 1994,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 50.22466300549176
#pretty=true表示格式化输出
}
2、显示集群系统
信息,包括CPU JVM等等
curl -XGET 172.17.19.29:9200/_cluster/stats?pretty=true3、集群的详细信息。包括节点、分片等。
curl -XGET 172.17.19.29:9200/_cluster/state?pretty=true4、关闭节点
#关闭指定192.168.1.1节点
curl -XPOST ‘http://192.168.1.1:9200/_cluster/nodes/_local/_shutdown’
curl -XPOST ‘http://localhost:9200/_cluster/nodes/192.168.1.1/_shutdown’
#关闭主节点
curl -XPOST ‘http://localhost:9200/_cluster/nodes/_master/_shutdown’
#关闭整个集群
curl -XPOST ‘http://localhost:9200/_shutdown?delay=10s’
curl -XPOST ‘http://localhost:9200/_cluster/nodes/_shutdown’
curl -XPOST ‘http://localhost:9200/_cluster/nodes/_all/_shutdown’
##delay=10s表示延迟10秒关闭
第三:_nodes系列
1、查询节点的状态
curl -XGET ‘http://localhost:9200/_nodes/stats?pretty=true’
curl -XGET ‘http://localhost:9200/_nodes/192.168.1.2/stats?pretty=true’
curl -XGET ‘http://localhost:9200/_nodes/process’
curl -XGET ‘http://localhost:9200/_nodes/_all/process’
curl -XGET ‘http://localhost:9200/_nodes/192.168.1.2,192.168.1.3/jvm,process’
curl -XGET ‘http://localhost:9200/_nodes/192.168.1.2,192.168.1.3/info/jvm,process’
curl -XGET ‘http://localhost:9200/_nodes/192.168.1.2,192.168.1.3/_all
curl -XGET ‘http://localhost:9200/_nodes/hot_threads
第四:索引操作
1、获取索引
curl -XGET ‘http://localhost:9200/{index}/{type}/{id}’ 2、索引数据
curl -XPOST ‘http://localhost:9200/{index}/{type}/{id}’ -d'{“a”:”avalue”,”b”:”bvalue”}’ 3、删除索引
curl -XDELETE 'http://localhost:9200/{index}/{type}/{id}'
curl -XDELETE 127.0.0.1:9200/logstash-2017.08.29#可以使用通配符删除统一后缀的索引
4、设置mapping
curl -XPUT http://localhost:9200/{index}/{type}/_mapping -d '{
"{type}" : {
"properties" : {
"date" : {
"type" : "long"
},
"name" : {
"type" : "string",
"index" : "not_analyzed"
},
"status" : {
"type" : "integer"
},
"type" : {
"type" : "integer"
}
}
}
}'
5、获取mapping
curl -XGET http://localhost:9200/{index}/{type}/_mapping6、搜索
curl -XGET 'http://localhost:9200/{index}/{type}/_search' -d '{
"query" : {
"term" : { "user" : "kimchy" } //查所有 "match_all": {}
},
"sort" : [{ "age" : {"order" : "asc"}},{ "name" : "desc" } ],
"from":0,
"size":100
}
curl -XGET 'http://localhost:9200/{index}/{type}/_search' -d '{
"filter": {"and":{"filters":[{"term":{"age":"123"}},{"term":{"name":"张三"}}]},
"sort" : [{ "age" : {"order" : "asc"}},{ "name" : "desc" } ],
"from":0,
"size":100
}文档更新时间: 2023-08-09 08:03 作者:月影鹏鹏