diff --git a/server/models/helpers/SearchHelper.test.ts b/server/models/helpers/SearchHelper.test.ts index e5f21f1441..3a44b196ba 100644 --- a/server/models/helpers/SearchHelper.test.ts +++ b/server/models/helpers/SearchHelper.test.ts @@ -133,10 +133,19 @@ describe("SearchHelper", () => { test("should handle backslashes in search term", async () => { const team = await buildTeam(); - const { results } = await SearchHelper.searchForTeam(team, { - query: "\\\\", + const collection = await buildCollection({ + teamId: team.id, }); - expect(results.length).toBe(0); + const document = await buildDocument({ + teamId: team.id, + collectionId: collection.id, + title: "test with backslash \\", + }); + const { results } = await SearchHelper.searchForTeam(team, { + query: "test with backslash \\", + }); + expect(results.length).toBe(1); + expect(results[0].document?.id).toBe(document.id); }); test("should return the total count of search results", async () => { diff --git a/server/models/helpers/SearchHelper.ts b/server/models/helpers/SearchHelper.ts index c63fd4bdc9..fb2a2aba5c 100644 --- a/server/models/helpers/SearchHelper.ts +++ b/server/models/helpers/SearchHelper.ts @@ -476,7 +476,7 @@ export default class SearchHelper { if (options.query) { // find words that look like urls, these should be treated separately as the postgres full-text // index will generally not match them. - const likelyUrls = getUrls(options.query); + let likelyUrls = getUrls(options.query); // remove likely urls, and escape the rest of the query. let limitedQuery = this.escapeQuery( @@ -486,6 +486,9 @@ export default class SearchHelper { .trim() ); + // Escape the URLs + likelyUrls = likelyUrls.map((url) => this.escapeQuery(url)); + // Extract quoted queries and add them to the where clause, up to a maximum of 3 total. const quotedQueries = Array.from(limitedQuery.matchAll(/"([^"]*)"/g)).map( (match) => match[1]