# How to serialize elastic4s queries for reuse?

**URL:** <https://discuss.elastic.co/t/how-to-serialize-elastic4s-queries-for-reuse/27220>\
**Category:** Elasticsearch\
**Created:** [August 11, 2015, 7:41pm UTC](https://discuss.elastic.co/t/how-to-serialize-elastic4s-queries-for-reuse/27220 "2015-08-11T19:41:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sergioasantiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sergioasantiago/32/4145_2.png) [@sergioasantiago](https://discuss.elastic.co/u/sergioasantiago)\
**Post date:** [August 11, 2015, 7:41pm UTC](https://discuss.elastic.co/t/how-to-serialize-elastic4s-queries-for-reuse/27220/1 "2015-08-11T19:41:15Z")

</div>

I'm using elastic4s to query elastic search, and now I need to cache this query to be used later for pagination purposes.

The query built process is a heavy calculation that is why I need the final query it self save in some local cache (i.e redis) to be retrieved and used later.

The problem I'm facing is, how to serialize or convert to string this query in a way that I can later deserialize and reuse.

Currently this is the approach I'm using:

**Storing:**

```
var searchDefinition = search.in(s"$indexName/$docType").query2(buildQuery)
Cache.save(key, searchDefinition._builder.toString)

```

**Restoring**

```
val cachedQuery = Cache.restore(key)
val searchDefinition = search.in(s"$indexName/$docType")
searchDefinition._builder.setQuery(cachedQuery)

```

The problem is:

Query rebuilt after restored from cache is broken. How it looks like:

```
Query: {
    "from" : 0,
    "size" : 20,
    "query":{
        "query" : {
            "function_score" : {
            ...

```

Note the double "query" tag.

So as it is not so clear in documentation, does anybody has a thought to work it around?

PS: I've posted in StackOverflow as well: [http://stackoverflow.com/questions/31943172/how-to-serialize-elastic4s-queries-for-reuse](http://stackoverflow.com/questions/31943172/how-to-serialize-elastic4s-queries-for-reuse)

Thanks!

---

<div class="post-metadata">

**Author:** ![sergioasantiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sergioasantiago/32/4145_2.png) [@sergioasantiago](https://discuss.elastic.co/u/sergioasantiago)\
**Post date:** [August 12, 2015, 9:01pm UTC](https://discuss.elastic.co/t/how-to-serialize-elastic4s-queries-for-reuse/27220/2 "2015-08-12T21:01:58Z")

</div>

Finally discover the problem. Query returned by

```
searchDefinition._builder.toString

```

return as well the key:

```
"query" : {
    ...
}

```

I could manage to solve the problem sanitizing the query string by removing the extra query string manually and then call:

```
val cachedQuery = Cache.restore(key)
search.in(s"$indexName/$docType").rawQuery(cachedQuery)

```

Worked like a charm!

---

<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 5, 2017, 11:56pm UTC](https://discuss.elastic.co/t/how-to-serialize-elastic4s-queries-for-reuse/27220/3 "2017-07-05T23:56:08Z")

</div>


