Call custom ES api from inside plugin

Hi all, my need is to call a custom api like /elasticsearchip:port/path/to/api and pass two parameter in query string like ?parm1=value&param2=value. Which is the best and clean solution? I’m trying to make a server.route and use callWithRequest transport.request but just want an advice to find the most robust way to achieve that..(just because I’ve got always 500 internal server error when call api)..thank you

To confirm, you have created an Elasticsearch API which adds a new endpoint you're attempting to query?

If I am following that, you have a few options off the top of my head.

You could use XMLHttpRequest, request, or whatever your flavor is and hit the endpoint directly.

Alternatively, you could create an elasticsearch-js plugin to add support for your new endpoint and create a new cluster with that plugin.

const config = Object.assign({ plugins: [myPlugin] }, server.config().get('elasticsearch'));
const cluster = server.plugins.elasticsearch.createCluster('whatever', config);

You can then obtain the cluster by using server.plugins.elasticsearch.getCluster('whatever'); which contains callWithRequest.

Here is documentation on extending elasticsearch-js https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/extending_core_components.html

I attempted to allow for adding plugins after client instantiation but ran into problems and out of time. Feel free to revive and see if you can resolve the issues if you're up to it: https://github.com/elastic/elasticsearch-js/pull/482. This would have removed the need of creating a new cluster.

Hi @tylersmalley, thank you for your response.
I ended using callWithRequest and transport.request of elasticsearch inside a server.route.

But now I've a problem, after calling the API the backend answer me after 30s and I've this:

>  Request error, retrying
>  POST http://elasticIP:9200/path/of/api => socket hang up

And my http request get an 504 Gatway timeout.
I tried to set elasticsearch.requestTimeout but is seems to not solve my problem.
I've also tried to set requestTimeout params inside the transport.request but the request hangs up always at 30s...very very strange

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