Evaluate non-empty list to true in mustache

Quick question on mustache:

Is it possible to evaluate a conditional on a list just once? That is, instead of iterating and applying the template to each element in the array, evaluate it as non-empty and do stuff in it only once. For example

{"terms": { "foo": "{{#toJson}}bar{{/toJson}}" }}

would be rendered as

{"terms": {"foo": ["bar1", "bar2", "bar3"]}}

if array is a non-empty list, and if empty the block is not evaluated. Tags with # and / are placeholders. Hope this makes sense.

Note: If this is syntactically not possible with mustache, we can obviously just surround all of the sub-queries with something like a bool-should block. This however necessitates some akward stuff with commas or a final match_all, match_none query.

Just in case you have not seen it, there is a quick paragraph about conditionals in mustache in the search template docs https://www.elastic.co/guide/en/elasticsearch/reference/7.5/search-template.html#search-template-conditional-clauses

I am however not absolutely sure, if that solves your use-case in this problem.

1 Like

Unfortunately this does not seem to be a use case which is covered by mustache ... anyway, we were able to restructure the query in a way that uses the elements of the array (which is probably what you'd rather do.)

1 Like

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