Blog Query Elasticsearch Template Using Knowi

Query Elasticsearch Template Using Knowi

Query Elasticsearch Template Using Knowi

Query Elasticsearch template in Knowi by allowing the end-user queries to pass as a parameter into the search template. This prevents your query-building logic from being exposed to the end user.  

This blog post covers the following topics: 

What is an Elasticsearch Template?

An Elasticsearch template is like a stored procedure. It is a standard query with replacement variables (template parameters) expressed using the Mustache scripting language. These templates work like a placeholder for variables that are defined in Elasticsearch queries. 

Please refer to the Elasticsearch Documentation to learn more about search templates.

Benefits of Having an Elasticsearch Template?

Elasticsearch templates make it easy to convert the user input into Elasticsearch queries. Simply modify the template for id and parameter(s) based on your requirements and run the query against it. 

Elasticsearch Template Example

    "id": "my-search-template",
    "params": {

        "query_string": "Marketing"

Let’s understand the benefit of the Elasticsearch template with the help of an example: 

“Application X is one of the fastest growing products in Silicon Valley. As this application was evolving, the code responsible for building the query was at risk of becoming heavy and unmanageable. On top of that, the development team of Application X also identified the new types of queries that needed to be executed but didn’t precisely fit into the current query-building logic. 

In other words, Application X scaled to a level where it needed flexibility, concerning the number of queries and type of queries it could handle without exposing the Elasticsearch Query DSL (Domain Specific Language). 

This is where the Elasticsearch template comes to the rescue.” 

By simply passing parameters into the search template, end users can execute the query and searches without seeing the query-building logic. If you need to add fields to your search query, simply modify the template, query it, and view the results.  

Query an Elasticsearch Template

Knowi automatically detects Elasticsearch templates in the code editor and allows you to query it as shown below:

Important Notes

Note I: Knowi’s Elasticsearch date type handling doesn’t detect Epoch like the rest of the datasource.

Note II: Knowi does not support running multiple templated searches in the same query. 

Note III: Knowi does not support creating the Elasticsearch template. 


Querying the Elasticsearch template simplifies the process of generating Elasticsearch queries within Knowi. Troubleshooting is also easier because you can invoke the same template that your application is using without the need to worry about whether the Java API is building the query the way it should. So, if you are writing an Elasticsearch query from scratch, consider querying an existing Elasticsearch template instead. 

Want to see how Knowi can help?

Sign up here for a 15-minute demo where we will learn more about your use case and recommend a few features that may be a good fit for what you're trying to accomplish.

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email
About the Author: