小白学ES 08 - 对Elasticsearch的索引进行CRUD操作

1 创建索引

  • 创建语法:

    PUT index
    {
        "settings": { ... any settings ... },
        "mappings": {
            "type1": { ... any mappings ... },
            "type2": { ... any mappings ... },
            ...
        }
    }
    
  • 创建示例:

    PUT address
    {
        "settings": {
            "number_of_shards": 1,		// 默认分片数为5 (复制时请去掉注释)
        	"number_of_replicas": 0		    // 默认副本数为1
        },
        "mappings": {
        	"province": {
            	"properties": {
            		"description": {
            	  		"type": "text"
            		}
          		}
        	}
      	}
    }
    
  • 响应信息:

    {
      "acknowledged": true,
      "shards_acknowledged": true,
      "index": "address"
    }
    

2 查看索引

  • 查看示例:

    GET address
    
    // 也可同时查看多个索引, 类似于删除操作: 
    GET *
    GET _all
    GET index_*
    GET address,shop
    
    // 也可指定返回某些指定项的结果:
    GET address/_settings,_mappings
    
  • 弃用提示: 查看索引时, 若通过","分隔要返回的结果, Elasticsearch将抛出如下错误:

    #! Deprecation: Requesting comma-separated features is deprecated and will be removed in 6.0+, retrieve all features instead.

    意为: Elasticsearch不推荐使用逗号分隔功能, 将在6.0+中删除. 建议不要使用",", 而是直接检索全部数据, 或检索某一项的结果.

  • 响应信息:

    {
      "address": {
        "aliases": {},
        "mappings": {
          "province": {
            "properties": {
              "description": {
                "type": "text"
              }
            }
          }
        },
        "settings": {
          "index": {
            "creation_date": "1542108754899",
            "number_of_shards": "1",
            "number_of_replicas": "0",
            "uuid": "MMpLNHzZR8K1k48rJplWVw",
            "version": {
              "created": "5061099"
            },
            "provided_name": "address"
          }
        }
      }
    }
    
  • 索引不存在异常:

    扫描二维码关注公众号,回复: 4119810 查看本文章

    如果要查看的索引不存在, 将抛出如下异常信息:

    {
      "error": {
        "root_cause": [
          {
            "type": "index_not_found_exception",
            "reason": "no such index",
            "resource.type": "index_or_alias",
            "resource.id": "addre",
            "index_uuid": "_na_",
            "index": "addre"
          }
        ],
        "type": "index_not_found_exception",
        "reason": "no such index",
        "resource.type": "index_or_alias",
        "resource.id": "addre",
        "index_uuid": "_na_",
        "index": "addre"
      },
      "status": 404
    }
    

3 修改索引

  • 修改示例:

    PUT address/_settings
    {
        "number_of_replicas": 1		// number_of_shards参数只能在创建索引时设置, 不支持修改
    }
    

4 删除索引

删除索引需要指明索引名称、别名或通配符.

Elasticsearch支持同时删除多个索引, 或使用 _all通配符* 删除全部索引.

  • 删除示例:

    DELETE address			// 删除指定索引
    DELETE index1,index2		// 删除多个索引
    DELETE index_*			// 按通配符删除以'index_'开头的索引
    DELETE _all				// 删除全部索引
    
  • 为避免误删除全部索引, 可在配置文件elasticsearch.yml中作如下配置:

    action.destructive_requires_name: true  # 要求操作索引时必须指定索引的名称
    

5 打开/关闭索引

只能关闭一个打开的索引;

只能打开一个已经关闭的索引;

关闭的索引只能显示索引元数据, 不能进行读写操作.

  • 操作示例:

    POST address/_close
    POST address/_open
    
    // 可以使用_all打开或关闭全部索引, 也可使用通配符(*)配合操作
    
  • 注意事项:

    • 修改已经关闭了的索引, 将抛出如下错误:

      {
        "error": {
          "root_cause": [
            {
              "type": "illegal_argument_exception",
              "reason": "Can't update [index.number_of_replicas] on closed indices [[address/MMpLNHzZR8K1k48rJplWVw]] - can leave index in an unopenable state"
            }
          ],
          "type": "illegal_argument_exception",
          "reason": "Can't update [index.number_of_replicas] on closed indices [[address/MMpLNHzZR8K1k48rJplWVw]] - can leave index in an unopenable state"
        },
        "status": 400
      }
      
    • 使用_all或通配符标注索引的操作, 也会受到[删除索引]中 action.destructive_requires_name=true 的限制.

    • 关闭的索引会继续占用磁盘空间, 却又不能使用 —— 造成磁盘空间的浪费.

    • 可以在配置文件中禁止使用关闭索引的功能: settingscluster.indices.close.enable=true, 默认为true, 即开启

版权声明

作者: ma_shoufeng(马瘦风)

出处: CSDN 马瘦风的博客

您的支持是对博主的极大鼓励, 感谢您的阅读.

本文版权归博主所有, 欢迎转载, 但未经博主同意必须保留此段声明, 且在文章页面明显位置给出原文链接, 否则博主保留追究相关人员法律责任的权利.

猜你喜欢

转载自blog.csdn.net/ma_shou_feng/article/details/84109246