Why is .doc() missing in UpdateOperation?

Hi, as an junior Java developer try to understand the things by using the "easy way".

I actually don't understand what requirements like this mean and can't write code from the scratch with that information:

update(Function<Builder<TDocument,TPartialDocument>, ObjectBuilder<UpdateOperation<TDocument,TPartialDocument>>> fn) : ObjectBuilder<BulkOperation> - Builder

I actually need to first program the elaborated way to see what is happening in the background.

So for example when i have something like this:

client.delete(dere -> dere.index("foo").id(delete_candidate));

I could put it in a BulkRequest by doing as follows:

BulkRequest.Builder brb = new BulkRequest.Builder();
DeleteOperation deop2 = new DeleteOperation.Builder().index("foo").id(delete_candidate).build();
BulkOperation bo = new BulkOperation.Builder().delete(deop2).build();
brb.operations(bo);

or i can actually shorten this up by writing:

brb.operations(buop -> buop.delete(deop -> deop.index("foo").id(delete_candidate)));

"Lesson learned": Just copy the lampda-expression into the delete-part of the brb.operations.

Same for indexing:

Rechnung r5 = new Rechnung(13293, Calendar.getInstance().getTime(), 79.99f, Gläubiger, Schuldner);
client.index(inre -> inre.index("foo").document(r5));

BulkOperation bo2 = new BulkOperation.Builder().index(i -> i.index("foo").document(r5)).build();
brb.operations(bo2);

brb.operations(buop -> buop.index(inop -> inop.index("foo").document(r5)));

"Lesson learned": Just copy the lampda-expression into the index-part of the brb.operations.

But if i do so for updating:

client.update(upre -> upre.index("foo").id("bar").doc(r5), Rechnung.class);

"Do what i have learned": copy the lampda-expression into the update-part of the brb.operations.

brb.operations(buop -> buop.update(upop -> upop.index("foo").id("bar").doc(r5), Rechnung.class));

This results in eclipse telling me "The target type of this expression must be a functional interface". After hours of searching i found

which tells, that ".doc" does not exist. Eclipse didn't!

Why is .doc missing?

Actuall solution (compileable code but gives java.lang.IllegalArgumentException: Class class co.elastic.clients.elasticsearch.core.bulk.UpdateOperation cannot be read from JSON):

ObjectMapper mapper = new ObjectMapper();
String myjson = mapper.writeValueAsString(r5);
UpdateOperation.Builder<Rechnung, Void> up = new UpdateOperation.Builder<Rechnung, Void>();
up.index("foo");
up.id("bar");
up.withJson(new StringReader(myjson));
BulkOperation bo3 = new BulkOperation.Builder().update(up.build()).build();
brb.operations(bo3);

or "short" Lambda:

brb.operations(buop -> buop.update(upop -> {
ObjectMapper mapper = new ObjectMapper();
String myjson = "";
try {
	myjson = mapper.writeValueAsString(r5);
} catch (JsonProcessingException e) {
	e.printStackTrace();
}
return upop.index("foo").id("bar").withJson(new StringReader(myjson));
}));

Well these both are not short and not nice looking.
Is there actually a shorter and more nice looking method?

Thanks - Enomine

Hey there. Found a working code for me:

BulkRequest.Builder brb = new BulkRequest.Builder().index(rechnungen10);
Rechnung r6 = new Rechnung();
r6.Rechnungsbetrag = 49.99f;
brb.operations(buop -> buop.update(uo -> uo.id("" + Rechnungsnummer).action(ua -> ua.doc(r6))));

Found at New JAVA API Documentation -> elasticsearch-java/RequestTest.java at main · elastic/elasticsearch-java · GitHub

Elasticsearch Java API Client should be more like this skripting thing =) ... How do you call it?

Painless

Thanks - Enomine

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.