文章目录

  • 1. painless简介
  • 2. script使用
    • 1. Ingest processor
      • 1.使用的变量
      • 2. 可以产生的影响Side Effects
      • 3. 返回Return
      • 4.使用样例
    • 2. Update
      • 1.使用的变量
      • 2. 可以产生的影响Side Effects
      • 3. 返回Return
      • 4.使用样例
    • 3. Update by query
      • 1.使用的变量
      • 2. 可以产生的影响Side Effects
      • 3. 返回Return
      • 4.使用样例
    • 4. Reindex
      • 1.使用的变量
      • 2. 可以产生的影响Side Effects
      • 3. 返回Return
      • 4.使用样例
    • 5. Sort
      • 1.使用的变量
      • 2. 可以产生的影响Side Effects
      • 3. 返回Return
      • 4.使用样例
    • 6. Score
      • 1.使用的变量
      • 2. 可以产生的影响Side Effects
      • 3. 返回Return
      • 4.使用样例
    • 7. Field
      • 1.使用的变量
      • 2. 可以产生的影响Side Effects
      • 3. 返回Return
      • 4.使用样例
    • 8. Filter
      • 1.使用的变量
      • 2. 可以产生的影响Side Effects
      • 3. 返回Return
      • 4.使用样例
    • 9. search template
      • 1. 使用存储的mustache进行查询
      • 2. 直接使用mustache查询

1. painless简介

painless 在不同的上下文中能够使用的文档的字段等都是不同的,需要特殊处理

在上下文中评估painless脚本。每个上下文都有可用作局部变量的值,控制可用类的白名单以及这些类(API)中的方法和字段,以及是否以及返回哪种类型的值。

通常在下表的上下文之一内执行painless脚本。请注意,这不一定是完整的列表,因为自定义插件和专用代码可能会定义使用painless脚本的新方法。

painless 总共可以运行在以下的各种上下文当中,每个上下文的介绍分为两部分,一部分是painless script的介绍(更全面),一部分是es文档的介绍(举例更生动)

感觉相对更常用的Ingest processor
Update
Update by query
Reindex
Sort
Score
Field
Filter感觉不是很常用的
Metric aggregation initialization
Metric aggregation map
Metric aggregation combine
Metric aggregation reduce
Bucket script aggregation
Bucket selector aggregationSimilarity
Weight
Minimum should match
Watcher condition
Watcher transform

2. script使用

1. Ingest processor

1.使用的变量

params (Map, read-only): User-defined parameters passed in as part of the query.
ctx[’_index’] (String): The name of the index.
ctx[’_type’] (String): The type of document within an index.
ctx (Map): Contains extracted JSON in a Map and List structure for the fields that are part of the document.

2. 可以产生的影响Side Effects

  1. ctx[’_index’]: Modify this to change the destination index for the current document.
  2. ctx[’_type’]: Modify this to change the type for the current document.
  3. ctx (Map): Modify the values in the Map/List structure to add, modify, or delete the fields of a document.

ingest 对应的是doc还没有进行存储的时候,所以这个时候没有_source字段,直接使用ctx.field就可以获取到对应的字段

3. 返回Return

void
No expected return value.

4.使用样例

修改index name

PUT _ingest/pipeline/my_index
{"description": "use index:my_index and type:_doc","processors": [{"script": {"source": """ctx._index = 'my_index';ctx._type = '_doc';"""}}]
}PUT any_index/_doc/1?pipeline=my_index
{"message": "text"
}返回{"_index": "my_index","_type": "_doc","_id": "1","_version": 1,"result": "created","_shards": {"total": 2,"successful": 1,"failed": 0},"_seq_no": 89,"_primary_term": 1,
}可以看到index的name变了

修改文档字段,增加减少

{"script": {"lang": "painless","source": "ctx.field_a_plus_b_times_c = (ctx.field_a + ctx.field_b) * params.param_c","params": {"param_c": 10}}
}

2. Update

1.使用的变量

params (Map, read-only): User-defined parameters passed in as part of the query.
ctx[‘op’] (String): The name of the operation.
ctx[’_routing’] (String, read-only): The value used to select a shard for document storage.
ctx[’_index’] (String, read-only): The name of the index.
ctx[’_type’] (String, read-only): The type of document within an index.
ctx[’_id’] (int, read-only): The unique document id.
ctx[’_version’] (int, read-only): The current version of the document.
ctx[’_now’] (long, read-only): The current timestamp in milliseconds.
ctx[’_source’] (Map): Contains extracted JSON in a Map and List structure for the fields existing in a stored document.

2. 可以产生的影响Side Effects

ctx[‘op’]: Use the default of index to update a document. Set to none to specify no operation or delete to delete the current document from the index.
ctx[’_source’]: Modify the values in the Map/List structure to add, modify, or delete the fields of a document.

3. 返回Return

void

4.使用样例

POST /seats/_update/3
{"script": {"source": "ctx._source.sold = true; ctx._source.cost = params.sold_cost","lang": "painless","params": {"sold_cost": 26}}
}
PUT test/_doc/1
{"counter" : 1,"tags" : ["red"]
}修改一个field的值
POST test/_update/1
{"script" : {"source": "ctx._source.counter += params.count","lang": "painless","params" : {"count" : 4}}
}增加一个数组field
POST test/_update/1
{"script" : {"source": "ctx._source.tags.add(params.tag)","lang": "painless","params" : {"tag" : "blue"}}
}

移除一个数组元素,感觉这个就是在写代码,

elasticsearch_script_02相关推荐

最新文章

  1. 【Java】身份证号码验证
  2. PWA项目实战分享(听书APP)
  3. 《DBA修炼之道:数据库管理员的第一本书》——1.2节独特的优势
  4. mqtt 之 last will(遗愿)
  5. springcloud ribbon retryTemplate操作流程分析
  6. 开始Unity3D参观考察
  7. hdu 1255(线段树+离散化)
  8. Java设计模式学习总结(15)——行为型模式之责任链模式
  9. Windows server 2008 R2 DHCP服务器的架设
  10. 用windbg分析minidump
  11. PowerMock进行mock测试
  12. cad怎么转换成dwf格式?学会这招,解决98%工作烦恼
  13. 如何把Mysql卸载干净?(亲测有效)
  14. java事务 深入Java事务的原理与应用
  15. php富强民主,给你的网站添加“富强民主”点击特效
  16. 收藏 | 百度、美团 ClickHouse、Flink 干货总结!
  17. USACO4.3.2——质数方阵暴力解法
  18. 使用流报错:stream has already been operated upon or closed
  19. Java并发——Executor框架详解(Executor框架结构与框架成员)
  20. 抖音为什么这么火?抖音用户暴涨的秘密在哪?

热门文章

  1. kafka数据可靠性深度解读
  2. 设计模式:模板方法模式(Template Method)
  3. SpringMVC中@ResponseBody的相关注意点
  4. 【主题演讲】探索云、视频会议,编解码的奥妙
  5. 360视频云Web前端HEVC播放器实践剖析
  6. Java基础之代码执行顺序深入解析
  7. 仅需少量视频观看记录,就可以精准推断你的习惯
  8. 由STGW下载慢问题引发的网络传输学习之旅
  9. 报名 | 腾讯组织的区块链技术沙龙,本周六在深圳!
  10. flask + celery实现定时任务和异步