Skip to content

Commit

Permalink
refactor: Match Against 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
ymkim97 authored Jan 16, 2024
1 parent 1a6143f commit 339eb27
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
3 changes: 1 addition & 2 deletions infra/mysql/initdb.d/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/moabam/api/domain/room/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -56,11 +57,9 @@ public List<Room> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 339eb27

Please sign in to comment.