1.Index API: 创建并建立索引
PUT twitter/tweet/1 { "user" : "kimchy", "post_date" : "2009-11-15T14:12:12", "message" : "trying out Elasticsearch" }
官方文档参考:Index API。
2.Get API: 获取文档
curl -XGET 'http://localhost:9200/twitter/tweet/1'
官方文档参考:Get API。
3.DELETE API: 删除文档
$ curl -XDELETE 'http://localhost:9200/twitter/tweet/1'
官方文档参考:Delete API。
4.UPDATE API: 更新文档
PUT test/type1/1{ "counter" : 1, "tags" : ["red"]}
官方文档参考:Update API。
5.Multi Get API: 一次批量获取文档
PUT 'localhost:9200/_mget { "docs" :
[ {"_index" : "test", "_type" : "type", "_id" : "1" }, { "_index" : "test", "_type" : "type", "_id" : "2" } ] }
官方文档参考:Multi Get API。
6.Bulk API: 批量操作,增删改查
1.本地文件批量操作
e$ curl -s -XPOST localhost:9200/blog/user/_bulk --data-binary @requests requests文件内容如下 {"index":{"_id":"25"}} {"name":"黎明","id":25} {"index":{"_id":"26"}} {"name":"小明","id":26} {"index":{"_id":"26"}} {"name":"雄安","id":27} {"index":{"_id":"28"}} {"name":"笑话","id":28}
2.resp 方法
curl -H "Content-Type: application/json" -XPOST 'http://47.52.199.51:9200/book/english/_bulk' -d' {"index":{"_id":"17"}} {"name":"cddd","id":17} {"index":{"_id":"18"}} {"name":"cddd","id":18} {"index":{"_id":"19"}} {"name":"cddd","id":19} {"index":{"_id":"20"}} {"name":"cddd","id":20} '
官方文档参考:Bulk API。
7.DELETE By Query API: 查询删除
POST /book/_delete_by_query { "query":{ "match":{ "name": "yangxioa" } } }
7.1.删除所有
POST /book/_delete_by_query { "query":{ "match_all":{} } }
7.2.支持路由查询(routing=XXX,匹配分片数)
POST twitter/_delete_by_query?routing=1 { "query": { "range" : { "age" : { "gte" : 10 } } } }
{ "took" : 147, // 整个操作从开始到结束的毫秒数 "timed_out": false, // true如果在通过查询执行删除期间执行的任何请求超时 ,则将此标志设置为。 "total": 119, // 已成功处理的文档数。 "deleted": 119, // 已成功删除的文档数。 "batches": 1, // 通过查询删除拉回的滚动响应数。 "version_conflicts": 0, // 按查询删除的版本冲突数。 "noops": 0, // 对于按查询删除,此字段始终等于零。它只存在,以便通过查询删除,按查询更新和reindex API返回具有相同结构的响应。 "retries": { // 通过查询删除尝试的重试次数。bulk是重试的批量操作search的数量,是重试的搜索操作的数量。 "bulk": 0, "search": 0 }, "throttled_millis": 0, // 请求睡眠符合的毫秒数requests_per_second。 "requests_per_second": -1.0, // 在通过查询删除期间有效执行的每秒请求数。 "throttled_until_millis": 0, //在按查询响应删除时,此字段应始终等于零。它只在使用Task API时有意义,它指示下一次(自纪元以来的毫秒数),为了符合,将再次执行受限制的请求 "failures" : [ ] //如果在此过程中存在任何不可恢复的错误,则会出现故障数组。如果这是非空的,那么请求因为那些失败而中止。逐个查询是使用批处理实现的, 任何故障都会导致整个进程中止,但当前批处理中的所有故障都会被收集到数组中。您可以使用该conflicts选项来防止reindex在版本冲突中中止。 }
官方文档参考:Delete By Query API。
8.update更新api
8.1.脚本更新
POST test/_doc/1/_update { "script" : { "source": "ctx._source.counter += params.count", "lang": "painless",// ES语言类型 "params" : { "count" : 4 } } }
8.2.新增字段
POST test/_doc/1/_update { "script" : "ctx._source.new_field = 'value_of_new_field'" }
8.3.删除字段
POST test/_doc/1/_update { "script" : "ctx._source.remove('new_field')" }
8.4.存在就更新
POST test/_doc/1/_update { "script" : { "source": "if (ctx._source.tags.contains(params.tag)) { ctx.op = 'delete' } else { ctx.op = 'none' }", "lang": "painless", "params" : { "tag" : "green" } } }
8.5.更新部分字段
POST test/_doc/1/_update { "doc" : { "name" : "new_name" } }
8.6.upsert:存在就更新,不存在插入
POST test/_doc/1/_update { "script" : { "source": "ctx._source.counter += params.count", "lang": "painless", "params" : { "count" : 4 } }, "upsert" : { "counter" : 1 } }
官方文档参考:Update 脚本更新API
9.UPDATE BY QUERY API:查询更新
9.1.更新,重新索引
POST twitter/_update_by_query?conflicts=proceed
{ "took" : 147, "timed_out": false, "updated": 120, "deleted": 0, "batches": 1, "version_conflicts": 0, "noops": 0, "retries": { "bulk": 0, "search": 0 }, "throttled_millis": 0, "requests_per_second": -1.0, "throttled_until_millis": 0, "total": 120, "failures" : [ ] }
ES内部自带实现乐观锁控制,先查询出要更新的记录的版本号,更新时匹配版本号时候一致。
所有更新和查询失败都会导致_update_by_query中止并failures在响应中返回。已执行的更新仍然存在。换句话说,该过程不会回滚,只会中止。当第一个失败导致中止时,失败的批量请求返回的所有失败都将在failures元素中返回; 因此,可能存在相当多的失败实体。
如果您只想计算版本冲突,不要导致_update_by_query 中止,您可以conflicts=proceed在URL或"conflicts": "proceed",改配置当第一个冲突时会会继续执行,version_conflicts冲突数量。
9.2.查询更新
POST twitter/_update_by_query?conflicts=proceed { "query": { "term": { "user": "kimchy" } } }
9.3.查询脚本更新
POST twitter/_update_by_query { "script": { "source": "ctx._source.likes++", "lang": "painless" }, "query": { "term": { "user": "kimchy" } } }
也可以同时在多个索引和多个类型上完成这一切,就像搜索API一样:
POST twitter,blog / _doc,post / _update_by_query
routing则路由将复制到滚动查询,将进程限制为与该路由值匹配的分片:
POST twitter/_update_by_query?routing=1
默认情况下,_update_by_query使用1000的滚动批次。可以使用scroll_sizeURL参数更改批量大小:
POST twitter/_update_by_query?scroll_size=100
9.4.使用TASK API获取所有正在运行的逐个查询请求的状态
GET _tasks?detailed=true&actions=*byquery
结果:
{ "nodes" : { "r1A2WoRbTwKZ516z6NEs5A" : { "name" : "r1A2WoR", "transport_address" : "127.0.0.1:9300", "host" : "127.0.0.1", "ip" : "127.0.0.1:9300", "attributes" : { "testattr" : "test", "portsfile" : "true" }, "tasks" : { "r1A2WoRbTwKZ516z6NEs5A:36619" : { "node" : "r1A2WoRbTwKZ516z6NEs5A", "id" : 36619, "type" : "transport", "action" : "indices:data/write/update/byquery", "status" : { "total" : 6154, "updated" : 3500, "created" : 0, "deleted" : 0, "batches" : 4, "version_conflicts" : 0, "noops" : 0, "retries": { "bulk": 0, "search": 0 } "throttled_millis": 0 }, "description" : "" } } } } }
使用任务ID,您可以直接查找任务:
GET /_tasks/taskId:1
可以使用任务取消API取消任何按查询更新:
POST _tasks/task_id:1/_cancel
手动切片:
POST twitter/_update_by_query { "slice": { "id": 0, "max": 2 }, "script": { "source": "ctx._source['extra'] = 'test'" } }
官方文档参考:Update By Query API
10.Reindex API:重新索引
10.1.复制整个索引
最基本的形式_reindex只是将文档从一个索引复制到另一个索引。这会将twitter索引中的文档复制到new_twitter索引中(前提是要有相同的索引类型):
POST _reindex { "source": { "index": "twitter" }, "dest": { "index": "new_twitter" } }
10.2.复制匹配的文档
POST _reindex { "source": { "index": "twitter", "type": "_doc", "query": { "term": { "user": "kimchy" } } }, "dest": { "index": "new_twitter" } }
10.3.复制多个索引文档
POST _reindex
{
"source": {
"index": ["book", "blog"],
"type": ["english", "user"]
},
"dest": {
"index": "book1"
}
}
ES 6.3只支持一个索引一个类型,所以上面这个并没有实验成功!提示:
"reason": "Rejecting mapping update to [book1] as the final mapping would have more than 1 type: [english, user]"
10.4.是否覆盖版本号
POST reindex
{ "source": { "index": ["book"], "type": ["english"] }, "dest": { "index": "book1", "version_type":"external" } }
“external”:表示使用source的版本号覆盖dest的版本号,当source的版本号<=dest的版本号会提示冲突,“internal”:表示保持dest的版本号自增。
10.5.只复制不存在的记录,已经存在的记录提示冲突
POST _reindex { "source": { "index": ["book"], "type": ["english"] }, "dest": { "index": "book1", "op_type": "create" } }
默认情况下,版本冲突会中止该_reindex过程,但可以通过"conflicts": "proceed"请求正文中的设置对它们进行计数
10.6.排序复制指定数量
POST _reindex
{ "size":10, "source": { "index": ["book"], "sort": { "name": "desc" } }, "dest": { "index": "book1", "op_type": "create" } }
如果报错禁止排序:Fielddata is disabled on text fields by...
聚合这些操作用单独的数据结构(fielddata)缓存到内存里了,需要单独开启:
PUT book/_mapping/english { "properties": { "name": { "type": "text", "fielddata": true } } }
10.7.复制部分字段
POST _reindex {"source": { "index": "book", "_source": ["age", "name"] }, "dest": { "index": "book1" } }
10.8.过滤修改元数据再复制
POST _reindex
{ "size":2, "source": { "index": "book", "_source": ["age", "name"] }, "dest": { "index": "book1",
"routing": "=age" // 根据age进行路由
}, "script": { "source": "if (ctx._source.age == 12) {ctx._source.age++}", "lang": "painless" } }
就像在_update_by_query,您可以设置ctx.op更改在目标索引上执行的操作:
noop-
设置
ctx.op = "noop"脚本是否确定不必在目标索引中编制索引。这种无操作将noop在响应机构的计数器中报告。 delete-
ctx.op = "delete"如果脚本确定必须从目标索引中删除文档,请进行 设置 。
10.9.从远程复制
POST _reindex { "source": { "remote": { "host": "http://otherhost:9200", "username": "user", "password": "pass" }, "index": "source", "query": { "match": { "test": "data" } } }, "dest": { "index": "dest" } }
10.10.查看重建索引任务
GET _tasks?detailed=true&actions=*reindex
官方文档参考:Reindex API
11.term Vectors:分词api
11.1. term的基本信息
# term_freq:在在该字段中的频率
# position:词在该字段中的位置
# start_offset:从什么偏移量开始的
# end_offset: 到什么偏移量结束
11.2 term的统计信息
如果启用了term的统计信息,即term_statistics设为true,那么有哪些统计信息呢?
11.3字段的统计信息
如果启用了字段统计信息,即field_statistics设为true,那么有哪些统计信息呢?
# sum_doc_freq: 一个字段中所有term的文档频率之和
# doc_count: 有多少个文档包含这个字段
# sum_ttf:sum total term frequency的缩写,一个字段中的每一个term的在所有文档出现之和
term statistics和field statistics并不精准,不会被考虑有的doc可能被删除了
11.5采集term信息的方式
采集term信息的方式有两种:index-time(从已经存储的索引中查看) 和 query-time(及时生成)
11.6 index-time方式
需要在mapping配置一下,然后建立索引的时候,就直接生成这些词条和文档的统计信息
PUT /website { "mappings": { "article":{ "properties":{ "text":{ "type": "text", "term_vector": "with_positions_offsets", "store": "true", "analyzer" : "fulltext" } } } }, "settings": { "analysis": { "analyzer": { "fulltext":{ "type": "custom", "tokenizer": "whitespace", "filter": [ "lowercase", "type_as_payload" ] } } } } }