# Using pre registered search template is not working

**URL:** https://discuss.elastic.co/t/using-pre-registered-search-template-is-not-working/276063
**Category:** Elasticsearch
**Tags:** language-clients
**Created:** [June 15, 2021, 11:29pm UTC](https://discuss.elastic.co/t/using-pre-registered-search-template-is-not-working/276063 "2021-06-15T23:29:24Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![amarga83](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amarga83/32/90370_2.png) [@amarga83](https://discuss.elastic.co/u/amarga83)
#### Post date: [June 15, 2021, 11:29pm UTC](https://discuss.elastic.co/t/using-pre-registered-search-template-is-not-working/276063/1 "2021-06-15T23:29:24Z")

</div>

Hi everyone,

I created a search template using kibana as follows:

```auto

Post _scripts/mytemplateid3
{
  "script": {
    "lang": "mustache",
    "source": {
      "query": {
        "match": {
          "author": "{{query_string}}"
        }
      }
    }
  }
}

```

Then I ran a GET request like the following and it worked ok:

```auto
GET /spo-demo/_search/template
{
  "id": "mytemplateid3", 
  "params": {
    "query_string": "Annie"
  }
}

```

And the output looks like this:

```auto
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.9808291,
    "hits" : [
      {
        "_index" : "spo-demo",
        "_type" : "_doc",
        "_id" : "fTR_pHkB4DiV7smmisqK",
        "_score" : 0.9808291,
        "_source" : {
          "displayurl" : "url1",
          "author" : "Annie Martinez",
          "totalUsers" : "2"
        }
      }
    ]
  }
}

```

So far so good, then I decide to use the search template in my java code like this, but for some reason I am not getting any results:

```auto
        SearchTemplateRequest request = new SearchTemplateRequest();
        request.setRequest(new SearchRequest("spo-demo"));

        request.setScriptType(ScriptType.STORED);
        request.setScript("mytemplateid3");

        Map<String, Object> params = new HashMap<>();
        params.put("field", "query_string");
        params.put("value", "Annie");
        params.put("size", 5);
        request.setScriptParams(params);

        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));;

        SearchTemplateResponse response = client.searchTemplate(request, RequestOptions.DEFAULT);
        SearchResponse searchResponse = response.getResponse();
        SearchHits hits = searchResponse.getHits();

```

Any idea why it is not working in Java but it works in kibana?

Below my index mappings if anyone is interested:

```auto
{
  "spo-demo" : {
    "mappings" : {
      "properties" : {
        "author" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "displayurl" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "totalUsers" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![amarga83](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amarga83/32/90370_2.png) [@amarga83](https://discuss.elastic.co/u/amarga83)
#### Post date: [June 16, 2021, 10:11pm UTC](https://discuss.elastic.co/t/using-pre-registered-search-template-is-not-working/276063/2 "2021-06-16T22:11:28Z")

</div>

I figured how to fix this. It seems like I misread the following Elasticsearch documentation: [Search Template API | Java REST Client [master] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-search-template.html#_registered_templates:)

As I mentioned in the question I created the following search template

```auto
Post _scripts/mytemplateid3
{
  "script": {
    "lang": "mustache",
    "source": {
      "query": {
        "match": {
          "author": "{{query_string}}"
        }
      }
    }
  }
}

```

While reading the documentation I thought I needed to set the parameters as follow:

```auto
Map<String, Object> params = new HashMap<>();
params.put("field", "query_string");
params.put("value", "Annie");

```

Instead I was supposed to set them like this:

```auto
Map<String, Object> params = new HashMap<>();
params.put("field", "author");
params.put("query_string", "Annie");

```

I thought "value" was a reserved word and that was not the case. "value" was just the name that was provided in the documentation sample.

---

<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: [July 14, 2021, 10:11pm UTC](https://discuss.elastic.co/t/using-pre-registered-search-template-is-not-working/276063/3 "2021-07-14T22:11:52Z")

</div>

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