Search code examples
templateselasticsearchmustache

Elasticsearch template not reading params


I've been following the example at this site Parameterizing Queries in Solr and Elasticsearch in the ES section. Note, it is an older ES version that the author is working with, but I don't think that should affect this situation. I am using version 1.6 of ES.

I have a template {{ES_HOME}}/config/scripts/test.mustache that looks like the below snippet. Note the {{q}} parameter in "query".

{ 
  "query": {
    "multi_match": {
      "query": "{{q}}",
      "analyzer": "keyword",
      "fields": [
          "description^10",
          "name^50",
      ]
    }
  },
  "aggregations": {
    "doctype" : {
      "terms" : { 
        "field" : "doctype.untouched"      
      }
    }
  }
}

I POST to http://localhost:9200/forward/_search/template with the following message body

{
  "template": {
    "file": "test",
    "params": {
      "q": "a"
    }
  }
}

It runs the template, but gets 0 hits, and returns the following:

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 0,
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "doctype": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": []
        },
    }
}

Alternatively, if I hard code "a" into where {{q}} is, and post to the template, it correctly returns results for the query "a". Am I doing something inherently wrong in my design?


Solution

  • According to the docs, the params object should be outside of the template object

    {
        "template": {
            "file": "test"
        },
        "params": {
            "q": "a"
        }
    }