Elasticsearch系列篇之创建索引
2017-04-05 14:17
543 查看
Elasticsearch的index类似于关系型数据库的库的概念,在保存数据前,要先创建索引
使用curl命令创建
创建一个新的索引,并设置分片数和副本数
创建一个twitter的索引, 设置为3个分片,2个副本,默认5个分片,1个副本
创建一个新的索引test,设置分片数为1,通过mapping初始化一个type1,type1有一个属性field1
创建索引twitter,并添加类型tweet
添加一个type到存在的索引
添加一个新的field到存在的type中(在twitter这个type中添加一个field use_name
如果已经存在index,或者type, field,再次添加同样的type和field将会报错,不能更新
可以更新fileld的情况
type的属性是对象,这种情况可以更新对象
上面的代码创建了my_index索引,并添加了一个user type, user的属性为对象name, name有两个属性first,user_id,这种情况就可以更新对象的属性
上面的代码更新了name对象,添加了last属性,更新了user_id属性,添加了它的ignore_above属性
总结:
创建索引时可以直接制定type(通过mapping)
添加新的type到存在的索引,使用url(_mapping)
添加新的field到存在的type,使用url (_mapping)
不能更新存在的field
使用curl命令创建
创建一个新的索引,并设置分片数和副本数
创建一个twitter的索引, 设置为3个分片,2个副本,默认5个分片,1个副本
curl -XPUT http://localhost:9200/twitter -d' { "settings" : { "index" : { "number_of_shards" : 3, "number_of_replicas" : 2 } } }'
创建一个新的索引test,设置分片数为1,通过mapping初始化一个type1,type1有一个属性field1
curl -XPUT http://localhost:9200/test -d' { "settings" : { "number_of_shards" : 1 }, "mappings" : { "type1" : { "properties" : { "field1" : { "type" : "text" } } } } }'
创建索引twitter,并添加类型tweet
curl -XPUT 'http://localhost:9200/twitter/' -d ' { "mappings": { "tweet": { "properties": { "message": { "type": "string" } } } } }'
添加一个type到存在的索引
curl -XPUT 'http://localhost:9200/twitter/_mapping/user' -d ' { "properties": { "username": { "type":"string" } } }'
添加一个新的field到存在的type中(在twitter这个type中添加一个field use_name
curl -XPUT 'http://localhost:9200/twitter/_mapping/user' -d ' { "properties": { "address": { "type":"string" } } }'
如果已经存在index,或者type, field,再次添加同样的type和field将会报错,不能更新
可以更新fileld的情况
type的属性是对象,这种情况可以更新对象
curl -XPUT 'http://localhost:9200/my_index -d' { "mappings": { "user": { "properties": { "name": { "properties": { "first": { "type": "text" } } }, "user_id": { "type": "keyword" } } } } }'
上面的代码创建了my_index索引,并添加了一个user type, user的属性为对象name, name有两个属性first,user_id,这种情况就可以更新对象的属性
curl -XPUT 'http://localhost:9200/my_index/_mapping/user -d' { "properties": { "name": { "properties": { "last": { "type": "text" } } }, "user_id": { "type": "keyword", "ignore_above": 100 } } }'
上面的代码更新了name对象,添加了last属性,更新了user_id属性,添加了它的ignore_above属性
总结:
创建索引时可以直接制定type(通过mapping)
添加新的type到存在的索引,使用url(_mapping)
添加新的field到存在的type,使用url (_mapping)
不能更新存在的field
相关文章推荐
- ElasticSearch源码解析(三):索引创建
- Elasticsearch创建索引和映射结构详解
- Elasticsearch创建索引和映射结构详解
- Elasticsearch索引创建快照
- elasticsearch-jdbc 使用数据库数据创建索引
- elasticsearch核心知识--47.创建更新删除索引以及自定义分词器。
- elasticsearch 字段别名(创建索引并指定索引类型的mapping)
- Elasticsearch创建索引
- elasticsearch 简介和创建索引初步
- Elasticsearch之curl创建索引
- Java操作ElasticSearch之创建索引
- Elasticsearch生成json,创建索引(把数据写入索引中)
- ElasticSearch创建索引(index)和添加映射(mapping)
- perl put 请求创建elasticsearch 索引
- elasticsearch 5.6.4自动创建索引与mapping映射关系 +Java语言
- Elasticsearch创建索引和映射结构详解
- ElasticSearch(6)--使用Java客户端创建索引和映射
- ElasticSearch Marvel自动创建索引的分片和副本数目设置
- Elasticsearch创建索引
- Elasticsearch之curl创建索引库和索引时注意事项