@ types / elasticsearch SearchResponse的类型错误

问题描述 投票:2回答:1

当我使用@ nestjs / elasticsearch的ElasticsearchService时,响应的结果与@ types / elasticsearch的SearchResponse类型不匹配,因为SearchResponse是一个对象,但我实际上得到一个包含SearchResponse对象和Http状态代码的数组,有人知道如何把它关掉?

例:

[
  {
    ...,
    "aggregations": {
      "backendVersions": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": "1.0.0",
            "doc_count": 1
          }
        ]
      }
    }
  },
  200
]
typescript elasticsearch http-status-codes nestjs
1个回答
2
投票

@ nestjs / elasticsearch的ElasticsearchService用bindNodeCallback包装es客户端,它在doc中有解释。

所以这:

client.search({
  index: 'my-index',
  body: { foo: 'bar' }
}, (err, body, statusCode, headers) => {
  if (err) console.log(err)
})

将被转换为一个observable,它将使用回调中的args减去数组中的错误值。

service.search({
  index: 'my-index',
  body: { foo: 'bar' }
}).subscribe(value => {
  console.log(value); // [body, statusCode, headers]
});

你无法关闭它,但你可以使用getClient()直接使用elasticsearch客户端:

const searchResponse = await service.getClient().search({
  index: 'my-index',
  body: { foo: 'bar' }
});

还要记住会有breaking changes in @elastic/elasticsearch [7.x]

client.search({
  index: 'my-index',
  body: { foo: 'bar' }
}, (err, { body, statusCode, headers, warnings }) => {
  if (err) console.log(err)
});
© www.soinside.com 2019 - 2024. All rights reserved.