ElasticSearch备考 -- 查询模版

news/2024/10/4 5:24:03 标签: elasticsearch, es, 学习, 搜索引擎

一、题目

### 基础版

Create a search template for the above query, so that the template

(i) is named "with_response_and_tag",

(ii) has a parameter "with_min_response" to represent the lower bound of the `response` field,

(iii) has a parameter "with_max_response" to represent the upper bound of the `response` field,

(iv) has a parameter "with_tag" to represent a possible value of the `tags` field

Test the "with_response_and_tag" search template by setting the

parameters as follows:

(i) "with_min_response": 400,

(ii) "with_max_response": 500

(iii) "with_tag": "security"

### 进阶版

Update the "with_response_and_tag" search template, so that

(i) if the "with_max_response" parameter is not set, then don't set an upper bound to the `response` value, and

(ii) if the "with_tag" parameter is not set, then do not apply that filter at all

Test the "with_response_and_tag" search template by setting only the "with_min_response" parameter to 500

Test the "with_response_and_tag" search template by setting the parameters as follows:

(i) "with_min_response": 500,

(ii) "with_tag": "security"

二、思考

此题主要考察的是查询模版,这在日常工作中用到的比较少,需要注意写法和格式。如下是官方API提供一个模版样例,可以参照这个进行改造查询模版。此外创建模版后要注意验证模版,因为创建模版成功并不代表可以正常执行。

PUT _scripts/my-search-template
{
  "script": {
    "lang": "mustache",
    "source": {
      "query": {
        "match": {
          "message": "{{query_string}}"
        }
      },
      "from": "{{from}}",
      "size": "{{size}}"
    }
  }
}

三、解题

Step 1、【基础版】创建模版

PUT _scripts/with_response_and_tag
{
  "script": {
    "lang": "mustache",
    "source": {
      "query": {
        "range": {
          "response": {
            "gte": "{{with_max_response}}",
            "lte": "{{with_min_response}}"
          }
        },
        "match": {
          "tags": "{{with_tag}}"
        }
      }
    }
  }
}

Step 2、【基础版】验证模版

POST _render/template
{
  "id": "with_response_and_tag",
  "params": {
    "with_max_response": "500",
    "with_min_response": "400",
    "with_tag":"security"
  }
}

Step 3、【基础版】执行模版查询

GET my-index/_search/template
{
  "id": "with_response_and_tag",
  "params": {
    "with_max_response": "500",
    "with_min_response": "400",
    "with_tag":"security"
  }
}

Step 4、【进阶版】更新模版

这里有几点需要注意

  • source里可以使用三引号包裹,这样就可以不使用转移符
  • 如果参数中有使用条件,则使用 {{#condition}}content{{/condition}},外面不要引号
  • 如果是if else判断条件,则使用 {{#condition}}if content{{/condition}} {{^condition}}else content{{/condition}},外面不带引号
  • 默认值方式,{{my-var}}{{^my-var}}default value{{/my-var}},外面需要引号
PUT _scripts/with_response_and_tag
{
  "script": {
    "lang": "mustache",
    "source": """
      {
        "query": {
            "bool": {
              "filter": [
                {
                  "range": {
                    "response": {
                      "gte": "{{with_min_response}}"
                      {{#with_max_response}}
                      ,"lte": "{{with_max_response}}"
                      {{/with_max_response}}
                    }
                  }
                }, 
                {
                  {{#with_tag}}
                  "match": {
                    "tags":"{{with_tag}}"
                  }  
                  {{/with_tag}}
                }
            ]
          }
        }
      }
    """
  }
}

四、总结

  • 查询模版,这在日常工作中用到的比较少,需要注意写法和格式,多联系
  • 参数外层需要通过双引号包裹,"{{param}}"
  • 不用包裹双引号的有:
    • {{#toJson}}tags{{/toJson}}

    • {{#condition}}content{{/condition}}

    • {#condition}}if content{{/condition}} {{^condition}}else content{{/condition}}


参考资料

  • Search templates | Elasticsearch Guide [8.1] | Elastic

送一波福利:

福利一

有需要内推JD的同学,可以私信或留言,我帮您内推,流程快!!!

有需要内推JD的同学,可以私信或留言,我帮您内推,流程快!!!

有需要内推JD的同学,可以私信或留言,我帮您内推,流程快!!!

福利二

福利三


http://www.niftyadmin.cn/n/5689496.html

相关文章

AG-Pose 部署笔记

目录 6D目标姿态估计新标杆:AG-Pose融合几何感知 HouseCat6D 室内数据集 依赖项安装: FPS(Farthest Point Sampling)--最远点采样算法 furthest_point_sample fps安装报错: GroundingDINO报错 win11 编译报错: linux安装报错解决: AttributeError: module gorill…

[3.4]【机器人运动学MATLAB实战分析】PUMA560机器人逆运动学MATLAB计算

PUMA560是六自由度关节型机器人,其6个关节都是转动副,属于6R型操作臂。各连杆坐标系如图1,连杆参数如表1所示。 图1 PUMA560机器人的各连杆坐标系 表1 PUMA560机器人的连杆参数 用代数法对其进行运动学反解。具体步骤如下: 1、求θ1 PMUMA56

Unity3D播放GIF图片使用Animation来制作动画

系列文章目录 unity工具 文章目录 系列文章目录👉前言👉一、下载GIF动图,用PS制作导出帧动画图片👉二、使用Animation制作动画👉三、脚本控制动画播放👉壁纸分享👉总结👉前言 unity播放gif图片,本身是不支持的,但是可以使用其他方法来实现, 1.有一种使用System…

计算机视觉与深度学习 | 读取、处理和写入激光雷达点云数据(附matlab代码)

===================================================== github:https://github.com/MichaelBeechan CSDN:https://blog.csdn.net/u011344545 ===================================================== 激光雷达点云数据 1、读取和显示点云2、选择所需的一组点3、将所选点写…

自动驾驶-轨迹拼接

在进行自动驾驶的规划之前,要确定当前帧轨迹规划的起点,这个起点常被误认为是当前车辆的位置,即每次以车辆的当前位置进行轨迹规划;其实不是这样的,直观上,这会导致本次次规划的轨迹同上次规划的轨迹之间是…

c#代码介绍23种设计模式_16迭代器模式

目录 1、迭代器模式的介绍 2、迭代器模式的定义 3、迭代器模式的结构 4、代器模式角色组成 5、迭代器实现 6、迭代器模式的适用场景 7、迭代器模式的优缺点 8、.NET中迭代器模式的应用 9、实现思路 1、迭代器模式的介绍 迭代器是针对集合对象而生的,对于集合对象而言…

[Day 83] 區塊鏈與人工智能的聯動應用:理論、技術與實踐

區塊鏈在物聯網中的應用 區塊鏈技術與物聯網(IoT)結合,為許多領域提供了強大的解決方案。傳統的IoT架構常面臨數據隱私和安全問題,而區塊鏈的去中心化和加密技術則能有效增強IoT系統的安全性、透明性和效率。本文將探討區塊鏈如何…

C++语言学习(7):《C++程序设计原理与实践》第二章笔记

这一章很简单,可以直接跳过。 编译: 链接: 可移植性的说明,以及错误的类型细分: 编译时错误链接时错误运行时错误(逻辑错误) 思考题 Q & A “Hello, World!” 程序的目的是什么&…