Elasticsearch Scripts总结

前言

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"
          }
        }
      }
    }
  }
}


 

  • 3
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: node scripts/build.js 是一个命令行指令,用于运行一个名为 build.js 的脚本文件。该脚本文件通常用于构建或打包项目,例如将多个文件合并成一个文件,压缩文件大小等。该指令需要在命令行中输入,并且需要在项目的根目录下执行。 ### 回答2: node scripts/build.js 是一个使用 Node.js 运行的脚本文件。 通常情况下,我们可以通过命令行运行该脚本文件,如 `node scripts/build.js`。 此脚本文件可能是为了构建项目或执行特定的构建任务而创建的。它可能包含一系列的构建步骤,例如编译代码、压缩文件、打包资源等。 通过运行该脚本文件,我们可以自动化执行这些构建任务,从而节省了手动执行每个步骤的时间和精力。 脚本文件的具体内容和功能取决于项目的需求和开发者的要求。在脚本中,我们可能会使用一些构建工具或库,如Webpack、Gulp、Grunt等,来帮助我们更有效地进行构建操作。 脚本文件可以根据我们的需要进行自定义和扩展。我们可以根据不同的开发环境或需求编写不同的脚本,实现不同的构建任务。 总之,通过命令行运行 node scripts/build.js,我们可以执行项目的构建任务,提高开发效率和代码质量。 ### 回答3: 执行`node scripts/build.js`命令时,会运行一个名为`build.js`的脚本文件。这个脚本文件主要用于构建(build)某个项目或应用程序。 一般来说,`build.js`脚本会包含一系列的任务或操作,用于处理项目中的各种资源和文件,以生成最终需要部署或发布的版本。这些操作可以包括: 1. 打包文件:将项目中的各个文件进行打包,将它们合并为一个或多个输出文件。通常使用工具如Webpack或Rollup进行打包,以优化文件大小和加载速度。 2. 转换代码:将源代码转换为可执行或可运行的形式。这可能涉及将ES6/ES7代码转换为ES5,使用Babel或TypeScript进行类型检查和编译等。 3. 优化资源:对图片、CSS、JavaScript等资源进行压缩和优化,以减少文件大小和提高访问速度。可以使用工具如imagemin、terser等来实现。 4. 生成文档:根据项目的注释或配置文件,生成项目的文档或API文档。JSDoc或TypeDoc是生成JavaScript文档的常用工具。 5. 运行测试:执行项目的单元测试或集成测试,以确保代码的质量和功能的正确性。一般使用测试框架如Jest、Mocha等来执进行自动化测试。 6. 清理旧文件:在构建之前,可能会先清理掉之前构建生成的旧文件,以确保每次构建都是基于最新的源代码。 `node scripts/build.js`的具体逻辑和功能取决于该脚本的实现内容。根据项目的需求和开发者的要求,可以自定义构建流程和操作,以满足特定的构建需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值