Link Search Menu Expand Document Documentation Menu

辅助方法

辅助方法简化了复杂 API 任务的使用。有关客户端的完整 API 文档和更多示例,请参阅JS 客户端 API 文档

批量助手

批量助手简化了执行复杂的批量 API 请求。批量助手支持同类型的操作。或者,您可以使用client.bulk方法执行多种类型的批量操作。例如,您可以在一个批量请求中发送deleteindex操作。有关更多信息,请参阅批量指南

用法

以下代码创建了一个批量助手实例

const { Client } = require('@opensearch-project/opensearch')
const documents = require('./docs.json')

const client = new Client({ ... })

const result = await client.helpers.bulk({
  datasource: documents,
  onDocument (doc) {
    return {
      index: { _index: 'example-index' }
    }
  }
})

console.log(result)

批量助手操作返回一个包含以下字段的对象

{
  total: number,
  failed: number,
  retry: number,
  successful: number,
  time: number,
  bytes: number,
  aborted: boolean
}

批量助手配置选项

创建新的批量助手实例时,可以使用以下配置选项。

选项 数据类型 必需/默认 描述
数据源 字符串或对象的数组、异步生成器或可读流 必需 表示您需要创建、删除、索引或更新的文档。
onDocument 功能 必需 一个函数,用于对给定数据源中的每个文档调用。它返回要为此文档执行的操作。(可选)对于createindex操作,可以通过返回新文档作为函数结果的一部分来操纵文档。
并发数 整数 可选。默认值为 5。 并行执行的请求数量。
刷新字节数 整数 可选。默认值为 5,000,000。 要发送的最大批量体大小(以字节为单位)。
刷新间隔 整数 可选。默认值为 30,000。 读取最后一个文档后,刷新主体前等待的时间(毫秒)。
onDrop 功能 可选。默认值为noop 一个函数,用于在达到最大重试次数后无法索引的每个文档调用。
完成后刷新 布尔型 可选。默认值为false 在批量操作结束时是否应对所有受影响的索引运行刷新。
重试次数 整数 可选。默认为客户端的maxRetries值。 在对该文档调用onDrop之前,操作的重试次数。
等待时间 整数 可选。默认值为 5,000。 重试操作前等待的时间(毫秒)。

示例

以下示例演示了索引、创建、更新和删除批量助手操作。有关更多信息和高级索引操作,请参阅 GitHub 中的opensearch-js指南

索引

索引操作在文档不存在时创建新文档,在文档已存在时重新创建文档。

以下批量操作将文档索引到example-index

client.helpers.bulk({
  datasource: arrayOfDocuments,
  onDocument (doc) {
    return {
      index: { _index: 'example-index' }
    }
  }
})

以下批量操作将文档覆盖索引到example-index

client.helpers.bulk({
  datasource: arrayOfDocuments,
  onDocument (doc) {
    return [
      {
        index: { _index: 'example-index' }
      },
      { ...doc, createdAt: new Date().toISOString() }
    ]
  }
})

创建

创建操作仅在文档不存在时创建新文档。

以下批量操作在example-index中创建文档

client.helpers.bulk({
  datasource: arrayOfDocuments,
  onDocument (doc) {
    return {
      create: { _index: 'example-index', _id: doc.id }
    }
  }
})

以下批量操作将文档覆盖创建到example-index

client.helpers.bulk({
  datasource: arrayOfDocuments,
  onDocument (doc) {
    return [
      {
        create: { _index: 'example-index', _id: doc.id }
      },
      { ...doc, createdAt: new Date().toISOString() }
    ]
  }
})

更新

更新操作用发送的字段更新文档。文档必须已存在于索引中。

以下批量操作更新arrayOfDocuments中的文档

client.helpers.bulk({
  datasource: arrayOfDocuments,
  onDocument (doc) {
    // The update operation always requires a tuple to be returned, with the
    // first element being the action and the second being the update options.
    return [
      {
        update: { _index: 'example-index', _id: doc.id }
      },
      { doc_as_upsert: true }
    ]
  }
})

以下批量操作将文档覆盖更新到arrayOfDocuments

client.helpers.bulk({
  datasource: arrayOfDocuments,
  onDocument (doc) {
    return [
      {
        update: { _index: 'example-index', _id: doc.id }
      },
      {
        doc: { ...doc, createdAt: new Date().toISOString() },
        doc_as_upsert: true
      }
    ]
  }
})

删除

删除操作删除一个文档。

以下批量操作从example-index中删除文档

client.helpers.bulk({
  datasource: arrayOfDocuments,
  onDocument (doc) {
    return {
      delete: { _index: 'example-index', _id: doc.id }
    }
  }
})

https://github.com/opensearch-project/opensearch-js/tree/main/guides

剩余 350 字符

有问题?

想贡献?