首页 > 其他分享 >es查询script 用法

es查询script 用法

时间:2023-10-29 22:33:02浏览次数:30  
标签:脚本 示例 script myParam 用法 查询 my es

在Elasticsearch中,可以使用脚本(Script)来执行自定义的逻辑和计算。脚本可以用于查询、过滤、排序等各种操作。下面是关于脚本使用语法的详细说明,并附上示例说明:

  1. 脚本语言:Elasticsearch支持多种脚本语言,如Painless、Groovy等。但从Elasticsearch 7.0版本开始,默认只支持Painless脚本语言。
  2. 脚本位置:脚本可以直接内联到查询请求中,也可以事先存储在Elasticsearch的脚本库中,通过ID引用。
  3. 脚本类型:根据使用场景,脚本可以分为以下两种类型:
  • 查询脚本(Query Script):用于在查询阶段评估文档是否匹配查询条件。
  • 更新脚本(Update Script):用于更新文档的字段值。
  1. 脚本参数:脚本可以接受参数,以便在脚本执行时传递额外的信息。参数可以是静态的值,也可以是动态的字段值。
  2. 脚本示例:
    (a) 内联脚本示例:
GET /my-index/_search
{
  "query": {
    "bool": {
      "must": {
        "script": {
          "script": {
            "source": "doc['my_field'].value > params.myParam",
            "lang": "painless",
            "params": {
              "myParam": 10
            }
          }
        }
      }
    }
  }
}

上述示例中,我们使用内联脚本来检查my_field字段的值是否大于参数myParam的值。如果满足条件,则文档被视为匹配。

(b) 存储脚本示例:

首先,将脚本存储到Elasticsearch的脚本库中(例如,通过PUT API):

PUT /_scripts/my_script
{
  "script": {
    "source": "doc['my_field'].value > params.myParam",
    "lang": "painless"
  }
}

然后,在查询请求中引用该脚本:

GET /my-index/_search
{
  "query": {
    "bool": {
      "must": {
        "script": {
          "id": "my_script",
          "params": {
            "myParam": 10
          }
        }
      }
    }
  }
}

上述示例中,我们将脚本存储在名为my_script的脚本ID下,并在查询中使用该脚本进行条件判断。

标签:脚本,示例,script,myParam,用法,查询,my,es
From: https://blog.51cto.com/u_15535797/8081927

相关文章

  • 无涯教程-Clojure - Desktop – Displaying Labels函数
    可以在标签类的帮助下显示标签。以下程序显示了有关如何使用它的示例。(nsweb.core(:gen-class)(:require[seesaw.core:asseesaw]))(defn-main[&args](defndisplay[content](let[window(seesaw/frame:title"Example")](->win......
  • 云原生架构实战04 kubeSphere 平台安装
    一、Kubernetes上安装KubeSpherehttps://kubesphere.com.cn/安装步骤选择4核8G(master)、8核16G(node1)、8核16G(node2)三台机器,按量付费进行实验,CentOS7.9安装Docker安装Kubernetes安装KubeSphere前置环境安装KubeSphere1、安装Dockersudoyumremovedocker*sudoyuminstall-yyum......
  • iptables 使用总结
       Linux系统的防火墙功能是由内核实现的2.0版内核中,包过滤机制是ipfw,管理工具是ipfwadm2.2版内核中,包过滤机制是ipchain,管理工具是ipchains2.4版及以后的内核中,包过滤机制是netfilter,管理工具是iptablesiptables用户态位于/......
  • Marine pollution resources
    MarinePollutionTheworld’smarinepollutioncomesinmanyforms–fromtoxicchemicals,sewageandfertiliserstoplastics,discardedfishingnetsandeventhenoisefromshippinganddrilling.Over80%ofitoriginatesfromland-basedactivities(WWF,......
  • Desertification
    Desertification, also called desertization, the process by which natural or human causes reduce the biological productivity of drylands (arid and semiarid lands). Declines in productivity may be the result of climate change, de......
  • SQL多表联合查询用法介绍
    SQL多表联合查询用法介绍一、联合查询基本概念联合查询,又称为多表查询,是指针对两个或多个表进行查询,并将结果集合并起来展示的过程。在实际应用中,联合查询是很常见的查询方式,因为单张表无法满足复杂的查询需求。二、联合查询的语法联合查询的语法格式如下:SELECTcolumn1,co......
  • Desertification of China
    Chinaisoneofthecountriesthatseethelargestdesertificationareaswiththelargestpopulationaffected,andthemostseverewind-sandhazardsintheworld.Accordingtotheresultsofthesixthnationalmonitoringsurveyondesertificationreleased......
  • Java基础 InetAddress
    publicstaticvoidmain(String[]args)throwsException{//address是IP的对象,也是一台电脑的对象InetAddressaddress=InetAddress.getByName("172.18.153.251");System.out.println(address.getHostName());System.out.println(address.getHostAddress()......
  • Desertification
    Desertification,alsocalled desertization,theprocessbywhichnaturalorhumancausesreducethebiologicalproductivityof drylands (aridandsemiaridlands).Declinesinproductivitymaybetheresultof climatechange, deforestation,overgrazing, ......
  • Could not resolve placeholder '' in value "${}"
    Couldnotresolveplaceholder''invalue"${}"背景用Jenkins构建maven自动化打包时,因为需要从properties文件读取参数来区分是本地Debug版还是正式上线版,配置完就不能用了。FailedtoloadApplicationContext原因两个错误都出现,因为本来是用IDEA的默认配置的resources文......