Insert the json into elastic using java

hi i am having below json obect i need to push this data into elasticserach index i already creadted index in elastic ,can any one help me in this .

"data"
{
"counter":"0",
"create_ts":"12-12-2018",
"email":"srinivas.thouti@gmail.com",
"last_updated_ts":"16-12-2018",
"source_site":"CC",
"url":"httpppppp"
}

See https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-index.html

which maven dependency i need to use and can you provide exact example for this.

The Maven dependency is in the documentation I linked to.
There are also examples.

If you can't make it work, share what you did so far and we can probably help.

I do have an example but might be much more complex than your original question though. Anyway, here we go:

thanks its working fine for me i used below code
Email email=new Email();
email.setCounter("0");
email.setCreatets(java.time.LocalDate.now()+"");
email.setEmail("srinivas.thouti@gmail.com");
email.setLast_updated_ts(java.time.LocalDate.now()+"");
email.setSource("Customer Centricity");
email.setUrl(summary);

final ObjectMapper mapper = new ObjectMapper();
RestHighLevelClient esClient=new RestHighLevelClient(RestClient.builder(HttpHost.create("http://apsrp06825:9209")));
 byte[] bytes =  mapper.writeValueAsBytes(email);
 esClient.index(new IndexRequest("email_subscription", "data", email.idAsString()).source(bytes, XContentType.JSON));

but my question is i need to return some automated genarated value of id how can i do this

what do you mean?

i mean while inserting record into elastic i am generating id value, now i need to get that incremented value as return while inserting.

one more query sir how can i get particular record from index.
for example i have id like this 8gH1xWQBtT8Pa64GFEMX i need to get all details using java.

There is nothing like an automatic sequence you can use in elasticsearch. But elasticsearch can always generate unique ids for you if you don't specify the Id.

POST index/_doc
{
   // whatever 
}

To get a document using java use https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-get.html

hi sir can i get the incremented id after inserting the record into elastic

No. As I said there is nothing like this out of the box.
You can simulate it though with something described in https://www.elastic.co/guide/en/elasticsearch/guide/2.x/partial-updates.html#_updates_and_conflicts. But you'd better do it within your application or using another mechanism (which I don't know).

is there any possible to update particular record using java in index.

Yes. The document index API.

hi sir i am unable to find out exact example and i am prepared like this
Email email=new Email();
email.setCounter(Integer.parseInt(subData.getString("counter"))+1+"");
email.setCreatets(subData.getString("createts"));
email.setEmail(subData.getString("email"));
email.setSource(subData.getString("source"));
email.setLast_updated_ts(java.time.LocalDate.now()+" "+java.time.LocalTime.now());
email.setUrl(subData.getString("url"));
String jsonStr = mapper.writeValueAsString(email);
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index("email_subscription");
updateRequest.type("data");
updateRequest.id("21.33.81.65.32.45");
updateRequest.doc(jsonBuilder().startObject()
.field("gender", "male")
.endObject());
esClient.update(updateRequest).getId();

but i am getting some issue in the code of jsonBuilder().startObject()

Please format your code, logs or configuration files using </> icon as explained in this guide and not the citation button. It will make your post more readable.

Or use markdown style like:

```
CODE
```

This is the icon to use if you are not using markdown format:

There's a live preview panel for exactly this reasons.

Lots of people read these forums, and many of them will simply skip over a post that is difficult to read, because it's just too large an investment of their time to try and follow a wall of badly formatted text.
If your goal is to get an answer to your questions, it's in your interest to make it as easy to read and understand as possible.
Please update your post.

Please share what is the exact problem you have.
Also, I don't recommend using the update API unless you absolutely need it but prefer the index document API.

i am unable to find out exact example for update particular record by using id in elastic search and i am prepared like this to update record in elastic index

Email email=new Email();
email.setCounter(Integer.parseInt(subData.getString("counter"))+1+"");
email.setCreatets(subData.getString("createts"));
email.setEmail(subData.getString("email"));
email.setSource(subData.getString("source"));
email.setLast_updated_ts(java.time.LocalDate.now()+" "+java.time.LocalTime.now());
email.setUrl(subData.getString("url"));
String jsonStr = mapper.writeValueAsString(email);
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index("email_subscription");
updateRequest.type("data");
updateRequest.id("21.33.81.65.32.45");
updateRequest.doc(jsonBuilder().startObject()
.field("gender", "male")
.endObject());
esClient.update(updateRequest).getId();

but i am getting some issue in the code of jsonBuilder().startObject()

Great job for the formatting!

Now, could you look at this:

Please share what is the exact problem you have.
Also, I don't recommend using the update API unless you absolutely need it but prefer the index document API.

will you provide example for update the particular document in elastic using java.i used Index Document Api. below is my prepared query for update when i run this code record not updated in elastic

Email email=new Email();
	     email.setCounter(Integer.parseInt(subData.getString("counter"))+1+"");
	   	email.setCreatets(subData.getString("create_ts"));
	   	email.setEmail(subData.getString("email"));
	   	email.setSource(subData.getString("source_site"));
	   	email.setLast_updated_ts(java.time.LocalDate.now()+" "+java.time.LocalTime.now());
	   	email.setUrl(subData.getString("url"));
	   	email.setId("d7jfy2QB1c0X8IBjl2oJ");
	   	UpdateRequest updateRequest = new UpdateRequest("email_subscription", "data", "d7jfy2QB1c0X8IBjl2oJ")
	   	        .script(new Script("ctx._source.last_updated_ts = \""+email.getLast_updated_ts()+"\","+"ctx._source.counter = \""+email.getCounter()+"\","
	   	        		+"ctx._source.create_ts = \""+email.getCreatets()+"\","+"ctx._source.email = \""+email.getEmail()+"\","+"ctx._source.source_site = \""+email.getSource()+"\","+
	   	        		"ctx._source.url = \""+email.getUrl()+"\""));
	   	esClient.update(updateRequest).getGetResult();

Example here: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-update.html

is it possible delete and insert the record into elastic at a time .
my scenario is i have record with id ' 1' delete that record and insert with some updated values with same id.

DELETE test
PUT test/_doc/1
{
  "foo": "bar"
}
PUT test/_doc/1
{
  "foo": "baz"
}

The later call actually does something like:

DELETE test/_doc/1
PUT test/_doc/1
{
  "foo": "baz"
}
1 Like