Elasticsearch Mustache optional parameters
A workaround probably not the most elegant would be to change template query to be a should
clause and add a match_all
clause for empty list.
example:
{ "filter" : { "bool" : { "should" : [ { "terms" : { "status" : [ "{{#ProductIDs}}","{{.}}","{{/ProductIDs}}"] }} {{^ProductIDs}}, {"match_all":{}} {{/ProductIDs}} ] } }}
Didn't try it, but shouldn't something like this work?
{ "filter" : { "bool" : { "must" : [ {{#ProductIDs}} {{^ProductIDs.isEmpty}} { "terms" : { "Product.ProductID" : [{{#ProductIDs}}{{.}},{{/ProductIDs}}] } } {{/ProductIDs.isEmpty}} {{#ProductIDs.isEmpty}} {"match_all":{}} {{/ProductIDs.isEmpty}} {{/ProductIDs}} {{^ProductIDs}} {"match_all":{}} {{/ProductIDs}} ] } }}
Ain't pretty, maybe there's better way.
My suggestion to overcome this using a JSON template is:
{ "query": { "bool": { "must": [ { "script": { "script": { "inline": "1==1 {{#ProductIDs}} && [\"{{#ProductIDs}}\",\"{{.}}\",\"{{/ProductIDs}}\"].contains(doc['Product.ProductID'].value){{/ProductIDs}}", "lang": "painless" } } } ] }}