概述
-
内置脚本实现自增操作
1 PUT accounts/person/12
{
"age":0
}
2 GET accounts/person/12
返回:
{
"_index": "accounts",
"_type": "person",
"_id": "12",
"_version": 2,
"found": true,
"_source": {
"age": 0
}
}
3 自增操作
POST /accounts/person/12/_update
{
"script": "ctx._source.age+=1"
}
返回:
{
"_index": "accounts",
"_type": "person",
"_id": "12",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
4 查看
GET accounts/person/12
返回:
{
"_index": "accounts",
"_type": "person",
"_id": "12",
"_version": 3,
"found": true,
"_source": {
"age": 1
}
}
age完成了自增
-
外部脚本实现根据输入参数完成数组元素的增加
1 增加脚本文件路径:elasticsearch-5.2.0/config/scripts/add_tags.groovy
内容:
ctx._source.tags+=new_tag
2 发送命令
1 PUT accounts/person/13
{
"age":0,
"tags":[]
}
2 GET accounts/person/13
{
"_index": "accounts",
"_type": "person",
"_id": "13",
"_version": 1,
"found": true,
"_source": {
"age": 0,
"tags": []
}
}
3 POST /accounts/person/13/_update
{
"script": {
"lang": "groovy", //脚本语言
"file": "add_tags",//脚本文件名
"params": {
"new_tag":"xyz"//传入的参数
}
}
}
返回:
{
"_index": "accounts",
"_type": "person",
"_id": "13",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
4 GET accounts/person/13
{
"_index": "accounts",
"_type": "person",
"_id": "13",
"_version": 2,
"found": true,
"_source": {
"age": 0,
"tags": [
"xyz"
]
}
}
-
外部脚本实现按照输入参数来删除document
1 增加脚本文件路径:elasticsearch-5.2.0/config/scripts/delete.groovy
内容:如果num为输入的参数,者删除
ctx.op=ctx._source.age==age?'delete':'none'
2 发送命令
1 GET accounts/person/13
返回:
{
"_index": "accounts",
"_type": "person",
"_id": "13",
"_version": 3,
"found": true,
"_source": {
"age": 0,
"tags": [
"xyz",
"xyz"
]
}
}
2 POST /accounts/person/13/_update
{
"script": {
"lang": "groovy",
"file": "delete",
"params": {
"age":1
}
}
}
返回:result为noop,者删除失败
{
"_index": "accounts",
"_type": "person",
"_id": "13",
"_version": 3,
"result": "noop",
"_shards": {
"total": 0,
"successful": 0,
"failed": 0
}
}
3 POST /accounts/person/13/_update
{
"script": {
"lang": "groovy",
"file": "delete",
"params": {
"age":0
}
}
}
返回:result为deleted,删除成功
{
"_index": "accounts",
"_type": "person",
"_id": "13",
"_version": 4,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
4 GET accounts/person/13
返回
{
"_index": "accounts",
"_type": "person",
"_id": "13",
"found": false
}
-
upsert: 如果修改的document不存在,就执行upsert中的初始化操作,如果存在者进行partial update操作
1 直接进行partial update操作
POST /accounts/person/15/_update
{
"doc": {
"age":10
}
}
返回错误:
{
"error": {
"root_cause": [
{
"type": "document_missing_exception",
"reason": "[person][15]: document missing",
"index_uuid": "qgPjJUv6Sm-VBesoOMlNPA",
"shard": "2",
"index": "accounts"
}
],
"type": "document_missing_exception",
"reason": "[person][15]: document missing",
"index_uuid": "qgPjJUv6Sm-VBesoOMlNPA",
"shard": "2",
"index": "accounts"
},
"status": 404
}
2 增加upsert参数
POST /accounts/person/15/_update
{
"doc": {
"age":10
},
"upsert": {
"age":0
}
}
返回:
{
"_index": "accounts",
"_type": "person",
"_id": "15",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
3 GET accounts/person/15
返回:
{
"_index": "accounts",
"_type": "person",
"_id": "15",
"_version": 1,
"found": true,
"_source": {
"age": 0//age为upsert中初始化的内容
}
}
最后
以上就是幽默星月为你收集整理的ElasticSearch--使用groovy脚本的全部内容,希望文章能够帮你解决ElasticSearch--使用groovy脚本所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复