diff --git a/packages/server/src/api/routes/tests/search.spec.ts b/packages/server/src/api/routes/tests/search.spec.ts index 6ed9245ee6..e3f17c36cb 100644 --- a/packages/server/src/api/routes/tests/search.spec.ts +++ b/packages/server/src/api/routes/tests/search.spec.ts @@ -2697,7 +2697,7 @@ describe.each([ }) }) - isSql && + !isLucene && describe("$and", () => { beforeAll(async () => { table = await createTable({ @@ -2771,7 +2771,7 @@ describe.each([ }) }) - isSql && + !isLucene && describe("$or", () => { beforeAll(async () => { table = await createTable({ diff --git a/packages/shared-core/src/filters.ts b/packages/shared-core/src/filters.ts index f4e40f50cb..e16e27fd30 100644 --- a/packages/shared-core/src/filters.ts +++ b/packages/shared-core/src/filters.ts @@ -464,14 +464,18 @@ export const runQuery = (docs: Record[], query: SearchFilters) => { ) => (doc: Record) => { for (const [key, testValue] of Object.entries(query[type] || {})) { - const result = test(deepGet(doc, removeKeyNumbering(key)), testValue) + const valueToCheck = + type === LogicalOperator.AND || type === LogicalOperator.OR + ? doc + : deepGet(doc, removeKeyNumbering(key)) + const result = test(valueToCheck, testValue) if (query.allOr && result) { return true } else if (!query.allOr && !result) { return false } } - return true + return !query.allOr } const stringMatch = match( @@ -674,15 +678,34 @@ export const runQuery = (docs: Record[], query: SearchFilters) => { const and = match( LogicalOperator.AND, - (_docValue: Record, _testValue: any) => { - // TODO - return false + (docValue: Record, conditions: SearchFilters[]) => { + if (!conditions.length) { + return false + } + for (const condition of conditions) { + const matchesCondition = runQuery([docValue], condition) + if (!matchesCondition.length) { + return false + } + } + return true } ) const or = match( - LogicalOperator.AND, - (_docValue: Record, _testValue: any) => { - // TODO + LogicalOperator.OR, + (docValue: Record, conditions: SearchFilters[]) => { + if (!conditions.length) { + return false + } + for (const condition of conditions) { + const matchesCondition = runQuery([docValue], { + ...condition, + allOr: true, + }) + if (matchesCondition.length) { + return true + } + } return false } )