Initializing a SearchResponse object to null with the high level REST client

Can anyone shed light on how to achieve this initialization or a workaround? I am trying to initialize the SearchResponse object before a conditional loop and populate the object inside the loop. The motivation is to make my code concise and easily maintainable.

My question on stack overflow: https://stackoverflow.com/questions/61150500/initialize-an-elasticsearch-searchresponse-object-before-running-a-query

Documentation: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-search.html#java-rest-high-search-response

Cheers!
-Gideon

Welcome !

As the SearchResponse object is an object generated by the HLClient, I don't understand why you would initialize it.

Thank you, David. I must initialize the searchResponse object here (rather , then inside of the if loops), so that I can reference the object to calculate totalHits, elapsed time and facets (aggregation) data from the response after the if-loops. Otherwise, I will need to create the searchResponse object inside of the if loop that executes and will need to include the code that calculates totalHits, etcetera multiple times - once inside of each if loop.

Why do you need to initialize something that will be overwritten by the call of search of the elasticsearch client?

Good question. The reason is so that I can still reference the object after the loop.

Ended up just calling a new function that wraps together the lines of code that compute totalHits, elapsed time and aggregation data. Now I just call that one function in each conditional.

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