基本配置
本页介绍 JavaScript 客户端的基本配置选项。
const { Client } = require('@elastic/elasticsearch')
const client = new Client({
cloud: { id: '<cloud-id>' },
auth: { apiKey: 'base64EncodedKey' },
maxRetries: 5,
sniffOnStart: true
})
要使用的 Elasticsearch 端点。可以是单个字符串或字符串数组
node: 'http://localhost:9200'
nodes: ['http://localhost:9200', 'http://localhost:9201']
或者它可以是一个表示节点的对象(或对象数组)
node: {
url: new URL('http://localhost:9200'),
tls: 'tls options',
agent: 'http agent options',
id: 'custom node id',
headers: { 'custom': 'headers' },
roles: {
master: true,
data: true,
ingest: true,
ml: false
}
}
默认值: null
您的身份验证数据。您可以使用基本身份验证和 ApiKey。有关详细信息,请参阅 身份验证。
基本认证
auth: {
username: 'elastic',
password: 'changeme'
}
ApiKey 身份验证
auth: {
apiKey: 'base64EncodedKey'
}
Bearer 身份验证,对 服务账户令牌 有用。请注意,它不处理自动令牌刷新
auth: {
bearer: 'token'
}
类型: number
默认值: 3
每个请求的最大重试次数。
类型: number
默认值: 无值
每个请求的最大请求超时时间(毫秒)。
类型: number
默认值: 3000
每个 Ping 请求的最大超时时间(毫秒)。
类型: number, boolean
默认值: false
每隔 n
毫秒执行一次嗅探操作。
嗅探可能不是最佳解决方案。在使用各种 sniff
选项之前,请查阅这篇 博客文章。
类型: boolean
默认值: false
客户端启动后执行一次嗅探。务必查阅嗅探最佳实践的 博客文章。
类型: string
默认值: '_nodes/_all/http'
嗅探期间 Ping 的端点。务必查阅嗅探最佳实践的 博客文章。
类型: boolean
默认值: false
连接故障时执行嗅探。务必查阅嗅探最佳实践的 博客文章。
类型: string
默认值: 'ping'
配置节点复活策略。
选项: 'ping'
, 'optimistic'
, 'none'
类型: boolean
默认值: false
为每个请求添加 accept-encoding
请求头。
类型: string, boolean
默认值: false
启用 gzip 请求体压缩。
选项: 'gzip'
, false
类型: http.SecureContextOptions
默认值: null
类型: string, URL
默认值: null
如果您使用 http(s) 代理,可以在此处填写其 URL。客户端将自动处理与它的连接。
const client = new Client({
node: 'http://localhost:9200',
proxy: 'http://localhost:8080'
})
const client = new Client({
node: 'http://localhost:9200',
proxy: 'http://user:pwd@localhost:8080'
})
类型: http.AgentOptions, function
默认值: null
http 代理 选项,或者是一个返回实际 http 代理实例的函数。如果您想完全禁用 http 代理(并禁用 keep-alive
功能),请将 agent 设置为 false
。
const client = new Client({
node: 'http://localhost:9200',
agent: { agent: 'options' }
})
const client = new Client({
node: 'http://localhost:9200',
// the function takes as parameter the option
// object passed to the Connection constructor
agent: (opts) => new CustomAgent()
})
const client = new Client({
node: 'http://localhost:9200',
// Disable agent and keep-alive
agent: false
})
类型: function
指示是否应使用节点进行请求的过滤器。默认函数定义。
function defaultNodeFilter (conn) {
if (conn.roles != null) {
if (
// avoid master-only nodes
conn.roles.master &&
!conn.roles.data &&
!conn.roles.ingest &&
!conn.roles.ml
) return false
}
return true
}
类型: function
默认值: 'round-robin'
自定义选择策略。
选项: 'round-robin'
, 'random'
, 自定义函数
自定义函数示例
function nodeSelector (connections) {
const index = calculateIndex()
return connections[index]
}
类型: function
用于为每个请求生成请求 ID 的函数,它接受请求参数和选项作为两个参数。默认情况下,它为每个请求生成一个递增的整数。
自定义函数示例
function generateRequestId (params, options) {
// your id generation logic
// must be syncronous
return 'id'
}
类型: string, symbol
默认值: elasticsearch-js
用于在事件中识别客户端实例的名称。
类型: string
默认值: null
一个字符串,将用于为任何 X-Opaque-Id
请求头添加前缀。有关详细信息,请参阅 X-Opaque-Id
支持。
类型: object
默认值: {}
一组将在每个请求中发送的自定义请求头。
类型: object
默认值: null
一个自定义对象,您可以在事件中使用它进行可观测性。它将与 API 级别的上下文选项合并。
类型: boolean
默认值: true
如果为 true,则添加一个名为 'x-elastic-client-meta'
的请求头,其中包含一些最少遥测数据,例如客户端和平台版本。
类型: object
默认值: null
连接到 Elastic Cloud 的自定义配置。有关详细信息,请参阅 身份验证。
云配置示例
const client = new Client({
cloud: {
id: '<cloud-id>'
},
auth: {
username: 'elastic',
password: 'changeme'
}
})
默认值: true
boolean
、'proto'
、'constructor'
- 客户端可以保护您免受原型污染攻击。有关详细信息,请参阅 方括号是敌人。如果需要,您可以完全启用原型污染保护 (`false`) 或其中一项检查 (`'proto'` 或 `'constructor'`)。出于性能原因,默认情况下禁用此功能。要了解更多信息,请参阅 secure-json-parse
文档。
类型: string
默认值: null
如果配置,验证签署服务器证书的 CA 证书的指纹是否与提供的指纹匹配。仅接受 SHA256 摘要指纹。
类型: number
默认值: null
配置后,maxResponseSize
验证未压缩响应大小是否小于配置的数字。如果大于该数字,请求将被取消。maxResponseSize
不能大于 buffer.constants.MAX_STRING_LENGTH
的值。
类型: number
默认值: null
配置后,maxCompressedResponseSize
验证压缩响应大小是否小于配置的数字。如果大于该数字,请求将被取消。maxCompressedResponseSize
不能大于 buffer.constants.MAX_STRING_LENGTH
的值。
类型: object
默认值: 将替换 Error 元数据中已知敏感数据源的配置
关于如何从附加到 Error 对象的元数据中脱敏潜在敏感数据的选项
阅读有关敏感信息脱敏 的更多详细信息
类型: string
默认值: "stack"
设置为 "stack"
会假定传统的(非 Serverless)Elasticsearch 实例设置默认值。设置为 "serverless"
会设置默认值,以便更无缝地与 Elastic Cloud Serverless 配合使用,例如启用压缩并禁用假定存在多个 Elasticsearch 节点的特性。