From 8f5ca4936a51ead5916237d01224b87f1a8fd54b Mon Sep 17 00:00:00 2001 From: samithiwat Date: Tue, 31 Oct 2023 18:44:38 +0700 Subject: [PATCH] chore: update query --- apps/api/src/search/queries/course.ts | 56 +++++---------------------- 1 file changed, 10 insertions(+), 46 deletions(-) diff --git a/apps/api/src/search/queries/course.ts b/apps/api/src/search/queries/course.ts index 000b80b93..7428e58c1 100644 --- a/apps/api/src/search/queries/course.ts +++ b/apps/api/src/search/queries/course.ts @@ -4,6 +4,8 @@ import { ICourseSearchFilter } from '@api/course/course.resolver' export function buildCourseQuery(filter: ICourseSearchFilter): Record { // create the base query from values that guarantee is not undefined const boolMust: Record[] = [] + const boolMustNot: Record[] = [] + const boolFilter: Record[] = [] if (filter.keyword) { boolMust.push({ @@ -56,50 +58,14 @@ export function buildCourseQuery(filter: ICourseSearchFilter): Record 0 && !(containsSU && containsLetter)) { + if (containsSU) { + boolFilter.push({ + wildcard: { creditHours: { value: '*(S/U)*' } }, }) - } else if (containsSU) { - boolMust.push({ - nested: { - path: 'rawData', - query: { - nested: { - path: 'rawData.creditHours', - query: { - bool: { - must: [ - { - wildcard: { - 'rawData.creditHours': '*S/U*', - }, - }, - ], - }, - }, - }, - }, - }, + } else if (containsLetter) { + boolMustNot.push({ + wildcard: { creditHours: { value: '*(S/U)*' } }, }) } } @@ -155,9 +121,7 @@ export function buildCourseQuery(filter: ICourseSearchFilter): Record 0) { - result['bool'] = { must: boolMust } - } + result['bool'] = { must: boolMust, must_not: boolMustNot, filter: boolFilter } return result }