《Elasticsearch技术解析与实战》Chapter 2.1 Elasticsearch索引增删改查

栏目: 后端 · 发布时间: 6年前

内容简介:创建自定义字段类型的索引:更新分词器尝试修改索引主分片数:
PUT /lujiahao123
{
  "acknowledged": true,
  "shards_acknowledged": true
}
索引默认的主分片数量是5,每个主分片的副本数量是1。
复制代码

创建自定义字段类型的索引:

PUT /order
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 2
  },
  "mappings": {
    "carpoolType":{
      "properties": {
        "orderNo":{
          "type": "string",
          "index": "not_analyzed"
        }
      }
    }
  }
}
#! Deprecation: The [string] field is deprecated, please use [text] or [keyword] instead on [orderNo]
{
  "acknowledged": true,
  "shards_acknowledged": true
}
ES5.0版本中string类型被拆分为text和keyword类型: https://www.elastic.co/blog/strings-are-dead-long-live-strings
复制代码

2. 修改索引

PUT /order/_settings
{
  "number_of_replicas": 2
}
复制代码

更新分词器

POST /order/_close
PUT /order/_settings
{
  "analysis": {
    "analyzer": {
      "content":{
        "type":"customer",
        "tokenizer":"whitespace"
      }
    }
  }
}
POST /order/_open
添加分析器之前必须先关闭索引,添加之后再打开索引。
复制代码

尝试修改索引主分片数:

PUT /order/_settings
{
  "number_of_shards": 2
}
复制代码

错误提示:

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Can't update non dynamic settings [[index.number_of_shards]] for open indices [[order/2cOJ6Ga7THCyW10idoPPig]]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "Can't update non dynamic settings [[index.number_of_shards]] for open indices [[order/2cOJ6Ga7THCyW10idoPPig]]"
  },
  "status": 400
}

主分片是无法修改的,主分片个数一旦建立就无法修改,但是看错误原因里面说因为这个是open的索引,那是不是关闭的索引就可以修改主分片个数了呢?这个我们后面再解答。
复制代码

3. 删除索引

删除单个索引:
DELETE /order
删除索引需要指定索引名称,别名或者通配符
复制代码
删除多个索引:
DELETE order,order1,order2
DELETE order*
复制代码
删除全部索引:
DELETE _all
DELETE *
复制代码

使用_all 和通配符删除全部索引 为了防止误删除,设置config/elasticsearch.yml属性action.destructive_requires_name=true,以禁用通配符或_all删除索引。

4. 查询索引

GET /order
{
  "order": {
    "aliases": {},
    "mappings": {
      "carpoolType": {
        "properties": {
          "orderNo": {
            "type": "keyword"
          }
        }
      }
    },
    "settings": {
      "index": {
        "creation_date": "1555054886098",
        "number_of_shards": "3",
        "number_of_replicas": "2",
        "uuid": "uSh9K26CS_q1uZKaos7NRQ",
        "version": {
          "created": "5050099"
        },
        "provided_name": "order"
      }
    }
  }
}
复制代码

自定义返回结果的属性:

GET /order/_settings,_aliases
{
  "order": {
    "settings": {
      "index": {
        "creation_date": "1555054886098",
        "number_of_shards": "3",
        "number_of_replicas": "2",
        "uuid": "uSh9K26CS_q1uZKaos7NRQ",
        "version": {
          "created": "5050099"
        },
        "provided_name": "order"
      }
    },
    "aliases": {}
  }
}
复制代码

5. 打开/关闭索引

索引关闭只能显示索引元数据信息,不能够进行读写。

POST /order/_close
POST /order/_open
health status index   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   .kibana 8n5wnGEjRJ-aVa54l_jTjA   1   1          1            0      3.1kb          3.1kb
yellow open   order   uSh9K26CS_q1uZKaos7NRQ   3   2          0            0       486b           486b
复制代码

可以同时打开或关闭多个索引。如果指向不存在的索引会抛出错误,可通过配置ignore_unavailable=true 关闭异常提示。 禁止使用关闭索引功能,配置settingscluster.indices.close.enable为false,默认值是ture。

6. 关闭索引后尝试修改主分片个数

PUT /order/_settings
{
  "number_of_shards": 2
}
{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "final order setting [index.number_of_shards], not updateable"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "final order setting [index.number_of_shards], not updateable"
  },
  "status": 400
}
复制代码

解答了之前的问题,索引一旦建立,主分片数量不可改变。

Tips

本文同步发表在公众号,欢迎大家关注!:grin: 后续笔记欢迎关注获取第一时间更新!

《Elasticsearch技术解析与实战》Chapter 2.1 Elasticsearch索引增删改查

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web Development Recipes

Web Development Recipes

Brian P. Hogan、Chris Warren、Mike Weber、Chris Johnson、Aaron Godin / Pragmatic Bookshelf / 2012-1-22 / USD 35.00

You'll see a full spectrum of cutting-edge web development techniques, from UI and eye candy recipes to solutions for data analysis, testing, and web hosting. Make buttons and content stand out with s......一起来看看 《Web Development Recipes》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

SHA 加密
SHA 加密

SHA 加密工具