Insert the json into elastic using java


(srinivas) #9

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


(David Pilato) #10

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).


(srinivas) #11

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


(David Pilato) #12

Yes. The document index API.


(srinivas) #13

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()


(David Pilato) #14

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.


(srinivas) #15

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()


(David Pilato) #16

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.


(srinivas) #17

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();

(David Pilato) #18

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


(srinivas) #19

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.


(David Pilato) #20
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"
}

(srinivas) #21

hi i am getting below issue while inserting the date
ElasticsearchException[Elasticsearch exception [type=illegal_argument_exception, reason=Invalid format: "2018-07-25 14:28:04" is malformed at " 14:28:04"]]

will you help in this


(David Pilato) #22

Have a look at mapping for date fields. https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html


(srinivas) #23

hi i am unable to update existing record in elastic search using java can any provide me existing code please.


(David Pilato) #24

Because it is raising the error you shared?


(srinivas) #25

which maven dependency i need to use i am using below one

org.elasticsearch.client
transport
6.3.2

TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)

still now i am unable to import the class of PreBuiltTransportClient


(David Pilato) #26

I don't know what you are doing. But this class exists: https://artifacts.elastic.co/javadoc/org/elasticsearch/client/transport/6.3.2/org/elasticsearch/transport/client/PreBuiltTransportClient.html

May be you have a wrong dependency. Check what gives

mvn dependency:tree

(srinivas) #27

thanks for your replay .
i have one more question here

TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
				.addTransportAddress(new TransportAddress(InetAddress.getByName("host1"), 9209));

how can i pass host1 i have my own server for elastic .


(David Pilato) #28

host1 is whatever value you want. Your hostname, 127.0.0.1...
Note that I doubt that the port 9209 will work. I'd expect something like 9300.