Skip to content

Commit

Permalink
feat: lucene between now has the option to include the upper limit.
Browse files Browse the repository at this point in the history
  • Loading branch information
robgeurden committed Nov 14, 2024
1 parent a48486e commit eb5dd63
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
41 changes: 30 additions & 11 deletions packages/easy-mongo/src/Lucene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ export type SearchDefinition = Record<
(
v: string | number,
q?: Record<string, string | number>
) => RequireAtLeastOne<{ should?: Clauses; filter?: Clauses; must?: Clauses; mustNot?: Clauses; sort?: Record<string, 1 | -1>; facet?: Facet }>
) => RequireAtLeastOne<{
should?: Clauses;
filter?: Clauses;
must?: Clauses;
mustNot?: Clauses;
sort?: Record<string, 1 | -1>;
facet?: Facet;
}>
>;

type Compound = {
Expand Down Expand Up @@ -65,7 +72,11 @@ export const lucene = {
must: must(query, def),
}).filter(([_, v]) => v.length > 0),
e => e.reduce((res, [k, v]) => on(res, r => (r[k] = lucene.clauses(v))), should(query, def).length > 0 ? { minimumShouldMatch: 1 } : ({} as any)),
() => ifTrue(wildcard, () => ({ should: lucene.clauses([{ wildcard: lucene.wildcard() }]), minimumShouldMatch: 0 }))
() =>
ifTrue(wildcard, () => ({
should: lucene.clauses([{ wildcard: lucene.wildcard() }]),
minimumShouldMatch: 0,
}))
),
search: (c: Partial<Compound>, index?: string) => ({
$search: {
Expand All @@ -78,7 +89,12 @@ export const lucene = {
.mapDefined(([k, v]) => options[k]?.(v, query)?.sort)
.first();
return {
$search: { ...ifDefined(index, { index }), compound: lucene.compound(query, options), ...ifDefined(sort, { sort }), count: { type: count } },
$search: {
...ifDefined(index, { index }),
compound: lucene.compound(query, options),
...ifDefined(sort, { sort }),
count: { type: count },
},
};
},
searchMeta: (query: Record<string, string | number>, def: SearchDefinition, count: 'total' | 'lowerBound' = 'total', index?: string) => ({
Expand Down Expand Up @@ -138,14 +154,17 @@ export const lucene = {
after: (date: unknown): Operator => lucene.gte(toMongoType(date)),
before: (date: unknown): Operator => lucene.lt(toMongoType(date)),
between:
(after: unknown, before: unknown): Operator =>
(path: string) => ({
range: {
path,
gte: toMongoType(after),
lt: toMongoType(before),
},
}),
(after: unknown, before: unknown, includeLimit?: boolean): Operator =>
(path: string) => {
const upperLimit = includeLimit ? { lte: toMongoType(before) } : { lt: toMongoType(before) };
return {
range: {
path,
gte: toMongoType(after),
...upperLimit,
},
};
},
facets: (def: SearchDefinition) =>
entries(def)
.filter(([k, v]) => isDefined(v(k)?.facet))
Expand Down
5 changes: 5 additions & 0 deletions packages/easy-mongo/test/Lucene.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ describe('Lucene', () => {
expect(d).toStrictEqual({ range: { path: 'size', gte: 41, lt: 52 } });
});

test('between with inclusion of upper limit', () => {
const d = between(41, 52, true)('size');
expect(d).toStrictEqual({ range: { path: 'size', gte: 41, lte: 52 } });
});

test('between with dates', () => {
const d = between(date, date2)('start');
expect(d).toStrictEqual({ range: { path: 'start', gte: new Date(date), lt: new Date(date2) } });
Expand Down

0 comments on commit eb5dd63

Please sign in to comment.