From e58cd0a106ba6a5c7b5e5ec24ea97ce63673fcf7 Mon Sep 17 00:00:00 2001 From: Mel O'Hagan Date: Thu, 20 Apr 2023 17:30:45 +0100 Subject: [PATCH 1/4] Check substring if contains not array --- packages/backend-core/src/db/lucene.ts | 2 +- packages/shared-core/src/filters.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/backend-core/src/db/lucene.ts b/packages/backend-core/src/db/lucene.ts index 6f2f4fc991..49f345817e 100644 --- a/packages/backend-core/src/db/lucene.ts +++ b/packages/backend-core/src/db/lucene.ts @@ -309,7 +309,7 @@ export class QueryBuilder { return null } if (!Array.isArray(value)) { - return `${key}:${value}` + return `${key}:/${value?.toLowerCase()}/` } let statement = `${builder.preprocess(value[0], { escape: true })}` for (let i = 1; i < value.length; i++) { diff --git a/packages/shared-core/src/filters.ts b/packages/shared-core/src/filters.ts index 34186cf554..85c4cd7f5c 100644 --- a/packages/shared-core/src/filters.ts +++ b/packages/shared-core/src/filters.ts @@ -22,6 +22,7 @@ export const getValidOperatorsForType = ( Op.Empty, Op.NotEmpty, Op.In, + Op.Contains, ] const numOps = [ Op.Equals, From 68ba2402a0bd9c2f1ea30b060b69820e4a45bf35 Mon Sep 17 00:00:00 2001 From: Mel O'Hagan Date: Thu, 20 Apr 2023 17:51:58 +0100 Subject: [PATCH 2/4] Fix substring regex --- packages/backend-core/src/db/lucene.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/backend-core/src/db/lucene.ts b/packages/backend-core/src/db/lucene.ts index 49f345817e..63e31dd1eb 100644 --- a/packages/backend-core/src/db/lucene.ts +++ b/packages/backend-core/src/db/lucene.ts @@ -309,7 +309,7 @@ export class QueryBuilder { return null } if (!Array.isArray(value)) { - return `${key}:/${value?.toLowerCase()}/` + return `${key}:/.*${value?.toLowerCase()}.*/` } let statement = `${builder.preprocess(value[0], { escape: true })}` for (let i = 1; i < value.length; i++) { From 66a573dcc9208f7d9a54142a89f92df37ee0240e Mon Sep 17 00:00:00 2001 From: Mel O'Hagan Date: Thu, 20 Apr 2023 18:14:41 +0100 Subject: [PATCH 3/4] Fix fuzzy search internal DB --- packages/backend-core/src/db/lucene.ts | 26 ++++++++++++++------------ packages/shared-core/src/filters.ts | 8 +------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/packages/backend-core/src/db/lucene.ts b/packages/backend-core/src/db/lucene.ts index 63e31dd1eb..f8a9e196dc 100644 --- a/packages/backend-core/src/db/lucene.ts +++ b/packages/backend-core/src/db/lucene.ts @@ -309,7 +309,7 @@ export class QueryBuilder { return null } if (!Array.isArray(value)) { - return `${key}:/.*${value?.toLowerCase()}.*/` + return `${key}:${value}` } let statement = `${builder.preprocess(value[0], { escape: true })}` for (let i = 1; i < value.length; i++) { @@ -320,6 +320,18 @@ export class QueryBuilder { return `${key}:(${statement})` } + const fuzzy = (key: string, value: any) => { + if (!value) { + return null + } + value = builder.preprocess(value, { + escape: true, + lowercase: true, + type: "fuzzy", + }) + return `${key}:/.*${value}.*/` + } + const notContains = (key: string, value: any) => { const allPrefix = allOr ? "*:* AND " : "" const mode = allOr ? "AND" : undefined @@ -408,17 +420,7 @@ export class QueryBuilder { }) } if (this.#query.fuzzy) { - build(this.#query.fuzzy, (key: string, value: any) => { - if (!value) { - return null - } - value = builder.preprocess(value, { - escape: true, - lowercase: true, - type: "fuzzy", - }) - return `${key}:${value}~` - }) + build(this.#query.fuzzy, fuzzy) } if (this.#query.equal) { build(this.#query.equal, equal) diff --git a/packages/shared-core/src/filters.ts b/packages/shared-core/src/filters.ts index 85c4cd7f5c..20d79195f1 100644 --- a/packages/shared-core/src/filters.ts +++ b/packages/shared-core/src/filters.ts @@ -22,7 +22,6 @@ export const getValidOperatorsForType = ( Op.Empty, Op.NotEmpty, Op.In, - Op.Contains, ] const numOps = [ Op.Equals, @@ -55,13 +54,8 @@ export const getValidOperatorsForType = ( ops = stringOps.concat([Op.MoreThan, Op.LessThan]) } - // Filter out "like" for internal tables - const externalTable = datasource?.tableId?.includes("datasource_plus") - if (datasource?.type === "table" && !externalTable) { - ops = ops.filter(x => x !== Op.Like) - } - // Only allow equal/not equal for _id in SQL tables + const externalTable = datasource?.tableId?.includes("datasource_plus") if (field === "_id" && externalTable) { ops = [Op.Equals, Op.NotEquals, Op.In] } From 458b23fc1bc576b5e4b05e6f03fabcd3c8350468 Mon Sep 17 00:00:00 2001 From: Mel O'Hagan Date: Thu, 20 Apr 2023 21:57:48 +0100 Subject: [PATCH 4/4] Add escape for forward slash --- packages/backend-core/src/db/lucene.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/backend-core/src/db/lucene.ts b/packages/backend-core/src/db/lucene.ts index f8a9e196dc..5d21d4f4e4 100644 --- a/packages/backend-core/src/db/lucene.ts +++ b/packages/backend-core/src/db/lucene.ts @@ -243,7 +243,7 @@ export class QueryBuilder { } // Escape characters if (!this.#noEscaping && escape && originalType === "string") { - value = `${value}`.replace(/[ #+\-&|!(){}\]^"~*?:\\]/g, "\\$&") + value = `${value}`.replace(/[ \/#+\-&|!(){}\]^"~*?:\\]/g, "\\$&") } // Wrap in quotes