0

Every update fails for me. I'm not sure what I'm doing wrong:

    await search
      .update({
        index: 'posts',
        id: post._id,
        body: post,
      })
      .catch(err => {
        failed.updates += 1;
        console.log('update failed', post._id);
      });

Here is how the docs got inserted:

    await search
      .index({
        index: 'posts',
        id: post._id,
        body: post,
      })
      .catch(err => {
        failed.inserts += 1;
        console.error(err);
      });

Inserts work flawlessly. I'm wondering if nothing changed maybe update fails by default? Not sure why but they all fail.

Here is the error:


{
  "name": "ResponseError",
  "meta": {
    "body": {
      "error": {
        "root_cause": [
          {
            "type": "x_content_parse_exception",
            "reason": "[1:2] [UpdateRequest] unknown field [score]"
          }
        ],
        "type": "x_content_parse_exception",
        "reason": "[1:2] [UpdateRequest] unknown field [score]"
      },
      "status": 400
    },
    "statusCode": 400,
    "headers": {
      "content-type": "application/json; charset=UTF-8",
      "content-length": "221"
    },
    "warnings": null,
    "meta": {
      "context": null,
      "request": {
        "params": {
          "method": "POST",
          "path": "/posts/_update/5ea1947ebbb15d001c408c4d",
          "body": "{\"score\":2,\"views\":21,\"type\":\"link\",\"ranking\":2.7183885087703956,\"sponsored\":false,\"title\":\"Why a âreturn to normalâ could mean disaster for the stock market - MarketWatch\",\"url\":\"https://www.marketwatch.com/story/why-a-return-to-normal-could-mean-disaster-for-the-stock-market-2020-04-22\",\"thumb\":\"https://s.marketwatch.com/public/resources/images/MW-IE973_corona_ZH_20200422114239.jpg\",\"author\":{\"karma\":3910,\"subscriptions\":[\"5e4dad2e5a6930001d9bad99\",\"5e9a298303c86d001c8cf584\",\"5e9aae5d03c86d001c8cf5b9\",\"5e9aa4d703c86d001c8cf5b4\",\"5e622896def3ed002821b9bf\",\"5e507210bdbc8100286fbeee\",\"5e4e3a4d5a6930001d9badb0\",\"5e9da81bd0ff32001c5fd0b9\",\"5e9eecf3ef8abb001ddc2696\"],\"username\":\"marcolanz\",\"created\":\"2020-03-23T03:27:46.936Z\",\"bitcoinAddress\":\"1EsBjmHakFkTnRxxcsB8M1hNHdUnyLg2w7\",\"links\":[],\"id\":\"5e782cb248f8c00027f14f10\"},\"category\":{\"subscriberCount\":2,\"nsfw\":false,\"_id\":\"5e9eecf3ef8abb001ddc2696\",\"name\":\"StockMarket\",\"description\":\"Stock market\",\"owner\":\"5e782cb248f8c00027f14f10\",\"created\":\"2020-04-21T12:54:11.398Z\",\"__v\":0},\"votes\":[{\"user\":\"5e782cb248f8c00027f14f10\",\"vote\":1},{\"user\":\"5e92618ba33715001c54530b\",\"vote\":1}],\"comments\":[],\"created\":\"2020-04-23T13:13:34.989Z\",\"text\":null,\"upvotePercentage\":100,\"commentCount\":0,\"id\":\"5ea1947ebbb15d001c408c4d\"}",
          "querystring": "",
          "headers": {
            "User-Agent": "elasticsearch-js/7.6.1 (linux 4.15.0-96-generic-x64; Node.js v12.16.2)",
            "Content-Type": "application/json",
            "Content-Length": "1278"
          },
          "timeout": 30000
        },
        "options": {
          "warnings": null
        },
        "id": 33
      },
      "name": "elasticsearch-js",
      "connection": {
        "url": "http://es01:9200/",
        "id": "http://es01:9200/",
        "headers": {},
        "deadCount": 0,
        "resurrectTimeout": 0,
        "_openRequests": 1,
        "status": "alive",
        "roles": {
          "master": true,
          "data": true,
          "ingest": true,
          "ml": false
        }
      },
      "attempts": 0,
      "aborted": false
    }
  }
}

Here's the input:

  {
  score: 2,
  views: 21,
  type: 'link',
  ranking: 2.7183885087703956,
  sponsored: false,
  _id: 5ea1947ebbb15d001c408c4d,
  title: 'Why a âreturn to normalâ could mean disaster for the stock market - MarketWatch',
  url: 'https://www.marketwatch.com/story/why-a-return-to-normal-could-mean-disaster-for-the-stock-market-2020-04-22',
  thumb: 'https://s.marketwatch.com/public/resources/images/MW-IE973_corona_ZH_20200422114239.jpg',
  author: {
    karma: 3910,
    subscriptions: [
      5e4dad2e5a6930001d9bad99,
      5e9a298303c86d001c8cf584,
      5e9aae5d03c86d001c8cf5b9,
      5e9aa4d703c86d001c8cf5b4,
      5e622896def3ed002821b9bf,
      5e507210bdbc8100286fbeee,
      5e4e3a4d5a6930001d9badb0,
      5e9da81bd0ff32001c5fd0b9,
      5e9eecf3ef8abb001ddc2696
    ],
    _id: 5e782cb248f8c00027f14f10,
    username: 'marcolanz',
    password: '$2a$10$qDEn46Qr0uGr0pGDP5FWO.aD411ARsOdVJpgzevWAgz1XR0tUW6kK',
    created: 2020-03-23T03:27:46.936Z,
    __v: 0,
    ip: '201.21.39.32',
    bitcoinAddress: '1EsBjmHakFkTnRxxcsB8M1hNHdUnyLg2w7',
    links: []
  },
  category: {
    subscriberCount: 2,
    nsfw: false,
    _id: 5e9eecf3ef8abb001ddc2696,
    name: 'StockMarket',
    description: 'Stock market',
    owner: 5e782cb248f8c00027f14f10,
    created: 2020-04-21T12:54:11.398Z,
    __v: 0
  },
  votes: [
    { user: 5e782cb248f8c00027f14f10, vote: 1 },
    { user: 5e92618ba33715001c54530b, vote: 1 }
  ],
  comments: [],
  created: 2020-04-23T13:13:34.989Z,
  text: null
}

8
  • So what is the error you are getting in exception? Commented Apr 23, 2020 at 5:23
  • 1
    Can you please post the err that you are getting? console.log('update failed', post._id, err); Commented Apr 23, 2020 at 5:26
  • 1
    Actually, you need to do this: console.log('update failed', post._id, JSON.stringify(err)) so that the inner JSON object are also printed out Commented Apr 23, 2020 at 6:45
  • 1
    Thanks, can you show the content of the body you're sending in the update request? Commented Apr 23, 2020 at 7:23
  • 1
    stackoverflow.com/questions/57564374/… you need to wrap it in { doc: ... } Commented Apr 23, 2020 at 23:21

2 Answers 2

2
    await search
  .update({
    index: 'posts',
    id: post._id,
    body: { doc: post }, // this is the fix, wrap in "doc"
  })
  .catch(err => {
    failed.updates += 1;
    console.log('update failed', post._id);
  });
Sign up to request clarification or add additional context in comments.

Comments

0

With Elasticsearch 7.6 client, you need to wrap the body inside "doc" key. If you are using "script" along with "doc" then "doc" will be ignored.

Ref: https://www.elastic.co/guide/en/elasticsearch/reference/7.6/docs-update.html#_update_part_of_a_document

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.