elasticsearch基础篇(2):创建/查看/删除索引操作

1、创建索引

1.1、语法

Elasticsearch采用Rest风格API,因此其API就是一次http请求,你可以用任何工具发起http请求创建索引的请求格式:

  • 请求方式:PUT;
  • 请求路径:/索引库名;
  • 请求参数:json格式:
 {
      "settings": {
          "number_of_shards": 3,
          "number_of_replicas": 2
        }
  }

settings:索引库的设置
(1)number_of_shards:分片数量;
(2)number_of_replicas:副本数量;

1.2、创建一个索引wzy

#创建一个wzy的索引,分片数为1,副本数为1
PUT /wzy
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  }
}
【返回结果】
{
  "acknowledged": true,#表示索引创建成功
  "shards_acknowledged": true,#表示所需分片数量和副本数量启动成功
  "index": "wzy"#表示索引名称
}
#【注意】在创建索引时,一旦创建指定了分片数那么就不能修改,但是副本数可以修改

如果我们使用PUT /user?pretty创建一个索引user ,而pretty要求返回一个漂亮的json 结果。

1.3、创建索引注意事项

首先我们来看一下官网中对创建索引的一句说明:

Default for number_of_shards is 5,max is 1024
Default for number_of_replicas is 1 (ie one replica for each primary shard)

【注意】一个索引默认5个分片,一个副本;且一个索引最大分片数为1024;创建索引名是必须是小写,且名称不能重复。 

2、索引管理

2.1、查看索引

1、查看索引基本语法

#查看索引语法
GET /索引名

 2、查看索引

#kibana中查看索引
GET /wzy
#kibana查看索引返回结果
{
  "wzy": {
    "aliases": {},
    "mappings": {},
    "settings": {
      "index": {
        "creation_date": "1542772004868",
        "number_of_shards": "1",
        "number_of_replicas": "1",
        "uuid": "U9uJcIwKT8-quma-CQBEig",
        "version": {
          "created": "6020499"
        },
        "provided_name": "wzy"
      }
    }
  }
}

2.2、查看所有索引

在浏览器中输入http://192.168.2.10:9200/_cat/indices?v或者在kibana中输入GET /_cat/indices?v

如果提示信息为“health status index uuid pri rep docs.count docs.deleted store.size pri.store.size” 表示还没索引在ES中创建。

2.3、打开/关闭索引

#关闭索引
POST /索引名称/_close
#打开索引
POST /索引名称/_open

关闭的索引不能进行读写操作,几乎不占集群开销。关闭的索引可以打开,打开走的是正常的恢复流程。 

2.4、其他操作

【索引状态清理缓存】

#清除所有索引缓存
POST /_cache/clear
#清除一个索引状态缓存
POST /索引名称/_cache/clear
#清除多个索引
POST /wzy1,wzy2/_cache/clear 

【将缓存在内存中的索引数据刷新到持久存储中 】

POST wzy/_flush

3、删除索引

删除索引使用DELETE请求。

【基本语法】

DELETE /索引名称

【删除索引】

#在kibana中删除索引指令
DELETE /wzy
#返回结果
{
  "acknowledged": true
}
#再次查看
GET /wzy
#删除后再次查询返回结果
{
  "error": {
    "root_cause": [
      {
        "type": "index_not_found_exception",
        "reason": "no such index",
        "resource.type": "index_or_alias",
        "resource.id": "wzy",
        "index_uuid": "_na_",
        "index": "wzy"
      }
    ],
    "type": "index_not_found_exception",
    "reason": "no such index",
    "resource.type": "index_or_alias",
    "resource.id": "wzy",
    "index_uuid": "_na_",
    "index": "wzy"
  },
  "status": 404
}

【注意】可以一次删除多个索引(以逗号间隔);删除所有索引 _all 或 通配符 *

猜你喜欢

转载自blog.csdn.net/u013089490/article/details/84315994