How do you preregister a search template with conditional clauses?


(Ashok) #1

We use mustache search templates that have conditional clauses (those # symbols). Worked fine in 5.x when we could simply dump them to config/scripts folder... and ES would reload them pretty quick

Does not look like this file option is supported any more... 6.2.4 does not seem to process this folder... Is that correct or am I missing something?

And if we try to pre-register these templates, these '#' symbols cause issues... even when we escape the whole template source to be a String...

The documentation says that:

As written above, this template is not valid JSON because it includes the section markers like {{#line_no}}. For this reason, the template should either be stored in a file (see Pre-registered templateedit) or, when used via the REST API, should be written as a string:

This question was asked earlier by Jack_Fazackerley but was closed for no answers...

There is no path forward for these templates then in 6.x?

Thank you


#2

Could you send us your query template?
I faced this issue in the past, and i noticed the conditional clauses must be properly formatted.


(Ashok) #3

I think you are on to somethig there klof. Mucking with the format, I was able to put a simple template into cluster state. My real templates are hundreds of lines long so no luck there yet.

Here is the simple template that I was able to massage.

{
  "script": {
    "lang": "mustache",
    "source": {
      "query": {
        "match" : {
          "custom_all" : "{{queryString}}"
        }
      }
      {{#from}}
        ,"from": "{{from}}"
      {{/from}}
      {{^from}}
        ,"from": "0"
      {{/from}}
      {{#nresults}}
        ,"size": "{{nresults}}"
      {{/nresults}}
      {{^nresults}}
        ,"size": "100"
      {{/nresults}}
    }
  }
}

I can put it in cluster state if and only if

  • Escape the whole "source" object so we get a String value for "source" element

  • And turn the whole thing into a single line (remove all new lines)

Surprised by the 2nd requirement.

With those in place, I can run something like

curl -X POST -H 'Content-Type: application/json; charset=UTF-8' http://localhost:9200/_scripts/testing_conditionals_escaped_oneline -d @./scripts/testing_conditionals_escaped_oneline.mustache


(Ashok) #4

As for that surprise 2nd requirement, it turns out I had some hidden tabs in the old template files that were messing up. Removing those took care of it. No need to remove new lines.

Thx for the pointer klof!


#5

Yes exactly, remove all the new lines helps.
But, it's not the proper way to do it, especially when you have to maintain a huge template.
you should be able to insert the template "pretty_printed".
Do you mind to send your big template in a gist file?


(system) #6

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.