Skip to content

Commit

Permalink
Merge pull request #1054 from MTES-MCT/improve-owner-search
Browse files Browse the repository at this point in the history
perf: use PGTRGM for owner search
  • Loading branch information
loicguillois authored Dec 24, 2024
2 parents 9a9bcab + 8163bc6 commit 8146f6b
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions server/src/repositories/ownerRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,27 +203,29 @@ const searchOwners = async (
perPage?: number
): Promise<PaginatedResultApi<OwnerApi>> => {
const filterQuery = db(ownerTable)
.whereRaw(
`upper(unaccent(full_name)) like '%' || upper(unaccent(?)) || '%'`,
q
)
.orWhereRaw(
`upper(unaccent(full_name)) like '%' || upper(unaccent(?)) || '%'`,
q?.split(' ').reverse().join(' ')
);

const filteredCount: number = await db(ownerTable)
.whereRaw(
`upper(unaccent(full_name)) like '%' || upper(unaccent(?)) || '%'`,
q
)
.orWhereRaw(
`upper(unaccent(full_name)) like '%' || upper(unaccent(?)) || '%'`,
q?.split(' ').reverse().join(' ')
)
.count('id')
.first()
.then((_) => Number(_?.count));
.select('*')
.whereRaw(
`immutable_unaccent(full_name) ILIKE immutable_unaccent(?)`,
[`%${q}%`]
)
.orWhereRaw(
`immutable_unaccent(full_name) ILIKE immutable_unaccent(?)`,
[`%${q.split(' ').reverse().join(' ')}%`]
)
.orderBy('id', 'desc');

const filteredCount = await db(ownerTable)
.whereRaw(
`immutable_unaccent(full_name) ILIKE immutable_unaccent(?)`,
[`%${q}%`]
)
.orWhereRaw(
`immutable_unaccent(full_name) ILIKE immutable_unaccent(?)`,
[`%${q.split(' ').reverse().join(' ')}%`]
)
.count('id')
.first()
.then((row) => Number(row?.count));

const totalCount = await db(ownerTable)
.count('id')
Expand Down

0 comments on commit 8146f6b

Please sign in to comment.