painless语法入门[通俗易懂]

painless语法painless基础结构”script”:{“lang”:”…”,”source”|”id”:”…”,”params”:{…}}lang:定义脚本使用的语言,默认painlesssource,id:脚本的主体,source后面跟着内联的脚本代码,id后面跟着脚本的id,具体代码存在于脚本id对应的代码中params:定义一些变量的值,使用params可以减少脚本的编译次数.因为如果

大家好,又见面了,我是你们的朋友全栈君。

painless语法

painless基础结构


  "script": { 
   
    "lang":   "...",
    "source" | "id": "...",
    "params": { 
    ... }
  }
  1. lang: 定义脚本使用的语言, 默认painless
  2. source, id: 脚本的主体, source后面跟着内联的脚本代码, id后面跟着脚本的id, 具体代码存在于脚本id对应的代码中
  3. params: 定义一些变量的值, 使用params可以减少脚本的编译次数. 因为如果变量的值硬编码到代码中, 每次进行变量值得更改都会对脚本进行重新编译. 使用params则不会重新编译脚本.

script.context.field.max_compilations_rate=75/5m 脚本的默认编译频率, 定义脚本的编译频率为5分钟75个, 当超过75个时会抛出circuit_breaking_exception异常.

脚本代码的存储

// POST _scripts/{id}
POST _scripts/calculate-score
{ 
   
  "script": { 
   
    "lang": "painless",
    "source": "Math.log(_score * 2) + params['my_modifier']"
  }
}

// 脚本使用
GET my-index-000001/_search
{ 
   
  "query": { 
   
    "script_score": { 
   
      "query": { 
   
        "match": { 
   
            "message": "some message"
        }
      },
      "script": { 
   
        "id": "calculate-score", 
        "params": { 
   
          "my_modifier": 2
        }
      }
    }
  }
}

使用脚本对文档进行更新


PUT my-index-000001/_doc/1
{ 
   
  "counter" : 1,
  "tags" : ["red"]
}

// 将count加上4
POST my-index-000001/_update/1
{ 
   
  "script" : { 
   
    "source": "ctx._source.counter += params.count",
    "lang": "painless",
    "params" : { 
   
      "count" : 4
    }
  }
}

// 增加tags的元素
POST my-index-000001/_update/1
{ 
   
  "script": { 
   
    "source": "ctx._source.tags.add(params['tag'])",
    "lang": "painless",
    "params": { 
   
      "tag": "blue"
    }
  }
}

// If the list contains duplicates of the tag, this script just removes one occurrence.
// 如果集合中有多个相同的值, 只删除第一个
POST my-index-000001/_update/1
{ 
   
  "script": { 
   
    "source": "if (ctx._source.tags.contains(params['tag'])) { ctx._source.tags.remove(ctx._source.tags.indexOf(params['tag'])) }",
    "lang": "painless",
    "params": { 
   
      "tag": "blue"
    }
  }
}

// 增加新的字段
POST my-index-000001/_update/1
{ 
   
  "script" : "ctx._source.new_field = 'value_of_new_field'"
}

// 删除字段
POST my-index-000001/_update/1
{ 
   
  "script" : "ctx._source.remove('new_field')"
}

// 如果tags字段中包含green, 则删除该文档, 否则不做操作
POST my-index-000001/_update/1
{ 
   
  "script": { 
   
    "source": "if (ctx._source.tags.contains(params['tag'])) { ctx.op = 'delete' } else { ctx.op = 'none' }",
    "lang": "painless",
    "params": { 
   
      "tag": "green"
    }
  }
}

// doc.containsKey('field')

脚本变量

  • ctx._source.field: add, contains, remove, indexOf, length
  • ctx.op: The operation that should be applied to the document: index or delete
  • ctx._index: Access to document metadata fields
  • _score 只在script_score中有效
  • doc[‘field’], doc[‘field’].value: add, contains, remove, indexOf, length

脚本缓存

  1. You can change this behavior by using the script.cache.expire setting. Use the script.cache.max_size setting to configure the size of the cache.The size of scripts is limited to 65,535 bytes. Set the value of script.max_size_in_bytes to increase that soft limit.
  2. Cache sizing is important. Your script cache should be large enough to hold all of the scripts that users need to be accessed concurrently.

脚本优化

  1. 使用脚本缓存, 预先缓存可以节省第一次的查询时间
  2. 使用ingest pipeline进行预先计算
  3. 相比于_source.field_name使用doc[‘field_name’]语法速度更快, doc语法使用doc value , 列存储

// 根据分数相加结果进行排序
GET /my_test_scores/_search
{ 
   
  "query": { 
   
    "term": { 
   
      "grad_year": "2099"
    }
  },
  "sort": [
    { 
   
      "_script": { 
   
        "type": "number",
        "script": { 
   
          "source": "doc['math_score'].value + doc['verbal_score'].value"
        },
        "order": "desc"
      }
    }
  ]
}

// 在索引中新加一个字段存储计算结果
PUT /my_test_scores/_mapping
{ 
   
  "properties": { 
   
    "total_score": { 
   
      "type": "long"
    }
  }
}

// 使用ingest pipeline先将计算结果作为值存储起来
PUT _ingest/pipeline/my_test_scores_pipeline
{ 
   
  "description": "Calculates the total test score",
  "processors": [
    { 
   
      "script": { 
   
        "source": "ctx.total_score = (ctx.math_score + ctx.verbal_score)"
      }
    }
  ]
}

// 重新索引时使用ingest pipeline
POST /_reindex
{ 
   
  "source": { 
   
    "index": "my_test_scores"
  },
  "dest": { 
   
    "index": "my_test_scores_2",
    "pipeline": "my_test_scores_pipeline"
  }
}

// 索引新文档时使用ingest pipeline
POST /my_test_scores_2/_doc/?pipeline=my_test_scores_pipeline
{ 
   
  "student": "kimchy",
  "grad_year": "2099",
  "math_score": 1200,
  "verbal_score": 800
}

// 查询
GET /my_test_scores_2/_search
{ 
   
  "query": { 
   
    "term": { 
   
      "grad_year": "2099"
    }
  },
  "sort": [
    { 
   
      "total_score": { 
   
        "order": "desc"
      }
    }
  ]
}


// stored field 用法
PUT my-index-000001
{ 
   
  "mappings": { 
   
    "properties": { 
   
      "full_name": { 
   
        "type": "text",
        "store": true
      },
      "title": { 
   
        "type": "text",
        "store": true
      }
    }
  }
}

PUT my-index-000001/_doc/1?refresh
{ 
   
  "full_name": "Alice Ball",
  "title": "Professor"
}

GET my-index-000001/_search
{ 
   
  "script_fields": { 
   
    "name_with_title": { 
   
      "script": { 
   
        "lang": "painless",
        "source": "params._fields['title'].value + ' ' + params._fields['full_name'].value"
      }
    }
  }
}

用到脚本的命令

  • function_score
  • script_score
  • aggregation
  • rescore
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/125872.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Dreamweaver 2019安装教程

    Dreamweaver 2019安装教程安装前先关闭杀毒软件和360卫士,注意安装路径不能有中文,安装包路径也不要有中文。1.选中【DreamweaverCC2019】压缩包,鼠标右击选择【解压到DreamweaverCC2019】。2.双击打开【DreamweaverCC2019】文件夹。3.选中【Set-up.exe】可执行文件,鼠标右击选择【以管理员身份运行】。4.点击文件夹小图标,然后选择【更改位置】。5.建议安装到除C盘以外的磁盘,可以在D盘或其他盘新建一个【DwCC2…

    2022年5月20日
    37
  • java基本输入语句_java键盘输入语句

    java基本输入语句_java键盘输入语句在Java中进行输入时,最常用的两种输入方式为:1.使用ScannerScanner使用步骤:导入包importjava.util.Scanner;//导包的动作必须出现在类定义的上方创建对象//newScanner(System.in)为固定格式,不可以改变Scannersc=newScanner(System.in); 接收数据inti=sc.nextInt(); //这里使用的为int型,如果改变,则需要改变sc.nextInt();

    2022年9月15日
    3
  • docker-jenkins安装node

    docker-jenkins安装node容器直接使用脚本安装报错执行如下命令即可解决gitconfig–global–unsethttp.proxygitconfig–global–unsethttps.proxy后续安装参考https://blog.csdn.net/qq_28686911/article/details/113114894

    2022年5月24日
    90
  • java集合系列——Map之TreeMap介绍(九)

    TreeMap是一个有序的key-value集合,基于红黑树(Red-Black tree)的 NavigableMap实现。该映射根据其键的自然顺序进行排序,或者根据创建映射时提供的 Comparator进行排序,具体取决于使用的构造方法。

    2022年2月26日
    119
  • 2020手机的像素密度ppi排行_5g手机排行榜最新2020年11月5g手机性价比排行榜

    2020手机的像素密度ppi排行_5g手机排行榜最新2020年11月5g手机性价比排行榜注:本文转载自网络,不代表本平台立场,仅供读者参考,著作权属归原创者所有。我们分享此文出于传播更多资讯之目的。如有侵权,请在后台留言联系我们进行删除,谢谢!相信对很多朋友们来说,一款好用的5G智能手机是非常值得入手的,小编为大家带来最新的20年11月值得购买的性价比5G手机榜单,相信会给您更好的手机选购指导,为你带来更好的智能生活体验,有喜欢的朋友们一起来看看吧。1、一加8Pro性能一…

    2022年5月8日
    168
  • ssm整合shiro框架的使用,实现权限管理「建议收藏」

    ssm整合shiro框架的使用,实现权限管理「建议收藏」ssm整合shiro框架,对用户的登录操作进行认证和授权,目的很纯粹就是为了增加系统的安全线,至少不要输在门槛上嘛。ssm整合shiro安全框架的步骤:1、引入shiro安全框架的所需jar包<!–shiro–><dependency><groupId>org.apache.shiro</groupI…

    2022年5月3日
    107

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号