From 9303e39bcff4deca6b3e95d9067650a6d2a8a4fd Mon Sep 17 00:00:00 2001 From: Marie <51697796+ijreilly@users.noreply.github.com> Date: Fri, 25 Oct 2024 16:17:19 +0200 Subject: [PATCH] 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. --- .../graphql-query-search-resolver.service.ts | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/packages/twenty-server/src/engine/api/graphql/graphql-query-runner/resolvers/graphql-query-search-resolver.service.ts b/packages/twenty-server/src/engine/api/graphql/graphql-query-runner/resolvers/graphql-query-search-resolver.service.ts index ac77cb9d7..b7d2c4dbd 100644 --- a/packages/twenty-server/src/engine/api/graphql/graphql-query-runner/resolvers/graphql-query-search-resolver.service.ts +++ b/packages/twenty-server/src/engine/api/graphql/graphql-query-runner/resolvers/graphql-query-search-resolver.service.ts @@ -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({