# How to serialize SearchRequest?

**URL:** <https://discuss.elastic.co/t/how-to-serialize-searchrequest/282793>\
**Category:** Elasticsearch\
**Created:** [August 30, 2021, 9:30am UTC](https://discuss.elastic.co/t/how-to-serialize-searchrequest/282793 "2021-08-30T09:30:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fatcloud](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fatcloud/32/93873_2.png) [@fatcloud](https://discuss.elastic.co/u/fatcloud)\
**Post date:** [August 30, 2021, 9:30am UTC](https://discuss.elastic.co/t/how-to-serialize-searchrequest/282793/1 "2021-08-30T09:30:31Z")

</div>

Hi, I'm new to elastic search, I need to log SearchRequest and use them later for testing/analysis purpose, what's the best practice to serialize/deserialize a SearchRequest in Java?

Thanks!

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 30, 2021, 10:47am UTC](https://discuss.elastic.co/t/how-to-serialize-searchrequest/282793/2 "2021-08-30T10:47:49Z")

</div>

Hey,

can you elaborate a little? In Java you would usually go with the High Level Rest Client to prevent doing all that serialization work yourself and possibly introduce errors. If that is not an option, maybe you can elaborate a little further what you are after.

> **[Java High Level REST Client | Java REST Client \[7.14\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.14/java-rest-high.html)**

--Alex

---

<div class="post-metadata">

**Author:** ![fatcloud](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fatcloud/32/93873_2.png) [@fatcloud](https://discuss.elastic.co/u/fatcloud)\
**Post date:** [August 30, 2021, 5:18pm UTC](https://discuss.elastic.co/t/how-to-serialize-searchrequest/282793/3 "2021-08-30T17:18:59Z")

</div>

Hi! Thanks for your reply!

I'd like to log the request and will need to use them later for testing/analysis purpose, so I want to serialize the requests to strings and later deserialize them.

---

<div class="post-metadata">

**Author:** ![fatcloud](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fatcloud/32/93873_2.png) [@fatcloud](https://discuss.elastic.co/u/fatcloud)\
**Post date:** [August 30, 2021, 5:19pm UTC](https://discuss.elastic.co/t/how-to-serialize-searchrequest/282793/4 "2021-08-30T17:19:34Z")

</div>

Hi! Thanks for your reply!

I'd like to log the request and will need to use them later for testing/analysis purpose, so I want to serialize the requests to strings and later deserialize them.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 31, 2021, 9:08am UTC](https://discuss.elastic.co/t/how-to-serialize-searchrequest/282793/5 "2021-08-31T09:08:35Z")

</div>

How about setting the logging, so you log the request and can reuse it from the logs? See [Logging | Java REST Client [7.14] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.14/java-rest-low-usage-logging.html)

---

<div class="post-metadata">

**Author:** ![fatcloud](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fatcloud/32/93873_2.png) [@fatcloud](https://discuss.elastic.co/u/fatcloud)\
**Post date:** [August 31, 2021, 10:23am UTC](https://discuss.elastic.co/t/how-to-serialize-searchrequest/282793/6 "2021-08-31T10:23:10Z")

</div>

Hmm, the doc says enable request tracer log may be expensive, in addition, I want to log the request from the client side instead of in the ES server, I was thinking of some way to just serialize/deserialize the `org.elasticsearch.action.search.SearchRequest` object, by looking at the API doc, this class has [writeTo(StreamOutput out)](https://www.javadoc.io/doc/org.elasticsearch/elasticsearch/7.14.0/org/elasticsearch/action/search/SearchRequest.html#writeTo(org.elasticsearch.common.io.stream.StreamOutput)) and [SearchRequest​(StreamInput in) constructor](https://www.javadoc.io/doc/org.elasticsearch/elasticsearch/7.14.0/org/elasticsearch/action/search/SearchRequest.html#%3Cinit%3E(org.elasticsearch.common.io.stream.StreamInput)), not sure if these are supposed to be used for my case.  
The [toString](https://github.com/opensearch-project/OpenSearch/blob/0ba0e7cc26060f964fcbf6ee45bae53b3a9941d0/server/src/main/java/org/opensearch/action/search/SearchRequest.java#L732) method also seems to write the request in a json like format, I might be able to use that as well, but relying on this implementation detail seems dangerous...  
I'm wondering if there is any good practice to do this..

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [September 1, 2021, 8:33am UTC](https://discuss.elastic.co/t/how-to-serialize-searchrequest/282793/7 "2021-09-01T08:33:57Z")

</div>

The request tracer referred to above works on the client side, not the ES server.

---

<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:** [September 29, 2021, 8:34am UTC](https://discuss.elastic.co/t/how-to-serialize-searchrequest/282793/8 "2021-09-29T08:34:32Z")

</div>

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