How to connect to ElasticSearch using Java transport client?


Manolis Karamanis

I am following ElasticSearch documentation on Java Client. I have ElasticSearch started and can interact with it using the Rest API. I want to use a Java client, so far I have a body like this:

public class TestElastic {

public static void main(String[] args) {
    try{
        TransportClient client = TransportClient.builder().build()
        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        JSONObject place = new JSONObject();
        place.put("name", "AAAAA");
        IndexResponse response = client.prepareIndex("my_database", "places", "1")
                .setSource(place)
                .get();
        System.out.println(response.toString());
        // Index name
        String _index = response.getIndex();
        System.out.println(_index);
        // Type name
        String _type = response.getType();
        System.out.println(_type);
        // Document ID (generated or not)
        String _id = response.getId();
        System.out.println(_id);
        // Version (if it's the first time you index this document, you will get: 1)
        long _version = response.getVersion();
        System.out.println(_version);
        // isCreated() is true if the document is a new one, false if it has been updated
        boolean created = response.isCreated();
        System.out.println(created);
        client.close();
    }catch (Exception ex){
        ex.printStackTrace();
    }
}

}

In the Java log, I can see the connection to 127.0.0.1:9300. But after the "prepare index" command, I don't see any errors and nothing is printed (I have some system output commands). There is also no relative relationship in ElasticSearch logs. When I create an index using Rest API, I can see it in the logs.

Manolis Karamanis

Ok, as @Val mentioned, I forgot to print the error. The problem is that JSONObject is not in the format that ElasticSearch wants. Map and HashMap are acceptable.

Related


Specify fields using the Elasticsearch Transport client

Tula Is there a way to specify the fields that ES returns via the transport client (specifically using the BoolQueryBuilder)? Using the REST API, this seems easy, for example, can I specify the result field in the elasticsearch query? But not sure how to use t

Specify fields using the Elasticsearch Transport client

Tula Is there a way to specify the fields that ES returns via the transport client (specifically using the BoolQueryBuilder)? Using the REST API, this seems easy, for example, can I specify the result field in the elasticsearch query? But not sure how to use t

How to handle transport errors in elasticsearch python client?

Hans I'm uploading a pandas dataframe to Elastic (using elasticsearch==6.3.1), it works fine if the dataframe size is less than 100MB, I'm using the solution from How to export pandas data to Elasticsearch ? def rec_to_actions(df): for record in df.to_dict

How to handle transport errors in elasticsearch python client?

Hans I'm uploading a pandas dataframe to Elastic (using elasticsearch==6.3.1), it works fine if the dataframe size is less than 100MB, I'm using the solution from How to export pandas data to Elasticsearch ? def rec_to_actions(df): for record in df.to_dict

How to handle transport errors in elasticsearch python client?

Hans I'm uploading a pandas dataframe to Elastic (using elasticsearch==6.3.1), it works fine if the dataframe size is less than 100MB, I'm using the solution from How to export pandas data to Elasticsearch ? def rec_to_actions(df): for record in df.to_dict

How to handle transport errors in elasticsearch python client?

Hans I'm uploading a pandas dataframe to Elastic (using elasticsearch==6.3.1), it works fine if the dataframe size is less than 100MB, I'm using the solution from How to export pandas data to Elasticsearch ? def rec_to_actions(df): for record in df.to_dict

How to handle transport errors in elasticsearch python client?

Hans I'm uploading a pandas dataframe to Elastic (using elasticsearch==6.3.1), it works fine if the dataframe size is less than 100MB, I'm using the solution from How to export pandas data to Elasticsearch ? def rec_to_actions(df): for record in df.to_dict

How to handle transport errors in elasticsearch python client?

Hans I'm uploading a pandas dataframe to Elastic (using elasticsearch==6.3.1), it works fine if the dataframe size is less than 100MB, I'm using the solution from How to export pandas data to Elasticsearch ? def rec_to_actions(df): for record in df.to_dict

Elasticsearch Transport client connection

Akshay Bijawe I am building a Search Web Application with a servlet connected to Elasticsearch. I have a question about Elasticsearch 's shipping module . I am using TransportClient in a class implementing ServletContextListener to open a connection to Elastic

How to query ElasticSearch using Java client?

Jason: The site only contains JSON documents, no Java client. Should I perform some kind of mapping? For example geolocation query : http://www.elasticsearch.org/guide/reference/query-dsl/geo-distance-range-filter.html How can I write such a query using the Ja

How to query ElasticSearch using Java client?

Jason: The site only contains JSON documents, no Java client. Should I perform some kind of mapping? For example geolocation query : http://www.elasticsearch.org/guide/reference/query-dsl/geo-distance-range-filter.html How can I write such a query using the Ja

How to sort by _doc using Elasticsearch Java Client

Garnersham Lavin I want to loop through the entire elasticsearch index/type. I am using scrolling in java client as below SearchResponse scrollResp = client.prepareSearch(test) .setSearchType(SearchType.SCAN) .setScroll(new TimeValue(60000))

How to query ElasticSearch using Java client?

Jason: The site only contains JSON documents, no Java client. Should I perform some kind of mapping? For example geolocation query : http://www.elasticsearch.org/guide/reference/query-dsl/geo-distance-range-filter.html How can I write such a query using the Ja

How to connect to RedisGraph using Java JRedisGraph client

Vikram Rawat The JRedisGraph documentation doesn't give an example on how to connect to the instance (IP, port). https://github.com/RedisGraph/JRedisGraph please help. Guy Curran Check out the overloaded constructor for RedisGraph. public RedisGraph(String hos

How to connect to RedisGraph using Java JRedisGraph client

Vikram Rawat The JRedisGraph documentation doesn't give an example on how to connect to the instance (IP, port). https://github.com/RedisGraph/JRedisGraph please help. Guy Curran Check out the overloaded constructor for RedisGraph. public RedisGraph(String hos

How to connect client to mongodb using java plugin?

Itaied I have a mongo database instance and I want all clients to connect to it. I write in mongo shell db.currentOp(true) I got all the clients and their properties. How to extract data in Java? I tried something like this, but it didn't work: mongoclient.g

How to connect client to mongodb using java plugin?

Itaied I have a mongo database instance and I want all clients to connect to it. In the mongo shell, I write db.currentOp(true) I got all the clients and their properties. How to extract data in Java? I've tried something like this, but it didn't work: mongo

How to connect client to mongodb using java plugin?

Itaied I have a mongo database instance and I want all clients to connect to it. In the mongo shell, I write db.currentOp(true) I got all the clients and their properties. How to extract data in Java? I've tried something like this, but it didn't work: mongo

How to connect to RedisGraph using Java JRedisGraph client

Vikram Rawat The JRedisGraph documentation doesn't give an example on how to connect to the instance (IP, port). https://github.com/RedisGraph/JRedisGraph please help. Guy Curran Check out the overloaded constructor for RedisGraph. public RedisGraph(String hos

How to connect to RedisGraph using Java JRedisGraph client

Vikram Rawat The JRedisGraph documentation doesn't give an example on how to connect to the instance (IP, port). https://github.com/RedisGraph/JRedisGraph please help. Guy Curran Check out the overloaded constructor for RedisGraph. public RedisGraph(String hos

How to connect to RedisGraph using Java JRedisGraph client

Vikram Rawat The JRedisGraph documentation doesn't give an example on how to connect to the instance (IP, port). https://github.com/RedisGraph/JRedisGraph please help. Guy Curran Check out the overloaded constructor for RedisGraph. public RedisGraph(String hos

How to connect client to mongodb using java plugin?

Itaied I have a mongo database instance and I want all clients to connect to it. In the mongo shell, I write db.currentOp(true) I got all the clients and their properties. How to extract data in Java? I've tried something like this, but it didn't work: mongo