# Call Elasticsearch queries from Custom Kibana Plugin

**URL:** https://discuss.elastic.co/t/call-elasticsearch-queries-from-custom-kibana-plugin/268179
**Category:** Kibana
**Tags:** kibana-plugin-development
**Created:** [March 24, 2021, 8:34am UTC](https://discuss.elastic.co/t/call-elasticsearch-queries-from-custom-kibana-plugin/268179 "2021-03-24T08:34:03Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![theonlycatonice](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/theonlycatonice/32/84590_2.png) [@theonlycatonice](https://discuss.elastic.co/u/theonlycatonice)
#### Post date: [March 24, 2021, 8:34am UTC](https://discuss.elastic.co/t/call-elasticsearch-queries-from-custom-kibana-plugin/268179/1 "2021-03-24T08:34:03Z")

</div>

Good day, I have searched high and low for an example that works in 2021 (ES 7.11) on how to implement ES querying from a Custom Kibana Plugin. The use case is there are config files stored in an index that we would like to CRUD to via a custom plugin. I have seen that elasticsearch-js shouldn't/can't be used client side and there seems to be no other documentation on how to go about this.

So far I have a test plugin that works with their example like so:

```auto
const onClickHandler = () => {
    indent preformatted text by 4 spaces
    // Use the core http service to make a response to the server API.

    http.get('/api/test_plugin/example').then((res) => {

      console.log(res);

      setTimestamp(res.time);

      // Use the core notifications service to display a success message.

      notifications.toasts.addSuccess(

        i18n.translate('testPlugin.dataUpdated', {

          defaultMessage: 'Data updated',

        })

      );

    });

  };

```

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/7/9/79cc17edaca27659db22c437c6d5d8a40fe2b744.png)

However, I do not know how to access the indices from my ReactJS based plugin. If there is any documentation or package I am not aware of, please let me know. Many previous questions had dead links or redirected to outdated documentation.

---

<div class="post-metadata">

### Author: ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)
#### Post date: [March 24, 2021, 9:55am UTC](https://discuss.elastic.co/t/call-elasticsearch-queries-from-custom-kibana-plugin/268179/2 "2021-03-24T09:55:18Z")

</div>

On the server side of your plugin (where you implement your route `/api/test_plugin/example`), you can get access to an ES client via `context.core.elasticsearch.client.asCurrentUser` to do ES queries, then pass the results to the client.

This is an example in the Lens plugin: [kibana/field\_stats.ts at ee84e0b0b709d01c08ae42daa08b20c197f7b2a7 · elastic/kibana · GitHub](https://github.com/elastic/kibana/blob/ee84e0b0b709d01c08ae42daa08b20c197f7b2a7/x-pack/plugins/lens/server/routes/field_stats.ts#L41)

---

<div class="post-metadata">

### Author: ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)
#### Post date: [March 24, 2021, 10:24am UTC](https://discuss.elastic.co/t/call-elasticsearch-queries-from-custom-kibana-plugin/268179/3 "2021-03-24T10:24:51Z")

</div>

I'm building something similar right now for educational purposes.

There are multiple ways to achieve this but I chose to use Axios due to familiarity. On your client side you want to access your server side route by doing something like.

```auto
            axios({
                method: "post",
                url: "/api/crud/get_table_data",
                data: bodyFormData,
                headers: { "Content-Type": "application/json", "kbn-xsrf": "true" },
            })
                .then(function (response) {
                    tableDataSet(response.data.reply);
                });

```

Then on the server side it's what @flash1293 linked to but here is a smaller example and probably pretty close to what you want to do.

```auto
import { IRouter } from '../../../../src/core/server';
import { schema } from '@kbn/config-schema';

export function registerGetTableDataRoute(router: IRouter) {
    router.post(
        {
            path: '/api/crud/get_table_data',
            validate: {
                body: schema.object({
                    selectedIndex: schema.string(),
                    selectedFields: schema.string(),
                }),
            },
        },
        async (context, request, response) => {

            const client = context.core.elasticsearch.client.asCurrentUser;

            const { body: result } = await client.search({
                index: request.body.selectedIndex,
                _source_includes: request.body.selectedFields,
                version: true,
                size: 10000
            });

            const reply = result.hits.hits;

            return response.ok({
                body: {
                    reply,
                },
            });
        }
    );
}

```

---

<div class="post-metadata">

### Author: ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)
#### Post date: [March 24, 2021, 10:42am UTC](https://discuss.elastic.co/t/call-elasticsearch-queries-from-custom-kibana-plugin/268179/4 "2021-03-24T10:42:56Z")

</div>

Thanks for the reduced example, @aaron-nimocks . It's possible to use axios, the recommended way is to use the core http service (as you seem to do already in your original post). So if you start from scratch now, it makes sense to stick with that (it will take care of xsrf protection and the like automatically)

---

<div class="post-metadata">

### Author: ![theonlycatonice](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/theonlycatonice/32/84590_2.png) [@theonlycatonice](https://discuss.elastic.co/u/theonlycatonice)
#### Post date: [March 24, 2021, 11:40am UTC](https://discuss.elastic.co/t/call-elasticsearch-queries-from-custom-kibana-plugin/268179/5 "2021-03-24T11:40:53Z")

</div>

Thank you for the quick replies. I'm very new to ES and some of these examples are going over my head 😅

---

<div class="post-metadata">

### Author: ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)
#### Post date: [March 24, 2021, 11:52am UTC](https://discuss.elastic.co/t/call-elasticsearch-queries-from-custom-kibana-plugin/268179/6 "2021-03-24T11:52:45Z")

</div>

Maybe installing [my plugin](https://github.com/aaron-nimocks/kibana-plugin-snippets/blob/main/crud.zip) and seeing how it works will help since it's doing something similar. Just want to make clear that this is not even close to being finished and wasn't meant for human consumption. 🙂

It's just a project I am using to help learn everything, same as you are doing. This one allows you to select any index pattern, then select the fields you want to view, and then finally goes get's the data to fill a table. The pre-search filters and any edit functionality has not been added yet.

Hopefully this will help with your learning curve.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/1/3/13ff5c89c2f1f7e776a9016c98079c8c2f936f84.png)

---

<div class="post-metadata">

### Author: ![theonlycatonice](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/theonlycatonice/32/84590_2.png) [@theonlycatonice](https://discuss.elastic.co/u/theonlycatonice)
#### Post date: [March 25, 2021, 7:04am UTC](https://discuss.elastic.co/t/call-elasticsearch-queries-from-custom-kibana-plugin/268179/7 "2021-03-25T07:04:03Z")

</div>

Thank you so much! This seems to have worked for me 🙂 posting here for others

```auto
function queryElasticSearch() {
    // registerGetTableDataRoute(CoreSetup);
    axios({
      method: 'post',
      url: `/api/console/proxy?path=_cat/indices&method=GET`,
      headers: { 'kbn-xsrf': 'kibana' }
    }).then((response) => {
      console.log(response);
    }).catch(function (error) {
      console.log(error);
    });;
  }

```

---

<div class="post-metadata">

### Author: ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)
#### Post date: [March 25, 2021, 8:12am UTC](https://discuss.elastic.co/t/call-elasticsearch-queries-from-custom-kibana-plugin/268179/8 "2021-03-25T08:12:12Z")

</div>

That might work, however make sure to check each minor version upgrade whether it still works. The console API is not considered public AFAIK so we don't make any effort to keep it backwards compatible.

---

<div class="post-metadata">

### Author: ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)
#### Post date: [March 25, 2021, 8:55pm UTC](https://discuss.elastic.co/t/call-elasticsearch-queries-from-custom-kibana-plugin/268179/9 "2021-03-25T20:55:56Z")

</div>

There is an example in that plug-in called get\_index\_list.ts I believe and would look at swapping to that when you start getting comfortable with the code.

---

<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 22, 2021, 8:56pm UTC](https://discuss.elastic.co/t/call-elasticsearch-queries-from-custom-kibana-plugin/268179/10 "2021-04-22T20:56:22Z")

</div>

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