zoukankan      html  css  js  c++  java
  • 读《深入理解Elasticsearch》点滴-查询模版(结合官网手册,版本5.1)

    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
    }
  • 相关阅读:
    解决org.openqa.selenium.WebDriverException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms org.springframework.beans.BeanInstantiation
    jsp学习---css基础知识学习,float,position,padding,div,margin
    jsp学习---mvc模式介绍和el表达式,jstl标签库的使用入门
    java 查询 mongodb 中的objectid
    jsp学习---使用jsp和JavaBean实现超简单网页计算器
    jsp学习--JavaBean定义和在Jsp中使用JavaBean
    jsp学习--如何定位错误和JSP和Servlet的比较
    jsp学习--JSP运行原理,九大隐式对象和JSP常用标签
    jsp学习--基本语法和基础知识
    android开发学习---layout布局、显示单位和如何进行单元测试
  • 原文地址:https://www.cnblogs.com/jiangtao1218/p/8592813.html
Copyright © 2011-2022 走看看