es常用命令

es常用命令

1. 查看所有可查看项

[root@localhost ~]# curl http://192.168.20.100:9200/_cat
=^.^=
/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/master
/_cat/nodes
/_cat/tasks
/_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/thread_pool/{thread_pools}
/_cat/plugins
/_cat/fielddata
/_cat/fielddata/{fields}
/_cat/nodeattrs
/_cat/repositories
/_cat/snapshots/{repository}
/_cat/templates

2. 显示详细信息 ?v

[root@localhost ~]# curl http://192.168.20.100:9200/_cat/master?v
id                     host           ip             node
10iuRcM-RxieXxnbonHBJg 192.168.20.102 192.168.20.102 es3
[root@localhost ~]# 

3. 输出可以显示的列 ?help

[root@localhost ~]# curl http://192.168.20.100:9200/_cat/master?help
id   |   | node id    
host | h | host name  
ip   |   | ip address 
node | n | node name  

4. 指定输出的列 ?h

[root@localhost ~]# curl http://192.168.20.100:9200/_cat/master?h=ip,node
192.168.20.102 es3

5. 查看所有索引

[root@localhost ~]# curl http://192.168.20.100:9200/_cat/indices?v
health status index uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   kgc   aqgArqGnRJqBLqxyfW05vA   5   1          1            0      8.3kb          4.1kb
[root@localhost ~]# 

6. 创建索引

[root@localhost ~]# curl -XPUT http://192.168.20.100:9200/kgctest1?pretty
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "kgctest1"
}
[root@localhost ~]# 

7. 关闭索引

[root@localhost ~]# curl -XPOST http://192.168.20.100:9200/kgctest1/_close?pretty
{
  "acknowledged" : true
}

8. 开启索引

[root@localhost ~]# curl -XPOST http://192.168.20.100:9200/kgctest1/_open?pretty
{
  "acknowledged" : true,
  "shards_acknowledged" : true
}

9. 删除索引

[root@localhost ~]# curl -XDELETE http://192.168.20.100:9200/kgctest1?pretty
{
  "acknowledged" : true
}

10. 插入数据

[root@localhost ~]# curl -XPUT http://192.168.20.100:9200/kgctest1/fulltext/1?pretty -H 'Content-Type: application/json' -d'
> {
>     "name": "tiechui"
> }'
{
  "_index" : "kgctest1",
  "_type" : "fulltext",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

11. 取出fulltext类型的id为1的数据

[root@localhost ~]# curl -XGET 'http://192.168.20.100:9200/kgctest1/fulltext/1?pretty'
{
  "_index" : "kgctest1",
  "_type" : "fulltext",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "name" : "tiechui"
  }
}

12. 更新文档

[root@localhost ~]# curl -XPOST 'http://192.168.20.100:9200/kgctest1/fulltext/1/_update?pretty' -H 'Content-Type:application/json' -d'
> {
>    "doc": {"name": "wangtiechui"}
> }'
{
  "_index" : "kgctest1",
  "_type" : "fulltext",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

13. 删除文档

[root@localhost ~]# curl -XDELETE 'http://192.168.20.100:9200/kgctest1/fulltext/1?pretty'
{
  "_index" : "kgctest1",
  "_type" : "fulltext",
  "_id" : "1",
  "_version" : 3,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 1
}

14.查询所有记录

[root@localhost ~]# curl -XPOST 'http://192.168.20.100:9200/kgctest1/fulltext/_search?pretty' -H 'Content-Type:application/json' -d'
> {
>     "query": {"match_all":{}}
> }'
{
  "took" : 119,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
}

15.查询符合条件的记录

[root@localhost ~]# curl -XPOST 'http://192.168.20.100:9200/kgctest1/fulltext/_search?pretty' -H 'Content-Type:application/json' -d'
> {
>   "query": {"match": {"name": "wangtiechui"}}
> }'
{
  "took" : 81,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
}

16. 清空内存中的缓存clear

[root@localhost ~]# curl -XPOST 'http://192.168.20.100:9200/kgctest1/_cache/clear?pretty' -H 'Content-Type:application/json'
{
  "_shards" : {
    "total" : 10,
    "successful" : 10,
    "failed" : 0
  }
}

17.flush和refresh(强制刷新数据到磁盘)

flush:
[root@localhost ~]# curl -XPOST 'http://192.168.20.100:9200/kgctest1/_flush?pretty' -H 'Content-Type:application/json'
  "_shards" : {
    "total" : 10,
    "successful" : 10,
    "failed" : 0
  }
}

refresh:
[root@localhost ~]# curl -XPOST 'http://192.168.20.100:9200/kgctest1/_refresh?pretty' -H 'Content-Type:application/json'
{
  "_shards" : {
    "total" : 10,
    "successful" : 10,
    "failed" : 0
  }
}

refresh与flush的区别

当一个文档进入ES的初期, 文档是被存储到内存里的,默认经过1s之后, 会被写入文件系统缓存,这样该文档就可以被搜索到了,注意,此时该索引数据被没有最终写入到磁盘上。如果你对这1s的时间间隔还不满意, 调用_refresh就可以立即实现内存->文件系统缓存, 从而使文档可以立即被搜索到。 ES为了数据的安全, 在接受写入的文档的时候, 在写入内存buffer的同时, 会写一份translog日志,从而在出现程序故障/磁盘异常时, 保证数据的完整和安全。flush会触发lucene commit,并清空translog日志文件。 translog的flush是ES在后台自动执行的,默认情况下ES每隔5s会去检测要不要flush translog,默认条件是:每 30 分钟主动进行一次 flush,或者当 translog 文件大小大于 512MB主动进行一次 flush

你可能感兴趣的:(linux)