根据查询更新

编辑

最简单的_update_by_query_用法只是对索引中的每个文档执行更新,而无需更改源数据。这对于获取新的属性或其他在线映射更改非常有用。

'use strict'

const { Client } = require('@elastic/elasticsearch')
const client = new Client({
  cloud: { id: '<cloud-id>' },
  auth: { apiKey: 'base64EncodedKey' }
})

async function run () {
  await client.index({
    index: 'game-of-thrones',
    document: {
      character: 'Ned Stark',
      quote: 'Winter is coming.'
    }
  })

  await client.index({
    index: 'game-of-thrones',
    refresh: true,
    document: {
      character: 'Arya Stark',
      quote: 'A girl is Arya Stark of Winterfell. And I\'m going home.'
    }
  })

  await client.updateByQuery({
    index: 'game-of-thrones',
    refresh: true,
    script: {
      lang: 'painless',
      source: 'ctx._source["house"] = "stark"'
    },
    query: {
      match: {
        character: 'stark'
      }
    }
  })

  const result = await client.search({
    index: 'game-of-thrones',
    query: { match_all: {} }
  })

  console.log(result.hits.hits)
}

run().catch(console.log)