使用curl命令操作elasticsearch
第一:_cat系列
_cat系列提供了一系列查询elasticsearch集群状态的接口。你可以通过执行
curl -XGET localhost:9200/_cat
获取所有_cat系列的操作
/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/master
/_cat/nodes
/_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/plugins
/_cat/fielddata
/_cat/fielddata/{fields}
你也可以后面加一个v,让输出内容表格显示表头,举例
name component version type url
Prometheus analysis-mmseg NA j
Prometheus analysis-pinyin NA j
Prometheus analysis-ik NA j
Prometheus analysis-ik NA j
Prometheus analysis-smartcn 2.1.0 j
Prometheus segmentspy NA s /_plugin/segmentspy/
Prometheus head NA s /_plugin/head/
Prometheus bigdesk NA s /_plugin/bigdesk/
Xandu analysis-ik NA j
Xandu analysis-pinyin NA j
Xandu analysis-mmseg NA j
Xandu analysis-smartcn 2.1.0 j
Xandu head NA s /_plugin/head/
Xandu bigdesk NA s /_plugin/bigdesk/
Onyxx analysis-ik NA j
Onyxx analysis-mmseg NA j
Onyxx analysis-smartcn 2.1.0 j
Onyxx analysis-pinyin NA j
Onyxx head NA s /_plugin/head/
Onyxx bigdesk NA s /_plugin/bigdesk/
第二:_cluster系列
1、查询设置集群状态
curl -XGET localhost:9200/_cluster/health?pretty=true
pretty=true表示格式化输出
level=indices 表示显示索引状态
level=shards 表示显示分片信息
2、curl -XGET localhost:9200/_cluster/stats?pretty=true
显示集群系统信息,包括CPU JVM等等
3、curl -XGET localhost:9200/_cluster/state?pretty=true
集群的详细信息。包括节点、分片等。
3、curl -XGET localhost:9200/_cluster/pending_tasks?pretty=true
获取集群堆积的任务
3、修改集群配置
举例:
curl -XPUT localhost:9200/_cluster/settings -d '{
"persistent" : {
"discovery.zen.minimum_master_nodes" : 2
}
}'
transient 表示临时的,persistent表示永久的
4、curl -XPOST ‘localhost:9200/_cluster/reroute’ -d ‘xxxxxx’
对shard的手动控制,参考http://zhaoyanblog.com/archives/687.html
5、关闭节点
关闭指定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}’
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}/_mapping
6、搜索
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
}
PUT http://10.32.176.80:9200/shopping #创建索引
GET http://10.32.176.80:9200/shopping #获取某个索引
GET http://10.32.176.80:9200/_cat/indices?v #查看全部索引
POST http://10.32.176.80:9200/shopping/_doc/1001 #创建文档
PUT http://10.32.176.80:9200/shopping/_doc/1001 #全局修改文档
POST http://10.32.176.80:9200/shopping/_doc/2000/_update?pretty
{
"doc":{
"title":"小米"
}
}
#局部修改文档
GET http://10.32.176.80:9200/shopping/_doc/_search?q=qua:good
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mxyH8Vpe-1638349399746)(elasticsearch%E5%AD%A6%E4%B9%A0%E7%BA%AA%E8%A6%81.assets/image-20211104145024463.png)]
倒排索引
postman
{
“title”:“华为”,
“price”:3000,
“qua”:“good”
}
curl -i -XGET ‘localhost:9200/_count?pretty’ -H ‘content-Type:application/json’ -d ‘{ “query”:{“match_all”:{}}}’
index (索引) === database
mapping(映射) === 表结构
document(文档) ====数据
elasticsearch ik分词器
1.ik_smart 最少切分
2.ik_max_word 最细粒度划分
自定义分词器规则
添加xxx.dic文件
关于索引的基本操作
PUT /索引名/类型名/文档id
{请求体}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OxKKQ2nn-1638349399748)(elasticsearch%E5%AD%A6%E4%B9%A0%E7%BA%AA%E8%A6%81.assets/image-20211103153119968.png)]
eg:
curl -i -XPUT ‘10.32.176.80:9200/test1/type1/2’ -H ‘content-Type:application/json’ -d ‘{“name”: “狂神说1”,“age”: 4}’
PUT /test3/_doc/1
{
“name”:“狂神说123”,
“age”:13,
“birthday”:“1997-01-05”
}
GET test3
GET _cat/indices?v
POST /test3/_doc/1/_update
{
“doc”:{
“name”:“法外狂徒”
}
}
DELETE test1
关于文档的基本操作
基本操作
PUT /kuangshen/user/1
{
“name”: “狂神说”,
“age”: 23,
“desc”: “hahahaha”,
“tags”: [
“技术宅”,
“宅男”,
“直男”
]
}
POST /kuangshen/user/1/_update
{
“doc”:{
“name”:“狂神说java”
}
}
put如果不传值就会被覆盖,而post不会
简单的搜索
GET /kuangshen/user/_search?q=name:狂神说
复杂操作
select(排序,分页,高亮,模糊查询,精确查询)
GET kuangshen/user/_search
{
“query”: {
“match_all”: {}
},
“sort”: [
{
“age”: {
“order”: “asc”
}
}
],
“from”: 0,
“size”: 2
}
布尔值查询
must(and),所有条件都要符合 where id=1 and name=xxx
should (or),只要结果符合一个就会显示出来(两个条件满足其一)where id=1 or name=xxx
must_not 反向操作
GET kuangshen/user/_search
{
“query”: {
“bool”: {
“must”: [
{
“match”: {
“name”: “张”
}
}
],
“filter”: {
“range”: {
“age”: {
“gte”: 22,
“lte”: 26
}
}
}
}
}
}
GET kuangshen/user/_search
{
“query”: {
“match”: {
“tags”: “男 技术 宅”
}
}
}
精确查询
term查询是直接通过倒排索引指定的词条进程精确查找的
关于分词:
term,直接查询精确的
match,会使用分词器解析(先分析文档,再通过分析的文档进行查询)
两个类型 text,keyword
curl -i -XPUT ‘10.32.176.80:9200/testdb’ -H ‘content-Type:application/json’ -d ‘{
“mappings”: {
“properties”: {
“name”: {
“type”: “text”
},
“desc”: {
“type”: “keyword”
}
}
}
}’
精确查询
term查询是直接通过倒排索引指定的词条进程精确查找的
关于分词:
term,直接查询精确的
match,会使用分词器解析(先分析文档,再通过分析的文档进行查询)
两个类型 text,keyword
curl -i -XPUT ‘10.32.176.80:9200/testdb’ -H ‘content-Type:application/json’ -d ‘{
“mappings”: {
“properties”: {
“name”: {
“type”: “text”
},
“desc”: {
“type”: “keyword”
}
}
}
}’