# Trouble with migrating legacy plugin

**URL:** <https://discuss.elastic.co/t/trouble-with-migrating-legacy-plugin/271635>\
**Category:** Kibana\
**Tags:** kibana-plugin-development\
**Created:** [April 29, 2021, 12:25pm UTC](https://discuss.elastic.co/t/trouble-with-migrating-legacy-plugin/271635 "2021-04-29T12:25:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kibanaPluginDev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kibanaplugindev/32/87937_2.png) [@kibanaPluginDev](https://discuss.elastic.co/u/kibanaPluginDev)\
**Post date:** [April 29, 2021, 12:25pm UTC](https://discuss.elastic.co/t/trouble-with-migrating-legacy-plugin/271635/1 "2021-04-29T12:25:09Z")

</div>

We are really facing problems in implementing elasticsearch requests from Kibana Plugin using the core kibana API for elasticsearch? Also referred migration examples and the legacy to new platform migration document. Could you please provide sample search query implementation for http routes for third party kibana plugin?

> **[Migration Examples | Kibana Guide \[7.12\] | Elastic](https://www.elastic.co/guide/en/kibana/current/migrating-legacy-plugins-examples.html)**

> <https://github.com/elastic/kibana/blob/7.12/docs/development/core/server/kibana-plugin-core-server.httpservicesetup.md>

---

<div class="post-metadata">

**Author:** ![azasypkin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/azasypkin/32/42216_2.png) [@azasypkin](https://discuss.elastic.co/u/azasypkin)\
**Post date:** [April 29, 2021, 2:41pm UTC](https://discuss.elastic.co/t/trouble-with-migrating-legacy-plugin/271635/2 "2021-04-29T14:41:59Z")

</div>

Hey @kibanaPluginDev ,

It'd help if you could be more specific explaining what exactly you'd like to do and what version of Kibana you're using. Anyway here is the [example of ES API call made from the route handler](https://github.com/elastic/kibana/blob/24374d9c4a5111c634cc96d330235b553196450b/x-pack/plugins/security/server/routes/indices/get_fields.ts#L13) that may help.

Best,  
Oleg

---

<div class="post-metadata">

**Author:** ![kibanaPluginDev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kibanaplugindev/32/87937_2.png) [@kibanaPluginDev](https://discuss.elastic.co/u/kibanaPluginDev)\
**Post date:** [April 29, 2021, 3:50pm UTC](https://discuss.elastic.co/t/trouble-with-migrating-legacy-plugin/271635/3 "2021-04-29T15:50:39Z")

</div>

Hello @azasypkin ,  
We are using Kiabana 7.11.0. I have a few logs in our elasticsearch so I was trying to implement a **basic search query** using Kibana Core API. I had generated a new plugin template using kibana plugin generator.  
Our legacy plugin which we are trying to migrate is -

> **[BroadSoft-Xtended/BroadWorks-Dashboards-and-Discovery](https://github.com/BroadSoft-Xtended/BroadWorks-Dashboards-and-Discovery)**
>
> This repository contains the BroadWorks Dashboards and Discovery components to extend BroadWorks data mining, reporting, and analysis capabilities. - BroadSoft-Xtended/BroadWorks-Dashboards-and-Dis...

```auto
// server/plugin.ts
import {
  PluginInitializerContext,
  CoreSetup,
  CoreStart,
  Plugin,
  Logger,
} from '../../../src/core/server';

import { NewPluginPluginSetup, NewPluginPluginStart } from './types';
import { defineRoutes } from './routes';

export class NewPluginPlugin implements Plugin<NewPluginPluginSetup, NewPluginPluginStart> {
  private readonly logger: Logger;

  constructor(initializerContext: PluginInitializerContext) {
    this.logger = initializerContext.logger.get();
  }

  public setup(core: CoreSetup) {
    this.logger.debug('newPlugin: Setup');
    const router = core.http.createRouter();
    router.get(
      {
        path: '/api/new_plugin/example',
        validate: false,
      },
      async (context, request, response) => {
        const query = {
          query: {
            match_all: {},
          }}
        const results = await context.core.elasticsearch.client.asCurrentUser.search(query);
        return response.ok({
          body: { results },
        });
      }
    );

    return {};
  }

  public start(core: CoreStart) {
    this.logger.debug('newPlugin: Started');
    return {};
  }

  public stop() {}
}

```

```auto
// public/components/app.tsx
import React, { useState } from 'react';
import { i18n } from '@kbn/i18n';
import { FormattedMessage, I18nProvider } from '@kbn/i18n/react';
import { BrowserRouter as Router } from 'react-router-dom';

import {
  EuiButton,
  EuiHorizontalRule,
  EuiPage,
  EuiPageBody,
  EuiPageContent,
  EuiPageContentBody,
  EuiPageContentHeader,
  EuiPageHeader,
  EuiTitle,
  EuiText,
} from '@elastic/eui';

import { CoreStart } from '../../../../src/core/public';
import { NavigationPublicPluginStart } from '../../../../src/plugins/navigation/public';

import { PLUGIN_ID, PLUGIN_NAME } from '../../common';

interface NewPluginAppDeps {
  basename: string;
  notifications: CoreStart['notifications'];
  http: CoreStart['http'];
  navigation: NavigationPublicPluginStart;
}

export const NewPluginApp = ({ basename, notifications, http, navigation }: NewPluginAppDeps) => {
  // Use React hooks to manage state.
  const [timestamp, setTimestamp] = useState<string | undefined>();

  const onClickHandler = () => {
    // Use the core http service to make a response to the server API.
    http.get('/api/new_plugin/example').then((res) => {
      // eslint-disable-next-line no-console
      console.log(res.data);
    });
  };

  // Render the application DOM.
  // Note that `navigation.ui.TopNavMenu` is a stateful component exported on the `navigation` plugin's start contract.
  return (
    <Router basename={basename}>
      <I18nProvider>
        <>
          <navigation.ui.TopNavMenu
            appName={PLUGIN_ID}
            showSearchBar={true}
            useDefaultBehaviors={true}
          />
          <EuiPage restrictWidth="1000px">
            <EuiPageBody>
              <EuiPageHeader>
                <EuiTitle size="l">
                  <h1>
                    <FormattedMessage
                      id="newPlugin.helloWorldText"
                      defaultMessage="{name}"
                      values={{ name: PLUGIN_NAME }}
                    />
                  </h1>
                </EuiTitle>
              </EuiPageHeader>
              <EuiPageContent>
                <EuiPageContentHeader>
                  <EuiTitle>
                    <h2>
                      <FormattedMessage
                        id="newPlugin.congratulationsTitle"
                        defaultMessage="Sip Logs"
                      />
                    </h2>
                  </EuiTitle>
                  <EuiButton type="primary" size="s" onClick={onClickHandler}>
                      <FormattedMessage id="newPlugin.buttonText" defaultMessage="Get Logs" />
                    </EuiButton>
                </EuiPageContentHeader>
                <EuiPageContentBody>
                  <EuiText>
                    <EuiHorizontalRule />
                    <p>
                      <FormattedMessage
                        id="newPlugin.timestampText"
                        defaultMessage="Last timestamp: {time}"
                        values={{ time: timestamp ? timestamp : 'Unknown' }}
                      />
                    </p>
                  </EuiText>
                </EuiPageContentBody>
              </EuiPageContent>
            </EuiPageBody>
          </EuiPage>
        </>
      </I18nProvider>
    </Router>
  );
};

```

---

<div class="post-metadata">

**Author:** ![joshdover](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joshdover/32/42020_2.png) [@joshdover](https://discuss.elastic.co/u/joshdover)\
**Post date:** [May 5, 2021, 11:47am UTC](https://discuss.elastic.co/t/trouble-with-migrating-legacy-plugin/271635/4 "2021-05-05T11:47:27Z")

</div>

Hi there,

It would be helpful if you could describe exactly what you were having problems with in your implementation. Off hand, I noticed a few things:

I don't believe that your client-side code is accessing the right field of the response. I believe you should be accessing `res.response` (the same name of the field you return in `res.ok({ response })` on the server:

```auto
    http.get('/api/new_plugin/example').then((res) => {
      console.log(res.response);
    });

```

On the server side, your routing and search code looks pretty good, though you're not currently specifying an index to search on in your search request. It's worth noting that the object on `context.core.elasticsearch.client.asCurrentUser` is an instance of the `Client` class from the `@elastic/elasticsearch` package. You can find API examples here: [GitHub - elastic/elasticsearch-js: Official Elasticsearch client library for Node.js](https://github.com/elastic/elasticsearch-js)

I believe your query should look something closer to this:

```javascript
const results = await context.core.elasticsearch.client.asCurrentUser.search({
  index: 'my-index',
  body: {
    query: {
      match_all: {}
    }
  }
});

```

If you have any more specific questions, let us know!

---

<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:** [June 2, 2021, 11:48am UTC](https://discuss.elastic.co/t/trouble-with-migrating-legacy-plugin/271635/5 "2021-06-02T11:48:21Z")

</div>

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