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

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
        }
    }
}

上述 curl 命令创建了一个 twitter 索引,可以通过YAML或者JSON 设定其分片和副本,下述创建了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 部分。

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 表示是否在超时之前成功创建运行必要的分片。 注意 acknowledgedshards_acknowledged 可能返回 false, 但是索引创建成功。这些值只是表明是否操作在超时之前完成。如果acknowledgedfalse, 表示我们对集群更新 新创建的索引 操作超时,但它可能很快将被创建。如果shards_acknowledgedfalse,表示我们必要的分片启动 操作超时(默认只为主分片),即使集群成功更新新创建的索引(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 及其可能的值可以在这里 找到。

Delete Index /删除索引

删除索引 API操作:

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

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

删除索引API也可以应用于多个索引,通过使用逗号分隔,或者通过 (小心!)_all 或者 * 删除所有index

可以通过设置 action.destructive_requires_nametrue 来避免通配符或者 _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.

Last updated