feat(176): rule model + Lucene-subset compiler

This commit is contained in:
2026-07-18 02:41:50 +02:00
parent ae22107d98
commit fd786da649
3 changed files with 130 additions and 0 deletions
+46
View File
@@ -0,0 +1,46 @@
import { describe, expect, it } from 'vitest';
import { compile } from './compile';
import type { Group } from './types';
describe('compile', () => {
it('quotes text is', () => {
const g: Group = { match: 'all', children: [{ field: 'genre', operator: 'is', value: 'Horror' }] };
expect(compile(g)).toBe('genre:"Horror"');
});
it('emits NOT for isNot', () => {
const g: Group = { match: 'all', children: [{ field: 'genre', operator: 'isNot', value: 'Horror' }] };
expect(compile(g)).toBe('NOT genre:"Horror"');
});
it('wildcards contains and startsWith', () => {
expect(compile({ match: 'all', children: [{ field: 'title', operator: 'contains', value: 'night' }] })).toBe('title:*night*');
expect(compile({ match: 'all', children: [{ field: 'title', operator: 'startsWith', value: 'The' }] })).toBe('title:The*');
});
it('emits numeric ranges', () => {
expect(compile({ match: 'all', children: [{ field: 'minutes', operator: 'gt', value: '30' }] })).toBe('minutes:{30 TO *}');
expect(compile({ match: 'all', children: [{ field: 'minutes', operator: 'lt', value: '90' }] })).toBe('minutes:{* TO 90}');
expect(compile({ match: 'all', children: [{ field: 'minutes', operator: 'between', value: '30', value2: '90' }] })).toBe('minutes:[30 TO 90]');
expect(compile({ match: 'all', children: [{ field: 'minutes', operator: 'eq', value: '42' }] })).toBe('minutes:42');
});
it('emits date ranges', () => {
expect(compile({ match: 'all', children: [{ field: 'release_date', operator: 'after', value: '2000-01-01' }] })).toBe('release_date:{2000-01-01 TO *}');
expect(compile({ match: 'all', children: [{ field: 'release_date', operator: 'before', value: '2010-01-01' }] })).toBe('release_date:{* TO 2010-01-01}');
});
it('joins by AND / OR and parenthesizes nested groups', () => {
const g: Group = {
match: 'all',
children: [
{ field: 'type', operator: 'is', value: 'movie' },
{ match: 'any', children: [
{ field: 'genre', operator: 'is', value: 'Horror' },
{ field: 'genre', operator: 'is', value: 'Thriller' }
] }
]
};
expect(compile(g)).toBe('type:"movie" AND (genre:"Horror" OR genre:"Thriller")');
});
});
+52
View File
@@ -0,0 +1,52 @@
import { isGroup, type Group, type Rule } from './types';
// The compiler is the sole author of quoting. Quoted phrases escape only " and \.
function quote(value: string): string {
return `"${value.replace(/(["\\])/g, '\\$1')}"`;
}
// Wildcard/prefix values escape Lucene specials but keep the wildcard the operator adds.
function escapeWild(value: string): string {
return value.replace(/([+\-!(){}[\]^"~:\\/])/g, '\\$1');
}
function compileRule(rule: Rule): string {
const f = rule.field;
const v = rule.value;
switch (rule.operator) {
case 'is':
return `${f}:${quote(v)}`;
case 'isNot':
return `NOT ${f}:${quote(v)}`;
case 'contains':
return `${f}:*${escapeWild(v)}*`;
case 'startsWith':
return `${f}:${escapeWild(v)}*`;
case 'notContains':
return `NOT ${f}:${quote(v)}`;
case 'eq':
return `${f}:${v}`;
case 'gt':
case 'after':
return `${f}:{${v} TO *}`;
case 'lt':
case 'before':
return `${f}:{* TO ${v}}`;
case 'between':
return `${f}:[${v} TO ${rule.value2 ?? ''}]`;
default:
return '';
}
}
function compileGroup(group: Group): string {
const conn = group.match === 'all' ? ' AND ' : ' OR ';
return group.children
.map((child) => (isGroup(child) ? `(${compileGroup(child)})` : compileRule(child)))
.filter((s) => s.length > 0)
.join(conn);
}
export function compile(group: Group): string {
return compileGroup(group);
}
+32
View File
@@ -0,0 +1,32 @@
export type FieldType = 'text' | 'fulltext' | 'number' | 'date' | 'enum';
export type Match = 'all' | 'any';
export type Operator =
| 'is' | 'isNot' | 'contains' | 'startsWith' | 'notContains'
| 'eq' | 'gt' | 'lt' | 'between'
| 'before' | 'after';
export interface Rule {
field: string;
operator: Operator;
value: string;
value2?: string; // upper bound for `between`
}
export interface Group {
match: Match;
children: Array<Rule | Group>;
}
export function isGroup(node: Rule | Group): node is Group {
return (node as Group).children !== undefined;
}
// Which fields each catalog type exposes as operators (used by the UI and tests).
export const OPERATORS_BY_TYPE: Record<FieldType, Operator[]> = {
text: ['is', 'isNot', 'contains', 'startsWith'],
fulltext: ['contains', 'notContains'],
enum: ['is', 'isNot'],
number: ['eq', 'gt', 'lt', 'between'],
date: ['before', 'after', 'between']
};