# Java API: Reconstruct QueryBuilder object from query String/byte array

**URL:** <https://discuss.elastic.co/t/java-api-reconstruct-querybuilder-object-from-query-string-byte-array/4598>\
**Category:** Elasticsearch\
**Created:** [June 10, 2011, 10:56am UTC](https://discuss.elastic.co/t/java-api-reconstruct-querybuilder-object-from-query-string-byte-array/4598 "2011-06-10T10:56:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Felix\_S](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felix_s/32/2933_2.png) [@Felix\_S](https://discuss.elastic.co/u/Felix_S)\
**Post date:** [June 10, 2011, 10:56am UTC](https://discuss.elastic.co/t/java-api-reconstruct-querybuilder-object-from-query-string-byte-array/4598/1 "2011-06-10T10:56:14Z")

</div>

Hi!

I have a library of ElasticSearch queries, which I need to use as  
building blocks to construct other, larger, queries. This works just  
fine as long as I retain Java TermQueryBuilder objects (et cetera),  
but since I need to store the library, I have to convert them to byte  
arrays (which I use to construct Strings, as my database won't store  
arrays); I can't find another way to do store them permanently, as  
query builders aren't directly serializable. It is important to keep  
the ability to use the stored queries as clauses etc., and this I  
can't seem to find a way to do.

Is there a way to re-parse a query byte array (or String) into a query  
builder object? I realize I could generate the JSON for the big  
queries myself, but I need quite a few types of queries and that would  
mean quite a lot of effort to essentially duplicate functionality that  
ES already appears to have (after all, it does parse queries that i  
curl to it...)

Thanks  
Felix

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [June 10, 2011, 2:56pm UTC](https://discuss.elastic.co/t/java-api-reconstruct-querybuilder-object-from-query-string-byte-array/4598/2 "2011-06-10T14:56:46Z")

</div>

The way ES is structured, then the parsing phase of queries is at a "different level" then the generation part (it is associated with a concrete index, to allow for plugins adding custom query parser).

On Friday, June 10, 2011 at 1:56 PM, Felix wrote:

> Hi!
> 
> I have a library of Elasticsearch queries, which I need to use as  
> building blocks to construct other, larger, queries. This works just  
> fine as long as I retain Java TermQueryBuilder objects (et cetera),  
> but since I need to store the library, I have to convert them to byte  
> arrays (which I use to construct Strings, as my database won't store  
> arrays); I can't find another way to do store them permanently, as  
> query builders aren't directly serializable. It is important to keep  
> the ability to use the stored queries as clauses etc., and this I  
> can't seem to find a way to do.
> 
> Is there a way to re-parse a query byte array (or String) into a query  
> builder object? I realize I could generate the JSON for the big  
> queries myself, but I need quite a few types of queries and that would  
> mean quite a lot of effort to essentially duplicate functionality that  
> ES already appears to have (after all, it does parse queries that i  
> curl to it...)
> 
> Thanks  
> Felix

---

<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:** [July 6, 2017, 4:03am UTC](https://discuss.elastic.co/t/java-api-reconstruct-querybuilder-object-from-query-string-byte-array/4598/3 "2017-07-06T04:03:52Z")

</div>


