Skip to content

Commit

Permalink
[feat] #220 검색 쿼리 nori, ngram 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
NaMinhyeok committed Feb 14, 2024
1 parent 5a72119 commit 5deb803
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/main/java/com/dmarket/service/ElasticsearchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.FieldSort;
import co.elastic.clients.elasticsearch._types.FieldValue;
import co.elastic.clients.elasticsearch._types.SortOrder;
import co.elastic.clients.elasticsearch._types.query_dsl.MatchQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.MultiMatchQuery;
Expand All @@ -22,6 +23,8 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.elasticsearch.annotations.Setting;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -84,20 +87,35 @@ public SearchResponse<ProductDocument> getElasticSearchProducts(int pageNo, Stri
.gte(JsonData.of(minPrice))
.lte(JsonData.of(maxPrice))
)._toQuery();
// 검색어 필터링
// 검색어 필터링 기본
List<String> fields = Arrays.asList("product_name", "product_brand", "product_description");
Query byName = MultiMatchQuery.of(m -> m
.fields(fields)
.query(query)
)._toQuery();
// 검색어 필터링 nori
List<String> norifields = Arrays.asList("product_name.nori", "product_brand.nori", "product_description.nori");
Query byNoriname = MultiMatchQuery.of(m -> m
.fields(norifields)
.query(query)
)._toQuery();
// 검색어 필터링 ngram
List<String> ngramfields = Arrays.asList("product_name.ngram", "product_brand.ngram", "product_description.ngram");
Query byNgramName = MultiMatchQuery.of(m -> m
.fields(ngramfields)
.query(query)
)._toQuery();
// 검색
SearchResponse<ProductDocument> response = client.search(s -> s
.index("product-ngram")
.index("new-product")
.from(pageNo * pageSize)
.size(pageSize)
.query(q -> q
.bool(b -> b
.must(byName)
.should(byName)
.should(byNoriname)
.should(byNgramName)
.minimumShouldMatch("1")
.must(byPrice)
.must(byRating)
)
Expand All @@ -108,7 +126,7 @@ public SearchResponse<ProductDocument> getElasticSearchProducts(int pageNo, Stri
.order(SortOrder.Desc)
)
)
,
,
ProductDocument.class
);
return response;
Expand Down

0 comments on commit 5deb803

Please sign in to comment.