diff --git a/backend/src/search/query/ElasticSearchQuery.ts b/backend/src/search/query/ElasticSearchQuery.ts index 655d195c..94191c20 100644 --- a/backend/src/search/query/ElasticSearchQuery.ts +++ b/backend/src/search/query/ElasticSearchQuery.ts @@ -43,20 +43,15 @@ export class ElasticSearchQuery { score_mode: 'sum', functions: [ // 완전 일치에 가중치 조정 - { - filter: { term: { 'name.keyword': query } }, - weight: 20, - }, + ElasticSearchQueryBuilder.FILTER_TERM_NAME_KEYWORD(query), // name의 토큰 매칭 가중치 - ...tokens.map((token) => ({ - filter: { match: { name: token } }, - weight: 15, - })), + ...tokens.map((token) => + ElasticSearchQueryBuilder.FILTER_MATCH_NAME(token), + ), // formattedAddress의 토큰 매칭 가중치 - ...tokens.map((token) => ({ - filter: { match: { formattedAddress: token } }, - weight: 10, - })), + ...tokens.map((token) => + ElasticSearchQueryBuilder.FILTER_MATCH_FORMATTED_ADDRESS(token), + ), // 위치 정보 가중치 ...(location ? [ @@ -78,20 +73,8 @@ export class ElasticSearchQuery { query: { bool: { should: [ - { - prefix: { - name: { - value: query, - }, - }, - }, - { - prefix: { - formattedAddress: { - value: query, - }, - }, - }, + ElasticSearchQueryBuilder.PREFIX_NAME(query), + ElasticSearchQueryBuilder.PREFIX_FORMATTED_ADDRESS(query), ], }, }, diff --git a/backend/src/search/query/builder/ElasticSearchQueryBuilder.ts b/backend/src/search/query/builder/ElasticSearchQueryBuilder.ts index 88e63be0..bff42736 100644 --- a/backend/src/search/query/builder/ElasticSearchQueryBuilder.ts +++ b/backend/src/search/query/builder/ElasticSearchQueryBuilder.ts @@ -1,7 +1,11 @@ import { MatchBuilders } from '@src/search/query/builder/MatchBuilder'; -import { LocationQueryBuilder } from '@src/search/query/builder/LocationBuilder'; +import { LocationQueryBuilders } from '@src/search/query/builder/LocationBuilder'; +import { PrefixBuilders } from '@src/search/query/builder/PrefixBuilder'; +import { FilterBuilders } from '@src/search/query/builder/FilterQueryBuilder'; export const ElasticSearchQueryBuilder = { ...MatchBuilders, - ...LocationQueryBuilder, + ...LocationQueryBuilders, + ...PrefixBuilders, + ...FilterBuilders, }; diff --git a/backend/src/search/query/builder/LocationBuilder.ts b/backend/src/search/query/builder/LocationBuilder.ts index 916bddf5..cdb77ef3 100644 --- a/backend/src/search/query/builder/LocationBuilder.ts +++ b/backend/src/search/query/builder/LocationBuilder.ts @@ -1,4 +1,4 @@ -export const LocationQueryBuilder = { +export const LocationQueryBuilders = { GAUSS_LOCATION: ( lat: number, long: number, diff --git a/backend/src/search/query/builder/PrefixBuilder.ts b/backend/src/search/query/builder/PrefixBuilder.ts new file mode 100644 index 00000000..18ccc1fa --- /dev/null +++ b/backend/src/search/query/builder/PrefixBuilder.ts @@ -0,0 +1,13 @@ +const createPrefixQuery = (field: string, value: string) => ({ + prefix: { + [field]: { + value, + }, + }, +}); + +export const PrefixBuilders = { + PREFIX_NAME: (query: string) => createPrefixQuery('name', query), + PREFIX_FORMATTED_ADDRESS: (query: string) => + createPrefixQuery('formattedAddress', query), +};