简介
索引是具有相同结构的文档集合. 在 Elasticsearch 中索引是个非常重要的内容, 对 Elasticsearch 的大部分操作都是基于索引来完成的. 同时索引可以类比关系型数据库 MySQL 中的数据库 database
创建索引
创建索引的时候可以通过修改 number of shards 和 number of replicas 参数的数量来修改分片和副本的数量. 在默认的情况下分片的数量是 5 个, 副本的数量是 1 个
例如创建三个主分片两个副本分片的索引
- PUT /secisland
- {
- "settings": {
- "index":{"number_of_shards":3,"number_of_replicas":2}
- }
- }
- # 参数可以简写为
- {"settings": {"number_of_shards":3,"number_of_replicas":2}}
修改索引
- PUT /test_index/_settings
- {
- "number_of_replicas":1
- }
对于任何 Elasticsearch 文档而言, 一个文档会包括一个或者多个字段, 任何字段都要有自己的数据类型, 例如 string,integer,date 等. Elasticsearch 中是通过映射来进行字段和数据类型对应的. 在默认的情况下 Elasticsearch 会自动识别字段的数据类型. 同时 Elasticsearch 提供了 mappings 参数可以显式地进行映射.
- PUT /test_index1
- {
- "settings": {
- "number_of_shards":3,"number_of_replicas":2
- },
- "mappings": {
- "secilog": {
- "properties": {
- "logType": {
- "type": "string",
- "index": "not_analyzed"
- }
- }
- }
- }
- }
删除索引
DELETE /test_index
获取索引
- GET /test_index1
- {
- "test_index1": {
- "aliases": {},
- "mappings": {
- "secilog": {
- "properties": {
- "logType": {
- "type": "keyword"
- }
- }
- }
- },
- "settings": {
- "index": {
- "creation_date": "1544886365769",
- "number_of_shards": "3",
- "number_of_replicas": "2",
- "uuid": "Iz8evLbCQ1CS85owebKsgQ",
- "version": {
- "created": "5020299"
- },
- "provided_name": "test_index1"
- }
- }
- }
- }
过滤查询
索引的 settings 和 mappings 属性. 可配置的属性包括 settings, mappingswarmers 和 aliases.
GET /test_index1/_settings,_mapping, 如果索引不存在则返回 404
- {
- "test_index1": {
- "settings": {
- "index": {
- "creation_date": "1544886365769",
- "number_of_shards": "3",
- "number_of_replicas": "2",
- "uuid": "Iz8evLbCQ1CS85owEbKsgQ",
- "version": {
- "created": "5020299"
- },
- "provided_name": "test_index1"
- }
- },
- "mappings": {
- "secilog": {
- "properties": {
- "logType": {
- "type": "keyword"
- }
- }
- }
- }
- }
- }
关闭索引
映射管理
增加映射
- PUT /test_index2
- {
- "mappings": {
- "log": {
- "properties": {
- "message": {
- "type": "string"
- }
- }
- }
- }
- # 以上接口添加索引名为 test_index2, 文档类型为 log, 其中包含字段 message, 字段类型是字符串:
- PUT test_index2/_mapping/user
- {
- "properties": {
- "name":{"type": "string"}
- }
- }
- # 添加文档类型为 user, 包含字段 name, 字段类型是字符串.
- # 设置多个索引映射时
- PUT /{index}/_mapping/{type}
{index}可以有多种方式, 逗号分隔: 比如 testl,test2,test3.
all 表示所有索引 3 通配符 * 表示所有. test * 表示以 test 开头.
{type}需要添加或更新的文档类型.
{body}需要添加的字段或字段类型
获取映射
系统同时支持获取多个索引和类型的语法.
获取文档映射接口一次可以获取多个索引或文档映射类型. 该接口通常是如下格式:
host:port/{index}/ mapping/{type}, {index}和 {type} 可以接受逗号 (,) 分隔符, 也可以使用
- GET test_index2/_mapping/{
- string
- }
- GET /test_index2/_mapping/log,user
判断存在
健查索引或文档类型是否存在 存在返回 200 不存在返回 404
HEAD test_index2/secilog
索引别名
Elasticsearch 可以对一个或多个索引指定别名, 通过别名可以查询一个或多个索引内容, 在内部 Elasticsearch 会把别名映射在索引上, 别名不可以和索引名其他索引别名重复
- POST /_aliases
- {
- "actions":{"add":{"index":"test_index1","aliases":"othername1"}}
- }
- # 给 test_index 增加索引别名 othername1
修改别名
别名没有修改的语法, 当需要修改时, 先删除 , 在添加
- POST /_aliases
- {
- "actions":{"remove":{"index":"test_index1","aliases":"othername2"}}
- }
删除别名
DELETE http://{host}:{port}/{index}/_alias/{name}
查询别名
GET http://{host}:{port}/{index}/_alias/{name}
来源: http://www.bubuko.com/infodetail-2962470.html