What is the best way to use mustache False values feature in Elasticsearch template?
At the moment I am trying to select function based on boolean value. Rendering seems to be working according to the logic, but it prints empty double quotes and I cannot get rid of those.
Code example mustache template snippet:
"must": {
"function_score": {
"functions": [
"{{^isLocationFunctionNeeded}}",
{
"exp": {
"location": {
"origin": {
"lat": "0.0",
"lon": "0.0"
},
"offset": "1km",
"scale": "50km"
}
}
},
"{{/isLocationFunctionNeeded}}",
{
"random_score": {},
"weight": 0.00001
}
],
"score_mode": "sum"
}
}
Render snippet:
"must": {
"function_score": {
"functions": [
"",
{
"random_score": {},
"weight": 1.0E-5
}
],
"score_mode": "sum"
}
}
Error I get trying to run the template on ELK:
"error": {
"root_cause": [
{
"type": "parsing_exception",
"reason": "failed to parse [START_OBJECT]. malformed query, expected a [VALUE_STRING] while parsing functions but got a [function_score] instead",
"line": x (where "" is visible in Render snippet),
"col": x (where "" is visible in Render snippet)
}
],
"type": "x_content_parse_exception",
"reason": " x (where "" is visible in Render snippet),[bool] failed to parse field [must]",
"caused_by": {
"type": "parsing_exception",
"reason": "failed to parse [START_OBJECT]. malformed query, expected a [VALUE_STRING] while parsing functions but got a [function_score] instead",
"line": x (where "" is visible in Render snippet),,
"col": x (where "" is visible in Render snippet),
}
Without mustache values it's working fine. Also I noticed in some cases if you surround empty double quotes with random functions it tends to work sometimes. Seems Elastic don't like must cases starting with empty double quotes.
I also asked the same question in ELK community with no luck so far: https://discuss.elastic.co/t/mustache-double-quotes-problem-in-search-templates/318736
As an example for rendering template we can try using the following:
{
"script": {
"lang": "mustache",
"source": {
"must": {
"function_score": {
"functions": [
"{{^isLocationFunctionNeeded}}",
{
"exp": {
"location": {
"lat": "0.0",
"lon": "0.0"
},
"offset": "1km",
"scale": "50km"
}
},
"{{/isLocationFunctionNeeded}}",
{
"random_score": {},
"weight": 0.00001
}
],
"score_mode": "sum"
}
}
}
}
}
Calling template with params:
{
"id": "example_template",
"params": {
"isLocationFunctionNeeded" : true
}
}
CodePudding user response:
The query inside your template must be a full fledge query, not just must
. Also you need to enclose it in triple quotes """
, like this, and it will work
POST _scripts/example_template
{
"script": {
"lang": "mustache",
"source": """
{
"query": {
"bool": {
"must": {
"function_score": {
"functions": [
{{^isLocationFunctionNeeded}}
{
"exp": {
"location": {
"lat": "0.0",
"lon": "0.0"
},
"offset": "1km",
"scale": "50km"
}
},
{{/isLocationFunctionNeeded}}
{
"random_score": {},
"weight": 0.00001
}
],
"score_mode": "sum"
}
}
}
}
}
"""
}
}
If you cannot use the triple quotes (e.g. when using Postman), you need to send it as a one-line string and escape all quote characters:
POST _scripts/example_template
{
"script": {
"lang": "mustache",
"source": " { \"query\": { \"bool\": { \"must\": { \"function_score\": { \"functions\": [ {{^isLocationFunctionNeeded}} { \"exp\": { \"location\": { \"lat\": \"0.0\", \"lon\": \"0.0\" }, \"offset\": \"1km\", \"scale\": \"50km\" } }, {{/isLocationFunctionNeeded}} { \"random_score\": {}, \"weight\": 0.00001 } ], \"score_mode\": \"sum\" }}}}"
}
}