ES学习摘要 - 6. Document APIs
时间: 2019-07-27来源:OSCHINA
前景提要
【围观】麒麟芯片遭打压成绝版,华为亿元投入又砸向了哪里?>>>
6.1 Reading and Writing documents
一个索引会被切分为多个分片,每个分片会有多个副本。一个分片的所有副本被称为replication group,彼此之间保持同步。
replication group中存在一个主分区(primary shard)。主分区承担所有索引操作,同时负责把索引变更同步到其他副本。
Basic wirte model
所有索引操作首先都会根据document ID被路由到一个replication group,然后从中选出主分区。主分区需要验证这个操作,并同步到其他副本。在es的master节点上为每个replication group维护一个in-sync副本列表,主分区需要把索引操作同步到所有in-sync副本上。只有当所有需要同步的副本都反馈完成时,才反馈给client操作完成。
若主分区自身操作失败,则该node会向master请求重新选择primary shard。master自己也会主动监测所有分片副本,会重新选择失效的主分区。当主分区接受请求之后,它就会确保副本获得同步。
Basic read model
接受读请求的node会执行如下行为:
1. 找出所有需要查询的分区(可能属于多个不同索引)
2. 为每个分区选出一个合适的副本
3. 将查询发送到选出的副本上
4. 收集结果并返回
6.2 Index API
PUT twitter/_doc/1 {.....} 添加索引
POST twitter/_doc/ {....} 添加索引,自动生成ID
PUT twitter/_doc/1?op_type=create or PUT twitter/_doc/1/_create doc存在则直接失败,不更新
POST twitter/_doc?routing=kimchy 使用kimchy作为route函数的入参
添加document API会自动创建index以及dynamic type mapping
index.write.wait_for_active_shards(默认为1)参数会在写入document前一直等待直到有wait_for_active_shards个或许副本为止(算上primary shard)。这个参数只作用在写入操作开始前,操作开始之后若replica失效也会导致实际写入副本数少于该配置。
6.3 Get API
通过ID获得doc:GET twitter/_doc/0 返回结果中_source包含了doc原文
get API默认是实时的,和refresh间隔无关
通过_source参数指定是否返回_source结构或者部分fields:
GET twitter/_doc/0?_source=false
GET twitter/_doc/0?_source=*.id,retweeted
GET twitter/_doc/1/_source 只返回_source
6.4 Delete API
根据ID删除一个doc: DELETE /twitter/_doc/1
可以添加以下参数
wait_for_active_shards 等待直到线上有wait_for_active_shards个活跃副本
DELETE /twitter/_doc/1?timeout=5m 指定超时时间
6.5 Update API
PUT test/_doc/1 { "counter" : 1,"tags" : ["red"]} 构建/替代一个document
POST test/_doc/1/_update { "doc" : { "name" : "new_name" }} 部分更新一个doc
scripted updates用scripts更新
6.6 Multi Get API
通过一次调用获得多个docs:
GET /_mget { "docs" : [ { "_index" : "test", "_type" : "_doc", "_id" : "1" }, { "_index" : "test", "_type" : "_doc", "_id" : "2" } ] }
也可以在URI中指定index\type:
GET /test/_mget {.....}
GET /test/type/_mget {....}
6.7 Bulk API
/_bulk, /{index}/_bulk, /{index}{type}/_bulk 下可以执行批量操作
6.8 ?refresh
通过在index、update、delete、bulk请求中添加?refresh参数可以控制变更可见的时间:
?refresh or ?refresh=true
立即更新索引,有较大性能影响
?refresh=wait_for
请求一直等待直到索引更新完再返回
?refresh=false(默认值)
无影响,依靠索引默认的刷新机制

科技资讯:

科技学院:

科技百科:

科技书籍:

网站大全:

软件大全:

热门排行