Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Match Against 변경 #258

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}