ES中script脚本的使用

一、script脚本的作用

通过使用脚本,可以在 Elasticsearch 计算自定义表达式。例如,可以使用脚本作为字段返回计算值,或者计算查询的自定义得分。

小结:
1、字段的提取
2、表达式计算

二、支持哪些script脚本语言

默认的脚本语言采用的是painless
在这里插入图片描述

三、script脚本使用示例

1、查询中使用script脚本

PUT my-index-000001/_doc/1
{
  "my_field": 5
}
GET my-index-000001/_search
{
  "script_fields": {
    "my_doubled_field": {
      "script": { 
        "source": "doc['my_field'].value * params['multiplier']", 
        "params": {
          "multiplier": 2
        }
      }
    }
  }
}

2、创建单独的脚本

说明:
通过_scripts命令创建单独的脚本,并通过lang属性指定脚本的语言是painless。
source标签中,可以获取到索引文档中的值。
通过id指定脚本名称来使用独立的脚本。

创建脚本:

POST _scripts/calculate-score
{
  "script": {
    "lang": "painless",
    "source": "Math.log(_score * 2) + params['my_modifier']"
  }
}

获取脚本:

GET _scripts/calculate-score

使用脚本:

GET my-index-000001/_search
{
  "query": {
    "script_score": {
      "query": {
        "match": {
            "message": "some message"
        }
      },
      "script": {
        "id": "calculate-score", 
        "params": {
          "my_modifier": 2
        }
      }
    }
  }
}

删除脚本:

DELETE _scripts/calculate-score

3.通过脚本更新索引字段信息

PUT my-index-000001/_doc/1
{
  "counter" : 1,
  "tags" : ["red"]
}


POST my-index-000001/_update/1
{
  "script" : {
    "source": "ctx._source.counter += params.count",
    "lang": "painless",
    "params" : {
      "count" : 4
    }
  }
}

4.在mappings属性中定义运行时字段

查询的时候,可以直接使用mappings属性中定义的运行时字段day_of_week

PUT my-index-000001/
{
  "mappings": {
    "runtime": {
      "day_of_week": {
        "type": "keyword",
        "script": {
          "source": "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT))"
        }
      }
    },
    "properties": {
      "@timestamp": {"type": "date"}
    }
  }
}

5.在查询请求中定义运行时字段

如果没有在索引的mappings属性中定义运行时字段,那么也可以通过_search查询时,通过runtime_mappings来定义运行时字段。

GET my-index-000001/_search
{
  "runtime_mappings": {
    "day_of_week": {
      "type": "keyword",
      "script": {
        "source": "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT))"
      }
    }
  },
  "aggs": {
    "day_of_week": {
      "terms": {
        "field": "day_of_week"
      }
    }
  }
}

6.使用脚本自定义计算得分

GET index/_search
{
  "query": {
    "script_score": {
      "query": {
        "match": { "body": "elasticsearch" }
      },
      "script": {
        "source": "_score * saturation(doc['pagerank'].value, 10)" 
      }
    }
  }
}

四、script脚本使用的安全性

1、脚本类型限制:script.allowed_types

Elasticsearch 支持2中类型的脚本: inline and stored.
可以通过在elasticsearch.yml配置文件中的script.allowed_types属性来指定允许执行的脚本类型。
相关配置选项:

  • both 同时支持inline和stored
  • inline 内联脚本
  • stored 存储脚本
  • none 都不支持

2、脚本上下文限制:script.allowed_contexts

默认情况下,所有脚本上下文都是允许的。使用 script.allowed_contexts 设置指定允许的上下文。若要指定不允许上下文,请将 script.allowe_contexts 设置为 none。

示例:允许脚本仅在评分和更新上下文中运行:

script.allowed_contexts: score, update

总结

本文主要介绍了ES中script脚本的使用。
其主要作用是:提取字段属性,进行表达式计算
最典型的使用场景是:定义运行时字段

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

斗者_2013

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值