# 创建/删除/获取->索引

## Create Index /创建索引

创建索引（index API）允许实例化一个索引。Elasticsearch提供支持多种索引,包括跨多个索引执行操作。

### Index Settings/索引设置

每个索引创建的时候可以包含与之关联的特定设置。

```
curl -XPUT 'localhost:9200/twitter?pretty' -d'
{
    "settings" : {
        "index" : {
            "number_of_shards" : 3,　　　# 1
            "number_of_replicas" : 2    # 2
        }
    }
}
```

| [![](https://www.elastic.co/guide/en/elasticsearch/reference/current/images/icons/callouts/1.png)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html#CO119-1) | 默认`number_of_shards` 为 5                      |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------- |
| [![](https://www.elastic.co/guide/en/elasticsearch/reference/current/images/icons/callouts/2.png)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html#CO119-2) | 默认`number_of_replicas` 为 1 (对于主 shard 只有一个副本) |

上述 curl 命令创建了一个 twitter 索引，可以通过[YAML](http://www.yaml.org/)或者[JSON](http://www.json.org/) 设定其分片和副本，下述创建了3个分片，2个副本的索引：

```
curl -XPUT 'localhost:9200/twitter?pretty' -d'
{
    "settings" : {
        "index" : {
            "number_of_shards" : 3,
            "number_of_replicas" : 2
        }
    }
}'
```

或者简化为：

```
curl -XPUT 'localhost:9200/twitter?pretty' -d'
{
    "settings" : {
        "number_of_shards" : 3,
        "number_of_replicas" : 2
    }
}'
```

> 注意：你不需要显式的在 `settings` 中指定 `index` 。

关于在创建索引时所有不同的索引级别设置,请看 [index modules](https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html) 部分。

### Mappings/映射

创建索引允许提供包含一个或多个映射的设置：

```
curl -XPUT 'localhost:9200/test?pretty' -d'
{
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "type1" : {
            "properties" : {
                "field1" : { "type" : "text" }
            }
        }
    }
}'
```

### Aliases/别名

创建索引可以设置其别名：

```
 curl -XPUT 'localhost:9200/test?pretty' -d'
{
    "aliases" : {
        "alias_1" : {},
        "alias_2" : {
            "filter" : {
                "term" : {"user" : "kimchy" }
            },
            "routing" : "kimchy"
        }
    }
}'
```

### Wait For Active Shards/等待激活分片数量

默认情况下,创建索引会在必要主分片已经创建成功并运行或请求超时后返回HTTP响应。创建索引的返回的响应:

```
{
    "acknowledged": true,
    "shards_acknowledged": true
}
```

`cknowledged` 表示是否在集群成功创建索引, 同时 `shards_acknowledged` 表示是否在超时之前成功创建运行必要的分片。 注意 `acknowledged` 和 `shards_acknowledged` 可能返回 `false`, 但是索引创建成功。这些值只是表明是否操作在超时之前完成。如果`acknowledged` 是 `false`, 表示我们对集群更新 新创建的索引 操作超时，但它可能很快将被创建。如果`shards_acknowledged` 是 `false`,表示我们必要的分片启动 操作超时（默认只为主分片），即使集群成功更新新创建的索引(i.e. `acknowledged=true`).

我们可以通过设置 `index.write.wait_for_active_shards` 来改变默认的设置（注意：更改此设置也会影响 `wait_for_active_shards` 值在所有随后的写操作）

```
curl -XPUT 'localhost:9200/test?pretty' -d'
{
    "settings": {
        "index.write.wait_for_active_shards": "2"
    }
}'
```

或者通过请求参数 `wait_for_active_shards`:

```
curl -XPUT 'localhost:9200/test?wait_for_active_shards=2&pretty'
```

有关 wait\_for\_active\_shards 及其可能的值可以在[这里](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#index-wait-for-active-shards) 找到。

## Delete Index /删除索引

删除索引 API操作：

```
curl -XDELETE 'localhost:9200/twitter?pretty'
```

上述命令删除了 `twitter 索引，删除必须指定索引,别名或通配符表达式。`

删除索引API也可以应用于多个索引，通过使用逗号分隔，或者通过 （小心！）`_all` 或者 `* 删除所有index`。

可以通过设置 `action.destructive_requires_name` 为 `true 来避免通配符或者 _all 操作。这个设置也可以通过集群api设置。`

## Get Index /获取索引

Get Index API允许获得一个或多个索引的信息

```
curl -XGET 'localhost:9200/twitter?pretty'
```

上面的例子获得 twitter 索引的信息。API必须指定一个索引,别名或通配符表达式。

API也可以应用于多个索引,通过使用 \_all 或者 \*作为索引名。

### Filtering index information /筛选需要信息

返回的信息可以筛选，通过在URL后指定一个逗号分隔的列表:

```
curl -XGET 'localhost:9200/twitter/_settings,_mappings?pretty'
```

上述命令返回 twitter 的 设置 和 映射。

可用设置为 `_settings`, `_mappings` 和 `_aliases`.
