PUT请求,必须加id,否则会报错,POST请求可以不加id,自己生成id,
若有相同id的文档存在,则更新此文档,version+1
PUT http://192.168.94.151:9200/secisland/_doc/1/
{
"name": "aaaa",
"age": 13,
"message": "哈哈哈这个人好帅"
}
{
"_index": "secisland",
"_type": "_doc",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 13
}
返回值中_shards提供了索引创建的过程信息
- total 文档被创建的时候,在多少分片中进行了操作
- successful 成功建立索引分片的数量,当成功创建后,成功创建索引分片的数量最少是1
- failed 失败建立索引分片的数量
Create 如果ID已经存在,会失败
Index 如果ID不存在,创建新文档,否则,先删除现有的文档,再创建新的文档,版本会增加
Index文档
- Index 和 Create不一样的地方:如果文档不存在,就索引新的文档。否则现有文档会被删除,新的文档被索引。版本信息+1
PUT /users/_doc/2
{
"tags":["green", "pink", "red"]
}
{
"_index" : "users",
"_type" : "_doc",
"_id" : "2",
"_version" : 3,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 3,
"_primary_term" : 1
}
op_type=index
1.自动创建索引
在执行数据添加时,如果索引不存在或者type不存在会自动创建,字段不存在,字段的类型也会自动创建
可以通过配置文件设置action.auto_create_index为false来禁用自动创建索引。
可以通过配置文件设置index.mapper.dynamic为false来禁用自动映射字段类型。
官网对于自动创建的设置的介绍,可以开启、关闭,可以用前缀+或-表示允许或者拒绝匹配类型的索引。
curl -X PUT "localhost:9200/_cluster/settings" -H 'Content-Type: application/json' -d'
{
"persistent": {
"action.auto_create_index": "twitter,index10,-index1*,+ind*"
}
}
'
curl -X PUT "localhost:9200/_cluster/settings" -H 'Content-Type: application/json' -d'
{
"persistent": {
"action.auto_create_index": "false"
}
}
'
curl -X PUT "localhost:9200/_cluster/settings" -H 'Content-Type: application/json' -d'
{
"persistent": {
"action.auto_create_index": "true"
}
}
'
2. 版本号
每个文档有一个版本号,版本号的具体值放在创建索引的返回值中("_version")。通过版本号参数可以达到并发控制的效果,当在操作文档的过程中指定版本号,如果和版本号不一致时会被拒绝。
新版本采用_seq_no与_primary_term来代替version处理并发问题
PUT http://192.168.94.151:9200/secisland/_doc/1?if_seq_no=3&if_primary_term=11
{
"message": "大家好,很高兴认识大家"
}
返回值
{
"_index": "secisland",
"_type": "_doc",
"_id": "1",
"_version": 5,
"result": "updated",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 4,
"_primary_term": 12
}
默认情况下对文档的操作版本号从1开始递增,包括修改文档和删除文档。当然版本号还可以从外部获取,要启用此功能,需要将version_type设置为external。使用外部版本号时,在操作文档时,系统通过对比参数中的版本号是否大于文档中的版本号来做判断,必须大于才执行操作。
PUT http://192.168.94.151:9200/secisland/_doc/1?version=6&version_type=external
{
"message": "大家好,很高兴认识大家"
}
3. 操作类型
es支持通过op_type=create参数强制命令执行创建操作,只有系统中不存在文档的时候才会创建成功。如果不指定操作类型,如果存在此文档,则会更新此文档。
PUT http://192.168.94.151:9200/secisland/_doc/1?op_type=create
{
"name": "小明",
"age": 23,
"message": "大家好,很高兴认识大家"
}
返回结果报错
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[1]: version conflict, document already exists (current version [6])",
"index_uuid": "w06eDiDJR4SUiaw_VlXflA",
"shard": "2",
"index": "secisland"
}
],
"type": "version_conflict_engine_exception",
"reason": "[1]: version conflict, document already exists (current version [6])",
"index_uuid": "w06eDiDJR4SUiaw_VlXflA",
"shard": "2",
"index": "secisland"
},
"status": 409
}
当不指定op_type=create时,则更新此文档
另一种操作写法
http://192.168.94.151:9200/secisland/_doc/1/_create
4. 自动创建ID
当创建文档的时候,如果不指定ID,系统会自动创建ID,并且op_type默认设置为create,必须用post请求
5. 分片的选择
默认情况下,分片的选择是通过ID的散列值进行控制。这个只可以通过router参数进行手动控制。可以在每个操作的基础上直接通过哈希函数的值来指定分片的选择。
PUT http://192.168.94.151:9200/secisland/_doc/1?routing=aaa
{
"name": "小花",
"age": 24,
"message": "我的名字叫小花"
}
返回结果,分片的选择是通过routing=aaa参数的哈希值来确定的
{
"_index": "secisland",
"_type": "_doc",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 13
}
6. 其它说明
- 刷新 更新的时候可以指定refresh参数为true来立即刷新所有副本,当refresh设置为true的时候,系统做了充分优化,不会对系统产生任何影响。
- 超时 可以通过设置timeout参数来修改超时时间 timeout=5s,表示超时时间是5秒。
7. 超时设置
?timeout=5m 5min超时
curl -X PUT "localhost:9200/twitter/_doc/1?timeout=5m" -H 'Content-Type: application/json' -d'
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
'
\