Table of Content
Spring Boot Elasticsearch Client 설정하기
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
의존성 추가
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch:2.5.6'
}
설정
코맨트를 해제하면 검색엔진에 전송되는 쿼리를 로그에 출력해 준다.
application.yml
#logging:
# level:
# org:
# springframework:
# data:
# elasticsearch:
# client:
# WIRE: trace
elasticsearch:
host: localhost
port: 9200
@Configuration
public class ElasticsearchClientConfig extends AbstractElasticsearchConfiguration {
@Value("${elasticsearch.host}")
private String host;
@Value("${elasticsearch.port}")
private int port;
@Override
@Bean
public RestHighLevelClient elasticsearchClient() {
SnakeCaseFieldNamingStrategy a;
final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo(host + ":" + port)
.usingSsl() // connect to https
.build();
return RestClients.create(clientConfiguration).rest();
}
@Bean
public ElasticsearchOperations elasticsearchOperations() {
return new ElasticsearchRestTemplate(elasticsearchClient());
}
}
엔터티 생성
Entity 대신 Document 어노테이션이 붙는다.
@Setter
@Getter
@NoArgsConstructor
@Document(indexName = "auto_complete")
public class ESAutoComplete implements Persistable<Integer> {
@Id
@Field(type = FieldType.Long)
Integer id;
@NotNull
@Field(type = FieldType.Text, name = "search_string")
String searchString;
@Override
public Integer getId() {
return id;
}
@Override
public boolean isNew() {
return false;
}
}
리포지터리 생성
@Repository
public interface ESAutoCompleteRepository extends ElasticsearchRepository<ESAutoComplete, Integer> {
}
서비스 생성
@Service
public class ESAutoCompleteService extends ESCustomGenericService<ESAutoCompleteDto, ESAutoComplete, Integer, Integer> {
private final ESAutoCompleteMapper mapper = Mappers.getMapper(ESAutoCompleteMapper.class);
@Autowired
private ElasticsearchOperations operations;
public ESAutoCompleteService(ElasticsearchRepository<ESAutoComplete, Integer> repository) {
super(repository, "자동완성");
}
@Override
public SearchResponseDto search(Map<String, String> params) throws UnsupportedEncodingException {
ESAutoCompleteSearchDto searchDto = new ESAutoCompleteSearchDto(params);
Pageable pageable = PageRequest.of(searchDto.getPageNo(), searchDto.getPageSize());
BoolQueryBuilder boolQueryBuilder = boolQuery();
boolQueryBuilder
.must(matchQuery("search_string.jaso",
searchDto.getQueryString()).operator(Operator.AND).analyzer("suggest_search_analyzer").boost(5.0f))
.should(matchPhraseQuery("search_string.ngram",
searchDto.getQueryString()).analyzer("my_ngram_analyzer").boost(25.0f));
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
queryBuilder
.withPageable(pageable)
.withQuery(boolQueryBuilder);
SearchHits<ESAutoComplete> articles = operations
.search(queryBuilder.build(), ESAutoComplete.class, IndexCoordinates.of("auto_complete"));
List<SearchHit<ESAutoComplete>> searchHitList = articles.getSearchHits();
ArrayList<ESAutoComplete> list = new ArrayList<>();
for (SearchHit<ESAutoComplete> item : searchHitList) {
list.add(item.getContent());
}
List<Object> results = Arrays.asList(toDto(list).toArray());
int totalCount = (int) articles.getTotalHits();
return new SearchResponseDto(results, pageable, totalCount);
}
}
```
```java