学习
ElasticSearch7.6入门学习笔记
黑马Elasticsearch使用文档
Es 查询数据
查询所有数据
在第二行文本框中输入_search
,第三行输入{}
。_search
特指查询操作
,{}
指查询所有数据
查询单个索引所有数据
在第二行文本框中输入test/_search
,第三行输入{}
。test为索引名称
,_search特指查询操作,{}指查询所有数据
查询单个索引单个类型所有数据
在第二行文本框中输入test/xudongmaster/_search
,第三行输入{}。test为索引名称
,xudongmaster为类型名称
,_search特指查询操作,{}指查询所有数据
根据指定条件查询数据
在第二行文本框中输入test/_search
,第三行输入以下json数据。test为索引名称,_search特指查询操作,json数据指查询password属性为123456的数据
{
"query":{
"bool":{
"must":[{
"term":{
"password":"123456"
}
}]
}
}
}
curl 查询es
Warning: Binary output can mess up your terminal. Use “–output -“ to tell
curl命令忽略不受信任的https安全限制 -k / --insecure
其他
# 检查es版本信息
curl IP:9200
# 查询指定所有库的所有数据
curl -XGET http://ip:port/索引名/_search?pretty
# 模糊匹配
curl -H "Content-Type:application/json" -XGET http://ip:port/索引名/_search?pretty
-d '{"query":{"match":{"name":"赵"}}}'
# 多条件查询 bool must:必须满足的条件,must_not:必须不能满足的条件,should:应该,可有可无
curl -H "Content-Type:application/json" -XGET http://ip:port/索引名/_search?pretty
-d '{"query":{"bool":{"must":{"match":{"name":"赵"}}}}}'
# 精准查询term
curl -H "Content-Type:application/json" -XGET http://ip:port/索引名/_search?pretty
-d '{"query":{"term":{"name":"赵雷"}}}'
# 精准多个词匹配
curl -H "Content-Type:application/json" -XGET http://ip:port/索引名/_search?pretty
-d '{"query":{"terms":{"name":["赵雷",""]}}}'
# 范围查询
curl -H "Content-Type:application/json" -XGET http://ip:port/索引名/_search?pretty
-d '{"query":{"range":{"age":{"gt":"20","lte":"25"}}}}'
curl -XGET 'http://localhost:9200/{index}/{type}/_search' -d '放 es 的 query 语句'
-XGET 可以不写,默认是 -XGET
可以后面加一个?v,让输出内容表格显示表头
curl -XGET http://es_pod_ip:9200/_cat
设置请求头
-H "Content-Type: application/json"
pretty=true表示格式化输出
可以 curl http://es_pod_ip:9200/_cat?v&pretty=true
curl -XGET 'http://192.168.22.161:9200/index/fulltext/_search'
-d '{
“query” : {
“query_string” : {
“default_field” : “content”,
“query” : “中国美国”
}
}
}'
curl -XGET -u "账号:密码" -H "Content-Type: application/json" 'ip地址:端口/索引名/_search' -d '{
"query": {
"bool": {
"should": [
{"term": {
"employee_ldap": {
"value": "guilhermepacheco1"
}
}}
]
}
},
"from": 0,
"size": 20,
"_source": ["_id","data_source","mdata_create_time","employee_ldap","hr_status","employee_name","employee_ldap"]
}'
_cat系列
_cat
系列提供了一系列查询elasticsearch集群状态
的接口
curl -XGET http://es_pod_ip:9200/_cat
# 查看集群是否健康
curl http://IP:9200/_cat/health?v
# 查看节点列表和磁盘剩余
curl http://IP:9200/_cat/nodes?v
heap.percent ,堆内存占用百分比
ram.percent ,内存占用百分比
cpu ,CPU占用百分比
load_1m load_5m load_15m ,一分钟五分钟十五分钟负载
node.role , m:master eligible node, d:data node, i:ingest node。master *表示节点是集群中的主节点current master
name ,节点名
# 列出所有索引及存储大小
curl http://IP:9200/_cat/indices?v
#查看每个数据节点上的分片数(shards),以及每个数据节点磁盘剩余
curl http://IP:9200/_cat/allocation
1)shards ,节点说承载的分片数
2)disk.indices ,索引占用的空间大小
3)disk.used ,节点所在机器已使用磁盘空间
4)disk.avail ,磁盘可用容量
5)disk.total, 磁盘总容量
6)disk.percent ,磁盘便用率
7)ip ,节点所属机器IP地址
8)node ,节点名
# 查看每个节点正在运行的插件
curl http://IP:9200/_cat/plugins
# 显示master节点信息
curl http://IP:9200/_cat/master
# 查看整个集群的doc的总数,以及单个索引的总数
curl http://IP:9200/_cat/count
# 某个索引文档数
curl http://IP:9200/_cat/count/my_index1?v
_cluster系列
# 查询设置集群状态
curl -XGET localhost:9200/_cluster/health?pretty=true
level=indices 表示显示索引状态
level=shards 表示显示分片信息
# 显示集群系统信息,包括CPU JVM等等,ES全局状态
curl -XGET localhost:9200/_cluster/stats?pretty=true
# 集群的详细信息。包括节点、分片等。
curl -XGET localhost:9200/_cluster/state?pretty=true
_nodes系列
# 查询节点的状态
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
索引操作
# 查看索引mapping结构
curl -XGET http://localhost:9200/my-index
# 获取索引
curl -XGET 'http://localhost:9200/{index}/{type}/{id}'
# 查询某个索引库index下的所有数据(在url后面加上一个pretty=true则会对返回结果进行格式化)
curl -XGET http://localhost:9200/索引/_search?pretty=true
# 查询某个type下的所有数据(在url后面加上一个pretty=true则会对返回结果进行格式化)
curl -XGET http://localhost:9200/索引/type_name/_search?pretty=true
{
"_index" : "xxx_index",
"_type" : "xxx",
"_id" : "1:13:1649419440000:2",
"_score" : 1.0,
"_source" : {
}
}
# 根据id查询具体的一条记录
curl -XGET http://localhost:9200/my-index/my-type/1?pretty=true
# 输出
# {
# "_index" : "my-index",
# "_type" : "my-type",
# "_id" : "1",
# "_version" : 2,
# "found" : true,
# "_source" : {
# "age" : 18,
# "name" : "xiaoming"
# }
# }
# 查询一条索引文档中的具体的字段
curl -XGET http://localhost:9200/my-index/my-type/1?_source=name
# 输出
# {"_index":"my-index","_type":"my-type","_id":"1","_version":2,"found":true,"_source":{"name":"xiaoming"}}
# 如果要查询多个字段,使用","进行隔开
curl -XGET http://localhost:9200/my-index/my-type/1?_source=name,age
# 输出
# {"_index":"my-index","_type":"my-type","_id":"1","_version":2,"found":true,"_source":{"name":"xiaoming","age":18}}
# 获取source所有数据
curl -XGET http://localhost:9200/my-index/my-type/1?_source
# 输出
# {"_index":"my-index","_type":"my-type","_id":"1","_version":2,"found":true,"_source":{"name":"xiaoming","age":18}}
# 根据条件进行查询name是xiaoming的
curl -XGET http://localhost:9200/my-index/my-type/_search?q=name:xiaoming
# 输出
# {"took":14,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":0.2876821,"hits":[{"_index":"my-index","_type":"my-type","_id":"1","_score":0.2876821,"_source":{"age":18,"name":"xiaoming"}}]}}
# 根据条件进行查询name是xiaoming和name是xiaoxue的,xiaoming和xiaoxue之间用","隔开
curl -XGET http://localhost:9200/my-index/my-type/_search?q=name:xiaoming, xiaoxue
_doc
# 指定地址、index、es类型、数据类型(json)、字段信息、查询数(5)
curl -XGET 'http://127.0.0.1:9200/page/_doc/_search' -H "Content-Type: application/json" -d '
{
"query": {
"match": {
"domain": "xxxxxxxxxxxxx.onion"
}
},
"size": 5
}'
# 指定地址、index、es类型、数据类型(json)、字段信息、查询数(5)、通过crawl_time字段排序来查询
curl -XGET 'http://10.42.101.51:9200/page/_doc/_search' -H "Content-Type: application/json" -d '
{
"query": {
"match": {
"domain": "xxxxxxxxxxxxxxx.onion"
}
},
"sort":{
"crawl_time":{
"order":"desc"
}
},
"size": 5
}'
# 查询documentId为01的一条数据
curl -XGET http://ip:port/索引名/_doc/01?pretty