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:
@ -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(
|
||||
|
||||
@ -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}:*`;
|
||||
});
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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}, '')`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user