Elasticsearch进阶笔记第三十六篇

282 阅读4分钟

Elasticsearch高手进阶篇(75)

elasticsearch高手进阶_使用search template将搜索模板化

搜索模板

  • search template,高级功能,就可以将我们的一些搜索进行模板化,然后的话,每次执行这个搜索,就直接调用模板,给传入一些参数就可以了

  • 越高级的功能,越少使用,可能只有在你真的遇到特别合适的场景的时候,才会去使用某个高级功能。但是,这些高级功能你是否掌握,其实就是普通的es开发人员,和es高手之间的一个区别。高手,一般来说,会把一个技术掌握的特别好,特别全面,特别深入,也许他平时用不到这个技术,但是当真的遇到一定的场景的时候,高手可以基于自己的深厚的技术储备,立即反应过来,找到一个合适的解决方案。

search template入门

 GET /waws_website/blogs/_search/template
 {
   "inline" : {
     "query": { 
       "match" : { 
         "{{field}}" : "{{value}}" 
       } 
     }
   },
   "params" : {
       "field" : "title",
       "value" : "博客"
   }
 }
 
 {
   "took": 1,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 2,
     "max_score": 0.28582606,
     "hits": [
       {
         "_index": "waws_website",
         "_type": "blogs",
         "_id": "2",
         "_score": 0.28582606,
         "_source": {
           "title": "我的第一篇博客",
           "content": "大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!"
         }
       },
       {
         "_index": "waws_website",
         "_type": "blogs",
         "_id": "1",
         "_score": 0.28582606,
         "_source": {
           "title": "我的第一篇博客",
           "content": "大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!"
         }
       }
     ]
   }
 }
  • 上面的搜索语句会翻译成下面的搜索语句
 GET /blog_website/blogs/_search
 {
   "query": { 
     "match" : { 
       "title" : "博客" 
     } 
   }
 }
 ​
 search template:"{{field}}" : "{{value}}" 

toJson

 GET /waws_website/blogs/_search/template
 {
   "inline": "{"query": {"match": {{#toJson}}matchCondition{{/toJson}}}}",
   "params": {
     "matchCondition": {
       "title": "博客"
     }
   }
 }
 
 {
   "took": 1,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 2,
     "max_score": 0.28582606,
     "hits": [
       {
         "_index": "waws_website",
         "_type": "blogs",
         "_id": "2",
         "_score": 0.28582606,
         "_source": {
           "title": "我的第一篇博客",
           "content": "大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!"
         }
       },
       {
         "_index": "waws_website",
         "_type": "blogs",
         "_id": "1",
         "_score": 0.28582606,
         "_source": {
           "title": "我的第一篇博客",
           "content": "大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!"
         }
       }
     ]
   }
 }

join

 GET /waws_website/blogs/_search/template
 {
   "inline": {
     "query": {
       "match": {
         "title": "{{#join delimiter=' '}}titles{{/join delimiter=' '}}"
       }
     }
   },
   "params": {
     "titles": ["博客", "网站"]
   }
 }
 
 {
   "took": 1,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 2,
     "max_score": 0.28582606,
     "hits": [
       {
         "_index": "waws_website",
         "_type": "blogs",
         "_id": "2",
         "_score": 0.28582606,
         "_source": {
           "title": "我的第一篇博客",
           "content": "大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!"
         }
       },
       {
         "_index": "waws_website",
         "_type": "blogs",
         "_id": "1",
         "_score": 0.28582606,
         "_source": {
           "title": "我的第一篇博客",
           "content": "大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!"
         }
       }
     ]
   }
 }

default value

 POST /waws_website/blogs/1/_update
 {
   "doc": {
     "views": 5
   }
 }
 
 GET /waws_website/blogs/_search/template
 {
   "inline": {
     "query": {
       "range": {
         "views": {
           "gte": "{{start}}",
           "lte": "{{end}}{{^end}}20{{/end}}"
         }
       }
     }
   },
   "params": {
     "start": 1,
     "end": 10
   }
 }
 
 {
   "took": 2,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 1,
     "max_score": 1,
     "hits": [
       {
         "_index": "waws_website",
         "_type": "blogs",
         "_id": "1",
         "_score": 1,
         "_source": {
           "title": "我的第一篇博客",
           "content": "大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!",
           "views": 5
         }
       }
     ]
   }
 }
  • 其他
 GET /waws_website/blogs/_search/template
 {
   "inline": {
     "query": {
       "range": {
         "views": {
           "gte": "{{start}}",
           "lte": "{{end}}{{^end}}20{{/end}}"
         }
       }
     }
   },
   "params": {
     "start": 1
   }
 }
 
 # 等价于
 GET /waws_website/blogs/_search
 {
   "query": {
     "range": {
       "views": {
         "gte": 1,
         "lte": 20
       }
     }
   }
 }

conditional

es的config/scripts目录下,预先保存这个复杂的模板,后缀名是.mustache,文件名是conditonal image.png

# 模板文件格式
 {
   "query": {
     "bool": {
       "must": {
         "match": {
           "line": "{{text}}" 
         }
       },
       "filter": {
         {{#line_no}} 
           "range": {
             "line_no": {
               {{#start}} 
                 "gte": "{{start}}" 
                 {{#end}},{{/end}} 
               {{/start}} 
               {{#end}} 
                 "lte": "{{end}}" 
               {{/end}} 
             }
           }
         {{/line_no}} 
       }
     }
   }
 }
 
 GET /my_index/my_type/_search 

 {
   "took": 4,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 1,
     "max_score": 1,
     "hits": [
       {
         "_index": "my_index",
         "_type": "my_type",
         "_id": "1",
         "_score": 1,
         "_source": {
           "line": "我的博客",
           "line_no": 5
         }
       }
     ]
   }
 }
 
 GET /my_index/my_type/_search/template
 {
   "file": "conditional",
   "params": {
     "text": "博客",
     "line_no": true,
     "start": 1,
     "end": 10
   }
 }

保存search template

config/scripts,.mustache

提供一个思路

  • 比如说,一般在大型的团队中,可能不同的人,都会想要执行一些类似的搜索操作
  • 这个时候,有一些负责底层运维的一些同学,就可以基于search template,封装一些模板出来,
  • 然后是放在各个es进程的scripts目录下的
  • 其他的团队,其实就不用各个团队自己反复手写复杂的通用的查询语句了,直接调用某个搜索模板,传入一些参数就好了