5

我正在使用具有单个文档的单个索引的弹性搜索云。我正在使用@elastic/elasticsearch最新版本。我正在从 Firebase 云函数调用弹性搜索。

这是我的弹性客户端在云功能中的初始化方式

const { Client, errors } = require('@elastic/elasticsearch');
const elasticClient = new Client({
    cloud: {
        id: 'xxxxxxxx',
    },
    auth: {
        username: 'xxxx',
        password: 'xxxxxx'
    },
    maxRetries: 5,
    requestTimeout: 60000,
});

这是查询弹性搜索的云功能

exports.stickerSearch = functions.runWith(runtimeOpts).https.onRequest(async (req, res) => {
    try {
        const searchQuery = req.query.query;
        const searchResult = await elasticClient.search(
            {
                index: "packs",
                from: 0,
                q: searchQuery,
                size: 20,
                sort: 'dataCreatedAt'
            });
        res.status(searchResult.statusCode).send(searchResult.body.hits);
    }
    catch (e) {        
        console.log("search error", e)
        res.status(200).send({ "total": { "value": 0, "relation": "eq" }, "max_score": null, "hits": [] });
    }
});

当我通过 HTTP 调用此函数作为具有相同“查询”参数的 GET 请求时,该函数的一半按预期工作并返回搜索结果,另一半则失败并出现以下错误:

{ ResponseError: Response Error
    at IncomingMessage.response.on (/srv/node_modules/@elastic/elasticsearch/lib/Transport.js:287:25)
    at emitNone (events.js:111:20)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:139:11)
    at process._tickDomainCallback (internal/process/next_tick.js:219:9)
  name: 'ResponseError',
  meta: 
   { body: '400 Bad Request',
     statusCode: 400,
     headers: { 'content-type': 'text/plain; charset=utf-8', connection: 'close' },
     warnings: null,
     meta: { 
        context: null,
        request: [Object],
        name: 'elasticsearch-js',
        connection: [Object],
        attempts: 0,
        aborted: false 
} } }

我不知道为什么相同的请求有时会失败。

4

0 回答 0