fixes on search (#11955)

In this PR
- enable search by email
- search with ' ' (spaces) string and special characters do not throw
entry error

closes https://github.com/twentyhq/twenty/issues/11447 &
https://github.com/twentyhq/core-team-issues/issues/860
This commit is contained in:
Etienne
2025-05-12 10:59:10 +02:00
committed by GitHub
parent 650f8f5963
commit ca6e979ead
8 changed files with 104 additions and 22 deletions

View File

@ -1,5 +1,6 @@
import { Injectable } from '@nestjs/common';
import { isNonEmptyString } from '@sniptt/guards';
import { FieldMetadataType } from 'twenty-shared/types';
import { getLogoUrlFromDomainName } from 'twenty-shared/utils';
import { Brackets, ObjectLiteral } from 'typeorm';
@ -96,7 +97,7 @@ export class SearchService {
...(imageIdentifierField ? [imageIdentifierField] : []),
].map((field) => `"${field}"`);
const searchQuery = searchTerms
const searchQuery = isNonEmptyString(searchTerms)
? queryBuilder
.select(fieldsToSelect)
.addSelect(

View File

@ -2,12 +2,12 @@ export const formatSearchTerms = (
searchTerm: string,
operator: 'and' | 'or' = 'and',
) => {
if (searchTerm === '') {
if (searchTerm.trim() === '') {
return '';
}
const words = searchTerm.trim().split(/\s+/);
const formattedWords = words.map((word) => {
const escapedWord = word.replace(/[\\:'&|!()]/g, '\\$&');
const escapedWord = word.replace(/[\\:'&|!()@<>]/g, '\\$&');
return `${escapedWord}:*`;
});

View File

@ -30,15 +30,8 @@ describe('getTsVectorColumnExpressionFromFields', () => {
const result = getTsVectorColumnExpressionFromFields(fields);
const expected = `
to_tsvector('simple', COALESCE("nameFirstName", '') || ' ' || COALESCE("nameLastName", '') || ' ' || COALESCE("jobTitle", '') || ' ' ||
COALESCE(
replace(
"emailsPrimaryEmail",
'@',
' '
),
''
)
)
COALESCE("emailsPrimaryEmail", '') || ' ' ||
COALESCE(SPLIT_PART("emailsPrimaryEmail", '@', 2), ''))
`.trim();
expect(result.trim()).toBe(expected);

View File

@ -82,15 +82,9 @@ const getColumnExpression = (
switch (fieldType) {
case FieldMetadataType.EMAILS:
return `
COALESCE(
replace(
${quotedColumnName},
'@',
' '
),
''
)
`;
COALESCE(${quotedColumnName}, '') || ' ' ||
COALESCE(SPLIT_PART(${quotedColumnName}, '@', 2), '')`;
default:
return `COALESCE(${quotedColumnName}, '')`;
}