Can't connect to ElasticSearch server using Java API

后端 未结 4 900
醉梦人生
醉梦人生 2020-12-29 05:22

I am trying to connect to an ElasticSearch server using the Java API. I using elasticsearch service to start/stop and elasticsearch head for visualising the cluster. The clu

相关标签:
4条回答
  • 2020-12-29 05:31

    This is for users who are using spring-boot spring-data elasticsearch. Please ensure that the version of your client is the same as the server.

    <library name="Gradle: org.elasticsearch:elasticsearch:1.5.2">
      <CLASSES>
        <root url="jar://$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.elasticsearch/elasticsearch/1.5.2/47aafc6bf8f23ed8dcbf6a1db174fb0b8e44a8db/elasticsearch-1.5.2.jar!/" />
      </CLASSES>
      <JAVADOC />
      <SOURCES>
        <root url="jar://$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.elasticsearch/elasticsearch/1.5.2/d1e0b7b758ce7bd5d6e3757896054b09f28f372d/elasticsearch-1.5.2-sources.jar!/" />
      </SOURCES>
    </library>
    
    0 讨论(0)
  • 2020-12-29 05:39

    First, version is really important, Java API is strong related with the version you are using,

    This solution works in elasticSearch v 5.4.1 with java 8

    make sure that you are getting all dependencies, and the child dependencies (not only elasticSearch jar), use maven, ivy etc for getting the dependencies tree.

    in ivy for example :

     <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/transport -->
      <dependency org="org.elasticsearch.client" name="transport" rev="5.4.1"  conf="default"/>
    

    //--------------------- then in a class you can call:

     TransportClient client;
    
        client = new PreBuiltTransportClient(Settings.EMPTY).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
    
    0 讨论(0)
  • 2020-12-29 05:49

    In case anybody else stumbles on this thread, I found out another reason you could receive this error is because the cluster_name has been changed to something other than the default.

    To see your current cluser_name:

    curl -XGET localhost:9200/_cluster/nodes?pretty=true
    

    Use code similar to what OP has above:

    Settings settings = ImmutableSettings.settingsBuilder()
        .put("cluster.name", "test").build();
    Client client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
    

    Source: http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/client.html

    0 讨论(0)
  • 2020-12-29 05:52

    Just so this question is marked answered for others who may experience the same issue (the answer is actually at the end of the question).

    I ran into the same issue and it turned out to be a discrepancy between the version of the JAR used by the Java client and the version the server was running. Your best bet is to ensure an exact match, then the instructions given here just work w/o any tweaking.

    Another thing to check is that your Java client is using the correct port, which is not the one used by http clients. The correct port defaults to 9300 rather than 9200, which is used by the latter.

    0 讨论(0)
提交回复
热议问题