1、为什么使用查询模版

让应用程序开发者只需要把查询传递给elasticsearch,而不需要考虑查询语句的构造、查询DSL语法、查询结果过滤等细节知识。

2、使用版本5.1,查询模版在5.6中发生变化。注意。官网地址  https://www.elastic.co/guide/en/elasticsearch/reference/5.1/search-template.html

3、注册查询模版

  方法一:使用配置文件,需要copy到集群所有节点

You can register search templates by storing it in the config/scripts directory, in a file using the .mustache extension. In order to execute the stored template, reference it by it’s name under the template key:

GET /_search/template
{
"file": "storedTemplate",
"params": {
"query_string": "search for these words"
}
}

  方法二:.script索引

You can also register search templates by storing it in the cluster state. There are REST APIs to manage these indexed templates.

POST /_search/template/<templatename>
{
"template": {
"query": {
"match": {
"title": "{{query_string}}"
}
}
}
}

4、查看查询模版

This template can be retrieved by

GET /_search/template/<templatename>

which is rendered as:

{
"template": {
"query": {
"match": {
"title": "{{query_string}}"
}
}
}
}

5、删除查询模版

This template can be deleted by

DELETE /_search/template/<templatename>

6、使用查询模版

To use an indexed template at search time use:

GET /_search/template
{
"id": "templateName",
"params": {
"query_string": "search for these words"
}
}

7、验证查询模版

Validating templatesedit

A template can be rendered in a response with given parameters using

GET /_render/template
{
"inline": {
"query": {
"terms": {
"status": [
"{{#status}}",
"{{.}}",
"{{/status}}"
]
}
}
},
"params": {
"status": [ "pending", "published" ]
}
} This call will return the rendered template: {
"template_output": {
"query": {
"terms": {
"status": [
"pending",
"published"
]
}
}
}
} status array has been populated with values from the params object. File and indexed templates can also be rendered by replacing inline with file or id respectively. For example, to render a file template GET /_render/template
{
"file": "my_template",
"params": {
"status": [ "pending", "published" ]
}
} Pre-registered templates can also be rendered using GET /_render/template/<template_name>
{
"params": {
"..."
}
}

8、其他

File and indexed templates can also be rendered by replacing inline with file or id respectively. For example, to render a file template

GET /_render/template
{
"file": "my_template",
"params": {
"status": [ "pending", "published" ]
}
} Pre-registered templates can also be rendered using GET /_render/template/<template_name>
{
"params": {
"..."
}
} Explainedit You can use explain parameter when running a template: GET /_search/template
{
"file": "my_template",
"params": {
"status": [ "pending", "published" ]
},
"explain": true
} Profilingedit You can use profile parameter when running a template: GET /_search/template
{
"file": "my_template",
"params": {
"status": [ "pending", "published" ]
},
"profile": true
}

读《深入理解Elasticsearch》点滴-查询模版(结合官网手册,版本5.1)的更多相关文章

  1. 2022年官网下安装Elasticsearch最全版与官网查阅方法(8.1.0最新安装)

    目录 一.环境整合(需要提前装好) 构建工具(参考工具部署方式) 二.官方下载Elasticsearch部署安装 1.百度搜索"Elasticsearch",或者访问官网https ...

  2. 读《深入理解Elasticsearch》点滴-查询二次评分

    理解二次评分 二次评分是指重新计算查询返回文档中指定个数文档的得分,es会截取查询返回的前N个,并使用预定义的二次评分方法来重新计算他们的得分 小结 有时候,我们需要显示查询结果,并且使得页面上靠前文 ...

  3. 读《深入理解Elasticsearch》点滴-聚合-top_hits

    以下是官网手册(部分)(v5.1) 直接直接看官网手册 https://www.elastic.co/guide/en/elasticsearch/reference/5.1/search-aggre ...

  4. elasticsearch聚合查询

    作者注:本文系作者自己的理解.希望大家多多交流指正 官网java API term是代表完全匹配,也就是精确查询,搜索前不会再对搜索词进行分词,所以我们的搜索词必须是文档分词集合中的一个 TermsB ...

  5. 从查询重写角度理解elasticsearch的高亮原理

    一.高亮的一些问题 elasticsearch提供了三种高亮方式,前面我们已经简单的了解了elasticsearch的高亮原理; 高亮处理跟实际使用查询类型有十分紧密的关系,其中主要的一点就是muti ...

  6. 一文读懂一条 SQL 查询语句是如何执行的

    2001 年 MySQL 发布 3.23 版本,自此便开始获得广泛应用,随着不断地升级迭代,至今 MySQL 已经走过了 20 个年头. 为了充分发挥 MySQL 的性能并顺利地使用,就必须正确理解其 ...

  7. mysql系列:加深对脏读、脏写、可重复读、幻读的理解

    关于相关术语的专业解释,请自行百度了解,本文皆本人自己结合参考书和自己的理解所做的阐述,如有不严谨之处,还请多多指教. 事务有四种基本特性,叫ACID,它们分别是: Atomicity-原子性,Con ...

  8. 《深入理解Elasticsearch》README

    书目 <深入理解ElasticSearch>拉斐尔·酷奇,马雷克·罗戈任斯基[著]张世武,余洪森,商旦[译] 机械工业出版社,2016.1 本系列包括以下8篇笔记 第01章 Elastic ...

  9. elasticsearch 分页查询实现方案——Top K+归并排序

    elasticsearch 分页查询实现方案 1. from+size 实现分页 from表示从第几行开始,size表示查询多少条文档.from默认为0,size默认为10,注意:size的大小不能超 ...

随机推荐

  1. mybatis 源码分析(六)StatementHandler 主体结构分析

    分析到这里的时候,mybatis 初始化.接口.事务.缓存等主要功能都已经讲完了,现在就还剩下 StatementHandler 这个真正干活的家伙没有分析了:所以接下来的博客内容主要和数据库的关系比 ...

  2. unity之初级工程师

    一.值类型与引用类型 值类型的变量直接存储数据,而引用类型的变量持有的是数据的引用,数据存储在数据堆中.值类型变量声明后,不管是否已经赋值,编译器为其分配内存.值类型的实例通常是在线程栈上分配的(静态 ...

  3. 搭建Spark高可用集群

      Spark简介 官网地址:http://spark.apache.org/ Apache Spark™是用于大规模数据处理的统一分析引擎. 从右侧最后一条新闻看,Spark也用于AI人工智能 sp ...

  4. json-server的安装及使用

    首先介绍一下什么是json-server,用处是什么,其实很简单:JSON-Server 是一个 Node 模块,运行 Express 服务器,你可以指定一个 json 文件作为 api 的数据源. ...

  5. 【阿里云IoT+YF3300】5. Alink物模型之服务下发

    名词解释: 服务:设备的功能模型之一,设备可被外部调用的能力或方法,可设置输入参数和输出参数.相比于属性,服务可通过一条指令实现更复杂的业务逻辑,如执行某项特定的任务.    -摘自阿里云物联网产品文 ...

  6. 爬虫——cookie模拟登陆

    cookie适用于抓取需要登录才能访问的页面网站 cookie和session机制 http协议为无连接协议,cookie: 存放在客户端浏览器,session: 存放在Web服务器 人人网登录案例 ...

  7. CodeForces 758 D Ability To Convert

    Ability To Convert 题意:给你一个n进制的60位的数,但是由于Alexander只会写0->9,所以他就会用10来表示十而不是A(假设进制>10); 题解:模拟就好了,先 ...

  8. Atcoder D - Black and White Tree(树dp+博弈)

    题目链接:http://agc014.contest.atcoder.jp/tasks/agc014_d 题意:有一棵树先手涂白色,后手涂黑色,直到不能再涂为止.涂完后再把所有黑色直接相邻的白色都变成 ...

  9. codeforces 766 D. Mahmoud and a Dictionary(种类并查集+stl)

    题目链接:http://codeforces.com/contest/766/problem/D 题意:给你n个单词,m个关系(两个单词是反义词还是同义词),然后问你所给的关系里面有没有错的,最后再给 ...

  10. c++11特性学习总结

    ubuntu 16.04 自带gcc 5.4 支持c++11 ubuntu 18.04 自带gcc 7.3 支持c++14 查看编译器支持: c++11 c++14 c++17 c++11 featu ...