feat: Add TS vector field filters support (#12376)

# Implementation Details
- Added support for 5 operators: `contains`, `containsAny`,
`containsAll`, `matches`, and `fuzzy`
- Works on any field of type `TS_VECTOR`
- Added PostgreSQL `pg_trgm` extension for fuzzy search functionality.
The extension provides the `similarity()` function needed for text
similarity searches.
- Not implemented in GraphQL

## Tradeoffs & Decisions
1. **Fuzzy Search Performance**: Using `pg_trgm` for fuzzy search is
more accurate but slower than simple text matching. We might want to add
a similarity threshold parameter in the future to control the tradeoff
between accuracy and performance.

2. **Operator Naming**: Chose `contains`/`containsAny`/`containsAll` to
be consistent with existing filter operators, though they might be less
intuitive than `search`/`searchAny`/`searchAll`.

## Demo


https://github.com/user-attachments/assets/790fc3ed-a188-4b49-864f-996a37481d99

---------

Co-authored-by: Félix Malfait <felix.malfait@gmail.com>
Co-authored-by: Félix Malfait <felix@twenty.com>
This commit is contained in:
Abdul Rahman
2025-05-30 19:24:50 +05:30
committed by GitHub
parent b7473371b3
commit c7139cbc84
13 changed files with 76 additions and 28 deletions
@@ -10,13 +10,18 @@ type WhereConditionParts = {
params: ObjectLiteral;
};
export const computeWhereConditionParts = (
operator: string,
objectNameSingular: string,
key: string,
export const computeWhereConditionParts = ({
operator,
objectNameSingular,
key,
value,
}: {
operator: string;
objectNameSingular: string;
key: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value: any,
): WhereConditionParts => {
value: any;
}): WhereConditionParts => {
const uuid = Math.random().toString(36).slice(2, 7);
switch (operator) {
@@ -90,6 +95,23 @@ export const computeWhereConditionParts = (
sql: `"${objectNameSingular}"."${key}" @> ARRAY[:...${key}${uuid}]`,
params: { [`${key}${uuid}`]: value },
};
case 'search': {
const tsQuery = value
.split(/\s+/)
.map((term: string) => `${term}:*`)
.join(' & ');
return {
sql: `(
"${objectNameSingular}"."${key}" @@ to_tsquery('simple', :${key}${uuid}Ts) OR
"${objectNameSingular}"."${key}"::text ILIKE :${key}${uuid}Like
)`,
params: {
[`${key}${uuid}Ts`]: tsQuery,
[`${key}${uuid}Like`]: `%${value}%`,
},
};
}
case 'notContains':
return {
sql: `NOT ("${objectNameSingular}"."${key}"::text[] && ARRAY[:...${key}${uuid}]::text[])`,
@@ -105,7 +127,6 @@ export const computeWhereConditionParts = (
sql: `EXISTS (SELECT 1 FROM unnest("${objectNameSingular}"."${key}") AS elem WHERE elem ILIKE :${key}${uuid})`,
params: { [`${key}${uuid}`]: value },
};
default:
throw new GraphqlQueryRunnerException(
`Operator "${operator}" is not supported`,