diff --git a/src/main/java/com/umc/naoman/domain/photo/elasticsearch/repository/PhotoEsClientRepository.java b/src/main/java/com/umc/naoman/domain/photo/elasticsearch/repository/PhotoEsClientRepository.java index cf22bc4e..e28a209c 100644 --- a/src/main/java/com/umc/naoman/domain/photo/elasticsearch/repository/PhotoEsClientRepository.java +++ b/src/main/java/com/umc/naoman/domain/photo/elasticsearch/repository/PhotoEsClientRepository.java @@ -7,6 +7,7 @@ import co.elastic.clients.elasticsearch.core.BulkResponse; import co.elastic.clients.elasticsearch.core.SearchResponse; import co.elastic.clients.elasticsearch.core.search.Hit; +import co.elastic.clients.json.JsonData; import com.umc.naoman.domain.photo.elasticsearch.document.PhotoEs; import com.umc.naoman.domain.photo.entity.Photo; import com.umc.naoman.global.error.BusinessException; @@ -21,7 +22,9 @@ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; @Repository @@ -265,6 +268,39 @@ public List deletePhotoEsByShareGroupId(Long shareGroupId) { return rdsIdList; } + //특정 사진에 특정 맴버 다운로드 태그 추가 + public void addDownloadTag(List photoEs, Long memberId){ + List fieldValueList = photoEs.stream() + .map(photo -> FieldValue.of(photo.getName())) + .toList(); + String routing = photoEs.get(0).getShareGroupId().toString(); + Map params = new HashMap<>(); + params.put("memberId", JsonData.of(memberId)); + try { + elasticsearchClient.updateByQuery(u -> u + .index("photos_es") + .routing(routing) + .query(q -> q + .terms(t -> t + .field("name") + .terms(te -> te.value(fieldValueList)) + ) + ) + .script(s -> s + .inline(i->i + .source("if (!ctx._source.downloadTag.contains(params.memberId)) { " + + "ctx._source.downloadTag.add(params.memberId); }") + .lang("painless") + .params(params) + ) + + ) + ); + } catch (IOException e) { + throw new BusinessException(ElasticsearchErrorCode.ELASTICSEARCH_IOEXCEPTION, e); + } + } + String esTimeFormat(LocalDateTime localDateTime) { DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); return localDateTime.format(dateTimeFormatter); diff --git a/src/test/java/com/umc/naoman/domain/photo/elasticsearch/repository/PhotoEsClientRepositoryTest.java b/src/test/java/com/umc/naoman/domain/photo/elasticsearch/repository/PhotoEsClientRepositoryTest.java deleted file mode 100644 index 9e80cae8..00000000 --- a/src/test/java/com/umc/naoman/domain/photo/elasticsearch/repository/PhotoEsClientRepositoryTest.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.umc.naoman.domain.photo.elasticsearch.repository; - -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; - -import java.util.List; - -import static org.junit.jupiter.api.Assertions.*; - -@SpringBootTest -class PhotoEsClientRepositoryTest { - @Autowired - private PhotoEsClientRepository photoEsClientRepository; - @Test - public void test() { - List val = photoEsClientRepository.deletePhotoEsByFaceTag(1001L); - - } -} \ No newline at end of file