Skip to content

Commit

Permalink
[feat] #220 엘라스틱 데이터 response 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
ohdeng02 committed Feb 13, 2024
1 parent dfe4f21 commit 50be576
Show file tree
Hide file tree
Showing 8 changed files with 292 additions and 64 deletions.
7 changes: 3 additions & 4 deletions src/main/java/com/dmarket/controller/ProductController.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.dmarket.controller;

import co.elastic.clients.elasticsearch.core.SearchResponse;
import com.dmarket.domain.product.ProductDocument;
import com.dmarket.dto.request.QnaReqDto;
import com.dmarket.dto.request.ReviewReqDto;
import com.dmarket.dto.response.CMResDto;
import com.dmarket.dto.response.CategoryResDto;
import com.dmarket.dto.response.ProductResDto;
import com.dmarket.dto.response.QnaResDto;
import com.dmarket.service.ProductService;
import com.fasterxml.jackson.databind.node.ObjectNode;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -18,7 +18,6 @@
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@Slf4j
Expand Down Expand Up @@ -60,14 +59,14 @@ public ResponseEntity<CMResDto<Page<ProductResDto.ProductListResDto>>> getCatego

// 상품 목록 조건 검색 api
@GetMapping("/search")
public ResponseEntity<CMResDto<SearchResponse<ProductDocument>>> getSearchProducts(@RequestParam(required = true, value = "q") String query,
public ResponseEntity<CMResDto<SearchResponse<ObjectNode>>> getSearchProducts(@RequestParam(required = true, value = "q") String query,
@RequestParam(required = false, value = "sorter", defaultValue = "reviewCnt") String sorter,
@RequestParam(required = false, value = "min-price", defaultValue = "0") Integer minPrice,
@RequestParam(required = false, value = "max-price", defaultValue = "9999999") Integer maxPrice,
@RequestParam(required = false, value = "star", defaultValue = "0") Float star,
@RequestParam(required = false, value = "page", defaultValue = "0") int pageNo) throws IOException {
//Page<ProductResDto.ProductListResDto> products = productService.getSearchProducts(pageNo, query, sorter, minPrice, maxPrice, star);
SearchResponse<ProductDocument> products = productService.getSearchProducts(pageNo , query,
SearchResponse<ObjectNode> products = productService.getSearchProducts(pageNo , query,
sorter, minPrice, maxPrice, star);
log.info("데이터 조회 완료");
return new ResponseEntity<>(CMResDto.successDataRes(products), HttpStatus.OK);
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/dmarket/domain/document/ImgDocument.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.dmarket.domain.document;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.util.Date;

@Getter
@AllArgsConstructor
@NoArgsConstructor
//@Document(indexName = "product-ngram")
//@Mapping(mappingPath = "elastic/comment-mapping.json")
@JsonIgnoreProperties(ignoreUnknown=true)
public class ImgDocument {

@Id
@Field(name = "img_id", type = FieldType.Long)
private Long img_id;
@Field(name = "img_address", type = FieldType.Text)
private String img_address;
@Field(name = "product_id", type = FieldType.Long)
private Long product_id;
}
46 changes: 46 additions & 0 deletions src/main/java/com/dmarket/domain/document/ProductDocument.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.dmarket.domain.document;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.node.ArrayNode;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.util.Date;

@Getter
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "product-ngram")
//@Mapping(mappingPath = "elastic/comment-mapping.json")
@JsonIgnoreProperties(ignoreUnknown=true)
public class ProductDocument {

@Id
@Field(name = "product_id", type = FieldType.Long)
private Long product_id;
@Field(name = "category_id", type = FieldType.Long)
private Long category_id;
@Field(name = "product_brand", type = FieldType.Text)
private String product_brand;
@Field(name = "product_name", type = FieldType.Text)
private String product_name;
@Field(name = "product_price", type = FieldType.Integer)
private Integer product_price;
@Field(name = "product_sale_price", type = FieldType.Integer)
private Integer product_sale_price;
@Field(name = "product_description", type = FieldType.Text)
private String product_description;
@Field(name = "product_rating", type = FieldType.Float)
private Float product_rating;
@Field(name = "product_created_date", type = FieldType.Long)
private Date product_created_date;
@Field(name = "imgs_enriched", type = FieldType.Object)
private ImgDocument imgs_enriched;
@Field(name = "review_enriched", type = FieldType.Nested)
private ArrayNode reviews;
}
24 changes: 24 additions & 0 deletions src/main/java/com/dmarket/domain/document/ReviewDocument.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.dmarket.domain.document;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

@Getter
@AllArgsConstructor
@NoArgsConstructor
//@Document(indexName = "product-ngram")
//@Mapping(mappingPath = "elastic/comment-mapping.json")
@JsonIgnoreProperties(ignoreUnknown=true)
public class ReviewDocument {

@Id
@Field(name = "review_id", type = FieldType.Long)
private Long review_id;
@Field(name = "product_id", type = FieldType.Long)
private Long product_id;
}
37 changes: 0 additions & 37 deletions src/main/java/com/dmarket/domain/product/ProductDocument.java

This file was deleted.

112 changes: 108 additions & 4 deletions src/main/java/com/dmarket/service/ElasticsearchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import com.dmarket.domain.product.ProductDocument;
import com.dmarket.domain.document.ProductDocument;
import com.dmarket.dto.response.ProductResDto;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
Expand All @@ -27,6 +33,7 @@
@Transactional(readOnly = true)
@PropertySource("classpath:application-elastic.properties")
public class ElasticsearchService {

private final String serverUrl = "https://96cf190a5133454f97f8cf99af05d321.us-central1.gcp.cloud.es.io:443";
private final String apiKey = "dE5TSW5vMEJmNHV1eUxBVlVSR3Q6OUxrY2Q4T2tSVHFIT0lhcFRHR1BaZw==";

Expand All @@ -47,7 +54,7 @@ public class ElasticsearchService {

ElasticsearchClient client = new ElasticsearchClient(transport);

public SearchResponse<ProductDocument> getElasticSearchProducts(String query) throws IOException {
public SearchResponse<ObjectNode> getElasticSearchProducts(String query) throws IOException {
List<String> arr = new ArrayList<>();
arr.add("product_name"); arr.add("product_brand"); arr.add("product_description");
SearchResponse<ProductDocument> response = client.search(s -> s
Expand All @@ -57,6 +64,103 @@ public SearchResponse<ProductDocument> getElasticSearchProducts(String query) th
),
ProductDocument.class
);
return response;
for (Hit<ProductDocument> hit: response.hits().hits()) {
ProductDocument product = hit.source();
System.out.println("Product Name: " + product.getProduct_name());
}

// SphereDistanceAggregate reviews = response
// .aggregations().get("product_ids")
// ._custom()
// .to(SphereDistanceAggregate.class);
// for (Bucket bucket : reviews.buckets()) {
// System.out.println(bucket.key+ bucket.docCount);
// System.out.println("review Count: "+ bucket.reviewCount);
// }

// JsonData reviews = response
// .aggregations().get("product_ids")
// ._custom();
//
//
// JsonArray buckets = reviews.toJson()
// .asJsonObject()
// .getJsonArray("buckets");
//
// for (JsonValue item : buckets) {
// JsonObject bucket = item.asJsonObject();
// double key = bucket.getJsonNumber("key").doubleValue();
// double docCount = bucket.getJsonNumber("doc_count").longValue();
// System.out.println(key + docCount);
// }


SearchResponse<ObjectNode> test = client.search(s -> s
.index("product-ngram").size(5000)
.query(q -> q.multiMatch(qs ->
qs.fields(arr).query(query))
),
ObjectNode.class
);

List<ProductResDto.ProductListResDto> productList = new ArrayList<>();

for (Hit<ObjectNode> hit: test.hits().hits()) {

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date timeInDate =hit.source().getProduct_created_date();
String timeInFormat = sdf.format(timeInDate);

System.out.println(hit.source().getProduct_id());
System.out.println(timeInFormat);

comments.add(new ProductResDto.ProductListResDto(hit.source().getProduct_id(),
hit.source().getProduct_brand(),hit.source().getProduct_name(),
hit.source(),
hit.source().getTitle(), hit.source().getContents(),
hit.source().getSummary(),
timeInFormat,
hit.source().getViews(),
hit.source().getComment_size()));
}
return productList;
}

public static class SphereDistanceAggregate {
private final List<Bucket> buckets;
@JsonCreator
public SphereDistanceAggregate(
@JsonProperty("buckets") List<Bucket> buckets
) {
this.buckets = buckets;
}

public List<Bucket> buckets() {
return buckets;
};
}

public static class Bucket {
private final double key;
private final double docCount;
private final Object reviewCount;
@JsonCreator
public Bucket(
@JsonProperty("key") double key,
@JsonProperty("doc_count") double docCount,
@JsonProperty("review_count") double reviewCount) {
this.key = key;
this.docCount = docCount;
this.reviewCount = reviewCount;
}
public double key() {
return key;
}
public double docCount() {
return docCount;
}
public Object reviewCount() {
return reviewCount;
}
}
}
Loading

0 comments on commit 50be576

Please sign in to comment.