{"id":517158,"date":"2023-09-14T14:14:44","date_gmt":"2023-09-14T12:14:44","guid":{"rendered":"https:\/\/www.devoteam.com\/expert-view\/unlocking-the-power-of-elasticsearch-search-templates\/"},"modified":"2023-09-14T14:14:44","modified_gmt":"2023-09-14T12:14:44","slug":"unlocking-the-power-of-elasticsearch-search-templates","status":"publish","type":"expert-view","link":"https:\/\/devoteam.info\/me\/expert-view\/unlocking-the-power-of-elasticsearch-search-templates\/","title":{"rendered":"Unlocking the Power of Elasticsearch Search Templates"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">What if I told you that Elastic offers robust support for stored search features? With this capability, you can execute queries with a wide range of parameters, much like creating a parametric function in MySQL.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A Search Template is essentially a stored search that you can execute with varying input values. When Elasticsearch serves as your search backend, it enables you to seamlessly pass user inputs from a search bar as parameters for a Search Template. This empowers you to perform searches without exposing the intricate query syntax of Elasticsearch to your end users.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If your application relies on Elasticsearch as a key component, utilizing Search Templates offers you the flexibility to modify your searches without the need to alter your application&#8217;s codebase. It&#8217;s truly a remarkable feature, isn&#8217;t it? So, let&#8217;s embark on a deeper exploration of this incredible capability.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-prerequisites\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before writing a Search Template, you need to understand how to write basic DSL Queries. In <a href=\"https:\/\/medium.com\/@princesharma_37979\/mastering-elastic-dsl-essential-queries-every-elastic-developer-should-master-874f2fe5d3d0\" target=\"_blank\" rel=\"noreferrer noopener\">my previous blog<\/a> I have already explained this in very simple steps. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In case you are new to Elasticsearch, you can simply go through the blog on mastering DSL essential queries first. Only then you can understand the story of the Search Template.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-let-s-begin-with-the-search-templates\">Let\u2019s begin with the Search Templates<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Elasticsearch Search Templates (formerly known as &#8220;scripted templates&#8221;) allow you to predefine complex queries with placeholders for values that can be filled in at runtime. This can be useful for creating reusable and parameterized queries. I will try to explain the use of Elasticsearch Search Templates through some real world examples.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose you have a requirement from a client in which you have to fetch all the news articles published between 2021-01-01 and 2021-01-08 on index name news_articles.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">GET news_articles\/_search<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">{<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&#8220;query&#8221;: {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&#8220;range&#8221;: {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;publish_date&#8221;: {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;gte&#8221;: &#8220;2021-01-01&#8221;,<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;lt&#8221;: &#8220;2021-01-08&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you go through the basic DSL queries, it won\u2019t take much time to write queries for such requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now, let&#8217;s break down this query step by step:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>GET news_articles\/_search<\/strong>: This part of the query specifies the HTTP method (GET) and the index (news_articles) on which you want to perform the search. In Elasticsearch, data is organized into indices, and in this case, you are searching within an index named &#8220;news_articles.&#8221;<\/li>\n\n\n\n<li>The query body is enclosed within curly braces <strong>{}<\/strong> and is defined using JSON format.<\/li>\n\n\n\n<li><strong>&#8220;query&#8221;<\/strong>: This is the root element of the query DSL and indicates that you are defining a query for Elasticsearch.<\/li>\n\n\n\n<li><strong>&#8220;range&#8221;<\/strong>: Within the query, you are using a &#8220;range&#8221; query type. A range query allows you to search for documents where a specified field falls within a specified range of values.<\/li>\n\n\n\n<li><strong>&#8220;publish_date&#8221;<\/strong>: This specifies the field that you want to apply the range query to. In this case, you are looking at the &#8220;publish_date&#8221; field of the documents in the &#8220;news_articles&#8221; index.<\/li>\n\n\n\n<li><strong>&#8220;gte&#8221; and &#8220;lt&#8221;<\/strong>: These are parameters within the range query.<\/li>\n\n\n\n<li><strong>&#8220;gte&#8221;<\/strong> stands for &#8220;greater than or equal to,&#8221; and you have set it to <strong>&#8220;2021-01-01&#8221;<\/strong>, which means you want to find documents where the <strong>&#8220;publish_date&#8221;<\/strong> is greater than or equal to January 1, 2021.<\/li>\n\n\n\n<li><strong>&#8220;lt&#8221;<\/strong> stands for &#8220;less than,&#8221; and you have set it to <strong>&#8220;2021-01-08&#8221;<\/strong>, which means you want to find documents where the <strong>&#8220;publish_date&#8221;<\/strong> is less than January 8, 2021.<\/li>\n\n\n\n<li>In summary, this Elasticsearch query is searching for documents in the <strong>&#8220;news_articles&#8221;<\/strong> index where the <strong>&#8220;publish_date&#8221;<\/strong> falls within the date range of January 1, 2021, (inclusive) to January 8, 2021 (exclusive). It will return all matching documents that satisfy this condition.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-time-to-replace-above-query-with-a-search-template\">Time to replace above query with a Search Template<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Subsequently, another requirement emerged: you needed to retrieve additional weekly articles, spanning various date ranges such as 2021-01-08 to 2021-01-15, 2021-01-15 to 2021-01-22, and more, from the &#8216;news_article&#8217; index.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As you found yourself repeatedly rewriting the same query with different date ranges, you began to find it tedious. That&#8217;s when you stumbled upon an elegant feature called a Search Template. With this feature, you could define a Search Template with customizable parameters, akin to creating a parametric function that takes inputs, like x and y, performs operations on them, and returns a result. In this case, you could achieve the same level of flexibility and efficiency with your Elasticsearch queries.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can define a Search Template using the Elasticsearch PUT or POST API. You typically store templates in the .scripts index, but you can specify a custom index as well. In our scenario, we will just use the above query and put parameters as mentioned below:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">PUT _scripts\/weekly_articles<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">{<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&#8220;script&#8221;: {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&#8220;lang&#8221;: &#8220;mustache&#8221;,<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&#8220;source&#8221;: {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;query&#8221;: {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;range&#8221;: {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;publish_date&#8221;: {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;gte&#8221;: &#8220;{{start_date}}&#8221;,<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;lt&#8221;: &#8220;{{start_date}}||+1w&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"has-text-color wp-block-paragraph\" style=\"color:#f8485e\"><strong>Note: In this example, {{start_date}} is a placeholder that can be replaced with an actual value at runtime.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s break down this query step by step:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>PUT _scripts\/weekly_articles: This part of the query indicates that you are sending an HTTP PUT request to Elasticsearch to create or update a stored script named &#8220;weekly_articles.&#8221; In Elasticsearch, stored scripts are reusable pieces of code that can be used in various search queries and aggregations.<\/li>\n\n\n\n<li>The script definition is enclosed within curly braces <strong>{}<\/strong> and is defined using JSON format.<\/li>\n\n\n\n<li><strong>&#8220;script&#8221;<\/strong>: This is the root element of the script definition and specifies that you are defining a script.<\/li>\n\n\n\n<li><strong>&#8220;lang&#8221;<\/strong>: &#8220;mustache&#8221;: This line indicates the scripting language used for the script. In this case, it&#8217;s set to &#8220;mustache.&#8221; Elasticsearch supports different scripting languages, and &#8220;mustache&#8221; is one of them.<\/li>\n\n\n\n<li><strong>&#8220;source&#8221;<\/strong>: This is where you define the actual script logic. Inside &#8220;source&#8221;, you have another JSON object.<\/li>\n\n\n\n<li><strong>&#8220;query&#8221;<\/strong>: This is the key where you start defining the query logic that the script will use.<\/li>\n\n\n\n<li><strong>&#8220;range&#8221;<\/strong>: Within the query, you are using a &#8220;range&#8221; query type, similar to the previous example. It allows you to search for documents where a specified field falls within a specified range of values.<\/li>\n\n\n\n<li><strong>&#8220;publish_date&#8221;<\/strong>: This specifies the field that you want to apply the range query to. In this case, it&#8217;s the &#8220;publish_date&#8221; field.<\/li>\n\n\n\n<li><strong>&#8220;gte&#8221;<\/strong> and <strong>&#8220;lt&#8221;<\/strong>: These are parameters within the range query.<\/li>\n\n\n\n<li><strong>&#8220;gte&#8221;<\/strong> stands for &#8220;greater than or equal to.&#8221; It uses the value {{start_date}}, which is a placeholder for a date. The actual date value for <strong>&#8220;start_date&#8221;<\/strong> will be provided when you use this script.<\/li>\n\n\n\n<li><strong>&#8220;lt&#8221;<\/strong> stands for &#8220;less than.&#8221; It uses <strong>{{start_date}}||+1w<\/strong> as the value. {{start_date}} here represents the same placeholder for a date, and ||+1w is a Mustache template filter that adds one week to the &#8220;start_date&#8221; value. This means you want to find documents where the <strong>&#8220;publish_date&#8221;<\/strong> is less than one week after the provided &#8220;start_date&#8221;.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">In summary, this script definition creates a stored script named &#8220;weekly_articles&#8221; that you can later use in Elasticsearch queries. The script&#8217;s purpose is to find documents where the &#8220;publish_date&#8221; falls within a date range starting from the provided &#8220;start_date&#8221; and ending one week later. The actual value for &#8220;start_date&#8221; will be supplied when you use this script in a query.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-it-s-time-to-test-this\">It\u2019s time to test this<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To test a template with different params, use the render Search Template API.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">POST _render\/template<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">{<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&#8220;id&#8221;: \u201cweekly_articles&#8221;, # name of your Search Template as we created weekly_articles.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&#8220;params&#8221;: {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&#8220;start_date&#8221;: \u201c2021-01-01&#8243;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When rendered, the template outputs a search request body. In this request, &#8220;id&#8221; specifies the name of the Search Template, and &#8220;params&#8221; provide the values that should replace the placeholders defined in the template.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-execution\">Execution<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">GET your_index_name\/_search\/template<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">{<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&#8220;id&#8221;: &#8220;weekly_articles&#8221;,<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&#8220;params&#8221;: {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&#8220;start_date&#8221;: &#8220;2021-04-01&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this request, &#8220;id&#8221; specifies the name of the Search Template, and &#8220;params&#8221; provide the values that should replace the placeholders defined in the template.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-as-you-are-doing-so-well-so-you-have-got-another-requirement-requirement-3\">As you are doing so well, so you have got another requirement (requirement 3)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This time the date range is flexible using start_date and end_date parameters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As you already know about Search Templates and you are following the examples in this blog. So, It is not a tough requirement for you.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s write the Search Template.<\/p>\n\n\n\n<h3 class=\"wp-block-heading has-text-color\" id=\"h-we-will-copy-the-above-template-as-it-is\" style=\"color:#f8485e\">We will copy the above template as it is<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">PUT _scripts\/weekly_articles<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">{<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&#8220;script&#8221;: {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&#8220;lang&#8221;: &#8220;mustache&#8221;,<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&#8220;source&#8221;: {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;query&#8221;: {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;range&#8221;: {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;publish_date&#8221;: {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;gte&#8221;: &#8220;{{start_date}}&#8221;,<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;lt&#8221;: &#8220;{{start_date}}||+1w&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<h3 class=\"wp-block-heading has-text-color\" id=\"h-in-it-we-will-add-another-parameter-end-date-and-the-job-is-done-this-is-how-your-final-query-will-look-like\" style=\"color:#f8485e\">In it we will add another parameter {{end_date}} and the job is done. This is how your final query will look like.<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">PUT _scripts\/weekly_articles<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">{<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&#8220;script&#8221;: {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&#8220;lang&#8221;: &#8220;mustache&#8221;,<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&#8220;source&#8221;: {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;query&#8221;: {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;range&#8221;: {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;publish_date&#8221;: {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;gte&#8221;: &#8220;{{start_date}}&#8221;,<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;lt&#8221;: &#8220;{{end_date}}&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-it-s-testing-time\">It&#8217;s testing time<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">POST _render\/template<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">{<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&#8220;id&#8221;: \u201cweekly_articles&#8221;, # name of your Search Template as we created weekly_articles.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&#8220;params&#8221;: {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&#8220;start_date&#8221;: \u201c2021-01-01&#8243;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;\u201cend_date\u201d : \u201c2021-01-08\u201d<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You will notice that the template outputs a search request body with success. That means it is working.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-it-is-time-to-optimize-it\">It is time to optimize it<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To streamline the management of Search Templates, the client has opted for a more efficient approach: instead of creating separate Search Templates for requirements 2 and 3, they propose developing a single template that can cater to both.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For instance, if the &#8216;end_date&#8217; parameter is either absent or empty, the template should automatically return weekly articles by simply extending the &#8216;start_date&#8217; value by one week. On the other hand, if both &#8216;start_date&#8217; and &#8216;end_date&#8217; are provided as parameters, the template should retrieve articles within the specified date range.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This template-driven search mechanism employs a default value strategy. To assign a default value to a variable, use the following syntax:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">{{my-var}}{{^my-var}}default value{{\/my-var}}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In our scenario, the default value is set to {{start_date}}+1w.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now our query like this:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">PUT _scripts\/top_blogs<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">{<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&#8220;script&#8221;: {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&#8220;lang&#8221;: &#8220;mustache&#8221;,<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&#8220;source&#8221;: {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;query&#8221;: {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;range&#8221;: {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;publish_date&#8221;: {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;gte&#8221;: &#8220;{{start_date}}&#8221;,<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;lt&#8221;: &#8220;{{end_date}}{{^end_date}}{{start_date}}||+1w{{\/end_date}}&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-cleanup-process\">Cleanup process<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can delete a Search Template when it&#8217;s no longer needed:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>DELETE \/_scripts\/my_search_template<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This will remove the template from Elasticsearch.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-here-are-some-important-points-to-keep-in-mind\">Here are some important points to keep in mind<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Elasticsearch supports Mustache templates by default. You can use other template languages by installing plugins.<\/li>\n\n\n\n<li>Search Templates can make your queries more maintainable and reusable, especially when you have complex queries that need to be reused with slight variations.<\/li>\n\n\n\n<li>Templates can help prevent SQL injection-like attacks by parameterizing your queries.<\/li>\n\n\n\n<li>Be cautious when allowing user inputs to directly influence the template parameters, as it can still pose security risks if not properly sanitized.<\/li>\n\n\n\n<li>Ensure that you have the appropriate permissions to create, read, and delete scripts\/templates in your Elasticsearch cluster.<\/li>\n\n\n\n<li>Elasticsearch Search Templates can be a powerful tool for managing and reusing complex queries, but they should be used with care to avoid security risks and maintain query performance.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">I want to extend my heartfelt gratitude to you for dedicating your valuable time to explore this blog. I sincerely hope you found it insightful and gained new knowledge about Elasticsearch Search Templates. If you have any queries or seek further exploration of this topic, please don&#8217;t hesitate to reach out.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For those eager to delve deeper into this subject, I encourage you to explore the <a href=\"https:\/\/www.elastic.co\/guide\/en\/elasticsearch\/reference\/current\/search-template.html#search-template-set-default-values\" target=\"_blank\" rel=\"noreferrer noopener\">official Elastic documentation<\/a>, which provides an extensive resource on Elasticsearch Search Templates.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What if I told you that Elastic offers robust support for stored search features? With this capability, you can execute queries with a wide range of parameters, much like creating a parametric function in MySQL. A Search Template is essentially a stored search that you can execute with varying input values. When Elasticsearch serves as [&hellip;]<\/p>\n","protected":false},"featured_media":361997,"template":"","categories":[],"tags":[3266,2729],"industry":[],"class_list":["post-517158","expert-view","type-expert-view","status-publish","has-post-thumbnail","hentry","tag-elastic-me","tag-netherlands-me"],"acf":[],"cards":"\n\t<div class=\"single-post-card\">\n\n\t\t<figure class=\"wp-block-post-featured-image\"><a href=\"https:\/\/devoteam.info\/me\/expert-view\/unlocking-the-power-of-elasticsearch-search-templates\/\" target=\"_self\" ><img width=\"2560\" height=\"1700\" src=\"https:\/\/devoteam.info\/wp-content\/uploads\/2024\/12\/GettyImages-113174155_edited-scaled-1.jpg\" class=\"attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"Unlocking the Power of Elasticsearch Search Templates\" style=\"aspect-ratio:4\/3;width:100%;object-fit:cover;\" decoding=\"async\" loading=\"lazy\" srcset=\"https:\/\/devoteam.info\/wp-content\/uploads\/2024\/12\/GettyImages-113174155_edited-scaled-1.jpg 2560w, https:\/\/devoteam.info\/wp-content\/uploads\/2024\/12\/GettyImages-113174155_edited-scaled-1-300x199.jpg 300w, https:\/\/devoteam.info\/wp-content\/uploads\/2024\/12\/GettyImages-113174155_edited-scaled-1-1024x680.jpg 1024w, https:\/\/devoteam.info\/wp-content\/uploads\/2024\/12\/GettyImages-113174155_edited-scaled-1-768x510.jpg 768w, https:\/\/devoteam.info\/wp-content\/uploads\/2024\/12\/GettyImages-113174155_edited-scaled-1-1536x1020.jpg 1536w, https:\/\/devoteam.info\/wp-content\/uploads\/2024\/12\/GettyImages-113174155_edited-scaled-1-2048x1360.jpg 2048w\" sizes=\"auto, (max-width: 2560px) 100vw, 2560px\" \/><\/a><\/figure>\n\n\t\t\n\t\t<div class=\"wp-block-group is-vertical is-layout-flex wp-container-core-group-is-layout-43282307 wp-block-group-is-layout-flex\">\n\t<p style=\"font-style:normal;font-weight:700\" class=\"has-link-color wp-elements-1 wp-block-lp-post-type has-text-color has-primary-color has-small-font-size\">Expert View<\/p>\n\n\t\t\n\t\t<h3 style=\"font-style:normal;font-weight:400\" class=\"wp-block-post-title has-base-font-size\"><a href=\"https:\/\/devoteam.info\/me\/expert-view\/unlocking-the-power-of-elasticsearch-search-templates\/\" target=\"_self\" >Unlocking the Power of Elasticsearch Search Templates<\/a><\/h3><\/div>\n\t\t\n\t<\/div>\n\n","yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v28.4 (Yoast SEO v28.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Unlocking the Power of Elasticsearch Search Templates | Devoteam<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/devoteam.info\/me\/expert-view\/unlocking-the-power-of-elasticsearch-search-templates\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Unlocking the Power of Elasticsearch Search Templates\" \/>\n<meta property=\"og:description\" content=\"What if I told you that Elastic offers robust support for stored search features? With this capability, you can execute queries with a wide range of parameters, much like creating a parametric function in MySQL. A Search Template is essentially a stored search that you can execute with varying input values. When Elasticsearch serves as [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/devoteam.info\/me\/expert-view\/unlocking-the-power-of-elasticsearch-search-templates\/\" \/>\n<meta property=\"og:site_name\" content=\"Devoteam\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/devoteam.info\\\/me\\\/expert-view\\\/unlocking-the-power-of-elasticsearch-search-templates\\\/\",\"url\":\"https:\\\/\\\/devoteam.info\\\/me\\\/expert-view\\\/unlocking-the-power-of-elasticsearch-search-templates\\\/\",\"name\":\"Unlocking the Power of Elasticsearch Search Templates | Devoteam\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/devoteam.info\\\/me\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/devoteam.info\\\/me\\\/expert-view\\\/unlocking-the-power-of-elasticsearch-search-templates\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/devoteam.info\\\/me\\\/expert-view\\\/unlocking-the-power-of-elasticsearch-search-templates\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/devoteam.info\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/GettyImages-113174155_edited-scaled-1.jpg\",\"datePublished\":\"2023-09-14T12:14:44+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/devoteam.info\\\/me\\\/expert-view\\\/unlocking-the-power-of-elasticsearch-search-templates\\\/#breadcrumb\"},\"inLanguage\":\"en-SA\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/devoteam.info\\\/me\\\/expert-view\\\/unlocking-the-power-of-elasticsearch-search-templates\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-SA\",\"@id\":\"https:\\\/\\\/devoteam.info\\\/me\\\/expert-view\\\/unlocking-the-power-of-elasticsearch-search-templates\\\/#primaryimage\",\"url\":\"https:\\\/\\\/devoteam.info\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/GettyImages-113174155_edited-scaled-1.jpg\",\"contentUrl\":\"https:\\\/\\\/devoteam.info\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/GettyImages-113174155_edited-scaled-1.jpg\",\"width\":2560,\"height\":1700},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/devoteam.info\\\/me\\\/expert-view\\\/unlocking-the-power-of-elasticsearch-search-templates\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/devoteam.info\\\/me\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Expert View\",\"item\":\"https:\\\/\\\/devoteam.info\\\/me\\\/expert-view\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Unlocking the Power of Elasticsearch Search Templates\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/devoteam.info\\\/me\\\/#website\",\"url\":\"https:\\\/\\\/devoteam.info\\\/me\\\/\",\"name\":\"Devoteam\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/devoteam.info\\\/me\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-SA\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Unlocking the Power of Elasticsearch Search Templates | Devoteam","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/devoteam.info\/me\/expert-view\/unlocking-the-power-of-elasticsearch-search-templates\/","og_locale":"en_US","og_type":"article","og_title":"Unlocking the Power of Elasticsearch Search Templates","og_description":"What if I told you that Elastic offers robust support for stored search features? With this capability, you can execute queries with a wide range of parameters, much like creating a parametric function in MySQL. A Search Template is essentially a stored search that you can execute with varying input values. When Elasticsearch serves as [&hellip;]","og_url":"https:\/\/devoteam.info\/me\/expert-view\/unlocking-the-power-of-elasticsearch-search-templates\/","og_site_name":"Devoteam","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/devoteam.info\/me\/expert-view\/unlocking-the-power-of-elasticsearch-search-templates\/","url":"https:\/\/devoteam.info\/me\/expert-view\/unlocking-the-power-of-elasticsearch-search-templates\/","name":"Unlocking the Power of Elasticsearch Search Templates | Devoteam","isPartOf":{"@id":"https:\/\/devoteam.info\/me\/#website"},"primaryImageOfPage":{"@id":"https:\/\/devoteam.info\/me\/expert-view\/unlocking-the-power-of-elasticsearch-search-templates\/#primaryimage"},"image":{"@id":"https:\/\/devoteam.info\/me\/expert-view\/unlocking-the-power-of-elasticsearch-search-templates\/#primaryimage"},"thumbnailUrl":"https:\/\/devoteam.info\/wp-content\/uploads\/2024\/12\/GettyImages-113174155_edited-scaled-1.jpg","datePublished":"2023-09-14T12:14:44+00:00","breadcrumb":{"@id":"https:\/\/devoteam.info\/me\/expert-view\/unlocking-the-power-of-elasticsearch-search-templates\/#breadcrumb"},"inLanguage":"en-SA","potentialAction":[{"@type":"ReadAction","target":["https:\/\/devoteam.info\/me\/expert-view\/unlocking-the-power-of-elasticsearch-search-templates\/"]}]},{"@type":"ImageObject","inLanguage":"en-SA","@id":"https:\/\/devoteam.info\/me\/expert-view\/unlocking-the-power-of-elasticsearch-search-templates\/#primaryimage","url":"https:\/\/devoteam.info\/wp-content\/uploads\/2024\/12\/GettyImages-113174155_edited-scaled-1.jpg","contentUrl":"https:\/\/devoteam.info\/wp-content\/uploads\/2024\/12\/GettyImages-113174155_edited-scaled-1.jpg","width":2560,"height":1700},{"@type":"BreadcrumbList","@id":"https:\/\/devoteam.info\/me\/expert-view\/unlocking-the-power-of-elasticsearch-search-templates\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/devoteam.info\/me\/"},{"@type":"ListItem","position":2,"name":"Expert View","item":"https:\/\/devoteam.info\/me\/expert-view\/"},{"@type":"ListItem","position":3,"name":"Unlocking the Power of Elasticsearch Search Templates"}]},{"@type":"WebSite","@id":"https:\/\/devoteam.info\/me\/#website","url":"https:\/\/devoteam.info\/me\/","name":"Devoteam","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/devoteam.info\/me\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-SA"}]}},"uagb_featured_image_src":{"full":["https:\/\/devoteam.info\/wp-content\/uploads\/2024\/12\/GettyImages-113174155_edited-scaled-1.jpg",2560,1700,false],"thumbnail":["https:\/\/devoteam.info\/wp-content\/uploads\/2024\/12\/GettyImages-113174155_edited-scaled-1-150x150.jpg",150,150,true],"medium":["https:\/\/devoteam.info\/wp-content\/uploads\/2024\/12\/GettyImages-113174155_edited-scaled-1-300x199.jpg",300,199,true],"medium_large":["https:\/\/devoteam.info\/wp-content\/uploads\/2024\/12\/GettyImages-113174155_edited-scaled-1-768x510.jpg",768,510,true],"large":["https:\/\/devoteam.info\/wp-content\/uploads\/2024\/12\/GettyImages-113174155_edited-scaled-1-1024x680.jpg",1024,680,true],"1536x1536":["https:\/\/devoteam.info\/wp-content\/uploads\/2024\/12\/GettyImages-113174155_edited-scaled-1-1536x1020.jpg",1536,1020,true],"2048x2048":["https:\/\/devoteam.info\/wp-content\/uploads\/2024\/12\/GettyImages-113174155_edited-scaled-1-2048x1360.jpg",2048,1360,true]},"uagb_author_info":{"display_name":"Julien Pawlowski","author_link":"https:\/\/devoteam.info\/me\/author\/"},"uagb_comment_info":0,"uagb_excerpt":"What if I told you that Elastic offers robust support for stored search features? With this capability, you can execute queries with a wide range of parameters, much like creating a parametric function in MySQL. A Search Template is essentially a stored search that you can execute with varying input values. When Elasticsearch serves as&hellip;","_links":{"self":[{"href":"https:\/\/devoteam.info\/me\/wp-json\/wp\/v2\/expert-view\/517158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devoteam.info\/me\/wp-json\/wp\/v2\/expert-view"}],"about":[{"href":"https:\/\/devoteam.info\/me\/wp-json\/wp\/v2\/types\/expert-view"}],"version-history":[{"count":0,"href":"https:\/\/devoteam.info\/me\/wp-json\/wp\/v2\/expert-view\/517158\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devoteam.info\/me\/wp-json\/wp\/v2\/media\/361997"}],"wp:attachment":[{"href":"https:\/\/devoteam.info\/me\/wp-json\/wp\/v2\/media?parent=517158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devoteam.info\/me\/wp-json\/wp\/v2\/categories?post=517158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devoteam.info\/me\/wp-json\/wp\/v2\/tags?post=517158"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/devoteam.info\/me\/wp-json\/wp\/v2\/industry?post=517158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}