1

I Have this strange error while I'm trying to update an index on elasticJS using express:

router.put('/update/:id', async (req, res) => {
  try {
    console.log('Entered updateuser', req.params.id);
    const salt = await bcrypt.genSalt(10);
    let password = await bcrypt.hash(req.body.password, salt);

    const data = {
      email: req.body.email,
      firstName: req.body.firstName,
      lastName: req.body.lastName,
      phone: req.body.phone,
      password
    };

    await client.update({
      index: 'users',
      type: 'mytype',
      id: req.params.id,
      body: data
    });
    res.json({ msg: 'Updated Successfully' });
  } catch (err) {
    // res.status(500).send(err.message);
    console.log(err);
  }
});

I've checked all the parameters are there but I get an error :

use the endpoint /{index}/_update/{id} instead."' ],

[0]      meta:
[0]       { context: null,
[0]         request: [Object],
[0]         name: 'elasticsearch-js',
[0]         connection: [Object],
[0]         attempts: 0,
[0]         aborted: false } } }
[0] Entered updateuser GIt7NW0BKLEzmnN0prT2
[0] { ResponseError: x_content_parse_exception
[0]         request: [Object],
[0]         name: 'elasticsearch-js',
[0]         connection: [Object],
[0]         attempts: 0,
[0]         aborted: false } } }

does anyone know whats the problem?

Alexander
  • 1,288
  • 5
  • 19
  • 38
  • the request body sent to elastic is wrong. That means some variable in `data` variable either is missing or is not sent correctly – Nikos M. Sep 15 '19 at 16:11
  • @NikosM. well I checked all the fields and nothing is missing.. – Alexander Sep 15 '19 at 16:20
  • maybe they are in the wrong format – Nikos M. Sep 15 '19 at 16:21
  • @NikosM. they are all strings, I don't think it is, maybe i'm missing some parameter? – Alexander Sep 15 '19 at 16:26
  • this is the error `ResponseError: x_content_parse_exception`, a quick lookup yields: https://discuss.elastic.co/t/problems-with-role-based-access-x-content-parse-exception/148796, https://github.com/elastic/elasticsearch/issues/30261 – Nikos M. Sep 15 '19 at 16:29

1 Answers1

6

If you want to use update api and update part of a document you have to wrap your changes in doc.

await client.update({
  index: 'users',
  type: 'mytype',
  id: req.params.id,
  body: { 
     doc: data // <===
  }
});

Update part of a document

Mehdi Shakeri
  • 584
  • 3
  • 11