ES-脚本

脚本

简单使用
POST product/_update/2
{
  "script": {
    "source": "ctx._source.salary+=1" #将薪水字段的值 + 1
  }
}

在这里插入图片描述
预定义变量

POST product/_update/2
{
  "script": {
    "lang": "painless",
    "source": "ctx._source.salary+=params.num",
    "params": {
      "num": 1
    }
  }
}

多行代码
下面脚本的意思是有这条数据就先 + num 然后乘scale,没有就插入这条数据。

POST product/_update/2
{
  "script": {
    "lang": "painless",
    "source": 
    """ #写在"""中多行代码
    ctx._source.salary+=params.num;
    ctx._source.salary*=params.scale;
    """,
    "params": {
      "num": 1,
      "scale" : 2
    }
  },
  "upsert": {
    "name": "xiaomi nfc phone2",
    "type": "c",
    "price": 2999,
    "date": "2023-05-01",
    "desc": "xiaomi 2023 new1",
    "tags": [
      "88vip",
      "tmall",
      "newer"
    ]
  }
}
动态字段
GET /product/_search
{
  "script_fields": {
    "or_price": {
      "script": {
        "source": "doc['salary'].value"
      }
    },
    "disc_price" : {
      "script" : {
        "source": "[doc['salary'].value * 0.9,doc['salary'].value * 0.8]"
      }
    }
  }
}

在这里插入图片描述

函数

定义一个全局函数,别人也可以使用。
全局函数里面不允许对文档直接进行赋值, “source”: “doc.salary.value += params.num” 这个+=就不行。

#定义函数
POST _scripts/myFun
{
  "script": {
    "lang": "painless",
    "source": "[doc['salary'].value * 0.9,doc['salary'].value * 0.8]"
  }
}
#使用函数
GET /product/_search
{
  "script_fields": {
    "disc_price" : {
      "script" : {
        "id" : "myFun"
      }
    }
  }
}

在这里插入图片描述
带参数的引用

POST _scripts/calc_x
{
  "script": {
    "lang": "painless",
    "source": "doc.salary.value + params.num"
  }
}
GET /product/_search
{
  "script_fields": {
    "disc_price" : {
      "script" : {
        "id" : "calc_x",
        "params": {
          "num" : 10,
          "scale" : 10
        }
      }
    }
  }
}

在这里插入图片描述

更多用法
POST /product/_update/2
{
  "script": {
    "lang": "painless", 
    "source": 
    """
       ctx._source.tags.add("abc");
       """
  }
}

在这里插入图片描述
for

POST /product/_update/2
{
  "script": {
    "lang": "painless", 
    "source": 
    """
    for (int i = 0; i < 3; i ++)
       ctx._source.tags.add("abc" + i);
       """
  }
}

在这里插入图片描述

if

POST /product/_update/3
{
  "script": {
    "lang": "painless",
    "source": """
    if (ctx._source.name ==~ /liyong.*/) {
      ctx._source.salary += 1000;
    } else {
       #不可以省略
       ctx.op = "noop";
    }
     """
  }
}

综合使用

先筛选出salary >1801000的数据,然后再聚合中使用脚本进行聚合

get product/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "range": {
          "salary": {
            "gt": 1801000
          }
        }
      }
    }
  },
  "aggs": {
    "tag_agg": {
      "sum": {
        "script": {
          "source": "doc['tags.keyword'].length"
        }
      }
    }
  }
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值