索引管理

索引管理的引入

在es中增加文档时,如下面的语句会动态创建cutomer的index

PUT /customer/_doc/1
{
  "name": "John Doe"
}

而这个index实际上已经自动创建了它里面的字段(name)的类型,我看下这个索引的mapping

{
  "customer": {
    "aliases": {},
    "mappings": {
      "properties": {
        "name": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    }
  }
}

那么如果我们需要对这个建立索引的过程做更多的控制:比如想要确保这个索引有数量适中的主分片,并且在我们索引任何数据之前,分析器和映射已经被建立好。那么就会引入两点:第一个禁止自动创建索引,第二个是手动创建索引。

禁止自动创建索引

可以通过在config/elaticsearch.yml的每个节点增加如下配置

action.auto_create_index: false

手动创建索引

PUT /my_index
{
    "settings": { ... any settings ... },
    "mappings": {
        "properties": { ... any properties ... }
    }
}
  • settings用来设置分片、副本等信息
  • mapping字段映射、类型等

索引管理

创建索引

PUT /test-index-users
{
  "settings": {
		"number_of_shards": 1,
		"number_of_replicas": 1
	},
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "age": {
        "type": "long"
      },
      "remarks": {
        "type": "text"
      }
    }
  }
}

修改索引

PUT /test-index-users/_settings
{
  "settings": {
    "number_of_replicas": 0
  }
}

打开或关闭索引

一旦索引被关闭,那么这个索引只能显示元数据信息, 不能够进行读写操作

POST /test-index/_close
POST /test-index/_open

删除索引

DELETE /test-index

查看索引

GET /test-index/_mapping