Skip to content

Commit

Permalink
Merge pull request #141 from woowa-techcamp-2024/refactor/140-elastic…
Browse files Browse the repository at this point in the history
…search-source-filter

[refactor] Elasticsearch에서 검색할 때 restaurantId 필드만 조회하도록 변경
  • Loading branch information
hellomatia authored Aug 28, 2024
2 parents 6579db4 + a5c1830 commit e0918f8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package woowa.team4.bff.common.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand All @@ -17,6 +18,7 @@
/**
* 전역 예외 처리를 위한 클래스이다. 이 클래스는 애플리케이션에서 발생하는 다양한 예외를 잡아 적절한 HTTP 응답으로 변환한다.
*/
@Slf4j
@ControllerAdvice
public class GeneralExceptionHandler {

Expand Down Expand Up @@ -65,6 +67,7 @@ public ResponseEntity<?> handleNotFoundException(Exception e) {
@ExceptionHandler({IllegalArgumentException.class, IllegalStateException.class,
MethodArgumentNotValidException.class})
public ResponseEntity<?> handleBadRequestException(Exception e) {
log.error("잘못된 요청", e);
if (e instanceof MethodArgumentNotValidException) {
return newResponse(
((MethodArgumentNotValidException) e).getBindingResult().getAllErrors().get(0)
Expand Down Expand Up @@ -115,6 +118,7 @@ public ResponseEntity<?> handleHttpMessageNotReadableException(Exception e) {
*/
@ExceptionHandler({Exception.class, RuntimeException.class})
public ResponseEntity<?> handleException(Exception e) {
log.error("서버 오류", e);
return newResponse(e, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import woowa.team4.bff.search.document.RestaurantMenusDocument;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -67,7 +68,12 @@ private Map<String, RestaurantMenusDocument> readRestaurants(String filename) th
Map<String, RestaurantMenusDocument> restaurantMap = new HashMap<>();
CSVParser parser = new CSVParserBuilder().withSeparator(',').withQuoteChar('"').build();

try (CSVReader reader = new CSVReaderBuilder(new InputStreamReader(getClass().getResourceAsStream("/" + filename)))
InputStream resourceStream = getClass().getResourceAsStream("/" + filename);
if (resourceStream == null) {
log.info("{} 파일이 없습니다. 건너뛸게요!", filename);
return restaurantMap;
}
try (CSVReader reader = new CSVReaderBuilder(new InputStreamReader(resourceStream))
.withCSVParser(parser)
.build()) {
String[] line;
Expand All @@ -89,7 +95,12 @@ private Map<String, RestaurantMenusDocument> readRestaurants(String filename) th
private void readMenus(String filename, Map<String, RestaurantMenusDocument> restaurantMap) throws IOException, CsvValidationException {
CSVParser parser = new CSVParserBuilder().withSeparator(',').withQuoteChar('"').build();

try (CSVReader reader = new CSVReaderBuilder(new InputStreamReader(getClass().getResourceAsStream("/" + filename)))
InputStream resourceStream = getClass().getResourceAsStream("/" + filename);
if (resourceStream == null) {
log.info("{} 파일이 없습니다. 건너뛸게요!", filename);
return;
}
try (CSVReader reader = new CSVReaderBuilder(new InputStreamReader(resourceStream))
.withCSVParser(parser)
.build()) {
String[] line;
Expand Down Expand Up @@ -120,6 +131,7 @@ private void bulkSaveToElasticsearch(Iterable<RestaurantMenusDocument> documents
if (count % BATCH_SIZE == 0) {
elasticsearchOperations.bulkIndex(queries, elasticsearchOperations.getIndexCoordinatesFor(RestaurantMenusDocument.class));
queries.clear();
log.info("Bulk indexing {} documents", count);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.data.elasticsearch.client.elc.NativeQuery;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.FetchSourceFilter;
import org.springframework.stereotype.Service;
import woowa.team4.bff.interfaces.SearchService;
import woowa.team4.bff.search.document.RestaurantMenusDocument;
Expand Down Expand Up @@ -39,6 +40,7 @@ private NativeQuery buildNativeQuery(String keyword, String deliveryLocation) {
.query(nestedQuery -> nestedQuery.match(match -> match.field("menus.menuName").query(keyword))))
)))
.withSort(sort -> sort.score(score -> score.order(SortOrder.Desc)))
.withSourceFilter(FetchSourceFilter.of(new String[]{"restaurantId"}, null))
.withPageable(Pageable.ofSize(DEFAULT_MAX_SIZE))
.build();
}
Expand Down

0 comments on commit e0918f8

Please sign in to comment.