0

I am trying to pass an array of integers to ElasticSearch template using the below mustache template.

{{#filter5_terms}} 
"terms": {
"{{filter5_name}}": [
"{{#filter5_lt}}", 
"{{.}}",                                
"{{/filter5_lt}}"  ]
}
{{/filter5_terms}}

Above works, If I pass a string array (Ex: ["A","B"]. But the same is failing with the int array [1,2] with Nested: NumberFormatException[For input string: ""]; error.

Reference: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-template.html#_passing_an_array_of_strings

Can you please let me know if I am missing anything?

Thanks Anil

Anil Kumar
  • 127
  • 1
  • 10

2 Answers2

2

You really shouldn't rely on that, as the format is an inner implementation of Mustache and thus, subject to change. For example, if you try to emulate that using mustache.js, you'll get something like:

"terms: {
  "property": 3,4
}

To workaround this problem, you should add square brackets to the templated values. So, your example becomes:

"terms": {
  "{{filter5_name}}": [{{filter5_lt}}]
}

And that will get you what you want.

At least, this is true in mustache.js 2.2.1

Nicolás Fantone
  • 2,055
  • 1
  • 18
  • 24
1

I did fix this. We can use the below to replace the integer array into ElasticSearch query.

"terms": {
"{{filter5_name}}": {{filter5_lt}}
}

ElasticSearch documentation has an example to replace string arrays and I tried to use the same for integer arrays and it did not work.

So I had to use the above which is provided in Mustache templating examples.

Thanks Anil

Anil Kumar
  • 127
  • 1
  • 10
  • Hi Anil, Can I know the full syntax please ? I am trying the below , but not working . Can I know what I am doing wrong here ? POST _scripts/Select-Users-ByIDs { "script": { "lang": "mustache", "source": { "query": { "terms": { "{{filter5_name}}": "{{filter5_lt}}" } } } } } GET cs_user/cs_user/_search/template { "id": "Select-Users-ByIDs", "params": { "filter5_lt":[1,2], "filter5_name":"_id" } } – Bujji Jan 31 '19 at 21:09
  • In your solution the json is essentially malformed... To make it a valid ES query you have to wrap `{{filter5_lt}}` with quotes, then we are back to the very beginning.... : ( ``` { "terms" : { "{{filter5_name}}" : "{{filter5_lt}}" } } ``` – user1767408 Oct 31 '20 at 02:35