Function toJson in stored search template

Hello,

I'm trying to save and call a search template having a parameter that is transformed using the toJson function. Here is an example:

POST _scripts/testScript
{
"script": {
"lang": "mustache",
"source": "{ "query": { "terms" : { "status" : {{#toJson}}statuses{{/toJson}} }}}" }}

The render function is correctly generating the query:

GET _render/template/testScript
{
"params": {
"statuses" : [ "pending", "published" ]
}
}
}

Output:
{
"template_output" : {
"query" : {
"terms" : {
"status" : [
"pending",
"published"
]
}
}
}
}

However, when I call it to actually perform the search, I receive an error message:

GET _search/template
{
"id": "testScript"
"params": {
"statuses" : [ "pending", "published" ]
}
}

Output:
{
"error": {
"root_cause": [
{
"type": "json_parse_exception",
"reason": "Unexpected character ('"' (code 34)): was expecting comma to separate Object entries\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@43e766ba; line: 3, column: 4]"
}
],
"type": "json_parse_exception",
"reason": "Unexpected character ('"' (code 34)): was expecting comma to separate Object entries\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@43e766ba; line: 3, column: 4]"
},
"status": 500
}

I tried different strategies : inline definition, not inline, getting the terms keyword inside the parameter, etc. I always end up having the same parsing error.

Anyone has an idea what I'm misssing here? I'm using ES 7.3.2.

Thanks!

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