Elasticsearch 5.4 Indices(索引) API


前言

声明:本博客根据ELasticsearch官网文档翻译整理,转载请注明出处:http://blog.csdn.net/napoay


索引API可以用于管理单个索引、索引设置、别名、映射和索引模板。

一、索引管理

1.1 创建索引

创建索引

PUT twitter
 
 
  • 1

默认分片为5,副本为1.

创建索引并指定分片数和副本数:

PUT twitter
{
    "settings" : {
        "index" : {
            "number_of_shards" : 3, 
            "number_of_replicas" : 2 
        }
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

或者简写为:

PUT twitter
{
    "settings" : {
        "number_of_shards" : 3,
        "number_of_replicas" : 2
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

创建索引并指定mapping:

PUT test
{
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "type1" : {
            "properties" : {
                "field1" : { "type" : "text" }
            }
        }
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

1.2 删除索引

DELETE /twitter
 
 
  • 1

1.3 查看索引信息

查看所有的settings、别名、mapping,命令:

GET /twitter
 
 
  • 1

添加参数过滤信息:

GET twitter/_settings,_mappings
 
 
  • 1

1.4 索引是否存在

如果想知道集群中是否存在某个索引,可以使用以下命令:

HEAD twitter
 
 
  • 1

如果存在,返回状态码200:

200 - OK
 
 
  • 1

如果不存在,返回状态码404:

404 - Not Found
 
 
  • 1

1.5 关闭/打开索引

对于不使用的索引,关闭索引可以节省开销,但是索引关闭以后读写操作是无法进行的。

打开索引:

POST /my_index/_close
 
 
  • 1

关闭索引:

POST /my_index/_open
 
 
  • 1

可以同时关闭多个索引,如果其中有索引不存在会报异常,可以使用ignore_unavailable=true参数忽略不存在索引。

1.6 索引收缩

shrink index AP可以把一个索引变成一个更少分片的索引,但是收缩后的分片数必须是原始分片数的因子(因子就是所有可以整除这个数的数,不包括这个数自身),比如有8个分片的索引可以收缩为4、2、1,有15个分片的索引可以收缩为5、3、1,如果分片数为素数(7、11等),那么只能收缩为1个分片。 收缩索引之前,索引中的每个分片都要在同一个节点上。

收缩索引的完成过程:

  • 首先,创建了一个新的目标索引,设置与源索引相同,但新索引的分片数量较少。
  • 然后把源索引的段到硬链接到目标索引。(如果文件系统不支持硬链接,那么所有段都被复制到新索引中,这是一个耗费更多时间的过程。)
  • 最后,新的索引恢复使用,好像它是一个刚刚重新开放的封闭索引。

搜索之前,使索引为只读状态并使分片重新分配到同一个节点:

PUT /my_source_index/_settings
{
  "settings": {
    "index.routing.allocation.require._name": "shrink_node_name", 
    "index.blocks.write": true 
  }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

设置目标索引名和分片数,别名可选:

POST my_source_index/_shrink/my_target_index
{
  "settings": {
    "index.number_of_replicas": 1,
    "index.number_of_shards": 1, 
    "index.codec": "best_compression" 
  },
  "aliases": {
    "my_search_indices": {}
  }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

1.7 翻滚索引

二、mapping管理

2.1 设置mapping

put mapping可以给一个已存在的索引增加type的mapping,也可以给一个存在的type增加字段的mapping。

PUT twitter 
{
  "mappings": {
    "tweet": {
      "properties": {
        "message": {
          "type": "text"
        }
      }
    }
  }
}

PUT twitter/_mapping/user 
{
  "properties": {
    "name": {
      "type": "text"
    }
  }
}

PUT twitter/_mapping/tweet 
{
  "properties": {
    "user_name": {
      "type": "text"
    }
  }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

一般情况下字段的mapping设置是不可以更新的,有几个特例除外:

  • properties嵌套属性可以新增
  • ignore_above 参数的值可以更新
PUT my_index 
{
  "mappings": {
    "user": {
      "properties": {
        "name": {
          "properties": {
            "first": {
              "type": "text"
            }
          }
        },
        "user_id": {
          "type": "keyword"
        }
      }
    }
  }
}

PUT my_index/_mapping/user
{
  "properties": {
    "name": {
      "properties": {
        "last": { 
          "type": "text"
        }
      }
    },
    "user_id": {
      "type": "keyword",
      "ignore_above": 100 
    }
  }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

2.2 查看mapping

查看一个索引的mapping:

GET /twitter/_mapping
 
 
  • 1

查看一个索引的一个type的mapping:

GET /twitter/_mapping/tweet
 
 
  • 1

查看所有索引的mapping:

GET /_mapping
 
 
  • 1

或者:

GET /_all/_mapping
 
 
  • 1

2.3 获取字段mapping

get field mapping api可以查看索引的一个或多个字段的mapping,设置创建一个索引做测试:

PUT publications
{
    "mappings": {
        "article": {
            "properties": {
                "id": { "type": "text" },
                "title":  { "type": "text"},
                "abstract": { "type": "text"},
                "author": {
                    "properties": {
                        "id": { "type": "text" },
                        "name": { "type": "text" }
                    }
                }
            }
        }
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
GET publications/_mapping/article/field/title
GET publications/_mapping/article/field/id
GET publications/_mapping/article/field/author.id
 
 
  • 1
  • 2
  • 3

2.4 类型是否存在

查看索引是否存在某个type:

HEAD twitter/_mapping/tweet
 
 
  • 1

返回值为200说明存在,404说明不存在。

三、别名管理

3. 1 索引别名设置

可以给一个或多个索引设置别名,但是别名不能和已有索引名称相同。 
给索引名为test1的索引设置别名为alias1:

POST /_aliases
{
    "actions" : [
        { "add" : { "index" : "test1", "alias" : "alias1" } }
    ]
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

移除别名:

POST /_aliases
{
    "actions" : [
        { "remove" : { "index" : "test1", "alias" : "alias1" } }
    ]
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

更新别名的映射关系就是先移除再添加:

POST /_aliases
{
    "actions" : [
        { "remove" : { "index" : "test1", "alias" : "alias1" } },
        { "add" : { "index" : "test2", "alias" : "alias1" } }
    ]
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

也可以同时给多个索引设置同一个别名:

POST /_aliases
{
    "actions" : [
        { "add" : { "index" : "test1", "alias" : "alias1" } },
        { "add" : { "index" : "test2", "alias" : "alias1" } }
    ]
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

也可以使用通配符,一下所有以test开头的索引都设置别名为all_test_indices:

POST /_aliases
{
    "actions" : [
        { "add" : { "index" : "test*", "alias" : "all_test_indices" } }
    ]
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

四、索引配置

4.1 获取索引设置

查看索引的settings:

GET /twitter/_settings
 
 
  • 1

查看多个索引的settings:

GET /twitter,kimchy/_settings

GET /_all/_settings

GET /log_2013_*/_settings
 
 
  • 1
  • 2
  • 3
  • 4
  • 5

4.2 更新索引设置

修改副本:

PUT /twitter/_settings
{
    "index" : {
        "number_of_replicas" : 2
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

修改settings用于提高Bulk的导入性能,bulk之前设置刷新时间为-1,也就是bulk导入期间不再刷新:

PUT /twitter/_settings
{
    "index" : {
        "refresh_interval" : "-1"
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

bulk导入之后恢复刷新时间并强制段合并

PUT /twitter/_settings
{
    "index" : {
        "refresh_interval" : "1s"
    }
}
POST /twitter/_forcemerge?max_num_segments=5
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

4.3 分析器

查看分词标准分词结果:

GET _analyze
{
  "analyzer" : "standard",
  "text" : "this is a test"
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5

查看IK分词结果:

GET _analyze
{
  "analyzer" : "ik_smart",
  "text" : "北京今天局地高温"
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5

4.4 索引模板

索引模板可以自动匹配新创建的索引。

PUT _template/template_1
{
  "template": "te*",
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "type1": {
      "_source": {
        "enabled": false
      },
      "properties": {
        "host_name": {
          "type": "keyword"
        },
        "created_at": {
          "type": "date",
          "format": "EEE MMM dd HH:mm:ss Z YYYY"
        }
      }
    }
  }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

删除索引模板:

DELETE /_template/template_1
 
 
  • 1

查看索引模板:

GET /_template
 
 
  • 1

查看一个或多个:

GET /_template/template_1
GET /_template/template_1,template_2
 
 
  • 1
  • 2

五、监控管理

5.1 索引统计信息

GET /_stats
GET /index1,index2/_stats
 
 
  • 1
  • 2

以上命令返回的索引的相关信息非常多,可以通过参数过滤https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-stats.html

5.2 索引段

segment是比Lucene索引更小的单位,通过segment可以获取更多的关于分片和索引的信息。

查看索引的段信息:

GET test/_segments
 
 
  • 1

返回结果:

{
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "indices": {
    "test": {
      "shards": {
        "0": [
          {
            "routing": { "state": "STARTED", "primary": true, "node": "3dQd1RRVTMiKdTckM68nPQ" },
            "num_committed_segments": 0,
            "num_search_segments": 0,
            "segments": {} }
        ],
        "1": [
          {
            "routing": { "state": "STARTED", "primary": true, "node": "3dQd1RRVTMiKdTckM68nPQ" },
            "num_committed_segments": 0,
            "num_search_segments": 0,
            "segments": {} }
        ],
        "2": [
          {
            "routing": { "state": "STARTED", "primary": true, "node": "3dQd1RRVTMiKdTckM68nPQ" },
            "num_committed_segments": 0,
            "num_search_segments": 0,
            "segments": {} }
        ],
        "3": [
          {
            "routing": { "state": "STARTED", "primary": true, "node": "3dQd1RRVTMiKdTckM68nPQ" },
            "num_committed_segments": 1,
            "num_search_segments": 1,
            "segments": { "_1": { "generation": 1, "num_docs": 1, "deleted_docs": 0, "size_in_bytes": 3727, "memory_in_bytes": 2588, "committed": true, "search": true, "version": "6.5.0", "compound": true } } }
        ],
        "4": [
          {
            "routing": { "state": "STARTED", "primary": true, "node": "3dQd1RRVTMiKdTckM68nPQ" },
            "num_committed_segments": 1,
            "num_search_segments": 1,
            "segments": { "_0": { "generation": 0, "num_docs": 1, "deleted_docs": 0, "size_in_bytes": 3206, "memory_in_bytes": 2042, "committed": true, "search": true, "version": "6.5.0", "compound": true } } }
        ]
      }
    }
  }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97

统计索引占段内存:

curl -s "http://localhost:9200/_cat/segments/test?v&h=shard,segment,size,size.memory" |awk '{sum += $NF} END {print sum}'
 
 
  • 1

5.3 索引恢复

GET index1,index2/_recovery?human
GET _recovery?human&detailed=true
 
 
  • 1
  • 2

5.4 索引分片存储

六、状态管理

6.1 清除缓存

POST /twitter/_cache/clear

POST /kimchy,elasticsearch/_cache/clear

POST /_cache/clear

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

6.2 刷新

POST /twitter/_refresh

POST /kimchy,elasticsearch/_refresh

POST /_refresh

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

6.3 flush

POST twitter/_flush

POST kimchy,elasticsearch/_flush

POST _flush
 
 
  • 1
  • 2
  • 3
  • 4
  • 5

6.4 强制段合并(force merge)

POST /twitter/_forcemerge

POST /kimchy,elasticsearch/_forcemerge

POST /_forcemerge
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值