How to call elastic search API from custom Kibana Plugin

Hello Team,

I have created a custom plugin (using plugin generator) and added control like textbox ( I am using elastic UI framework). I am able to read the data from these UI controls.

I want to execute a search request based on the user inputs, Could you please let me know what is the recommended way to communicate with Elasticsearch and get the data. Currently i am making a rest call from main.js (my_plugin_name/public/components/main/main.js)

Git Url : my_test_plugin

Could you please let me know if i am missing something.
Thanks in advance for your help and support.

Thank you,
Aditya

You could use the elasticsearch javascript client. https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html

An example of how it is used in Kibana, you can see here: https://github.com/elastic/kibana/blob/8f8c19d32dbf6a9249bcdcb918cca2819a48ba40/src/es_archiver/cli.js#L115-L118

Thank you thomasneirynvk.
I tried to use elasticsearch.js , however I got the error that it is not allowed to use in browser.

While searching I found below link.

Any thoughts , suggestions about using above lib.

Just FYI
I am using Kibana 6.5 and code generated by plugin generator is in react.js

Please let me know if I am missing something.
Sorry if I am asking too basic information, I am new to Elasticsearch and UI development.

Thank you very much for your help and support.

Thank you,
Aditya

Hello Team,

I was able to call elasticsearch backend using elasticsearch.js
I added server route and called the method from route file and passed the parameters using path parameters.

import elasticsearch from 'elasticsearch'

export default function (server) {

	const client = new elasticsearch.Client({
		  host: 'localhost:9200',
		  log: 'trace'
			});


  server.route({
    path: '/api/HelloWorld/search_test/{searchterm}',
    method: 'GET',
    handler(req, reply) {
      const query =  req.params.searchterm;
	  client.search({
			
		 q: query,
		 index: '<index_name>',
		}).then(function (body) {
			var hits = body.hits.hits;
		}, function (error) {
			console.trace(error.message);
		});
			reply({ response: body.hits.hits });
		}
		
	

	  });

and called this method from main.js

const { httpClient } = this.props;
    httpClient.get('../api/HelloWorld/search_test/'+this.state.searchTerm).then((response) => {
      this.setState({ resp: response.data });
	  window.alert(this.state.resp);
	});

Code repo:
Custom Kibana Plugin

Could you please let me know if this is the right way to do it or if i am missing something.

Thank you very much for your help and support.

Thank you,
Aditya