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

View File

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