Fix broken filter in search resolver (#8064)

The recent addition of a "orWhere" condition to[ improve the search algo
quality](https://github.com/twentyhq/twenty/pull/7955) accidentally
broke the filter, being considered an independent "or" wondition while
we still want the filter to apply.
This commit is contained in:
Marie 2024-10-25 16:17:19 +02:00 committed by GitHub
parent 2e73d020a3
commit 9303e39bcf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,5 +1,8 @@
import { Injectable } from '@nestjs/common';
import graphqlFields from 'graphql-fields';
import { Brackets } from 'typeorm';
import { ResolverService } from 'src/engine/api/graphql/graphql-query-runner/interfaces/resolver-service.interface';
import {
Record as IRecord,
@ -37,6 +40,7 @@ export class GraphqlQuerySearchResolverService
objectMetadataItem,
objectMetadataMapItem,
objectMetadataMap,
info,
} = options;
const repository =
@ -80,16 +84,19 @@ export class GraphqlQuerySearchResolverService
const resultsWithTsVector = (await queryBuilderWithFilter
.andWhere(
searchTerms === ''
? `"${SEARCH_VECTOR_FIELD.name}" IS NOT NULL`
: `"${SEARCH_VECTOR_FIELD.name}" @@ to_tsquery(:searchTerms)`,
searchTerms === '' ? {} : { searchTerms },
)
.orWhere(
searchTermsOr === ''
? `"${SEARCH_VECTOR_FIELD.name}" IS NOT NULL`
: `"${SEARCH_VECTOR_FIELD.name}" @@ to_tsquery(:searchTermsOr)`,
searchTermsOr === '' ? {} : { searchTermsOr },
new Brackets((qb) => {
qb.where(
searchTerms === ''
? `"${SEARCH_VECTOR_FIELD.name}" IS NOT NULL`
: `"${SEARCH_VECTOR_FIELD.name}" @@ to_tsquery(:searchTerms)`,
searchTerms === '' ? {} : { searchTerms },
).orWhere(
searchTermsOr === ''
? `"${SEARCH_VECTOR_FIELD.name}" IS NOT NULL`
: `"${SEARCH_VECTOR_FIELD.name}" @@ to_tsquery(:searchTermsOr)`,
searchTermsOr === '' ? {} : { searchTermsOr },
);
}),
)
.orderBy(
`ts_rank_cd("${SEARCH_VECTOR_FIELD.name}", to_tsquery(:searchTerms))`,
@ -106,7 +113,11 @@ export class GraphqlQuerySearchResolverService
const objectRecords = await repository.formatResult(resultsWithTsVector);
const totalCount = await repository.count();
const selectedFields = graphqlFields(info);
const totalCount = isDefined(selectedFields.totalCount)
? await queryBuilderWithFilter.getCount()
: 0;
const order = undefined;
return typeORMObjectRecordsParser.createConnection({