# Cloning QueryBuilder

**URL:** <https://discuss.elastic.co/t/cloning-querybuilder/132680>\
**Category:** Elasticsearch\
**Created:** [May 21, 2018, 4:56pm UTC](https://discuss.elastic.co/t/cloning-querybuilder/132680 "2018-05-21T16:56:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdpark](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@jdpark](https://discuss.elastic.co/u/jdpark)\
**Post date:** [May 21, 2018, 4:56pm UTC](https://discuss.elastic.co/t/cloning-querybuilder/132680/1 "2018-05-21T16:56:31Z")

</div>

Hello,

What would be the best way to clone an AbtractQueryBuilder? I have a BoolQueryBuilder that I'd like to use as a filter as part of another query. I noticed that when I compose into another query later modify it, the formerly composed objects are also modified. But I'd like to make it stay at the time of composition. I figure cloning is one way to resolve this.

Looking at the Javadocs, it looks like copying may be possible with

- AbstractQueryBuilder.toXContent(...) and .fromXContent(...)
- AbstractQueryBuilder.toString() then use QueryBuilders.wrapperQuery(...)

But I'm not sure if this is the best way as I think this would serialize and deserialize unnecessarily.

Thank you.

---

<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:** [June 18, 2018, 4:56pm UTC](https://discuss.elastic.co/t/cloning-querybuilder/132680/2 "2018-06-18T16:56:32Z")

</div>

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