# ClasscastException when using SearchTemplateRequestBuilder from within a plugin

**URL:** <https://discuss.elastic.co/t/classcastexception-when-using-searchtemplaterequestbuilder-from-within-a-plugin/122572>\
**Category:** Elasticsearch\
**Created:** [March 5, 2018, 5:58pm UTC](https://discuss.elastic.co/t/classcastexception-when-using-searchtemplaterequestbuilder-from-within-a-plugin/122572 "2018-03-05T17:58:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ng.software.dev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ng.software.dev/32/70692_2.png) [@ng.software.dev](https://discuss.elastic.co/u/ng.software.dev)\
**Post date:** [March 5, 2018, 5:58pm UTC](https://discuss.elastic.co/t/classcastexception-when-using-searchtemplaterequestbuilder-from-within-a-plugin/122572/1 "2018-03-05T17:58:56Z")

</div>

Hi,

I have written an action plugin in which I need to call a search template.

The code is quite straightforward:

```
SearchResponse sr = new SearchTemplateRequestBuilder(client)
        .setScript("myTemplate")                       
        .setScriptType(ScriptType.STORED)     
        .setScriptParams(template_params)                   
        .setRequest(new SearchRequest("myIndex").types("myType")                    
        .get()                                              
        .getResponse();            

```

I can install the plugin, however when running this code (from within a class extending _BaseRestHandler_),  
I get the following exception:

```
java.lang.ClassCastException: org.elasticsearch.script.mustache.SearchTemplateRequest cannot be cast to org.elasticsearch.script.mustache.SearchTemplateRequest
at org.elasticsearch.script.mustache.TransportSearchTemplateAction.doExecute(TransportSearchTemplateAction.java:53) ~[?:?]
at org.elasticsearch.action.support.TransportAction.doExecute(TransportAction.java:146) ~[elasticsearch-5.6.7.jar:5.6.7]
at org.elasticsearch.action.support.TransportAction$RequestFilterChain.proceed(TransportAction.java:170) ~[elasticsearch-5.6.7.jar:5.6.7]
at org.elasticsearch.action.support.TransportAction.execute(TransportAction.java:142) ~[elasticsearch-5.6.7.jar:5.6.7]
at org.elasticsearch.action.support.TransportAction.execute(TransportAction.java:84) ~[elasticsearch-5.6.7.jar:5.6.7]
at org.elasticsearch.client.node.NodeClient.executeLocally(NodeClient.java:83) ~[elasticsearch-5.6.7.jar:5.6.7]
at org.elasticsearch.client.node.NodeClient.doExecute(NodeClient.java:72) ~[elasticsearch-5.6.7.jar:5.6.7]
at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:408) ~[elasticsearch-5.6.7.jar:5.6.7]
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:80) ~[elasticsearch-5.6.7.jar:5.6.7]
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:54) ~[elasticsearch-5.6.7.jar:5.6.7]
at org.elasticsearch.action.ActionRequestBuilder.get(ActionRequestBuilder.java:62) ~[elasticsearch-5.6.7.jar:5.6.7]
at com.doodle.myplugin.action.FullTextSearchAction.handleQuery(FullTextSearchAction.java:81) ~[?:?]

```

I have the following dependency in the classpath: _org.codelibs.elasticsearch.module:lang-mustache:5.6.7_. The same happens if I use _org.elasticsearch.plugin:lang-mustache-client_ instead. I'm puzzled at which dependency to pick by the way!

If I declare the dependency as provided, I get a _ClassNotFoundException_.

I'm pretty stuck at this point...

Any help in solving this would be greatly appreciated!

---

<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:** [April 2, 2018, 5:59pm UTC](https://discuss.elastic.co/t/classcastexception-when-using-searchtemplaterequestbuilder-from-within-a-plugin/122572/2 "2018-04-02T17:59:21Z")

</div>

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