Skip to content

Commit

Permalink
Merge pull request #380 from daedongbread/345-feature-opensearch-도입-및…
Browse files Browse the repository at this point in the history
…-데이터-처리-프로그램-구현

feat: 인기검색어 조회 Client용 API 신규 생성
  • Loading branch information
chris910512 authored Dec 11, 2023
2 parents 9ad8d43 + c8a2b35 commit c99e0b5
Show file tree
Hide file tree
Showing 19 changed files with 170 additions and 210 deletions.
1 change: 1 addition & 0 deletions src/docs/asciidoc/search.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ operation::v1/search-engine/document/bread[snippets='http-request,request-parame

=== 빵집명으로 검색 테스트 API [GET]
operation::v1/search-engine/document/bakery[snippets='http-request,request-parameters,http-response,response-fields']

Original file line number Diff line number Diff line change
@@ -1,41 +1,6 @@
package com.depromeet.breadmapbackend.domain.admin.bakery;

import java.security.SecureRandom;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import com.depromeet.breadmapbackend.domain.search.dto.OpenSearchIndex;
import com.depromeet.breadmapbackend.domain.search.dto.keyword.BakeryLoadData;
import com.depromeet.breadmapbackend.domain.search.dto.keyword.BreadLoadData;
import com.depromeet.breadmapbackend.domain.search.events.OpenSearchEventPublisher;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.depromeet.breadmapbackend.domain.admin.bakery.dto.AdminBakeryAlarmBar;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.AdminBakeryDto;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.AdminBakeryIsNewDto;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.AdminImageBarDto;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.AdminImageDto;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.AdminImageRegisterRequest;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.AdminProductDto;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.AdminSimpleBakeryDto;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.BakeryAddDto;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.BakeryAddRequest;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.BakeryLocationDto;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.BakeryProductsDto;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.BakeryUpdateReportDto;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.BakeryUpdateRequest;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.NewReviewDto;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.ProductAddReportDto;
import com.depromeet.breadmapbackend.domain.admin.bakery.dto.*;
import com.depromeet.breadmapbackend.domain.admin.bakery.param.AdminBakeryFilter;
import com.depromeet.breadmapbackend.domain.admin.bakery.param.AdminBakeryImageType;
import com.depromeet.breadmapbackend.domain.bakery.Bakery;
Expand All @@ -48,19 +13,16 @@
import com.depromeet.breadmapbackend.domain.bakery.product.report.ProductAddReportImage;
import com.depromeet.breadmapbackend.domain.bakery.product.report.ProductAddReportImageRepository;
import com.depromeet.breadmapbackend.domain.bakery.product.report.ProductAddReportRepository;
import com.depromeet.breadmapbackend.domain.bakery.report.BakeryAddReport;
import com.depromeet.breadmapbackend.domain.bakery.report.BakeryAddReportRepository;
import com.depromeet.breadmapbackend.domain.bakery.report.BakeryReportImage;
import com.depromeet.breadmapbackend.domain.bakery.report.BakeryReportImageRepository;
import com.depromeet.breadmapbackend.domain.bakery.report.BakeryUpdateReport;
import com.depromeet.breadmapbackend.domain.bakery.report.BakeryUpdateReportRepository;
import com.depromeet.breadmapbackend.domain.bakery.report.*;
import com.depromeet.breadmapbackend.domain.notice.dto.NoticeEventDto;
import com.depromeet.breadmapbackend.domain.notice.factory.NoticeType;
import com.depromeet.breadmapbackend.domain.review.Review;
import com.depromeet.breadmapbackend.domain.review.ReviewImage;
import com.depromeet.breadmapbackend.domain.review.ReviewImageRepository;
import com.depromeet.breadmapbackend.domain.review.ReviewProductRatingRepository;
import com.depromeet.breadmapbackend.domain.review.ReviewRepository;
import com.depromeet.breadmapbackend.domain.review.*;
import com.depromeet.breadmapbackend.domain.search.dto.OpenSearchIndex;
import com.depromeet.breadmapbackend.domain.search.dto.keyword.BakeryLoadData;
import com.depromeet.breadmapbackend.domain.search.dto.keyword.BreadLoadData;
import com.depromeet.breadmapbackend.domain.search.events.BakeryCreationEvent;
import com.depromeet.breadmapbackend.domain.search.events.BreadCreationEvent;
import com.depromeet.breadmapbackend.domain.search.events.BakeryDeletionEvent;
import com.depromeet.breadmapbackend.domain.user.User;
import com.depromeet.breadmapbackend.global.S3Uploader;
import com.depromeet.breadmapbackend.global.dto.PageResponseDto;
Expand All @@ -73,9 +35,23 @@
import com.depromeet.breadmapbackend.global.infra.feign.sgis.dto.SgisTranscoordDto;
import com.depromeet.breadmapbackend.global.infra.properties.CustomAWSS3Properties;
import com.depromeet.breadmapbackend.global.infra.properties.CustomSGISKeyProperties;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.security.SecureRandom;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Service
Expand All @@ -95,7 +71,6 @@ public class AdminBakeryServiceImpl implements AdminBakeryService {
private final S3Uploader s3Uploader;
private final SgisClient sgisClient;
private final ApplicationEventPublisher eventPublisher;
private final OpenSearchEventPublisher openSearchEventPublisher;
private final CustomSGISKeyProperties customSGISKeyProperties;
private final CustomAWSS3Properties customAWSS3Properties;
private final UpdateBakerySQSService updateBakerySQSService; // TODO : migrate to AOP
Expand Down Expand Up @@ -242,7 +217,8 @@ public BakeryAddDto addBakery(BakeryAddRequest request) {
.build()
);

openSearchEventPublisher.publishSaveBakery(new BakeryLoadData(bakery.getId(), bakery.getName(), bakery.getAddress(), bakery.getLongitude(), bakery.getLatitude()));
BakeryCreationEvent publishSaveBakery = new BakeryCreationEvent(this, new BakeryLoadData(bakery.getId(), bakery.getName(), bakery.getAddress(), bakery.getLongitude(), bakery.getLatitude()));
eventPublisher.publishEvent(publishSaveBakery);
}

return BakeryAddDto.builder().bakeryId(bakery.getId()).build();
Expand All @@ -257,9 +233,12 @@ public void updateBakery(Long bakeryId, BakeryUpdateRequest request) {

BakeryStatus status = request.getStatus();
if(status == BakeryStatus.POSTING) {
openSearchEventPublisher.publishSaveBakery(new BakeryLoadData(bakery.getId(), bakery.getName(), bakery.getAddress(), bakery.getLongitude(), bakery.getLatitude()));
BakeryCreationEvent publishSaveBakery = new BakeryCreationEvent(this, new BakeryLoadData(bakery.getId(), bakery.getName(), bakery.getAddress(), bakery.getLongitude(), bakery.getLatitude()));
eventPublisher.publishEvent(publishSaveBakery);

} else if(status == BakeryStatus.UNPOSTING) {
openSearchEventPublisher.publishDeleteBakery(bakeryId);
BakeryDeletionEvent publishDeleteBakery = new BakeryDeletionEvent(this, OpenSearchIndex.BAKERY_SEARCH, bakeryId);
eventPublisher.publishEvent(publishDeleteBakery);
}

bakery.update(request.getName(),
Expand All @@ -271,7 +250,8 @@ public void updateBakery(Long bakeryId, BakeryUpdateRequest request) {
request.getFacilityInfoList(), status);

if (request.getProductList() != null && !request.getProductList().isEmpty()) { // TODO
openSearchEventPublisher.publishDeleteAllProducts(bakeryId);
BakeryDeletionEvent publishDeleteBakery = new BakeryDeletionEvent(this, OpenSearchIndex.BAKERY_SEARCH, bakeryId);
eventPublisher.publishEvent(publishDeleteBakery);
for (BakeryUpdateRequest.ProductUpdateRequest productUpdateRequest : request.getProductList()) {
Product product;
if (productUpdateRequest.getProductId() == null) { // 새로운 product 일 때
Expand All @@ -289,7 +269,8 @@ public void updateBakery(Long bakeryId, BakeryUpdateRequest request) {
productUpdateRequest.getPrice(), productUpdateRequest.getImage());
}

openSearchEventPublisher.publishSaveBread(new BreadLoadData(product.getId(), product.getName(), bakeryId, bakery.getName(), bakery.getAddress(), bakery.getLongitude(), bakery.getLatitude()));
BreadCreationEvent publishSaveBread = new BreadCreationEvent(this, new BreadLoadData(product.getId(), product.getName(), bakeryId, bakery.getName(), bakery.getAddress(), bakery.getLongitude(), bakery.getLatitude()));
eventPublisher.publishEvent(publishSaveBread);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.depromeet.breadmapbackend.domain.search;

import com.depromeet.breadmapbackend.domain.admin.search.HotKeyword;

import java.util.List;

public interface HotKeywordService {
List<HotKeyword> getHotKeywordsRanking();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.depromeet.breadmapbackend.domain.search;

import com.depromeet.breadmapbackend.domain.admin.search.HotKeyword;
import com.depromeet.breadmapbackend.domain.admin.search.HotKeywordRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class HotKeywordServiceImpl implements HotKeywordService {

private final HotKeywordRepository hotKeywordRepository;
@Override
public List<HotKeyword> getHotKeywordsRanking() {
return hotKeywordRepository.findAllByOrderByRankingAsc();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.depromeet.breadmapbackend.domain.search;

import com.depromeet.breadmapbackend.domain.admin.search.Mapper;
import com.depromeet.breadmapbackend.domain.admin.search.dto.HotKeywordResponse;
import com.depromeet.breadmapbackend.domain.search.dto.SearchType;
import com.depromeet.breadmapbackend.domain.search.dto.keyword.response.KeywordSuggestionResponse;
import com.depromeet.breadmapbackend.domain.search.dto.keyword.response.RecentKeywords;
Expand All @@ -24,6 +26,7 @@ public class SearchV2Controller {

private final SearchService searchService;
private final SearchLogService searchLogService;
private final HotKeywordService hotKeywordService;

@GetMapping("/keyword")
@ResponseStatus(HttpStatus.OK)
Expand Down Expand Up @@ -58,4 +61,15 @@ public ApiResponse<KeywordSuggestionResponse> searchKeywordSuggestions(@RequestP
List<String> keywordSuggestions = searchService.searchKeywordSuggestions(keyword);
return new ApiResponse<>(KeywordSuggestionResponse.builder().keywordSuggestions(keywordSuggestions).build());
}

@GetMapping("/rank")
public ApiResponse<List<HotKeywordResponse>> getHotKeywords() {
return new ApiResponse<>(
hotKeywordService.getHotKeywordsRanking()
.stream()
.map(Mapper::of)
.toList()
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@
import org.springframework.context.ApplicationEvent;

@Getter
public class OpenSearchBakeryEvent extends ApplicationEvent {
public class BakeryCreationEvent extends ApplicationEvent {
Long bakeryId;
BakeryLoadData bakeryLoadData;

public OpenSearchBakeryEvent(Object source, BakeryLoadData bakeryLoadData) {
public BakeryCreationEvent(Object source, BakeryLoadData bakeryLoadData) {
super(source);
this.bakeryLoadData = bakeryLoadData;
}
public OpenSearchBakeryEvent(Object source, Long bakeryId) {
super(source);
this.bakeryId = bakeryId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import org.springframework.context.ApplicationEvent;

@Getter
public class OpenSearchDeleteBreadEvent extends ApplicationEvent {
public class BakeryDeletionEvent extends ApplicationEvent {
OpenSearchIndex openSearchIndex;
Long targetId;
Long bakeryId;

public OpenSearchDeleteBreadEvent(Object source, OpenSearchIndex openSearchIndex, Long targetId) {
public BakeryDeletionEvent(Object source, OpenSearchIndex openSearchIndex, Long bakeryId) {
super(source);
this.openSearchIndex = openSearchIndex;
this.targetId = targetId;
this.bakeryId = bakeryId;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package com.depromeet.breadmapbackend.domain.search.events;

import com.depromeet.breadmapbackend.domain.search.dto.keyword.BakeryLoadData;
import com.depromeet.breadmapbackend.domain.search.dto.keyword.BreadLoadData;
import lombok.Getter;
import org.springframework.context.ApplicationEvent;

@Getter
public class OpenSearchBreadEvent extends ApplicationEvent {
public class BreadCreationEvent extends ApplicationEvent {
Long breadId;
BreadLoadData breadLoadData;

public OpenSearchBreadEvent(Object source, BreadLoadData breadLoadData) {
public BreadCreationEvent(Object source, BreadLoadData breadLoadData) {
super(source);
this.breadLoadData = breadLoadData;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import org.springframework.context.ApplicationEvent;

@Getter
public class OpenSearchDeleteBakeryEvent extends ApplicationEvent {
public class BreadDelitionEvent extends ApplicationEvent {
OpenSearchIndex openSearchIndex;
Long targetId;
Long breadId;

public OpenSearchDeleteBakeryEvent(Object source, OpenSearchIndex openSearchIndex, Long targetId) {
public BreadDelitionEvent(Object source, OpenSearchIndex openSearchIndex, Long breadId) {
super(source);
this.openSearchIndex = openSearchIndex;
this.targetId = targetId;
this.breadId = breadId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

@Slf4j
@RequiredArgsConstructor
public class OpenSearchBakeryEventListener implements ApplicationListener<OpenSearchBakeryEvent> {
public class OpenSearchBakeryEventListener implements ApplicationListener<BakeryCreationEvent> {
private final OpenSearchService openSearchService;
@Override
public void onApplicationEvent(OpenSearchBakeryEvent event) {
public void onApplicationEvent(BakeryCreationEvent event) {
try {
openSearchService.convertDataAndLoadToEngine(OpenSearchIndex.BAKERY_SEARCH.getIndexNameWithVersion(), Collections.singletonList(event.getBakeryLoadData()));
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@

import com.depromeet.breadmapbackend.domain.search.OpenSearchService;
import com.depromeet.breadmapbackend.domain.search.dto.OpenSearchIndex;
import com.depromeet.breadmapbackend.domain.search.dto.keyword.BreadLoadData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
public class OpenSearchBreadEventListener implements ApplicationListener<OpenSearchBreadEvent> {
public class OpenSearchBreadEventListener implements ApplicationListener<BreadCreationEvent> {

private final OpenSearchService openSearchService;
@Override
public void onApplicationEvent(OpenSearchBreadEvent event) {
public void onApplicationEvent(BreadCreationEvent event) {
try {
openSearchService.convertDataAndLoadToEngine(OpenSearchIndex.BREAD_SEARCH.getIndexNameWithVersion(), Collections.singletonList(event.getBreadLoadData()));
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

@Slf4j
@RequiredArgsConstructor
public class OpenSearchDeleteBakeryEventListener implements ApplicationListener<OpenSearchDeleteBakeryEvent> {
public class OpenSearchDeleteBakeryEventListener implements ApplicationListener<BakeryDeletionEvent> {
private final OpenSearchService openSearchService;

@Override
public void onApplicationEvent(OpenSearchDeleteBakeryEvent event) {
public void onApplicationEvent(BakeryDeletionEvent event) {
try {
openSearchService.deleteIndex(event.getOpenSearchIndex(), event.targetId);
openSearchService.deleteIndex(event.getOpenSearchIndex(), event.bakeryId);
} catch (IOException e) {
log.error("====== onApplicationEvent Starting with " + event.getOpenSearchIndex() + event.getTargetId() + " has been error");
log.error("====== onApplicationEvent Starting with " + event.getOpenSearchIndex() + event.getBakeryId() + " has been error");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

@Slf4j
@RequiredArgsConstructor
public class OpenSearchDeleteBreadEventListener implements ApplicationListener<OpenSearchDeleteBakeryEvent> {
public class OpenSearchDeleteBreadEventListener implements ApplicationListener<BakeryDeletionEvent> {
private final OpenSearchService openSearchService;

@Override
public void onApplicationEvent(OpenSearchDeleteBakeryEvent event) {
public void onApplicationEvent(BakeryDeletionEvent event) {
try {
openSearchService.deleteIndex(event.getOpenSearchIndex(), event.targetId);
openSearchService.deleteIndex(event.getOpenSearchIndex(), event.bakeryId);
} catch (IOException e) {
log.error("====== onApplicationEvent Starting with " + event.getOpenSearchIndex() + event.getTargetId() + " has been error");
log.error("====== onApplicationEvent Starting with " + event.getOpenSearchIndex() + event.getBakeryId() + " has been error");
}
}
}
Loading

0 comments on commit c99e0b5

Please sign in to comment.