ElasticSearch--使用groovy脚本

  • 内置脚本实现自增操作

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中初始化的内容
  }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值