Как мне обновить элемент вasticsearch, используя node.js lib? - PullRequest
0 голосов
/ 23 апреля 2020

Каждое обновление терпит неудачу для меня. Я не уверен, что я делаю неправильно:

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

Вот как документы были вставлены:

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

Вставки работают без нареканий. Мне интересно, если ничего не изменилось, может быть, по умолчанию происходит сбой обновления? Не знаю почему, но все они терпят неудачу.

Вот ошибка:


{
  "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
    }
  }
}

Вот ввод:

  {
  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
}

1 Ответ

0 голосов
/ 24 апреля 2020
    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);
  });
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...