# How to make queries to elasticsearch from client side of kibana plugin

**URL:** https://discuss.elastic.co/t/how-to-make-queries-to-elasticsearch-from-client-side-of-kibana-plugin/254178
**Category:** Kibana
**Created:** [November 3, 2020, 4:34pm UTC](https://discuss.elastic.co/t/how-to-make-queries-to-elasticsearch-from-client-side-of-kibana-plugin/254178 "2020-11-03T16:34:44Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![K\_Chaitanya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/k_chaitanya/32/46387_2.png) [@K\_Chaitanya](https://discuss.elastic.co/u/K_Chaitanya)
#### Post date: [November 3, 2020, 4:34pm UTC](https://discuss.elastic.co/t/how-to-make-queries-to-elasticsearch-from-client-side-of-kibana-plugin/254178/1 "2020-11-03T16:34:44Z")

</div>

Hi,

I am trying to query elasticsearch indices from client side of kibana plugin.

I was unable to find any native kibana plugin components which can readily enable me to create a client with which I can make request to elasticsearch with all the Privileges and roles of user.

Currently I resorted to making a fetch request like this

```auto
fetch ("/api/console/proxy?path=indexName%2F_search&method=GET", requestOptions)

```

in app.tsx or other child modules of app.tsx in `public/components` folders of generated kibana plugin ( generated using plugin generator in scripts)

Which works fine in local development environment but, when deployed in prod gives me `404` for normal and works fine for superuser or admin users.

Setup info:

```
The whole setup is on version 7.9.1,

For building the plugin we are using 7.x branch of kibana

```

Thanks  
Chaitanya

---

<div class="post-metadata">

### Author: ![tsullivan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsullivan/32/31077_2.png) [@tsullivan](https://discuss.elastic.co/u/tsullivan)
#### Post date: [November 3, 2020, 8:07pm UTC](https://discuss.elastic.co/t/how-to-make-queries-to-elasticsearch-from-client-side-of-kibana-plugin/254178/2 "2020-11-03T20:07:11Z")

</div>

Hi, you'll want to add a dependency for the `data` plugin in your app, and use its services to fetch data from ES: [https://github.com/elastic/kibana/blob/7.x/src/plugins/data/README.md](https://github.com/elastic/kibana/blob/7.x/src/plugins/data/README.md)

---

<div class="post-metadata">

### Author: ![K\_Chaitanya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/k_chaitanya/32/46387_2.png) [@K\_Chaitanya](https://discuss.elastic.co/u/K_Chaitanya)
#### Post date: [November 11, 2020, 9:34am UTC](https://discuss.elastic.co/t/how-to-make-queries-to-elasticsearch-from-client-side-of-kibana-plugin/254178/3 "2020-11-11T09:34:03Z")

</div>

Hi @tsullivan,

Tried using the data plugin, while creating a custom search query like mentioned below:

```auto
    const [indexPattern, setIndexPattern] = useState<IndexPattern | null>();

    useEffect(() => {
      const setDefaultIndexPattern = async () => {
          const defaultIndexPattern = await data.indexPatterns.getDefault();
          setIndexPattern(defaultIndexPattern);
      };

      setDefaultIndexPattern();
      cussearch();
    }, [data]);

    const query = {
      query: {
        "bool": {
          "must": [
            {
              "match": {
                "name": "first"
              }
            }
          ]
        }
      },
      language: 'dsl'
    }

    const aggs = [
      { 
        id: 'count', 
        type: 'cardinality', 
        params: { 
          field: indexPattern.getFieldByName("_id").name 
        } 
      }
    ];

    const aggsDsl = data.search.aggs.createAggConfigs(indexPattern,aggs).toDsl()

    const searchSource = await data.search.searchSource.create();
    const searchResponse = await searchSource
      .setParent(undefined)
      .setField('index', indexPattern)
      .setField('query', query)
      .setField('aggs', aggsDsl)
      .setField('size',0)
      .fetch(); 

```

but `query` is not getting reflected in the response, only `aggs` is applied properly to the search API call.

`setField` says it requires `Query | undefined` type object but there is no function in data plugin which will build and return a `Query` type object.

and the same goes for the `filters` as well, there is no function in data plugin via which we can create `Filter` type object, though when I run `data.query.filterManager.getFilters()` I get the exsiting applied Filters.

How can I create Custom `Filter` and `Query` objects??

Thanks  
Chaitanya

---

<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: [December 9, 2020, 9:34am UTC](https://discuss.elastic.co/t/how-to-make-queries-to-elasticsearch-from-client-side-of-kibana-plugin/254178/4 "2020-12-09T09:34:03Z")

</div>

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

---

<div class="post-metadata">

### Author: ![tsullivan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsullivan/32/31077_2.png) [@tsullivan](https://discuss.elastic.co/u/tsullivan)
#### Post date: [December 10, 2020, 6:35pm UTC](https://discuss.elastic.co/t/how-to-make-queries-to-elasticsearch-from-client-side-of-kibana-plugin/254178/5 "2020-12-10T18:35:19Z")

</div>



---

<div class="post-metadata">

### Author: ![tsullivan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsullivan/32/31077_2.png) [@tsullivan](https://discuss.elastic.co/u/tsullivan)
#### Post date: [December 10, 2020, 6:40pm UTC](https://discuss.elastic.co/t/how-to-make-queries-to-elasticsearch-from-client-side-of-kibana-plugin/254178/6 "2020-12-10T18:40:50Z")

</div>

Hi, there is an excellent example plugin in the Kibana source repo to give a live example of what you are trying to do: [https://github.com/elastic/kibana/blob/master/examples/search\_examples/public/components/app.tsx](https://github.com/elastic/kibana/blob/master/examples/search_examples/public/components/app.tsx)

You can import interfaces from the Data plugin, and use the plugin API to create a `req` object as seen here: [https://github.com/elastic/kibana/blob/master/examples/search\_examples/public/components/app.tsx#L141](https://github.com/elastic/kibana/blob/master/examples/search_examples/public/components/app.tsx#L141)

Then you create a subscription to the search results with `data.search.search(req)` and handle the response data in the callback of the subscription as seen here: [https://github.com/elastic/kibana/blob/master/examples/search\_examples/public/components/app.tsx#L161](https://github.com/elastic/kibana/blob/master/examples/search_examples/public/components/app.tsx#L161)

If you intend to search using Elasticsearch DSL syntax, you do not need a custom search strategy.

---

<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: [November 4, 2022, 8:16am UTC](https://discuss.elastic.co/t/how-to-make-queries-to-elasticsearch-from-client-side-of-kibana-plugin/254178/7 "2022-11-04T08:16:53Z")

</div>


