Skip to content

Commit

Permalink
feat(Where): Add group operator for constructing queries (#96)
Browse files Browse the repository at this point in the history
* add grouping mechanism

---------

Authored-by: Michael Dill <[email protected]>
  • Loading branch information
MichaelDill authored Jul 7, 2023
1 parent 61225c9 commit b32e307
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/services/Where.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
60 changes: 60 additions & 0 deletions test/Where.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))');
});
});
});

0 comments on commit b32e307

Please sign in to comment.