执行 Painless 存储脚本
1.0 版引入
运行用 Painless 语言编写的存储脚本。
OpenSearch 提供了多种运行脚本的方式;以下部分展示了如何通过在 GET <index>/_search
请求体中传递脚本信息来运行脚本。
端点
GET books/_search
{
"script_fields": {
"total_ratings": {
"script": {
"id": "my-first-script"
}
}
}
}
请求字段选项
字段 | 数据类型 | 描述 |
---|---|---|
query | 对象 | 指定要处理的文档的过滤器。 |
script_fields | 对象 | 要在输出中包含的字段。 |
script(脚本) | 对象 | 为字段生成值的脚本的 ID。 |
请求示例
以下请求运行在创建或更新存储脚本中创建的存储脚本。该脚本计算每本书的评分总和,并在输出的 total_ratings
字段中显示该总和。
-
该脚本的目标是
books
索引。 -
"match_all": {}
属性值是一个空对象,表示处理索引中的每个文档。 -
total_ratings
字段值是my-first-script
执行的结果。参见创建或更新存储脚本。
GET books/_search
{
"query": {
"match_all": {}
},
"script_fields": {
"total_ratings": {
"script": {
"id": "my-first-script"
}
}
}
}
示例响应
GET books/_search
请求返回以下字段
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "books",
"_id" : "1",
"_score" : 1.0,
"fields" : {
"total_ratings" : [
12
]
}
},
{
"_index" : "books",
"_id" : "2",
"_score" : 1.0,
"fields" : {
"total_ratings" : [
15
]
}
},
{
"_index" : "books",
"_id" : "3",
"_score" : 1.0,
"fields" : {
"total_ratings" : [
8
]
}
}
]
}
}
响应正文字段
字段 | 数据类型 | 描述 |
---|---|---|
took | 整数 | 操作耗时(毫秒)。 |
timed_out | 布尔型 | 操作是否超时。 |
_shards | 对象 | 已处理的分片总数,以及成功、跳过和未处理的分片总数。 |
hits | 对象 | 包含有关已处理文档的高级信息以及 hits 对象的数组。参见Hits 对象。 |
Hits 对象
字段 | 数据类型 | 描述 |
---|---|---|
total | 对象 | 已处理的文档总数及其与 match 请求字段的关系。 |
max_score | 双精度浮点数 | 所有匹配项中返回的最高相关性得分。 |
hits | 数组 | 关于每个已处理文档的信息。参见Document 对象。 |
Document 对象
字段 | 数据类型 | 描述 |
---|---|---|
_index | 字符串 | 包含文档的索引。 |
_id | 字符串 | 文档 ID。 |
_score | 浮点型 | 文档的相关性得分。 |
fields | 对象 | 脚本返回的字段及其值。 |
运行带参数的 Painless 存储脚本
要在每次运行查询时向脚本传递不同的参数,请在 script_fields
中定义 params
。
示例
以下请求运行在创建或更新存储脚本中创建的存储脚本。该脚本计算每本书的评分总和,将总和值乘以 multiplier
参数,并在输出中显示结果。
-
该脚本的目标是
books
索引。 -
"match_all": {}
属性值是一个空对象,表示它处理索引中的每个文档。 -
total_ratings
字段值是multiplier-script
执行的结果。参见创建或更新带参数的存储脚本。 -
"multiplier": 2
在params
字段中是一个传递给存储脚本multiplier-script
的变量
GET books/_search
{
"query": {
"match_all": {}
},
"script_fields": {
"total_ratings": {
"script": {
"id": "multiplier-script",
"params": {
"multiplier": 2
}
}
}
}
}
示例响应
{
"took" : 12,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "books",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"fields" : {
"total_ratings" : [
16
]
}
},
{
"_index" : "books",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"fields" : {
"total_ratings" : [
30
]
}
},
{
"_index" : "books",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"fields" : {
"total_ratings" : [
24
]
}
}
]
}
}
使用 Painless 存储脚本对结果进行排序。您可以使用 Painless 存储脚本来排序结果。
示例请求
GET books/_search
{
"query": {
"match_all": {}
},
"script_fields": {
"total_ratings": {
"script": {
"id": "multiplier-script",
"params": {
"multiplier": 2
}
}
}
},
"sort": {
"_script": {
"type": "number",
"script": {
"id": "multiplier-script",
"params": {
"multiplier": 2
}
},
"order": "desc"
}
}
}
示例响应
{
"took" : 90,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "books",
"_type" : "_doc",
"_id" : "2",
"_score" : null,
"fields" : {
"total_ratings" : [
30
]
},
"sort" : [
30.0
]
},
{
"_index" : "books",
"_type" : "_doc",
"_id" : "1",
"_score" : null,
"fields" : {
"total_ratings" : [
24
]
},
"sort" : [
24.0
]
},
{
"_index" : "books",
"_type" : "_doc",
"_id" : "3",
"_score" : null,
"fields" : {
"total_ratings" : [
16
]
},
"sort" : [
16.0
]
}
]
}
}