Elastic常用命令

常用命令

查看版本信息

curl -XGET http://localhost:9200/?pretty

查看集群状态

curl -XGET http://localhost:9200/_cat/health?v
epoch      timestamp cluster            status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1547914366 16:12:46  fl-elastic-cluster green           2         2      0   0    0    0        0             0                  -                100.0%

查看节点信息

curl -XGET http://localhost:9200/_cat/nodes?v
ip              heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.100.111           32          75   0    0.00    0.01     0.05 mdi       *      node1
192.168.100.112           28          71   0    0.00    0.01     0.05 mdi       -      node2

查看主节点

curl -XGET http://localhost:9200/_cat/master?v

查看分区状态

curl -XGET http://localhost:9200/_cat/shards?v

查看磁盘使用

可以在浏览器上访问下面这个网址
http://host:9200/_cat/allocation?v&hh=shards,disk.indices,disk.used,disk.avail

查看内存使用

可以在浏览器上访问下面这个网址
http://host:9200/_cat/nodes?v&h=id,port,v,m,fdp,mc,mcs,sc,sm,qcm,fm,im,siwm,svmm

elastic默认mapping字段有1000个的限制,可以使用下面的命令进行修改(如修改为5000)
curl -XPUT http://localhost:9200/my_index/_setting -d ‘{“index.mapping.total_fields.limit”: 5000}’

创建索引(索引必须是小写)

例如创建一个my_index索引,默认为会5个分片,一个副本

curl -XPUT localhost:9200/my_index?pretty

查看索引

curl -XGET http://localhost:9200/_cat/indices?v

查看索引设置

curl -XGET localhost:9200/my_index/_settings?pretty

查看索引状态

curl -XGET http://localhost:9200/_cluster/health/my_index?pretty

删除索引

curl -XDELETE http://localhost:9200/my_index

手动创建mapping

curl -XPUT http://localhost:9200/my_index/_mapping/my_type?pretty -d '{
    "properties":{
        "name":{
            "type": "keyword" 
        },
        "age":{
            "type": "keyword" 
        }
    }
}'

使用脚本做mapping初始化时,可以下面的方式

curl -H "Content-Type: application/json" -XPUT http://localhost:9200/my_index/_mapping/my_type -d "$mapping_data"

当mapping数据量较多时,上面的方式curl会报参数过长,可以使用提交文件的方式

curl -H "Content-Type: application/json" -XPUT http://localhost:9200/my_index?pretty -d @mapping.json

查看mapping

curl -XGET http://localhost:9200/my_index/_mapping/my_type?pretty

写入文档

curl -H 'Content-Type:application/json' -XPUT 'localhost:9200/my_index/vip/1?pretty' -d '
{
   "name":"zhangsan"
}'

插入成功会返回如下信息

{
  "_index" : "customer",
  "_type" : "vip",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

查看操作

查询所有文档

kiabana上使用如下查询

GET _search
{
    "query": {
        "match_all": {}
    }
}

也可以使用curl进行查询

curl -H "Content-Type: application/json" -XGET http://localhost:9200/my_index/_search?pretty -d '{
    "query": {
        "match_all": {}
    }
}'

使用term进行精确查询

如查找my_index索引中name是zhangsan的文档

curl -H "Content-Type: application/json" -XGET http://localhost:9200/my_index/_search?pretty -d '{
    "query": {
        "constant_score": {
            "filter": {
                "term": {
                    "name" "zhangsan"
                }
            }
        }
    }
}'

使用terms查找多个值

如查找my_index索引中name是zhangsan或lisi的文档

curl -H "Content-Type: application/json" -XGET http://localhost:9200/my_index/_search?pretty -d '{
    "query": {
        "terms": {
            "name" : ["zhangsan","lisi"]
        }
    }
}'

使用bool进行逻辑查询

布尔过滤器
一个bool过滤器由三个部分组成
{
“bool”: {
“must”: [],
“should”: [],
“must_not”: [],
}
}
must: 所有条件都必须匹配,与AND等价
should: 至少有一个条件要匹配,与OR等价
must_not: 所有条件都不能匹配,与NOT等价

curl -H "Content-Type: application/json" -XGET http://localhost:9200/my_index/_search?pretty -d '{
    "query": {
        "bool": {
            "should": [
                {"term": {"age": "18"}},
                {"term": {"age": "20"}}
            ],
            "must_not": [
                "term" : {"name": "zhangsan"}
            ]
        }
    }
}'

使用range进行范围查询

range 查询可同时提供包含(inclusive)和不包含(exclusive)这两种范围表达式,可供组合的选项如下:
gt: 大于
lt: 小于
gte: 大于或等于
lte: 小于或等于

如查询my_index中age大于等于18,小于等于26的文档

curl -H "Content-Type: application/json" -XGET http://localhost:9200/my_index/_search?pretty -d '{
    "query": {
        "range": {
            "age": {
                "gte": 18,
                "lte": 26
            }
        }
    }
}'

match查询

curl -H "Content-Type: application/json" -XGET http://localhost:9200/my_index/_search?pretty -d '{
    "query": {
        "match": {
            "name" : "张三"
        }
    }
}'

正则表达式查询

curl -H "Content-Type: application/json" -XGET http://localhost:9200/my_index/_search?pretty -d '{
    "query": {
        "wildcard": {
            "name" : "张三*"
        }
    }
}'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值