# POST'ing Search Template as REST API causes Elastic to fail on start

**URL:** <https://discuss.elastic.co/t/posting-search-template-as-rest-api-causes-elastic-to-fail-on-start/82552>\
**Category:** Elasticsearch\
**Created:** [April 17, 2017, 9:01am UTC](https://discuss.elastic.co/t/posting-search-template-as-rest-api-causes-elastic-to-fail-on-start/82552 "2017-04-17T09:01:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pjmtdw](https://avatars.discourse-cdn.com/v4/letter/p/e99b99/32.png) [@pjmtdw](https://discuss.elastic.co/u/pjmtdw)\
**Post date:** [April 17, 2017, 9:01am UTC](https://discuss.elastic.co/t/posting-search-template-as-rest-api-causes-elastic-to-fail-on-start/82552/1 "2017-04-17T09:01:04Z")

</div>

I'm using Elasticsearch 5.3.0, with java version "1.8.0\_121", on Linux.  
Also confirmed the same behavior with latest commit of `5.x` and `master` branch.  
Is this an intended behavior?

According to [Pre Registered Templates](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html#pre-registered-templates), we can POST search template using REST API.  
It works as expected.  
However, when we restart the Elasticsearch after registering the template, it throws an exception.

Here's the step to reproduce.

1. Download `elasticsearch-5.3.0.tar.gz` from [official site](https://www.elastic.co/downloads/elasticsearch), and unarchive it.
2. Run elasticsearch using `./bin/elasticsearch`
3. Post search template using following command:

```auto
curl -H 'Content-Type:application/json' -X POST 'http://localhost:9200/_search/template/this_is_a_test' --data '{"template":{"query":{"match":{"title": "{{query_string}}"}}}}'

```

it returns  
`{"acknowledged":true}`

1. Quit Elasticsearch using `Ctrl+C`

2. Start Elasticsearch using `./bin/elasticsearch`

3. The server fails to start with following stacktrace (only showing partial log):

```auto
[2017-04-17T17:55:36,116][ERROR][o.e.g.GatewayMetaState] [0i1Lvk1] failed to read local state, exiting...
org.elasticsearch.ElasticsearchException: java.io.IOException: failed to read [id:1, legacy:false, file:/home/my_user/Downloads/elasticsearch-5.3.0/data/nodes/0/_state/global-1.st]
	at org.elasticsearch.ExceptionsHelper.maybeThrowRuntimeAndSuppress(ExceptionsHelper.java:150) ~[elasticsearch-5.3.0.jar:5.3.0]
	at org.elasticsearch.gateway.MetaDataStateFormat.loadLatestState(MetaDataStateFormat.java:334) ~[elasticsearch-5.3.0.jar:5.3.0]
	at org.elasticsearch.gateway.MetaStateService.loadGlobalState(MetaStateService.java:113) ~[elasticsearch-5.3.0.jar:5.3.0]
Caused by: java.io.IOException: failed to read [id:1, legacy:false, file:/home/my_user/Downloads/elasticsearch-5.3.0/data/nodes/0/_state/global-1.st]
	at org.elasticsearch.gateway.MetaDataStateFormat.loadLatestState(MetaDataStateFormat.java:327) ~[elasticsearch-5.3.0.jar:5.3.0]
	... 46 more
Caused by: org.elasticsearch.common.ParsingException: [stored script source] failed to parse field [options]
	at org.elasticsearch.common.xcontent.ObjectParser.parseValue(ObjectParser.java:369) ~[elasticsearch-5.3.0.jar:5.3.0]
	at org.elasticsearch.common.xcontent.ObjectParser.parseSub(ObjectParser.java:378) ~[elasticsearch-5.3.0.jar:5.3.0]
	at org.elasticsearch.common.xcontent.ObjectParser.parse(ObjectParser.java:169) ~[elasticsearch-5.3.0.jar:5.3.0]
	... 46 more
Caused by: java.lang.IllegalArgumentException: content_type cannot be user-specified
	at org.elasticsearch.script.StoredScriptSource$Builder.setOptions(StoredScriptSource.java:127) ~[elasticsearch-5.3.0.jar:5.3.0]
	at org.elasticsearch.script.StoredScriptSource$Builder.access$100(StoredScriptSource.java:87) ~[elasticsearch-5.3.0.jar:5.3.0]
	at org.elasticsearch.script.StoredScriptSource.lambda$static$4(StoredScriptSource.java:163) ~[elasticsearch-5.3.0.jar:5.3.0]
	... 46 more

```

---

<div class="post-metadata">

**Author:** ![nisha](https://avatars.discourse-cdn.com/v4/letter/n/76d3ee/32.png) [@nisha](https://discuss.elastic.co/u/nisha)\
**Post date:** [April 19, 2017, 5:57am UTC](https://discuss.elastic.co/t/posting-search-template-as-rest-api-causes-elastic-to-fail-on-start/82552/2 "2017-04-19T05:57:23Z")

</div>

I am also getting the same exception

---

<div class="post-metadata">

**Author:** ![Junzhe\_Ma](https://avatars.discourse-cdn.com/v4/letter/j/bc79bd/32.png) [@Junzhe\_Ma](https://discuss.elastic.co/u/Junzhe_Ma)\
**Post date:** [April 21, 2017, 2:04am UTC](https://discuss.elastic.co/t/posting-search-template-as-rest-api-causes-elastic-to-fail-on-start/82552/3 "2017-04-21T02:04:09Z")

</div>

I am also getting the same exception +1

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [April 21, 2017, 2:16pm UTC](https://discuss.elastic.co/t/posting-search-template-as-rest-api-causes-elastic-to-fail-on-start/82552/4 "2017-04-21T14:16:59Z")

</div>

This is indeed a bug and we are currently looking into it. You can track the progress here: [https://github.com/elastic/elasticsearch/issues/24227](https://github.com/elastic/elasticsearch/issues/24227)

---

<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:** [May 19, 2017, 2:17pm UTC](https://discuss.elastic.co/t/posting-search-template-as-rest-api-causes-elastic-to-fail-on-start/82552/5 "2017-05-19T14:17:10Z")

</div>

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