Calling Elasticsearch API from Kibana plugin

Follow-up for #337003
Similar to #268179

Hello,
The main idea for the plugin I develop is to call elastic to alter some already existing index (just like using SQL UPDATE method).
I'm trying to call elastic API through the route I have defined on the server side, followed up by context.core.elasticsearch.client.asCurrentUser.updateByQuery command.
Unfortunately, when making a request from within the plugin, my browser returns Error 500: Internal Server Error and I cannot fully understand why. Neither Kibana nor Elasticsearch logs don't show any errors and I'm out of ideas how to debug this problem futher.
I would be very grateful for any tips and insights about what I did wrong here.
I'm developing for ver. 8.5.3 with intention of updating to the latest one later.

My code for the client side:

const onSubmitHandler = () => {
//extracting data from eui form
    const form = document.querySelector('form')!;
    const data = new FormData(form);
    const { startDate, endDate } = dateRange;
    const choice = data.get('index') as string;
    const field = data.get('field') as string;
    const value = data.get('new_value') as string;
    const body_array = [];
    body_array.push(choice,field,value,startDate,endDate);
//request to Kibana-Elastic API
    http.fetch('/api/my_plugin/_update_by_query', {
        method: 'POST',
        body: body_array,
      }
    ).then(r => {
      alert(r);
    }).catch(error => alert(error));
    return false;
  };

Server side:

router.post(
    {
      path: '/api/my_plugin/_update_by_query',
      validate: false,
    },

    async (context, request, response)=>{
      try {
        const { body } = request;
        const client = context.core.elasticsearch.client.asCurrentUser;

//calling a request with data passed from the client side
        const { body: result }= await client.updateByQuery({
          "index": request.body[0],
          "script": {
            "source": "ctx._source.kpi_fixed = params.fieldValue",
            "params": {
              "fieldName": request.body[1] ,
              "fieldValue":  request.body[2]
            },
            "lang": "painless"
          },
          "query": {
            "range": {
              "@timestamp": {
                "format": "strict_date_optional_time",
                "gte":  request.body[3] ,
                "lte":  request.body[4]
              }
            }
          }
        });
       
        return response.ok({
          body: result,
        });
      }catch (e) {
        return  response.customError(wrapError(e));
      }

    }
  )

Hi @trosagnant,

As in the mentioned example, let's try to use this syntax:

const esClient = (await context.core).elasticsearch.client;
const { body: result }= await esClient.asCurrentUser.updateByQuery({
....

Hello @jughosta thanks for answering.
Unfortunately it didn't change anything, I still get the

{"statusCode":500,"error":"Internal Server Error","message":"Cannot read properties of undefined (reading 'client')"}

Are there any other possible solutions?

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