ES中script脚本的使用

ES中script脚本的使用,第1张

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脚本的使用。
其主要作用是:提取字段属性,进行表达式计算。
最典型的使用场景是:定义运行时字段。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5432721.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存