Elasticsearch 基本操作
集群
查看集群健康度
GET /_cat/health?v
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
1475247709 17:01:49 elasticsearch green 1 1 0 0 0 0 0 0 - 100.0%
查看集群健康度会获取三种状态
- 绿色——一切都好(群集完全正常工作)
- 黄色——所有数据都可用,但一些副本尚未分配(群集完全正常工作)
- 红色——有些数据不管出于什么原因都不可用(集群部分功能正常)
列出集群所有节点
GET /_cat/health?v
curl -X GET "localhost:9200/_cat/health?v"
列出所有的索引
GET /_cat/indices?v
curl -X GET "localhost:9200/_cat/indices?v"
索引的使用
创建索引
- 创建默认配置的索引 customer,5 个基础分片,1 个复制集(必须需要 2 个节点才可用)。
PUT /customer?pretty
curl -X PUT "localhost:9200/customer?pretty"
- 指定设置和映射
curl -X PUT "localhost:9200/test" -H 'Content-Type: application/json' -d'
{
"settings" : {
"number_of_shards" : 1,
"number_of_replicas" : 2,
"refresh_interval": "1s" //默认值
},
"mappings" : {
"_doc" : {
"properties" : {
"field1" : { "type" : "text" }
}
}
}
}
'
删除
- 删除索引可以使用模糊匹配等多种选择
DELETE /my_index?pretty
DELETE /index_one,index_two
DELETE /index_*
DELETE /_all
DELETE /*
curl -X DELETE "localhost:9200/customer?pretty"
# 结果
{
"acknowledged" : true
}
映射
单索引映射
- 创建索引时创建映射
curl -X PUT "localhost:9200/test" -H 'Content-Type: application/json' -d'
{
"mappings" : {
"_doc" : {
"properties" : {
"field1" : { "type" : "text" }
}
}
}
}
'
- 在存在的索引中添加映射
curl -X PUT "localhost:9200/twitter" -H 'Content-Type: application/json' -d'
{}
'
curl -X PUT