一、创建索引index
创建名为newindex的索引(索引名必须为小写): 提交请求newindex
PUT
,创建成功出现下面结果:
{
"acknowledged": true
}
查看新建的索引:newindex
GET
,结果如下:
{
"newindex": {
"aliases": { },
"mappings": { },
"settings": {
"index": {
"creation_date": "1491980889980",
"uuid": "DvFfvO0JQ46Bbb0u64rgsw",
"number_of_replicas": "1",
"number_of_shards": "5",
"version": {
"created": "2040499"
}
}
},
"warmers": { }
}
}
索引的两个重要配置: number_of_replicas 副本数量(默认1)
number_of_shards 分片数量(默认5)
创建索引时自定义副本和分片数量:
newindex1
PUT
{
"settings": {
"index": {
"number_of_replicas": "3",
"number_of_shards": "3"
}
}
}
二、创建类型type
在索引newindex中创建名为newtype的类型:
newindex/newtype/_mapping
PUT
{
"newtype": {}
}
再查看索引newindex,结果如下:
{
"newindex": {
"aliases": { },
"mappings": {
"newtype": { }
},
"settings": {
"index": {
"creation_date": "1491980889980",
"uuid": "DvFfvO0JQ46Bbb0u64rgsw",
"number_of_replicas": "1",
"number_of_shards": "5",
"version": {
"created": "2040499"
}
}
},
"warmers": { }
}
}
发现"mappings"(映射)中多了newtype。
每个类型(type)拥有自己的映射(mapping)或者模式定义(schema definition),一个映射定义了字段,每个字段的数据类型,以及字段被Elasticsearch处理的方式。
新增一个定义映射的类型newtype1:
newindex/newtype1/_mapping
PUT
{
"newtype1": {
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "string",
"index": "not_analyzed"
},
"birth": {
"type": "date"
}
}
}
}
查看新创建的newtype1的映射:
newindex/_mapping/newtype1
GET
{
"newindex": {
"mappings": {
"newtype1": {
"properties": {
"birth": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
},
"id": {
"type": "long"
},
"name": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
三、索引文档
向es中添加一个文档:
newindex/newtype1/1
PUT
{
"id": 1,
"name": "张三",
"birth": "2017-01-02"
}
返回结果如下,添加成功:
{
"_index": "newindex",
"_type": "newtype1",
"_id": "1",
"_version": 1,
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"created": true
}
查看文档:
newindex/newtype1/1
GET
{
"_index": "newindex",
"_type": "newtype1",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"id": 1,
"name": "张三",
"birth": "2017-01-02"
}
}
创建索引、类型,添加文档可以通过一条指令完成:
myindex/mytype/1 PUT
{
"id": 1,
"name": "张三",
"birth": "2017-01-02"
}
Elasticsearch检测索引、类型不存在时会自动创建,自动创建索引由配置项action.auto_create_index
控制,默认开启为true
四、动态映射
如果上面执行操作前,ES中没有myindex这个索引,那么默认会直接创建这个索引;并且type字段也会自动创建。也就是说,ES并不需要像传统的数据库事先定义表的结构。每个索引中的类型都有一个mapping映射,这个映射是动态生成的,因此当增加新的字段时,会自动增加mapping的设置。
通过在配置文件中设置
action.auto_create_index
为false
,可以关闭自动创建index这个功能。 自动创建索引功能,也可以设置黑名单或者白名单,比如: 设置action.auto_create_index
为+aaa*,-bbb*
,'+'号意味着允许创建aaa开头的索引,'-'号意味着不允许创建bbb开头的索引。
通过设置dynamic
控制动态映射,可选值:
true
:自动添加字段(默认)false
:忽略字段strict
:当遇到未知字段时抛出异常
向索引myindex中添加类型strict_type:
myindex/strict_type/_mapping
PUT
{
"strict_type": {
"dynamic": "strict",
"properties": {
"title": {
"type": "string"
},
"stash": {
"type": "object",
"dynamic": true
}
}
}
}
将 strict_type 的动态映射设置为了strict
,意味着如果向 myindex/strict_type 中添加文档时,如果文档中含有除 title 和 stash 的其他字段时,会抛出异常。测试一下:
myindex/strict_type/1
PUT
{
"title": "加一个新字段content",
"content": "未知字段会抛出异常"
}
结果如下:
{
"error": {
"root_cause": [
{
"type": "strict_dynamic_mapping_exception",
"reason": "mapping set to strict, dynamic introduction of [content] within [strict_type] is not allowed"
}
],
"type": "strict_dynamic_mapping_exception",
"reason": "mapping set to strict, dynamic introduction of [content] within [strict_type] is not allowed"
},
"status": 400
}
但是我们在字段 stash 中将dynamic
设置为了true
,意味着在 stash 对象内可以新增字段,测试一下:
myindex/strict_type/2
PUT
{
"title": "在stash中加一个新字段content",
"stash": {
"content": "这下可以加进来了"
}
}
结果添加成功:
{
"_index": "myindex",
"_type": "strict_type",
"_id": "2",
"_version": 1,
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"created": true
}
五、自定义动态映射
使用dynamic_templates
,可以完全控制新字段的映射,可以通过设置字段名或数据类型等应用一个完全不同的映射。
向索引 myindex 中添加一个自定义动态映射的类型 my_dynamic_type :
myindex/my_dynamic_type/_mapping
PUT
{
"my_dynamic_type": {
"dynamic_templates": [
{
"ord": {
"match": "*_ord",
"match_mapping_type": "string",
"mapping": {
"type": "string",
"index": "not_analyzed"
}
}
},
{
"date": {
"match": "date_*",
"match_mapping_type": "string",
"mapping": {
"type": "date",
"ignore_malformed": true
}
}
}
]
}
}
my_dynamic_type
的映射使用了动态模板,在动态模板dynamic_templates
中:
定义了一个名为
ord
的模板,这个模板匹配所有以_ord
结尾("match": "*_ord"
)字符串类型("match_mapping_type": "string"
)的字段,将此类字段类型映射为字符串并索引的时候不做解析("mapping": {"type": "string","index": "not_analyzed"}
)定义了一个名为
date
的模板,这个模板匹配所有以date_
开头("match": "date_*"
)字符串类型("match_mapping_type": "string"
)的字段,将此类字段类型映射为日期类型,并且忽略不能格式化为日期的值("mapping": {"type": "date","ignore_malformed": true}
)
看下效果:
myindex/my_dynamic_type/1
PUT
{
"id": 1,
"name": "张三",
"date_birth": "20170923",
"name_ord": "张三"
}
插入文档后查看 my_dynamic_type 的映射:
myindex/_mappping/my_dynamic_type
GET
{
"myindex": {
"mappings": {
"my_dynamic_type": {
"dynamic_templates": [ { "ord": { "mapping": { "type": "string","index": "not_analyzed"},"match": "*_ord","match_mapping_type": "string"}},{ "date": { "mapping": { "type": "date","ignore_malformed": true},"match": "date_*","match_mapping_type": "string"}}],
"properties": {
"date_birth": {
"type": "date",
"ignore_malformed": true,
"format": "strict_date_optional_time||epoch_millis"
},
"id": {
"type": "long"
},
"name": {
"type": "string"
},
"name_ord": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
可以看到properties
中已经将name_ord
字段映射为字符串类型,并不做解析;date_birth
也映射为了日期类型,再插入一个文档:
myindex/my_dynamic_type/2
PUT
{
"id": 2,
"name": "李四",
"date_birth": "未知",
"name_ord": "李四"
}
插入成功,date_birth
字段也可以接受不能被格式化为日期的值。
关于动态映射更多配置语法请参考官方文档:
https://www.elastic.co/guide/en/elasticsearch/reference/2.4/dynamic-templates.html
https://www.elastic.co/guide/en/elasticsearch/reference/2.4/mapping.html