Mustache toJSON tag issue

Hi All,
I'm trying to create a search template:

{
  "script": {
    "lang": "mustache",
    "source": """{
      "query": {
        "bool": {
          "must": [
            {{#docyear}}{
    		      "terms": {
		            "doc.year": "{{#toJson}}{{years}}{{/toJson}}"
			        }
		        },{{/docyear}}
		        {{#doctype}}{
		          "terms": {
		            "doc.type": "{{#toJson}}{{types}}{{/toJson}}"
			        }
		        },{{/doctype}}
		        {{#docslan}}{
		          "terms": {
		            "doc.slan": "{{#toJson}}{{langs}}{{/toJson}}"
			        }
		        },{{/docslan}}
            {{#regexp}}{
              "regexp": {
                "doc.num": {
                  "value": "{{regexp}}",
                  "flags": "ALL",
                  "case_insensitive": true,
                  "max_determinized_states": 10000
                }
              }
			      },{{/regexp}}
			      {
              "query_string": {
                "query": "{{query_string}}",
                "default_operator": "AND",
                "default_field": "EN.lo_entry.basic",
                "phrase_slop": 0
              }
            }
          ],
          "minimum_should_match": 1,
          "should": [
            {
              "exists": {
                "field": "{{query_lang}}"
              }
            }
          ]
        }
      },
      "from": "{{from}}{{^from}}0{{/from}}",
      "size": "{{size}}{{^size}}30{{/size}}",
      "sort": [
        {
          "doc.year": {
            "order": "desc"
          }
        }
      ],
      "_source": [
        {{^id_lo_only}}"{{query_lang}}",
        "EN",{{/id_lo_only}}
        "id_lo"{{^id_lo_only}},
        "id_doc",
        "id_mem",
        "doc"{{/id_lo_only}}
      ]{{^id_lo_only}},
      "highlight": {
        "number_of_fragments": 0,
        "pre_tags": "<b>",
        "post_tags": "</b>",
        "fields": {
          "EN.lo_entry.basic": {}
        }
      }{{/id_lo_only}}
    }"""
  }
}

If I render this with the following request:

{
  "id": "test-search-template",
  "params": {
    "query_string": "\"contract agents\"",
    "query_lang": "ES",
    "id_lo_only": true,
    "docslan": {"langs": ["EN","ES","FI"]},
    "doctype": {"types": ["Regulation"]},
    "docyear": {"years": ["2023","2018"]},
  }
}

I get empty result for the docyear, teh doctype and the docslan params:

{
  "template_output" : {
    "query" : {
      "bool" : {
        "must" : [
          {
            "terms" : {
              "doc.year" : ""
            }
          },
          {
            "terms" : {
              "doc.type" : ""
            }
          },
          {
            "terms" : {
              "doc.slan" : ""
            }
          },
          {
            "query_string" : {
              "query" : "\"contract agents\"",
              "default_operator" : "AND",
              "default_field" : "EN.lo_entry.basic",
              "phrase_slop" : 0
            }
          }
        ],
        "minimum_should_match" : 1,
        "should" : [
          {
            "exists" : {
              "field" : "ES"
            }
          }
        ]
      }
    },
    "from" : "0",
    "size" : "30",
    "sort" : [
      {
        "doc.year" : {
          "order" : "desc"
        }
      }
    ],
    "_source" : [
      "id_lo"
    ]
  }
}

If I follow the documentation and remove the {{}} brackets from the parameters (like below):

{
  "script": {
    "lang": "mustache",
    "source": """{
      "query": {
        "bool": {
          "must": [
            {{#docyear}}{
    		      "terms": {
		            "doc.year": "{{#toJson}}years{{/toJson}}"
			        }
		        },{{/docyear}}
		        {{#doctype}}{
		          "terms": {
		            "doc.type": "{{#toJson}}types{{/toJson}}"
			        }
		        },{{/doctype}}
		        {{#docslan}}{
		          "terms": {
		            "doc.slan": "{{#toJson}}langs{{/toJson}}"
			        }
		        },{{/docslan}}
            {{#regexp}}{
              "regexp": {
                "doc.num": {
                  "value": "{{regexp}}",
                  "flags": "ALL",
                  "case_insensitive": true,
                  "max_determinized_states": 10000
                }
              }
			      },{{/regexp}}
			      {
              "query_string": {
                "query": "{{query_string}}",
                "default_operator": "AND",
                "default_field": "EN.lo_entry.basic",
                "phrase_slop": 0
              }
            }
          ],
          "minimum_should_match": 1,
          "should": [
            {
              "exists": {
                "field": "{{query_lang}}"
              }
            }
          ]
        }
      },
      "from": "{{from}}{{^from}}0{{/from}}",
      "size": "{{size}}{{^size}}30{{/size}}",
      "sort": [
        {
          "doc.year": {
            "order": "desc"
          }
        }
      ],
      "_source": [
        {{^id_lo_only}}"{{query_lang}}",
        "EN",{{/id_lo_only}}
        "id_lo"{{^id_lo_only}},
        "id_doc",
        "id_mem",
        "doc"{{/id_lo_only}}
      ]{{^id_lo_only}},
      "highlight": {
        "number_of_fragments": 0,
        "pre_tags": "<b>",
        "post_tags": "</b>",
        "fields": {
          "EN.lo_entry.basic": {}
        }
      }{{/id_lo_only}}
    }"""
  }
}

With the same render as above I get the following error:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "json_parse_exception",
        "reason" : "Unexpected character ('2' (code 50)): was expecting comma to separate Object entries\n at [Source: (org.elasticsearch.common.io.stream.ByteBufferStreamInput); line: 7, column: 31]"
      }
    ],
    "type" : "json_parse_exception",
    "reason" : "Unexpected character ('2' (code 50)): was expecting comma to separate Object entries\n at [Source: (org.elasticsearch.common.io.stream.ByteBufferStreamInput); line: 7, column: 31]",
    "suppressed" : [
      {
        "type" : "illegal_state_exception",
        "reason" : "Failed to close the XContentBuilder",
        "caused_by" : {
          "type" : "i_o_exception",
          "reason" : "Unclosed object or array found"
        }
      }
    ]
  },
  "status" : 400
}

Please help me what I did wrong. Thanks.

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