From b32e3071ebeda68eb847bc7cc300a8722e798852 Mon Sep 17 00:00:00 2001 From: Michael Dill Date: Fri, 7 Jul 2023 13:57:05 -0500 Subject: [PATCH] feat(Where): Add group operator for constructing queries (#96) * add grouping mechanism --------- Authored-by: Michael Dill --- src/services/Where.ts | 2 ++ test/Where.spec.ts | 60 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/src/services/Where.ts b/src/services/Where.ts index 550a83b..7de8487 100644 --- a/src/services/Where.ts +++ b/src/services/Where.ts @@ -129,6 +129,8 @@ export class Where { queries.push(`(${Where.parseQueryValue(subkey, value[subkey])})`); } return `(${queries.join(' OR ')})`; + } else if (key.startsWith('group_')) { + queries.push(`(${Where.toQuerySyntax(value)})`); } else { queries.push(Where.parseQueryValue(key, value)); } diff --git a/test/Where.spec.ts b/test/Where.spec.ts index d2ab6a6..e8356a8 100644 --- a/test/Where.spec.ts +++ b/test/Where.spec.ts @@ -325,4 +325,64 @@ describe('Where', () => { expect(where).toEqual("owner.id:\"^(firstName>='Bob' AND firstName<'Boc' AND lastName>='Jones' AND lastName<'Jonet')\""); }); }); + + describe('with group queries', () => { + it('should create a valid lucene query for A AND (B OR C) AND (D OR E)', () => { + const where = Where.toSearchSyntax({ + id: 103, + group_1: { + startDate: { or: {max: '2022-01-01', isNull: true}}, + }, + group_2: { + endDate: { or: { min: '2026-12-31', isNull: true }}, + } + }); + expect(where).toEqual('id:103 AND (startDate: IS NULL OR startDate < \'2022-01-01\') AND (endDate: IS NULL OR endDate >= \'2026-12-31\')'); + }); + it('should create a valid lucene query with groups for A AND (B OR C) AND (D.id OR E.id)', () => { + const where = Where.toSearchSyntax({ + id: 103, + group_1: { + or: { + firstName: 'test', + lastName: 'test' + } + }, + group_2: { + or: { + owner: { + id: 103 + }, + secondaryOwners: { + id: 103 + } + } + } + }); + expect(where).toEqual('id:103 AND ((firstName:\"test\" OR lastName:\"test\")) AND ((owner.id:103 OR secondaryOwners.id:103))'); + }); + it('should create a valid lucene query with groups for (A AND (B OR C)) AND (D AND (E OR F))', () => { + const where = Where.toSearchSyntax({ + group_1: { + id: 103, + or: { + firstName: 'test', + lastName: 'test' + } + }, + group_2: { + id: 103, + or: { + owner: { + id: 103 + }, + secondaryOwners: { + id: 103 + } + } + } + }); + expect(where).toEqual('(id:103 AND (firstName:\"test\" OR lastName:\"test\")) AND (id:103 AND (owner.id:103 OR secondaryOwners.id:103))'); + }); + }); });