ElasticSearch备考 -- Search template

一、题目

基础版

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提供一个模版样例,可以参照这个进行改造查询模版。此外创建模版后要注意验证模版,因为创建模版成功并不代表可以正常执行。

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

三、解题

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

bash 复制代码
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、【基础版】验证模版

bash 复制代码
POST _render/template
{
  "id": "with_response_and_tag",
  "params": {
    "with_max_response": "500",
    "with_min_response": "400",
    "with_tag":"security"
  }
}

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

bash 复制代码
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}},外面需要引号
bash 复制代码
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}}

参考资料

送一波福利:

福利一

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

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

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

福利二

福利三

相关推荐
fatesunlove1 小时前
知识图谱重构电商搜索:下一代AI搜索引擎的底层逻辑
搜索引擎·ai·知识图谱
前端小崔1 小时前
从零开始学习three.js(18):一文详解three.js中的着色器Shader
前端·javascript·学习·3d·webgl·数据可视化·着色器
小生凡一1 小时前
搜索引擎工作原理|倒排索引|query改写|CTR点击率预估|爬虫
爬虫·搜索引擎
龙湾开发2 小时前
计算机图形学编程(使用OpenGL和C++)(第2版)学习笔记 10.增强表面细节(二)法线贴图
c++·笔记·学习·图形渲染·贴图
liang_20262 小时前
【HT周赛】T3.二维平面 题解(分块:矩形chkmax,求矩形和)
数据结构·笔记·学习·算法·平面·总结
虾球xz2 小时前
游戏引擎学习第290天:完成分离渲染
c++·人工智能·学习·游戏引擎
虾球xz2 小时前
游戏引擎学习第285天:“Traversables 的事务性占用”
c++·学习·游戏引擎
虾球xz3 小时前
游戏引擎学习第280天:精简化的流式实体sim
数据库·c++·学习·游戏引擎
深度学习入门3 小时前
学习深度学习是否要先学习机器学习?
人工智能·深度学习·神经网络·学习·机器学习·ai·深度学习入门
FAREWELL000753 小时前
Unity基础学习(十五)核心系统——音效系统
学习·unity·c#·游戏引擎