Skip to content

Commit

Permalink
refactor: es에서 사용하는 prefix 쿼리 분리 #138
Browse files Browse the repository at this point in the history
  • Loading branch information
koomchang committed Nov 20, 2024
1 parent 3148ea1 commit 2f84222
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
35 changes: 9 additions & 26 deletions backend/src/search/query/ElasticSearchQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
? [
Expand All @@ -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),
],
},
},
Expand Down
8 changes: 6 additions & 2 deletions backend/src/search/query/builder/ElasticSearchQueryBuilder.ts
Original file line number Diff line number Diff line change
@@ -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,
};
2 changes: 1 addition & 1 deletion backend/src/search/query/builder/LocationBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const LocationQueryBuilder = {
export const LocationQueryBuilders = {
GAUSS_LOCATION: (
lat: number,
long: number,
Expand Down
13 changes: 13 additions & 0 deletions backend/src/search/query/builder/PrefixBuilder.ts
Original file line number Diff line number Diff line change
@@ -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),
};

0 comments on commit 2f84222

Please sign in to comment.