前言
scripts的语法:
"script": {
"lang": "...",
"source" | "id": "...",
"params": { ... }
}
参数说明:
lang:脚本使用的语言,默认是painless。
source:脚本的核心部分,id应用于:stored script。
params:传递给脚本使用的变量参数。
在学习scripts时,发现有许多场景使用,比如update、update-by-query、reindex等,结合scripts语法说,lang会有painless、expression、mustache等选择;source中有ctx、doc['field_name']、_source等方式取值。因此这里做一个简单总结。
应用场景
1.update:对一个文档进行更新
PUT test/_doc/1
{
"counter" : 1,
"tags" : ["red"]
}
POST test/_update/1
{
"script": {
"source": "ctx._source.tags.add(params.tag)",
"lang": "painless",
"params": {
"tag": "blue"
}
}
}
GET test/_doc/1
2.update by query:对多个甚至所有文档进行更新
PUT test/_doc/1
{
"counter" : 1,
"tags" : ["red"]
}
POST test/_update_by_query
{
"script": {
"source": "ctx._source.counter++",
"lang": "painless"
}
}
GET test/_doc/1
3.reindex:将文档从一个索引复制到另一个索引
POST test/_bulk
{"index":{"_id":"1"}}
{"counter":1,"tags":["red"]}
{"index":{"_id":"2"}}
{"counter":2,"tags":["green"]}
{"index":{"_id":"3"}}
{"counter":3,"tags":["blue"]}
{"index":{"_id":"4"}}
{"counter":4,"tags":["white"]}
DELETE test_2
POST _reindex
{
"source": {
"index": "test"
},
"dest": {
"index": "test_2"
},
"script": {
"source": "if (ctx._source.tags.contains('red')) { ctx._source.tags.remove(ctx._source.tags.indexOf('red')) }",
"lang": "painless"
}
}
GET test2/_search
4.injest pipeline:对数据进行预处理
PUT _ingest/pipeline/test
{
"description": "use index:test",
"processors": [
{
"script": {
"source": """
ctx._index = 'test';
"""
}
}
]
}
PUT any_index/_doc/1?pipeline=test
{
"message": "hahaha"
}
GET test/_search
另外,ingest pipeline可以应用于update_by_query,reindex中。
5.script query:基于脚本的过滤查询,多用于filter中,且source需返回boolean类型
POST test/_bulk
{"index":{"_id":"1"}}
{"counter":1,"tags":["red"]}
{"index":{"_id":"2"}}
{"counter":2,"tags":["green"]}
{"index":{"_id":"3"}}
{"counter":3,"tags":["blue"]}
{"index":{"_id":"4"}}
{"counter":4,"tags":["white"]}
GET test/_search
{
"query": {
"bool": {
"filter": {
"script": {
"script": {
"source": "doc['counter'].value > 3",
"lang": "painless"
}
}
}
}
}
}
6.script score query/function score query:允许用户在检索中灵活修改文档score,来实现自己干预结果排名的目的
POST test/_bulk
{"index":{"_id":"1"}}
{"counter":1,"msg":"hello"}
{"index":{"_id":"2"}}
{"counter":2,"msg":"hello,hello"}
{"index":{"_id":"3"}}
{"counter":3,"msg":"hello,welcome"}
{"index":{"_id":"4"}}
{"counter":4,"msg":"ello"}
GET test/_search
{
"query": {
"match": {
"msg": "hello"
}
}
}
#script score query
GET test/_search
{
"query": {
"script_score": {
"query": {
"match": {
"msg": "hello"
}
},
"script": {
"source": "doc['counter'].value + _score "
}
}
}
}
7.script fields:允许为每个hit返回自定义的字段
POST test/_bulk
{"index":{"_id":"1"}}
{"counter":1,"msg":"hello"}
{"index":{"_id":"2"}}
{"counter":2,"msg":"hello,hello"}
{"index":{"_id":"3"}}
{"counter":3,"msg":"hello,welcome"}
{"index":{"_id":"4"}}
{"counter":4,"msg":"ello"}
GET test/_search
{
"query" : {
"match_all": {}
},
"script_fields" : {
"test1" : {
"script" : {
"lang": "painless",
"source": "params['_source']['counter']"
}
},
"test2" : {
"script" : {
"lang": "painless",
"source": "doc['msg'].value * params.factor",
"params" : {
"factor" : 2.0
}
}
}
}
}
8.search template:搜索模板
POST test/_bulk
{"index":{"_id":"1"}}
{"counter":1,"msg":"hello"}
{"index":{"_id":"2"}}
{"counter":2,"msg":"hello,hello"}
{"index":{"_id":"3"}}
{"counter":20,"msg":"hello,welcome"}
{"index":{"_id":"4"}}
{"counter":4,"msg":"ello"}
#定义搜索模板
POST _scripts/my_search_template
{
"script": {
"lang": "mustache",
"source": {
"query": {
"match": {
"msg": "{{query_string}}"
}
}
}
}
}
#查询模板
GET _scripts/my_search_template
#使用模板
GET test/_search/template
{
"id": "my_search_template",
"params": {
"query_string": "hello"
}
}
#验证模板
GET _render/template
{
"source": """
{
"query":{
"match":{
"msg":"hello"
}
}
}
""",
"params": {
"query_string": "hello"
}
}
#删除模板
DELETE _scripts/my_search_template
9.Stored scripts:脚本存储
POST _scripts/my_script
{
"script": {
"lang": "painless",
"source": "doc[params.field].value"
}
}
10.aggregation:聚合
POST test/_bulk
{"index":{"_id":"1"}}
{"counter":1,"msg":"hello"}
{"index":{"_id":"2"}}
{"counter":2,"msg":"hello,hello"}
{"index":{"_id":"3"}}
{"counter":20,"msg":"hello,welcome"}
{"index":{"_id":"4"}}
{"counter":4,"msg":"ello"}
POST /test/_search
{
"size": 0,
"aggs": {
"max_counter": {
"max": {
"script": {
"source": "doc.counter.value"
}
}
}
}
}
#使用stored scripts
POST _scripts/my_script
{
"script": {
"lang": "painless",
"source": "doc[params.field].value"
}
}
POST /test/_search
{
"size": 0,
"aggs": {
"max_counter": {
"max": {
"script": {
"id": "my_script",
"params": {
"field": "counter"
}
}
}
}
}
}