一、search template简介

elasticsearch提供了search template功能,其会在实际执行查询之前,对search template进行预处理并将参数填充到template中。

elasticsearch主要提供了两个API来支持search template

_scripts/用于对search template的维护;

_search/template使用search template进行搜索;

二、测试数据准备

批量index三个文档

POST _bulk

{ "index" : { "_index" : "search_template_test", "_type" : "_doc", "_id" : "1" } }
{ "name":"zhang san", "age":30, "man":true, "address":"Hebei LangFang"}
{ "index" : { "_index" : "search_template_test", "_type" : "_doc", "_id" : "2" } }
{ "name":"li si", "age":20, "man":true, "address":"BeiJing HaiDian"}
{ "index" : { "_index" : "search_template_test", "_type" : "_doc", "_id" : "3" } }
{ "name":"liu hui", "age":40, "man":false, "address":"NeiMengGu ChiFeng"}

查询查看三个索引的文档

GET search_template_test/_search
{
"took":6,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"skipped":0,
"failed":0
},
"hits":{
"total":3,
"max_score":1,
"hits":[
{
"_index":"search_template_test",
"_type":"_doc",
"_id":"2",
"_score":1,
"_source":{
"name":"li si",
"age":20,
"man":true,
"address":"BeiJing HaiDian"
}
},
{
"_index":"search_template_test",
"_type":"_doc",
"_id":"1",
"_score":1,
"_source":{
"name":"zhang san",
"age":30,
"man":true,
"address":"Hebei LangFang"
}
},
{
"_index":"search_template_test",
"_type":"_doc",
"_id":"3",
"_score":1,
"_source":{
"name":"liu hui",
"age":40,
"man":false,
"address":"NeiMengGu ChiFeng"
}
}
]
}
}

三、维护search template

我们新建id为search_template_test.match_name的search template,其主要是查询match 字段name;

POST _scripts/search_template_test.match_name
{
"script": {
"lang": "mustache",
"source": {
"query": {
"match": {
"name": "{{name_val}}"
}
}
}
}
}

查看我们创建的search template

GET _scripts/search_template_test.match_name

{
"_id":"search_template_test.match_name",
"found":true,
"script":{
"lang":"mustache",
"source":"{\"query\":{\"match\":{\"name\":\"{{name_val}}\"}}}",
"options":{
"content_type":"application/json; charset=UTF-8"
}
}
}

删除我们创建的search template

DELET _scripts/search_template_test.match_name
{
"acknowledged":true
}

elasticsearch提供了API支持我们查看预处理最终形成的查询语句;

POST _render/template/search_template_test.match_name
{
"params":{
"name_val":"zhang li"
}
} {
"template_output":{
"query":{
"match":{
"name":"zhang li"
}
}
}
}

四、使用search template

我们可以像下边这样使用search template

POST search_template_test/_search/template
{
"id": "search_template_test.match_name",
"params": {
"name_val":"zhang li"
}
} {
"took":1,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"skipped":0,
"failed":0
},
"hits":{
"total":2,
"max_score":0.2876821,
"hits":[
{
"_index":"search_template_test",
"_type":"_doc",
"_id":"2",
"_score":0.2876821,
"_source":{
"name":"li si",
"age":20,
"man":true,
"address":"BeiJing HaiDian"
}
},
{
"_index":"search_template_test",
"_type":"_doc",
"_id":"1",
"_score":0.2876821,
"_source":{
"name":"zhang san",
"age":30,
"man":true,
"address":"Hebei LangFang"
}
}
]
}
}

执行search template也支持使用explain来调试查看elasticsearch的打分情况;

POST search_template_test/_search/template
{
"id": "search_template_test.match_name",
"params": {
"name_val":"zhang li"
},
"explain":true
} {
"took":3,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"skipped":0,
"failed":0
},
"hits":{
"total":2,
"max_score":0.2876821,
"hits":[
{
"_shard":"[search_template_test][2]",
"_node":"Sl6S4Kn2Rh-BdNgaM3ZwJg",
"_index":"search_template_test",
"_type":"_doc",
"_id":"2",
"_score":0.2876821,
"_source":{
"name":"li si",
"age":20,
"man":true,
"address":"BeiJing HaiDian"
},
"_explanation":{
"value":0.2876821,
"description":"sum of:",
"details":[
{
"value":0.2876821,
"description":"weight(name:li in 0) [PerFieldSimilarity], result of:",
"details":[
......
]
}
]
}
},
{
"_shard":"[search_template_test][3]",
"_node":"Sl6S4Kn2Rh-BdNgaM3ZwJg",
"_index":"search_template_test",
"_type":"_doc",
"_id":"1",
"_score":0.2876821,
"_source":{
"name":"zhang san",
"age":30,
"man":true,
"address":"Hebei LangFang"
},
"_explanation":{
"value":0.2876821,
"description":"sum of:",
"details":[
{
"value":0.2876821,
"description":"weight(name:zhang in 0) [PerFieldSimilarity], result of:",
"details":[
......
]
}
]
}
}
]
}
}

执行search template也支持使用profile来调试查看elasticsearch的查询执行情况;

POST search_template_test/_search/template
{
"id": "search_template_test.match_name",
"params": {
"name_val":"zhang li"
},
"profile":true
} {
"took":11,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"skipped":0,
"failed":0
},
"hits":{
"total":2,
"max_score":0.2876821,
"hits":[ ]
},
"profile":{
"shards":[
{
"id":"[Sl6S4Kn2Rh-BdNgaM3ZwJg][search_template_test][0]",
"searches":[
{
"query":[
{
"type":"BooleanQuery",
"description":"name:zhang name:li",
"time_in_nanos":308301,
"breakdown":{
"score":0,
"build_scorer_count":0,
"match_count":0,
"create_weight":308300,
"next_doc":0,
"match":0,
"create_weight_count":1,
"next_doc_count":0,
"score_count":0,
"build_scorer":0,
"advance":0,
"advance_count":0
},
"children":[
{
"type":"TermQuery",
"description":"name:zhang",
"time_in_nanos":32901,
"breakdown":{
"score":0,
"build_scorer_count":0,
"match_count":0,
"create_weight":32900,
"next_doc":0,
"match":0,
"create_weight_count":1,
"next_doc_count":0,
"score_count":0,
"build_scorer":0,
"advance":0,
"advance_count":0
}
},
{
"type":"TermQuery",
"description":"name:li",
"time_in_nanos":15901,
"breakdown":{
"score":0,
"build_scorer_count":0,
"match_count":0,
"create_weight":15900,
"next_doc":0,
"match":0,
"create_weight_count":1,
"next_doc_count":0,
"score_count":0,
"build_scorer":0,
"advance":0,
"advance_count":0
}
}
]
}
],
"rewrite_time":42700,
"collector":[
{
"name":"CancellableCollector",
"reason":"search_cancelled",
"time_in_nanos":700,
"children":[
{
"name":"SimpleTopScoreDocCollector",
"reason":"search_top_hits",
"time_in_nanos":200
}
]
}
]
}
],
"aggregations":[ ]
}
]
}
}

五、使用search template处理复杂的查询

我们构建以下search template,其功能如下

可以通过name字段进行match,如果命中字段中的各个关键字相邻则有更高的权重打分,同时可以通过address字段进行模糊匹配,这三个查询只要命中一个即可;

man字段值必须符合输入的值;

POST _scripts/search_template_test.multi_search_template
{
"script":{
"lang":"mustache",
"source":{
"query":{
"bool":{
"should":[
{
"match":{
"name":"{{name}}"
}
},
{
"match_phrase_prefix":{
"name":"{{name}}"
}
},
{
"query_string":{
"default_field":"address",
"query":"{{#join delimiter=' '}}address{{/join delimiter=' '}}"
}
}
],
"filter":{
"term":{
"man":"{{man}}"
}
},
"minimum_should_match":1
}
}
}
}
}

通过以下参数命中前两条数据

POST search_template_test/_search/template
{
"id":"search_template_test.multi_search_template",
"params":{
"name":"zhang sa",
"address":[
"*bei*",
"*chi*"
],
"man":true
}
} {
"took":10,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"skipped":0,
"failed":0
},
"hits":{
"total":2,
"max_score":1.8630463,
"hits":[
{
"_index":"search_template_test",
"_type":"_doc",
"_id":"1",
"_score":1.8630463,
"_source":{
"name":"zhang san",
"age":30,
"man":true,
"address":"Hebei LangFang"
}
},
{
"_index":"search_template_test",
"_type":"_doc",
"_id":"2",
"_score":1,
"_source":{
"name":"li si",
"age":20,
"man":true,
"address":"BeiJing HaiDian"
}
}
]
}
}

我们将man字段设置为false,从而只匹配第三条记录;

POST search_template_test/_search/template
{
"id":"search_template_test.multi_search_template",
"params":{
"name":"zhang sa",
"address":[
"*bei*",
"*chi*"
],
"man":false
}
} {
"took":4,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"skipped":0,
"failed":0
},
"hits":{
"total":1,
"max_score":1,
"hits":[
{
"_index":"search_template_test",
"_type":"_doc",
"_id":"3",
"_score":1,
"_source":{
"name":"liu hui",
"age":40,
"man":false,
"address":"NeiMengGu ChiFeng"
}
}
]
}
}

elasticsearch之search template的更多相关文章

  1. Elasticsearch:search template

    我们发现一些用户经常编写了一些非常冗长和复杂的查询 - 在很多情况下,相同的查询会一遍又一遍地执行,但是会有一些不同的值作为参数来查询.在这种情况下,我们觉得使用一个search template(搜 ...

  2. elasticsearch.net search入门使用指南中文版(翻译)

    elasticsearch.net search入门使用指南中文版,elasticsearch.Net是一个非常底层且灵活的客户端,它不在意你如何的构建自己的请求和响应.它非常抽象,因此所有的elas ...

  3. elasticsearch.net search入门使用指南中文版

    原文:http://edu.dmeiyang.com/book/nestusing.html elasticsearch.net为什么会有两个客户端? Elasticsearch.Net是一个非常底层 ...

  4. Elasticsearch URI search 查询语法整理

    Elasticsearch URI search 一.请求体查询与空查询 1. 请求体查询(request body search) 简单查询语句(lite)是一种有效的命令行adhoc查询.但是,如 ...

  5. 可以执行全文搜索的原因 Elasticsearch full-text search Kibana RESTful API with JSON over HTTP elasticsearch_action es 模糊查询

    https://www.elastic.co/guide/en/elasticsearch/guide/current/getting-started.html Elasticsearch is a ...

  6. Elasticsearch的Search详解

    介绍 ES不是新技术,是将全文检索和数据分析.分布式整合到一起. 基于lucene开发,提供简单的restful api接口.java api接口.其他语言开发接口等. 实现了分布式的搜索引擎和分析引 ...

  7. elasticsearch 深入 —— Search After实时滚动查询

    Search After 一般的分页需求我们可以使用form和size的方式实现,但是这种分页方式在深度分页的场景下应该是要避免使用的.深度分页会随着请求的页次增加,所消耗的内存和时间的增长也是成比例 ...

  8. Beats:为 Beats => Logstash => Elasticsearch 架构创建 template 及 Dashboard

    文章转载自:https://elasticstack.blog.csdn.net/article/details/115341977 前一段时间有一个开发者私信我说自己的 Beats 连接到 Logs ...

  9. elasticsearch 深入 —— Search Type检索类型

    在此我们再给出那个查询的代码: $ curl -XGET localhost:9200/startswith/test/_search?pretty -d '{ "query": ...

  10. Elasticsearch:跨集群搜索 Cross-cluster search (CCS)

    转载自:https://blog.csdn.net/UbuntuTouch/article/details/104588232 跨集群搜索(cross-cluster search)使您可以针对一个或 ...

随机推荐

  1. 记一个深层的bug

    1. 业务场景 产品需要每隔几天进行一次组件的更新,在自动化测试中,每隔30s检测一次更新源上的某个文件MD5值是否与本地一致,不一致代表有更新的版本,开始更新. 2. 问题出现 一个再平常不过的繁忙 ...

  2. 齐博x1token字段,请务加在请求地址的头部header

    如下图所示,你必须在请求的头部加上 token参数,主要原因有两个.第一点,这个是登录标志,因为接口访问用不了cookie,所以只能通过这个header请求标志判断用户是否已经登录.第二点,系统有时候 ...

  3. 一步一图带你深入理解 Linux 虚拟内存管理

    写在本文开始之前.... 从本文开始我们就正式开启了 Linux 内核内存管理子系统源码解析系列,笔者还是会秉承之前系列文章的风格,采用一步一图的方式先是详细介绍相关原理,在保证大家清晰理解原理的基础 ...

  4. 解决ffmpeg的播放摄像头的延时优化问题(项目案例使用有效)

    在目前的项目中使用了flv的播放摄像头的方案,但是延时达到了7-8秒,所以客户颇有微词,没有办法,只能开始优化播放延时的问题,至于对接摄像头的方案有好几种,这种咱们以后在聊,今天只要聊聊聊优化参数的问 ...

  5. Pandas常用方法

    数据处理很多需要用到pandas,有两个基本类型:Series表示一维数据,DataFrame表示多维.以下是一些常用方法的整理: pandas.Series 创建 Series pandas.Ser ...

  6. python tcp select 多路复用

    1 #!/usr/bin/python 2 # -*- coding: UTF-8 -*- 3 # 文件名:tcpserver.py 4 5 import socket 6 import time 7 ...

  7. Python基础之函数:5、内置函数、迭代器对象、异常的捕获和处理

    目录 一.重要内置函数 1.zip() 2.filter() 3.sorted() 二.常见内置函数 1. abs() 2.all.any() 3.bin.oct.hex.int() 4.bytes( ...

  8. 可编程渲染管线(Scriptable Render Pipeline, SRP)

    原文链接 可编程渲染管线处理数据的流程可分为以下3大阶段 1. 应用阶段 这个阶段大概会由CPU处理4件事情.首先会对模型数据进行可见性判断.模型数据由顶点位置.法线方向.顶点颜色.纹理坐标等构成.然 ...

  9. C# 获取打开的EXCEL中某列的行数

    背景 在通过C#操作EXCEL时 获取行数 int iRowCount = worksheet.get_Range("A65535", oMissing).get_End(MExc ...

  10. JavaWeb3

    1. 会话技术 会话:一次会话中包含多次请求和响应 一次会话:浏览器第一次给服务器资源发送请求,会话建立,直到有一方断开为止 功能:在一次会话的范围内的多次请求间共享数据 方式: 客户端会话技术:Co ...