# Mustache double quotes problem in search templates

**URL:** <https://discuss.elastic.co/t/mustache-double-quotes-problem-in-search-templates/318736>\
**Category:** Elasticsearch\
**Created:** [November 11, 2022, 11:29am UTC](https://discuss.elastic.co/t/mustache-double-quotes-problem-in-search-templates/318736 "2022-11-11T11:29:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![D1sturbance](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/d1sturbance/32/98965_2.png) [@D1sturbance](https://discuss.elastic.co/u/D1sturbance)\
**Post date:** [November 11, 2022, 11:29am UTC](https://discuss.elastic.co/t/mustache-double-quotes-problem-in-search-templates/318736/1 "2022-11-11T11:29:18Z")

</div>

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:  
Template snippet:

```auto
                    "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:

```auto
 "must": {
                "function_score": {
                    "functions": [
                        "",
                        {
                            "random_score": {},
                            "weight": 1.0E-5
                        }
                    ],
                    "score_mode": "sum"
                }
            }

```

Error I get trying to run the template on ELK:

```auto
    "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.

---

<div class="post-metadata">

**Author:** ![canelia](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/canelia/32/141857_2.png) [@canelia](https://discuss.elastic.co/u/canelia)\
**Post date:** [November 14, 2022, 12:37am UTC](https://discuss.elastic.co/t/mustache-double-quotes-problem-in-search-templates/318736/2 "2022-11-14T00:37:55Z")

</div>

try it.

```auto
 "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"
                        }
                    }

```

---

<div class="post-metadata">

**Author:** ![D1sturbance](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/d1sturbance/32/98965_2.png) [@D1sturbance](https://discuss.elastic.co/u/D1sturbance)\
**Post date:** [November 14, 2022, 8:26am UTC](https://discuss.elastic.co/t/mustache-double-quotes-problem-in-search-templates/318736/3 "2022-11-14T08:26:36Z")

</div>

I've tried that and I am getting error while uploading template to ELK:

Snippet:

```auto
"reason": "com.fasterxml.jackson.core.JsonParseException: Unexpected character ('{' (code 123)): was expecting double-quote to start field name\n at

```

---

<div class="post-metadata">

**Author:** ![canelia](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/canelia/32/141857_2.png) [@canelia](https://discuss.elastic.co/u/canelia)\
**Post date:** [November 15, 2022, 12:17am UTC](https://discuss.elastic.co/t/mustache-double-quotes-problem-in-search-templates/318736/4 "2022-11-15T00:17:10Z")

</div>

try it.

```auto
POST /_scripts/function_template
{
	"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"
				}
			}
		}"""
	}
}

GET /_render/template
{
	"id": "function_template",
	"params": {}
}

```

---

<div class="post-metadata">

**Author:** ![D1sturbance](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/d1sturbance/32/98965_2.png) [@D1sturbance](https://discuss.elastic.co/u/D1sturbance)\
**Post date:** [November 15, 2022, 5:57am UTC](https://discuss.elastic.co/t/mustache-double-quotes-problem-in-search-templates/318736/5 "2022-11-15T05:57:38Z")

</div>

No use still cannot upload script to ELK engine. I took exact code you wrote to test it out.

```auto

{
    "error": {
        "root_cause": [
            {
                "type": "x_content_parse_exception",
                "reason": "[4:19] [stored script source] failed to parse object"
            }
        ],
        "type": "x_content_parse_exception",
        "reason": "[4:19] [stored script source] failed to parse object",
        "caused_by": {
            "type": "json_parse_exception",
            "reason": "Unexpected character ('{' (code 123)): was expecting comma to separate Object entries\n at [Source: (org.elasticsearch.common.io.stream.ByteBufferStreamInput); line: 4, column: 22]"
        }
    },
    "status": 400
}

```

---

<div class="post-metadata">

**Author:** ![D1sturbance](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/d1sturbance/32/98965_2.png) [@D1sturbance](https://discuss.elastic.co/u/D1sturbance)\
**Post date:** [November 15, 2022, 1:02pm UTC](https://discuss.elastic.co/t/mustache-double-quotes-problem-in-search-templates/318736/6 "2022-11-15T13:02:23Z")

</div>

Ok. Issue seems that in Postman it's not working, but in DEV tools it is working. Maybe you know how is that? 🙂

---

<div class="post-metadata">

**Author:** ![canelia](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/canelia/32/141857_2.png) [@canelia](https://discuss.elastic.co/u/canelia)\
**Post date:** [November 16, 2022, 12:25am UTC](https://discuss.elastic.co/t/mustache-double-quotes-problem-in-search-templates/318736/7 "2022-11-16T00:25:44Z")

</div>

Maybe it works in elasticsearch. 😃

---

<div class="post-metadata">

**Author:** ![D1sturbance](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/d1sturbance/32/98965_2.png) [@D1sturbance](https://discuss.elastic.co/u/D1sturbance)\
**Post date:** [November 16, 2022, 7:52am UTC](https://discuss.elastic.co/t/mustache-double-quotes-problem-in-search-templates/318736/8 "2022-11-16T07:52:23Z")

</div>

Seems that 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:

```auto
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\" }}}}"
  }
}

```

Basically from DEV tools perspective you are correct with triple double quotes. It solves the problem. But if you want to use other tools it has to be escaped one-line. 😃

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 14, 2022, 7:53am UTC](https://discuss.elastic.co/t/mustache-double-quotes-problem-in-search-templates/318736/9 "2022-12-14T07:53:12Z")

</div>

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