# How to call elastic search API from custom Kibana Plugin

**URL:** https://discuss.elastic.co/t/how-to-call-elastic-search-api-from-custom-kibana-plugin/166402
**Category:** Kibana
**Created:** [January 30, 2019, 5:02pm UTC](https://discuss.elastic.co/t/how-to-call-elastic-search-api-from-custom-kibana-plugin/166402 "2019-01-30T17:02:43Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![adityaPsl](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@adityaPsl](https://discuss.elastic.co/u/adityaPsl)
#### Post date: [January 30, 2019, 5:02pm UTC](https://discuss.elastic.co/t/how-to-call-elastic-search-api-from-custom-kibana-plugin/166402/1 "2019-01-30T17:02:43Z")

</div>

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](https://github.com/aditya-deshpadne/custom_plugin/tree/master/my_plugin_name)

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

Thank you,  
Aditya

---

<div class="post-metadata">

### Author: ![thomasneirynck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thomasneirynck/32/23313_2.png) [@thomasneirynck](https://discuss.elastic.co/u/thomasneirynck)
#### Post date: [January 30, 2019, 9:56pm UTC](https://discuss.elastic.co/t/how-to-call-elastic-search-api-from-custom-kibana-plugin/166402/2 "2019-01-30T21:56:15Z")

</div>

You could use the elasticsearch javascript client. [https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html](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](https://github.com/elastic/kibana/blob/8f8c19d32dbf6a9249bcdcb918cca2819a48ba40/src/es_archiver/cli.js#L115-L118)

---

<div class="post-metadata">

### Author: ![adityaPsl](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@adityaPsl](https://discuss.elastic.co/u/adityaPsl)
#### Post date: [February 1, 2019, 5:49am UTC](https://discuss.elastic.co/t/how-to-call-elastic-search-api-from-custom-kibana-plugin/166402/3 "2019-02-01T05:49:02Z")

</div>

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.

> **[How to Build Application Search with React and Elastic](https://www.elastic.co/blog/how-to-build-application-search-with-react-and-elastic-app-search)**
>
> Want to build the best search experience? Use React and Elastic App Search to create relevant search experiences that not only create great value, but feel great to use.

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

---

<div class="post-metadata">

### Author: ![adityaPsl](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@adityaPsl](https://discuss.elastic.co/u/adityaPsl)
#### Post date: [February 3, 2019, 4:15pm UTC](https://discuss.elastic.co/t/how-to-call-elastic-search-api-from-custom-kibana-plugin/166402/4 "2019-02-03T16:15:09Z")

</div>

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](https://github.com/aditya-deshpadne/hello_world)

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

---

<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: [March 3, 2019, 4:15pm UTC](https://discuss.elastic.co/t/how-to-call-elastic-search-api-from-custom-kibana-plugin/166402/5 "2019-03-03T16:15:13Z")

</div>

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