I keep getting caught out by Error: java.lang.IllegalStateException: Object builders can only be used once
errors.
This is my fault for creating a builder outside of the query lambda and then calling builder.build()
inside the lambda, eg:
BoolQuery.Builder bool = new BoolQuery.Builder().filter(....);
if (someCondition) {
bool.filter(f -> another conditional filter);
}
SearchResponse<ModeClass> response = elasticsearchService.search(s -> s
.query(q -> q.bool(bool.build())), ModelClass.class)
...
I was going to change code like this to call build()
outside of the lambda and pass it in, like so:
BoolQuery.Builder bool = new BoolQuery.Builder().filter(....);
if (someCondition) {
bool.filter(f -> another conditional filter);
}
Bool.Query boolQuery = bool.build();
SearchResponse<ModelClass> response = elasticsearchService.search(s -> s
.query(q -> q.bool(boolQuery)), ModelClass.class)
...
This way, if the lamda is re-executed (by a resilience4j Retry, for example), we're not calling build()
multiple times on the builder.
BUT I notice the docs state:
Builders are transient objects that should not be reused after calling
build()
Does this mean this is a bad idea? Why?
Once a builder is built, why can't it be used multiple times?