Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.annotations.InnerField;
import org.springframework.data.elasticsearch.annotations.MultiField;
import org.springframework.data.elasticsearch.annotations.Setting;

import com.mopl.moplcore.domain.content.entity.Type;
Expand Down Expand Up @@ -39,7 +41,10 @@ public class ContentDocument {
@Field(type = FieldType.Keyword)
private Type type;

@Field(type = FieldType.Text, analyzer = "nori")
@MultiField(
mainField = @Field(type = FieldType.Text, analyzer = "nori"),
otherFields = @InnerField(suffix = "raw", type = FieldType.Text, analyzer = "standard")
)
private String title;

@Field(type = FieldType.Text, analyzer = "nori")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import com.mopl.moplcore.domain.content.dto.ContentSearchRequest;
import com.mopl.moplcore.domain.content.dto.CursorResponseContentDto;
import com.mopl.moplcore.domain.content.entity.Content;
import com.mopl.moplcore.domain.content.entity.Type;
import com.mopl.moplcore.domain.content.exception.ContentNotFoundException;
import com.mopl.moplcore.domain.content.repository.ContentRepository;
import com.mopl.moplcore.domain.content.repository.ContentTagRepository;
Expand Down Expand Up @@ -139,18 +138,25 @@ private BoolQuery.Builder buildBasicFilters(ContentSearchRequest request) {
BoolQuery.Builder boolQuery = new BoolQuery.Builder();

if (request.getKeywordLike() != null && !request.getKeywordLike().trim().isEmpty()) {
String rawKeyword = request.getKeywordLike().trim();
String keyword = request.getKeywordLike().trim() + " ";
boolQuery.must(m -> m.bool(b -> b
// 1. nori 구문 일치 (완전 일치 우선)
.should(s -> s.matchPhrase(mp -> mp.field("title").query(keyword).boost(100.0f)))
.should(s -> s.matchPhrase(mp -> mp.field("description").query(keyword).boost(2.0f)))
// 2. nori 접두사 일치
.should(s -> s.matchPhrasePrefix(mp -> mp.field("title").query(keyword).boost(50.0f)))
.should(s -> s.matchPhrasePrefix(mp -> mp.field("description").query(keyword).boost(1.5f)))
// 3. nori 토큰 일치
.should(s -> s.match(mt -> mt.field("title").query(keyword).boost(3.0f)))
.should(s -> s.match(mt -> mt.field("description").query(keyword).boost(1.0f)))
.minimumShouldMatch("1")));
String[] words = rawKeyword.split("\\s+");

boolQuery.must(m -> m.bool(b -> {
b.should(s -> s.match(mt -> mt.field("title.raw").query(rawKeyword).boost(200.0f)));

float[] boost = {300.0f};
for (String word : words) {
String wordWithSpace = word + " ";
float currentBoost = boost[0];
b.should(s -> s.match(mt -> mt.field("title").query(wordWithSpace).boost(currentBoost)));
boost[0] *= 0.5f;
}

b.should(s -> s.match(mt -> mt.field("description").query(keyword).boost(1.0f)));

return b.minimumShouldMatch("1");
}));
}

if (request.getTypeEqual() != null) {
Expand Down Expand Up @@ -248,7 +254,6 @@ private String generateNextCursor(List<ContentDocument> docs, boolean hasNext, C
return CursorResponseContentDto.encodeCursor(cursor);
}


public ContentDto getContent(UUID id) {
Content content = contentRepository.findById(id).orElseThrow(() -> new ContentNotFoundException(id));
return toDto(content);
Expand Down