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

**URL:** <https://discuss.elastic.co/t/initializing-a-searchresponse-object-to-null-with-the-high-level-rest-client/227705>\
**Category:** Elasticsearch\
**Created:** [April 12, 2020, 9:26pm UTC](https://discuss.elastic.co/t/initializing-a-searchresponse-object-to-null-with-the-high-level-rest-client/227705 "2020-04-12T21:26:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gideongrossman](https://avatars.discourse-cdn.com/v4/letter/g/898d66/32.png) [@gideongrossman](https://discuss.elastic.co/u/gideongrossman)\
**Post date:** [April 12, 2020, 9:26pm UTC](https://discuss.elastic.co/t/initializing-a-searchresponse-object-to-null-with-the-high-level-rest-client/227705/1 "2020-04-12T21:26:59Z")

</div>

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](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](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-search.html#java-rest-high-search-response)

Cheers!  
-Gideon

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 13, 2020, 1:32am UTC](https://discuss.elastic.co/t/initializing-a-searchresponse-object-to-null-with-the-high-level-rest-client/227705/2 "2020-04-13T01:32:17Z")

</div>

Welcome !

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

---

<div class="post-metadata">

**Author:** ![gideongrossman](https://avatars.discourse-cdn.com/v4/letter/g/898d66/32.png) [@gideongrossman](https://discuss.elastic.co/u/gideongrossman)\
**Post date:** [April 13, 2020, 6:02am UTC](https://discuss.elastic.co/t/initializing-a-searchresponse-object-to-null-with-the-high-level-rest-client/227705/3 "2020-04-13T06:02:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 13, 2020, 8:20am UTC](https://discuss.elastic.co/t/initializing-a-searchresponse-object-to-null-with-the-high-level-rest-client/227705/4 "2020-04-13T08:20:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gideongrossman](https://avatars.discourse-cdn.com/v4/letter/g/898d66/32.png) [@gideongrossman](https://discuss.elastic.co/u/gideongrossman)\
**Post date:** [April 13, 2020, 6:29pm UTC](https://discuss.elastic.co/t/initializing-a-searchresponse-object-to-null-with-the-high-level-rest-client/227705/5 "2020-04-13T18:29:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gideongrossman](https://avatars.discourse-cdn.com/v4/letter/g/898d66/32.png) [@gideongrossman](https://discuss.elastic.co/u/gideongrossman)\
**Post date:** [April 13, 2020, 8:08pm UTC](https://discuss.elastic.co/t/initializing-a-searchresponse-object-to-null-with-the-high-level-rest-client/227705/6 "2020-04-13T20:08:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 11, 2020, 8:08pm UTC](https://discuss.elastic.co/t/initializing-a-searchresponse-object-to-null-with-the-high-level-rest-client/227705/7 "2020-05-11T20:08:14Z")

</div>

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