Skip to content

Commit

Permalink
fix: handle empty search query text
Browse files Browse the repository at this point in the history
  • Loading branch information
favoyang committed Oct 23, 2022
1 parent 1a82d86 commit 28b6430
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,14 @@ export default class RedisStorage implements IPluginStorage<RedisConfig> {
public async searchV1(query): Promise<any> {
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({
Expand All @@ -120,6 +121,7 @@ export default class RedisStorage implements IPluginStorage<RedisConfig> {
return searchResult;
} catch (error) {
this.logger.error(error, "searchV1 error: @{error}");
return [];
}
}

Expand Down

0 comments on commit 28b6430

Please sign in to comment.