Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

feat(backend:files): treat "_" as a term boundary in regex search

johavenbcd357722d31
diff --git a/backend/src/applications/files/services/files-search-manager.service.ts b/backend/src/applications/files/services/files-search-manager.service.ts
index 3705bd55..e485841b 100644
--- a/backend/src/applications/files/services/files-search-manager.service.ts
+++ b/backend/src/applications/files/services/files-search-manager.service.ts
@@ -14,6 +14,7 @@ import { genRegexPositiveAndNegativeTerms } from '../utils/files-search'
import { FilesParser } from './files-parser.service'
import { FILE_REPOSITORY } from '../constants/operations'
import { genIndexingKey } from '../utils/indexing'
+import { escapePath } from '../../../common/functions'
@Injectable()
export class FilesSearchManager {
@@ -66,7 +67,7 @@ export class FilesSearchManager {
const regexpTerms = genRegexPositiveAndNegativeTerms(search)
for await (const [_id, _type, paths] of this.filesParser.allPaths([userId], spaceIds, shareIds)) {
for (const p of paths) {
- const regexBasePath = new RegExp(`^/?${p.realPath}/?`)
+ const regexBasePath = new RegExp(`^/?${escapePath(p.realPath)}/?`)
if (!p.isDir) {
const f = await this.analyzeFile(p.realPath, p.pathPrefix, regexBasePath, regexpTerms)
if (f !== null) {
diff --git a/backend/src/applications/files/utils/files-search.ts b/backend/src/applications/files/utils/files-search.ts
index d42d3d8c..3714f723 100644
--- a/backend/src/applications/files/utils/files-search.ts
+++ b/backend/src/applications/files/utils/files-search.ts
@@ -4,6 +4,7 @@ import { MIN_CHARS_TO_SEARCH } from '../constants/indexing'
const regexMatchSearchBoolean = new RegExp(`([+-]?)(?:"([^"]+)"|(\\S+))`)
const regexMatchesSearchBoolean = new RegExp(regexMatchSearchBoolean.source, 'g')
const booleanOperators = new Set(['+', '-', '<', '>', '~', '*'])
+const BOUNDARY = '(?:\\b|_)'
const accentToBaseMap = new Map<string, string>([
['a', '[aàáâä]'],
['e', '[eèéêë]'],
@@ -85,12 +86,12 @@ export function genRegexPositiveAndNegativeTerms(search: string): RegExp {
const negativeTerms = analyzeTerms(search, true)
const p = positiveTerms
.map((t) => genAccentInsensitiveRegexpPattern(t))
- .map((t) => `(?=.*\\b${t})`)
+ .map((t) => `(?=.*${BOUNDARY}${t})`)
.join('')
if (!negativeTerms.length) return new RegExp(p, 'i')
const n = negativeTerms
.map((t) => genAccentInsensitiveRegexpPattern(t))
- .map((t) => `\\b${t}\\b`)
+ .map((t) => `${BOUNDARY}${t}${BOUNDARY}`)
.join('|')
return new RegExp(`^${p}(?!.*(${n})).*$`, 'i')
}