SpringBoot ElasticSearch Config

민지킴·2021년 6월 2일
0

application.yml

---
spring:
  profiles: elasticReal
  elasticsearch:
    clusterName: --클러스터 이름
    host: --호스트 주소
    port: 9300
    resthost: --호스트 주소
    restport: 9200
    profile: prd

---

ElasticSearchConfig

package cafe24.batch.count.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

import java.net.InetAddress;

@Configuration
@Slf4j
@EnableElasticsearchRepositories(basePackages = "cafe24.batch.count.repository.elasticsearch")
public class ElasticSearchConfig implements AutoCloseable {

	@Value("${spring.elasticsearch.clusterName}")
	private String clusterName;

	@Value("${spring.elasticsearch.host}")
	private String host;

	@Value("${spring.elasticsearch.port}")
	private int port;

	@Value("${spring.elasticsearch.resthost}")
	private String resthost;

	@Value("${spring.elasticsearch.restport}")
	private int restport;

	RestClient restClient;

	TransportClient client;

	@Bean(name = "lunaElasticsearchClient")
	public Client client() throws Exception {
		Settings settings = Settings.builder().put("cluster.name", clusterName).build();

		client = new PreBuiltTransportClient(settings);
		client.addTransportAddress(new TransportAddress(InetAddress.getByName(host), port));
		return client;
	}

	@Bean(name = "elasticsearchTemplate")
	public ElasticsearchOperations elasticsearchTemplate() throws Exception {
		log.info("elasticsearchTemplate");
		return new ElasticsearchTemplate(client());
	}

	@Bean(name = "lunaElasticsearchRestClient")
	public RestClient restClient() {
		restClient = RestClient.builder(new HttpHost(resthost, restport)).build();
		log.info("restClient");
		return restClient;
	}

	@Override
	public void close() throws Exception {
//		log.info("elasticClose()");
		client.close();
		restClient.close();
	}

}

ElasticSearch Index

package cafe24.batch.count.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Component
public class ElasticIndex {

    @Value("${spring.elasticsearch.profile}")
    public String ELASTIC_PROFILE;

    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM");

    public String getIndexDatea() {
        return formatter.format(LocalDate.now());
    }

}

ElasticSearch Repository

package cafe24.batch.count.repository.elasticsearch;

import cafe24.batch.count.vo.elasticsearch.ProductOriginDocument;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchCrudRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;

import java.util.Date;

@Repository("productInfoPagingRepository")
public interface ProductInfoPagingRepository extends ElasticsearchCrudRepository<ProductOriginDocument,Long>{
    Page<ProductOriginDocument> findByMallIdAndApiTypeAndTopicDivAndLogDateBetween(String key, String apiType, String topicDiv, String start_date, String end_date, Pageable pageable);

    Page<ProductOriginDocument> findByMallIdAndApiTypeAndTopicDivAndLogDateAfter(String key, String apiType, String topicDiv, String start_date, Pageable pageable);

    Page<ProductOriginDocument> findByMallIdAndApiTypeAndTopicDiv(String key, String apiType, String topicDiv, Pageable pageable);
}

or

package cafe24.batch.count.repository.elasticsearch;

import cafe24.batch.count.vo.elasticsearch.ProductOriginDocument;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

import java.awt.print.Pageable;
import java.util.Date;
import java.util.List;

@Repository("productInfoRepository")
public interface ProductInfoRepository extends ElasticsearchRepository<ProductOriginDocument,Long> {

    List<ProductOriginDocument> findByMallIdAndApiTypeAndTopicDivAndLogDateBetween(String mallid, String apitype, String topicdiv, String start, String end);


}

ElasticSearch Document

package cafe24.batch.count.vo.elasticsearch;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
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;

@Getter
@Setter
@ToString
@NoArgsConstructor
@Document(indexName = "cafe24-#{elasticIndex.ELASTIC_PROFILE}-origin-product-log", type = "_doc", createIndex = false)
public class ProductOriginDocument {

    // ID
    @Id
    private String id;

    // 호스팅번호
    @Field(type = FieldType.Integer)
    private int hostingNo;

    // 몰아이디
    @Field(type = FieldType.Text)
    private String mallId;

    // 서비스번호
    @Field(type = FieldType.Integer)
    private int serviceNo;

    // 세션아이디
    @Field(type = FieldType.Text)
    private String sessionId;

    // 현재일시
    @Field(type = FieldType.Date)
    private String logDate;

    // 토픽 구분
    @Field(type = FieldType.Text)
    private String topicDiv;

    // api 구분
    @Field(type = FieldType.Text)
    private String apiType;

    // 호출 URL
    @Field(type = FieldType.Text)
    private String endpointUrl;

    // 원문
    @Field(type = FieldType.Text, index = false)
    private String responseBody;

    // 응답 헤더
    @Field(type = FieldType.Text, index = false)
    private String responseHeader;

}
profile
하루하루는 성실하게 인생 전체는 되는대로

0개의 댓글