转自:https://blog.csdn.net/u013089490/article/details/84315994

1、创建索引

1.1、语法

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

  • 请求方式:PUT;
  • 请求路径:/索引库名;
  • 请求参数:json格式:
    1. {
    2. "settings": {
    3. "number_of_shards": 3,
    4. "number_of_replicas": 2
    5. }
    6. }
    settings:索引库的设置
    (1)number_of_shards:分片数量;
    (2)number_of_replicas:副本数量;

    1.2、创建一个索引wzy

    1. #创建一个wzy的索引,分片数为1,副本数为1
    2. PUT /wzy
    3. {
    4. "settings": {
    5. "number_of_shards": 1,
    6. "number_of_replicas": 1
    7. }
    8. }
    9. 【返回结果】
    10. {
    11. "acknowledged": true,#表示索引创建成功
    12. "shards_acknowledged": true,#表示所需分片数量和副本数量启动成功
    13. "index": "wzy"#表示索引名称
    14. }
    15. #【注意】在创建索引时,一旦创建指定了分片数那么就不能修改,但是副本数可以修改
    如果我们使用PUT /user?pretty创建一个索引user ,而pretty要求返回一个漂亮的json 结果。

    1.3、创建索引注意事项

    首先我们来看一下官网中对创建索引的一句说明:
    1. Default for number_of_shards is 5max is 1024
    2. Default for number_of_replicas is 1 (ie one replica for each primary shard)
    【注意】一个索引默认5个分片,一个副本;且一个索引最大分片数为1024;创建索引名是必须是小写,且名称不能重复。

    2、索引管理

    2.1、查看索引

    1、查看索引基本语法

    1. #查看索引语法
    2. GET /索引名

    2、查看索引

    1. #在kibana中删除索引指令
    2. DELETE /wzy
    3. #返回结果
    4. {
    5. "acknowledged": true
    6. }
    7. #再次查看
    8. GET /wzy
    9. #删除后再次查询返回结果
    10. {
    11. "error": {
    12. "root_cause": [
    13. {
    14. "type": "index_not_found_exception",
    15. "reason": "no such index",
    16. "resource.type": "index_or_alias",
    17. "resource.id": "wzy",
    18. "index_uuid": "_na_",
    19. "index": "wzy"
    20. }
    21. ],
    22. "type": "index_not_found_exception",
    23. "reason": "no such index",
    24. "resource.type": "index_or_alias",
    25. "resource.id": "wzy",
    26. "index_uuid": "_na_",
    27. "index": "wzy"
    28. },
    29. "status": 404
    30. }

    2.2、查看所有索引

    在浏览器中输入http://192.168.2.10:9200/_cat/indices?v或者在kibana中输入GET /_cat/indices?v
    elasticsearch基础篇(2):创建/查看/删除索引操作 - 图1
    如果提示信息为“health status index uuid pri rep docs.count docs.deleted store.size pri.store.size” 表示还没索引在ES中创建。

    2.3、打开/关闭索引

    1. #关闭索引
    2. POST /索引名称/_close
    3. #打开索引
    4. POST /索引名称/_open
    关闭的索引不能进行读写操作,几乎不占集群开销。关闭的索引可以打开,打开走的是正常的恢复流程

    2.4、其他操作

    【索引状态清理缓存】
    1. #清除所有索引缓存
    2. POST /_cache/clear
    3. #清除一个索引状态缓存
    4. POST /索引名称/_cache/clear
    5. #清除多个索引
    6. POST /wzy1,wzy2/_cache/clear
    【将缓存在内存中的索引数据刷新到持久存储中 】
    1. POST wzy/_flush

    3、删除索引

    删除索引使用DELETE请求。

    【基本语法】

    1. DELETE /索引名称

    【删除索引】

    1. #在kibana中删除索引指令
    2. DELETE /wzy
    3. #返回结果
    4. {
    5. "acknowledged": true
    6. }
    7. #再次查看
    8. GET /wzy
    9. #删除后再次查询返回结果
    10. {
    11. "error": {
    12. "root_cause": [
    13. {
    14. "type": "index_not_found_exception",
    15. "reason": "no such index",
    16. "resource.type": "index_or_alias",
    17. "resource.id": "wzy",
    18. "index_uuid": "_na_",
    19. "index": "wzy"
    20. }
    21. ],
    22. "type": "index_not_found_exception",
    23. "reason": "no such index",
    24. "resource.type": "index_or_alias",
    25. "resource.id": "wzy",
    26. "index_uuid": "_na_",
    27. "index": "wzy"
    28. },
    29. "status": 404
    30. }
    【注意】可以一次删除多个索引(以逗号间隔);删除所有索引 _all 或 通配符 *