Plugin Development - Saving Updates

How do I save things to Elastic from the server side of a plugin?

I have several use cases coming up where I will either need to insert a document into an index or use the Watcher API to save a watcher.

I may be crazy but I can't find much documentation around this type of thing. Right now I'm stumbling around and generally successful getting data by using the callWithRequest that I get out of server.plugins.elasticsearch.getCluster('data').

I'm wondering is there some way of either using callWithRequest to save a new document or is there something else completely that is used to save data. The CURL and other documentation for inserting documents makes sense if you are on the client side, but I'm on the server side (kibana) attempting to push data into Elastic.

Here's some psudo code of what I'm attempting:

  let {callWithRequest} = server.plugins.elasticsearch.getCluster('data');
  
  let document = {
    index: 'my-index',
    body: {
      fieldA: 'something',
      fieldB: 'awesome'
    }
  };
  
  callWithRequest(request, 'insert?', document).then((result) => reply('All good'));

And getting the document to insert is fine. Then what about the Watcher API, where .watches is read only and I have to use the defined methods: _xpack/watcher/watches/...

Please help :scream:

You should be able to use the 'index' action.

callWithRequest(request, 'index', {
  index: 'my-index',
  type: 'documents',
  id: 'document_12345',
  body: {
    fieldA: 'something',
    fieldB: 'awesome'
  }
});

let me know if that works for you.

Fantastic thanks, I'll give that a spin. Are the action types that callWithRequest can do documented somewhere (even if it's in the kibana source)?

Also the watcher API has 3 levels. What would it look like for that?

index: '_xpack',
type: 'watchers',
third: 'watcher',
id: 'fancy-watcher'

try this:

callWithRequest(request, 'transport.request', {
  path: '/_xpack/watcher/watch/fancy-watcher',
  method: 'POST',
  body: { ... }
});

Delicious, thanks. I'm getting more powerful every day :slight_smile:

Thanks for the quick reply.

You're welcome!

AFAIK, the callWithRequest is a wrapper around the elasticsearch.js module. Documentation can be found here

2 Likes

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