# Approaches used for query templating in ES?

**URL:** <https://discuss.elastic.co/t/approaches-used-for-query-templating-in-es/11680>\
**Category:** Elasticsearch\
**Created:** [April 24, 2013, 10:09am UTC](https://discuss.elastic.co/t/approaches-used-for-query-templating-in-es/11680 "2013-04-24T10:09:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Harwood1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood1/32/101255_2.png) [@Mark\_Harwood1](https://discuss.elastic.co/u/Mark_Harwood1)\
**Post date:** [April 24, 2013, 10:09am UTC](https://discuss.elastic.co/t/approaches-used-for-query-templating-in-es/11680/1 "2013-04-24T10:09:54Z")

</div>

I'm curious as to what approaches folks have used (if any) for wrapping ES  
services.

The objectives I have are:

1. Isolate browser clients from raw ES search syntax
2. Provide a server-side Java service that converts untrusted web requests  
with basic user criteria (e.g. keywords, date ranges etc) into trusted ES  
json query DSL and call ES then return results.

The service in 2) however should use a form of templating so that it is  
possible for admin staff to modify the query logic (e.g. prefix-lengths,  
boosts etc ) in a simple text file containing the query template rather  
than needing to edit and recompile Java code to tweak query logic.  
This is similar to the design practice of externalizing SQL statements used  
by a database app into a text file or using Lucene's XML query syntax and  
storing template queries as XSLT files that can be easily edited.

While XSLT served me well with Lucene apps I'm not sure what the  
alternatives are for the JSON-based syntax used by ES.

The templating options I can think of are:

1. Velocity
2. .js Javascript functions interpreted by Rhino
3. Groovy scripts

Velocity seems more suited to generating mark up like HTML and I expect  
using it would make butt-ugly JSON templates.  
Javascript functions would be a more natural language for JSON construction  
but any library dependencies e.g. date-formatting routines like moment.js  
would need pre-loading into Rhino environment.  
Groovy has the advantage that is Java-based and can access any number of  
libraries in the JVM through imports but its JSON-builder syntax has some  
subtle syntax differences to JSON that may be annoying in practice if you  
cut-and-paste example JSON from the ES docs and paste into your templates.

If JSON templating is too messy another option is create an XML wrapper for  
all ES functions only so that we can continue use of XSLT for templating.

Anyone else have any thoughts on this?

Cheers  
Mark

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 2:39am UTC](https://discuss.elastic.co/t/approaches-used-for-query-templating-in-es/11680/2 "2017-07-06T02:39:49Z")

</div>


