From 28b643073be25c6519a5591993b48fc64e3103e3 Mon Sep 17 00:00:00 2001 From: Favo Yang Date: Sun, 23 Oct 2022 14:26:57 +0800 Subject: [PATCH] fix: handle empty search query text --- src/plugin.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/plugin.ts b/src/plugin.ts index 5622589..0043862 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -91,13 +91,14 @@ export default class RedisStorage implements IPluginStorage { public async searchV1(query): Promise { this.logger.debug({ query }, "searchV1 query: @{query}"); // Redis-search treats hyphen as separator, refs https://forum.redis.com/t/query-with-dash-is-treated-as-negation/119 - const text = query.text.replace(/-/g, ' ').trim(); + const text = (query.text || "").replace(/-/g, ' ').trim(); if (!text) return []; const offset = query.from || 0; const num = query.size || 250; try { const result: any = await this.redisClient.call("FT.SEARCH", "ve-pkg-stat-idx", text, "return", "1", "stat", "LIMIT", String(offset), String(num)); const searchResult: any = []; + if (result.length <= 1) return []; for (let i = 2; i < result.length; i += 2) { const stat = JSON.parse(result[i][1]); searchResult.push({ @@ -120,6 +121,7 @@ export default class RedisStorage implements IPluginStorage { return searchResult; } catch (error) { this.logger.error(error, "searchV1 error: @{error}"); + return []; } }