# Create a plugin that needs a client

**URL:** <https://discuss.elastic.co/t/create-a-plugin-that-needs-a-client/125142>\
**Category:** Elasticsearch\
**Created:** [March 22, 2018, 8:56am UTC](https://discuss.elastic.co/t/create-a-plugin-that-needs-a-client/125142 "2018-03-22T08:56:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jettro](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jettro/32/3418_2.png) [@jettro](https://discuss.elastic.co/u/jettro)\
**Post date:** [March 22, 2018, 8:56am UTC](https://discuss.elastic.co/t/create-a-plugin-that-needs-a-client/125142/1 "2018-03-22T08:56:19Z")

</div>

Hi,  
I am working on a plugin that needs access to Elasticsearch to potentially run a query against another index. I have found a way, but it feels hacky. Does someone know a better way?

Thanks!

```
public class UseQueryPlugin extends Plugin implements ScriptPlugin {
    private UseQueryScriptEngine engine;

    @Override
    public ScriptEngine getScriptEngine(Settings settings, Collection<ScriptContext<?>> contexts) {
        this.engine = new UseQueryScriptEngine();
        return this.engine;
    }

    @Override
    public Collection<Object> createComponents(Client client,
                                               ClusterService clusterService,
                                               ThreadPool threadPool,
                                               ResourceWatcherService resourceWatcherService,
                                               ScriptService scriptService,
                                               NamedXContentRegistry xContentRegistry,
                                               Environment environment,
                                               NodeEnvironment nodeEnvironment,
                                               NamedWriteableRegistry namedWriteableRegistry) {
        this.engine.setClient(client);
        return asList();
    }
}
```

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [March 22, 2018, 4:57pm UTC](https://discuss.elastic.co/t/create-a-plugin-that-needs-a-client/125142/2 "2018-03-22T16:57:15Z")

</div>

What kind of script are you trying to do this in? Eg a scoring or filter script this would be a very slow idea.

---

<div class="post-metadata">

**Author:** ![jettro](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jettro/32/3418_2.png) [@jettro](https://discuss.elastic.co/u/jettro)\
**Post date:** [March 23, 2018, 7:47am UTC](https://discuss.elastic.co/t/create-a-plugin-that-needs-a-client/125142/3 "2018-03-23T07:47:51Z")

</div>

I know, I am dealing with a scoring script. But the alternative seems to be even worse. A very big table is now passed in the script parameters. This makes the request object to large and the request that is sent to all the shards as well. I want to evaluate and performance test the different options that I have. I am aware of the risks.

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [March 23, 2018, 8:39am UTC](https://discuss.elastic.co/t/create-a-plugin-that-needs-a-client/125142/4 "2018-03-23T08:39:59Z")

</div>

The route you suggested in your first post will work, although it is discouraged and frail. There is no guarantee that createComponents will always be called after script engine creation.

---

<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 20, 2018, 8:40am UTC](https://discuss.elastic.co/t/create-a-plugin-that-needs-a-client/125142/5 "2018-04-20T08:40:36Z")

</div>

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