본문 바로가기

ElasticSearch

elasticsearch 적용기

//ElasticSearch
implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch'
implementation 'org.elasticsearch.client:elasticsearch-rest-high-level-client:7.15.1'

 

해당 엔티티의 docs 클래스 작성

@Getter
@Document(indexName = "delivery")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class RestaurantDocument {

    @Id
    private long id;
    @Field(name = "restaurant_name", type = FieldType.Text)
    private String restaurantName;
    @Field(type = FieldType.Text)
    private String address;
    @Field(type = FieldType.Text)
    private Category category;
}

 

logstash config에서 가져오려고 설정했던 값들만 따로 document 클래스를 따로 만들어줬다.

 

package com.challnege.delivery.global.elasticsearch;

import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
import com.challnege.delivery.domain.restaurant.entity.Restaurant;
import lombok.RequiredArgsConstructor;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.client.erhlc.NativeSearchQuery;
import org.springframework.data.elasticsearch.client.erhlc.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class ESService {

    private final ElasticsearchOperations elasticsearchOperations;
    private final RestaurantSearchRepository restaurantSearchRepository;
    private final ElasticsearchTemplate elasticsearchTemplate;

    public Iterable<RestaurantDocument> searchAll() {
        return restaurantSearchRepository.findAll();
    }

    public List<RestaurantDocument> findBySearchOption(String restaurantName, String address, String category) {
        return restaurantSearchRepository.findBySearchOption(restaurantName, address, category);
    }

    public List<RestaurantDocument> findByCategory(String category) {
        return restaurantSearchRepository.findByCategory(category);
    }
}
package com.challnege.delivery.global.elasticsearch;

import com.challnege.delivery.domain.restaurant.entity.Restaurant;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface RestaurantSearchRepository extends ElasticsearchRepository<RestaurantDocument, Long> {

    @Query("{\"bool\": {\"should\": [{\"match\": {\"restaurant_name\": \"?0\"}},{\"match\": {\"address\": \"?1\"}},{\"match\": {\"category\": \"?2\"}}]}}")
    List<RestaurantDocument> findBySearchOption(String restaurantName, String address, String category);

    @Query("{\"bool\": {\"must\": [{\"match\": {\"address\": \"?0\"}}]}}")
    List<RestaurantDocument> findByCategory(String category);


}
package com.challnege.delivery.global.elasticsearch;

import com.challnege.delivery.domain.restaurant.entity.Restaurant;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.client.erhlc.NativeSearchQuery;
import org.springframework.data.elasticsearch.client.erhlc.NativeSearchQueryBuilder;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class TestController {

    private final ESService esService;

    @GetMapping("/test")
    public Iterable<RestaurantDocument> searchRestaurants() {
        return esService.searchAll();
    }

    @GetMapping("/test1")
    public List<RestaurantDocument> searchRestaurants(
            @RequestParam(required = false) String restaurantName,
            @RequestParam(required = false) String address,
            @RequestParam(required = false) String category) {
        return esService.findBySearchOption(restaurantName, address, category);
    }

    @GetMapping("test2")
    public List<RestaurantDocument> findByCategory(@RequestParam(required = false) String category) {
        return esService.findByCategory(category);
    }

}

 

간단하게 리스트로 값들을 받아봤다

다음 글에서는 페이징 처리 및 조금의 조건들이 더해진 쿼리들을 날려볼 생각

'ElasticSearch' 카테고리의 다른 글