· 新增索引
· 指定操作类型
· 自动创建id
· 高级特性:设置 是否自动创建索引
· 高级特性:设置 外部版本号
· 高级特性:设置 超时时间
一、索引、类型、文档之间的关系(类比传统关系型数据库)
索引 —— 数据库
类型 —— 表
文档 —— 行数据二、新增文档
PUT _index/_type/_id
{
...
}示例
PUT twitter/_doc/a
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}返回结果
{
"_index" : "twitter", // 索引
"_type" : "_doc", // 类型
"_id" : "a", // id
"_version" : 1, // 版本号
"result" : "created", // 操作的结果
"_shards" : {
"total" : 2, // 表示操作在多少主副本上执行
"successful" : 2, // 表示操作在多少主副本上被成功执行 "failed" : 0
},
"_seq_no" : 0, // 用于并发控制
"_primary_term" : 1
}尝试着在运行一次命令看看会返回什么结果
{
"_index" : "twitter",
"_type" : "_doc",
"_id" : "a",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 6,
"_primary_term" : 1
}
发现操作的结果是updated说明此时是更新一个文档
那么问题来了,如果运行命令之前并不知道系统中是否已有相同id的文档。并且只想执行插入操作时我们需要怎么做(类似关系型数据库插入相同主键的数据时会产生错误信息一样)
我们只需要加入参数op_type=create 则可以指定我们是想要新增文档
PUT twitter/_doc/a?op_type=create
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}或者
PUT twitter/_doc/a/_create
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}返回结果
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[a]: version conflict, document already exists (current version [2])",
"index_uuid": "elcvZ-X7RKSbdBowxPVqGA",
"shard": "0",
"index": "twitter"
}
],
"type": "version_conflict_engine_exception",
"reason": "[a]: version conflict, document already exists (current version [2])",
"index_uuid": "elcvZ-X7RKSbdBowxPVqGA",
"shard": "0",
"index": "twitter"
},
"status": 409
}创建时也可以不指定 id ,由系统自动生成
POST twitter/_doc/
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}返回结果
{
"_index" : "twitter",
"_type" : "_doc",
"_id" : "j9u9QWwBl4JvlcMkPH1z",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
注意:此时使用的为POST命令,没有使用PUT命令,如使用PUT命令则会出现错误
PUT twitter/_doc/
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}返回结果
{
"error": "Incorrect HTTP method for uri [/twitter/_doc/?pretty] and method [PUT], allowed: [POST]",
"status": 405
}高级内容:
1.索引自动创建设置
关闭索引自动创建
PUT _cluster/settings
{
"persistent": {
"action.auto_create_index": "false"
}
}关闭后试着插入数据
POST twitter_2/_doc/
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
返回结果
{
"error" : {
"root_cause" : [
{
"type" : "index_not_found_exception",
"reason" : "no such index [twitter_2]",
"resource.type" : "index_expression",
"resource.id" : "twitter_2",
"index_uuid" : "_na_",
"index" : "twitter_2"
}
],
"type" : "index_not_found_exception",
"reason" : "no such index [twitter_2]",
"resource.type" : "index_expression",
"resource.id" : "twitter_2",
"index_uuid" : "_na_",
"index" : "twitter_2"
},
"status" : 404
} 设置规则、符合规则的索引将被创建
PUT _cluster/settings
{
"persistent": {
"action.auto_create_index": "twitter,index10,-index1*,+ind*"
}
}插入新数据
POST index10/_doc/
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}返回结果
{
"_index" : "index10",
"_type" : "_doc",
"_id" : "4dvrQWwBl4JvlcMkHYrr",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
发现插入成功
2.版本号控制
可以手动设置外部版本号,当前使用的参数version_type=external外部版本号必须大于当前的内部版本号
当设置的版本号小于等于当前内部版本号时
post twitter/_doc/a?version=4&version_type=external
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}返回结果
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[a]: version conflict, current version [4] is higher or equal to the one provided [4]",
"index_uuid": "elcvZ-X7RKSbdBowxPVqGA",
"shard": "0",
"index": "twitter"
}
],
"type": "version_conflict_engine_exception",
"reason": "[a]: version conflict, current version [4] is higher or equal to the one provided [4]",
"index_uuid": "elcvZ-X7RKSbdBowxPVqGA",
"shard": "0",
"index": "twitter"
},
"status": 409
}
在尝试一下大于当前内部版本号
post twitter/_doc/a?version=5&version_type=external
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}返回结果
{
"_index" : "twitter",
"_type" : "_doc",
"_id" : "a",
"_version" : 5,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 14,
"_primary_term" : 2
} 其他 version_type
internal 只有当给定的版本与存储的文档版本相同时,才索引文档external 或 external_gt只有当给定的版本严格高于存储文档的版本,或者没有现有文档时,才对文档进行索引。给定的版本将作为新版本使用,并与新文档一起存储。所提供的版本必须是非负的长数字。
external_gte 只有当给定的版本等于或高于存储文档的版本时,才对文档进行索引。如果没有现有文档,操作也将成功。给定的版本将作为新版本使用,并与新文档一起存储。所提供的版本必须是非负的长数字。
3.设置超时时间
?timeout=5m在执行索引操作时,分配给执行索引操作的主碎片可能不可用。其中的一些原因可能是主碎片目前正在从网关中恢复或正在进行重新定位。默认情况下,索引操作将等待主碎片可用最多1分钟,然后失败并响应错误。可以使用timeout参数显式地指定它等待的时间。