개요

ES 5.6.0에 들어서면서 RestClient가 High/Low level Client로 바뀌는 대격변이 일어나 이 규격에 맞는 Client 설정 및 쿼리를 테스트 해보고 정리해둔다. 해당 스니펫은 ES 5.6.10으로 테스트 하였다.

의존성

elasticsearch는 필요시에 꽂아서 사용하는 용도이지 필수는 아니다.

    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>${es.version}</version>
    </dependency>

    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>transport</artifactId>
        <version>${es.version}</version>
    </dependency>

    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>${es.version}</version>
    </dependency>

EsConfig

@Configuration
public class EsConfig {

    @Bean
    public TransportClient transportClient() throws UnknownHostException {
    TransportClient client = new PreBuiltTransportClient(Settings.builder()
        .put("cluster.name", "alyes")
        .build())
        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(EsInfoUtil.getTransportEsUrl()), EsInfoUtil.getTransportEsPort()));

    return client;
    }

    @Bean
    public RestHighLevelClient client() {
    RestClient restClient = RestClient.builder(
        new HttpHost(EsInfoUtil.getRestEsUrl(), EsInfoUtil.getRestEsPort(), "http")
    ).build();
    return new RestHighLevelClient(restClient);
    }
}

쿼리 조회

@RunWith(SpringRunner.class)
@SpringBootTest
public class Es5610TestApplicationTests {

    @Autowired
    TransportClient client;

    @Autowired
    RestHighLevelClient restClient;

    @Test
    public void testHighLevelClient() {
    SearchResponse sr = client.prepareSearch("map_userlog_prd").setTypes("info").get();
    System.out.println(sr.toString());
    }

    @Test
    public void testRestClient() throws IOException {
    SearchRequest request = new SearchRequest("map_userlog_prd").types("info");
    SearchResponse sr = restClient.search(request);
    System.out.println(sr.toString());
    }
}