elasticsearch索引
映射
映射是用于进行字段类型确认的,将每个字段匹配为一种确定的数据类型
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
| GET video/_mapping/default
{ "video": { "mappings": { "default": { "properties": { "age": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "doc_id": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "doc_title": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "duration": { "type": "long" }, "keywords": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "number": { "type": "long" } } } } } }
|
在向索引中添加数据的时候,如果不存在该索引,会根据所添加的数据来进行创建索引,所生成的mapping也是根据添加的数据来进行自动判断的,当然一般这种自动生成的可能并不适用,我们也经常手动创建索引并指定映射
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
| PUT testmapping { "mappings": { "default": { "properties": { "age": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "doc_id": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "doc_title": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "duration": { "type": "long" }, "keywords": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "number": { "type": "long" } } } } }
|
对于是否自动创建索引可以在配置文件中进行配置
action.auto_create_index: false
对于已经创建好的索引,有时候也会新增字段
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| PUT testmapping/_mapping/default { "properties": { "type": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } }
|
设置
索引除了映射之外还有设置
1 2 3 4 5 6
| PUT index1 { "settings": { "number_of_shards": 1 } }
|
有些设置只能在创建索引的时候设置,而有些可以进行动态的修改
1 2 3 4 5 6
| PUT index1/_settings { "settings": { "number_of_replicas": 1 } }
|
对于不允许动态修改的设置调用会报错
打开/关闭索引
1 2 3 4 5
| # 关闭索引 POST testmapping/_close
# 打开索引 POST testmapping/_open
|
可以设置cluster.indices.close.enable为false来禁止使用关闭功能
设置别名
可以为索引设置别名,通过别名可以查询到一个或多个索引的内容,elasticsearch会自动把别名映射到相应的索引上,别名不能重复,也不能与索引名重复
添加别名
1 2 3 4 5 6 7 8 9 10 11
| POST _aliases { "actions": [ { "add": { "index": "testmapping", "alias": "tm" } } ] }
|
删除别名
1 2 3 4 5 6 7 8 9 10 11
| POST _aliases { "actions": [ { "remove": { "index": "testmapping", "alias": "tm" } } ] }
|