From 339eb27906dc36b5356b656ae6b3ab202b645095 Mon Sep 17 00:00:00 2001 From: Youngmyung Kim <83266154+ymkim97@users.noreply.github.com> Date: Tue, 16 Jan 2024 16:55:14 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20Match=20Against=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- infra/mysql/initdb.d/init.sql | 3 +-- src/main/java/com/moabam/api/domain/room/Room.java | 5 ++--- .../domain/room/repository/RoomSearchRepository.java | 11 +++++------ .../moabam/global/config/SqlFunctionContributor.java | 7 ++++++- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/infra/mysql/initdb.d/init.sql b/infra/mysql/initdb.d/init.sql index 455f5c60..0e60a142 100644 --- a/infra/mysql/initdb.d/init.sql +++ b/infra/mysql/initdb.d/init.sql @@ -216,8 +216,7 @@ create table room created_at datetime(6) not null, updated_at datetime(6), primary key (id), - FULLTEXT INDEX full_index_title (title) WITH PARSER ngram, - FULLTEXT INDEX full_index_manager_nickname (manager_nickname) WITH PARSER ngram + FULLTEXT INDEX full_index_room (title, manager_nickname) WITH PARSER ngram ); create table routine diff --git a/src/main/java/com/moabam/api/domain/room/Room.java b/src/main/java/com/moabam/api/domain/room/Room.java index d27135a8..4868448c 100644 --- a/src/main/java/com/moabam/api/domain/room/Room.java +++ b/src/main/java/com/moabam/api/domain/room/Room.java @@ -55,8 +55,7 @@ public class Room extends BaseTimeEntity { @Column(name = "id") private Long id; - @Column(name = "title", - columnDefinition = "VARCHAR(20) NOT NULL, FULLTEXT INDEX full_title (title) WITH PARSER ngram") + @Column(name = "title", length = 20) private String title; @Column(name = "password", length = 8) @@ -91,7 +90,7 @@ public class Room extends BaseTimeEntity { private String roomImage; @Column(name = "manager_nickname", - columnDefinition = "VARCHAR(30), FULLTEXT INDEX full_nickname (manager_nickname) WITH PARSER ngram") + columnDefinition = "VARCHAR(30), FULLTEXT INDEX full_text_room (title, manager_nickname) WITH PARSER ngram") private String managerNickname; @Column(name = "deleted_at") diff --git a/src/main/java/com/moabam/api/domain/room/repository/RoomSearchRepository.java b/src/main/java/com/moabam/api/domain/room/repository/RoomSearchRepository.java index 04f7893c..43d9fa7a 100644 --- a/src/main/java/com/moabam/api/domain/room/repository/RoomSearchRepository.java +++ b/src/main/java/com/moabam/api/domain/room/repository/RoomSearchRepository.java @@ -22,7 +22,8 @@ public class RoomSearchRepository { private static final double MATCH_THRESHOLD = 0.0; - private static final String MATCH_AGAINST_TEMPLATE = "function('match_against', {0}, {1})"; + private static final String MATCH_AGAINST_TEMPLATE_ONE = "function('match_against', {0}, {1})"; + private static final String MATCH_AGAINST_TEMPLATE_TWO = "function('match_against_two', {0}, {1}, {2})"; private final JPAQueryFactory jpaQueryFactory; @@ -56,11 +57,9 @@ public List searchWithKeyword(String keyword, RoomType roomType, Long room private BooleanExpression matchAgainst(String keyword) { keyword = "\"" + keyword + "\""; - return Expressions.numberTemplate(Double.class, MATCH_AGAINST_TEMPLATE, room.title, keyword) - .gt(MATCH_THRESHOLD) - .or(Expressions.numberTemplate(Double.class, MATCH_AGAINST_TEMPLATE, room.managerNickname, keyword) - .gt(MATCH_THRESHOLD)) - .or(Expressions.numberTemplate(Double.class, MATCH_AGAINST_TEMPLATE, routine.content, keyword) + return Expressions.numberTemplate(Double.class, MATCH_AGAINST_TEMPLATE_TWO, room.title, room.managerNickname, + keyword).gt(MATCH_THRESHOLD) + .or(Expressions.numberTemplate(Double.class, MATCH_AGAINST_TEMPLATE_ONE, routine.content, keyword) .gt(MATCH_THRESHOLD)); } } diff --git a/src/main/java/com/moabam/global/config/SqlFunctionContributor.java b/src/main/java/com/moabam/global/config/SqlFunctionContributor.java index 57614080..297fcc16 100644 --- a/src/main/java/com/moabam/global/config/SqlFunctionContributor.java +++ b/src/main/java/com/moabam/global/config/SqlFunctionContributor.java @@ -11,7 +11,12 @@ public class SqlFunctionContributor implements FunctionContributor { public void contributeFunctions(FunctionContributions functionContributions) { functionContributions.getFunctionRegistry() .registerPattern( - "match_against", "MATCH (?1) AGAINST (?2 IN BOOLEAN MODE)", + "match_against", "MATCH (?1) AGAINST (?2 IN NATURAL LANGUAGE MODE)", + functionContributions.getTypeConfiguration().getBasicTypeRegistry().resolve(DOUBLE)); + + functionContributions.getFunctionRegistry() + .registerPattern( + "match_against_two", "MATCH (?1, ?2) AGAINST (?3 IN NATURAL LANGUAGE MODE)", functionContributions.getTypeConfiguration().getBasicTypeRegistry().resolve(DOUBLE)); } }