엘라스틱서치2022. 10. 25. 17:25

# Deprecated된 RestHighLevelClient를 대체하기 위해 ElasticsearchClient를 활용.

 

public static CountResponse getCountResponse(ElasticsearchClient client, Query query) {
  CountRequest countRequest = new CountRequest.Builder().query(query).build();
  try {
    return client.count(countRequest);
  } catch (IOException e) {
    e.printStackTrace();
  }
  return null;
}

 

※ 매개변수로 넘어오는 Query 객체는 MatchQuery, RangeQuery, BoolQuery 등.

 

 

Posted by 홍규홍규
엘라스틱서치2022. 9. 13. 17:26

# Deprecated된 RestHighLevelClient를 대체하기 위해 ElasticsearchClient를 활용.

 

 ExistsRequest existsRequest = new ExistsRequest.Builder().index(index).build();
  try {
    BooleanResponse response = client.indices().exists(existsRequest);
    return response.value();
  } catch (IOException e) {
    return false;
  }
}

 

※ 참고. 매개변수는 다음과 같이 List 또는 String 타입

public final Builder index(List<String> list) {
  this.index = _listAddAll(this.index, list);
  return this;
}

public final Builder index(String value, String... values) {
  this.index = _listAdd(this.index, value, values);
  return this;
}
Posted by 홍규홍규
엘라스틱서치2022. 9. 13. 17:20

# Deprecated된 RestHighLevelClient를 대체하기 위해 ElasticsearchClient를 활용.

 

1. 버전 확인. (버전별 호환은 아래 사이트에서 확인할 수 있다.)

(https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#core.extensions)

 

2. 스프링부트 2.7.3 기준

  implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch'
  // 빈 생성 에러가 발생할 경우 아래도 추가해준다.
  implementation 'jakarta.json:jakarta.json-api:2.1.1'

 

3. Configuration 클래스에 Bean 추가

@Bean
public ElasticsearchClient esClient() {
  RestClient restClient = RestClient.builder(new HttpHost("url", 9200)).build();
  ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
  return new ElasticsearchClient(transport);
}

 

Posted by 홍규홍규