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

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

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

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

Hi @tsullivan,

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

    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

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

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

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

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

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