How to use High Level Rest Client in Spring Data E

2019-07-15 03:59发布

问题:

Spring Data ES 3.2.0.M1 still uses old TransportClient instead of HighLevelRestClient

Spring Data ES 3.2.0.M1 supports High Level Rest Client, see Add support for Java High Level REST Client. I've added Spring Data ES 3.2.0.M1 to the SB2 app:

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
            <version>3.2.0.M1</version>
        </dependency>

However, still TransportClient is used. There are two indication of that: exceptions on start-up:

o.e.transport.netty4.Netty4Transport     : exception caught on transport layer [NettyTcpChannel{localAddress=/127.0.0.1:61171, remoteAddress=localhost/127.0.0.1:8085}], closing connection

io.netty.handler.codec.DecoderException: java.io.StreamCorruptedException: invalid internal transport message format, got (48,54,54,50)
    at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:472) ~[netty-codec-4.1.33.Final.jar:4.1.33.Final]

and exception stacktrace when calling ElasticsearchTemplate:

    at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:349)
    at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:247)
    at org.elasticsearch.client.transport.TransportProxyClient.execute(TransportProxyClient.java:60)
    at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:382)
    at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:395)
    at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:384)
    at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:46)
    at org.springframework.data.elasticsearch.core.ElasticsearchTemplate.getSearchResponse(ElasticsearchTemplate.java:947)

Are there any config param to tell Spring Data ES to switch to new High Level Rest Client? The docs say nothing about it.

P.S. Spring Data ES 3.2.0.M1 has 6.4.3 ES client version:

Caused by: java.io.StreamCorruptedException: invalid internal transport message format, got (48,54,54,50)
    at org.elasticsearch.transport.TcpTransport.validateMessageHeader(TcpTransport.java:1327) ~[elasticsearch-6.4.3.jar:6.4.3]
    at org.elasticsearch.transport.netty4.Netty4SizeHeaderFrameDecoder.decode(Netty4SizeHeaderFrameDecoder.java:36) ~[transport-netty4-client-6.4.3.jar:6.4.3]
    at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:502) ~[netty-codec-4.1.33.Final.jar:4.1.33.Final]
    at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:441) ~[netty-codec-4.1.33.Final.jar:4.1.33.Final]
    ... 20 common frames omitted

back-end runs 6.4.2 version:

bash-4.4$ curl http://127.0.0.1:8085
{
  "name" : "NA17WWR",
  "cluster_name" : "494164851665",
  "cluster_uuid" : "7t3LoK7PRp-ur6FyxSmHwQ",
  "version" : {
    "number" : "6.4.2",
    "build_flavor" : "oss",
    "build_type" : "zip",
    "build_hash" : "04711c2",
    "build_date" : "2018-10-16T09:16:35.059415Z",
    "build_snapshot" : false,
    "lucene_version" : "7.4.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

回答1:

As mentioned in the issue you're referring to, the high level REST client is available in ElasticsearchRestTemplate (see PR #216) not in ElasticsearchTemplate, which they'll keep until ES 7 for backward compatibility reasons.

You can create one with the configuration below:

<bean name="elasticsearchTemplate"
      class="org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate">
    <constructor-arg name="client" ref="restClient"/>
</bean>

<elasticsearch:rest-client id="restClient"/>