Home > Net >  How to deep copy BoolQueryBuilder object in Java rest high level client?
How to deep copy BoolQueryBuilder object in Java rest high level client?

Time:12-24

I am trying to generate a new instance out of my existing instance of BoolQueryBuilder. I have tried the following approach but could not find any proper solution.

...

BoolQueryBuilder mQuery = QueryBuilder.boolQuery();
...

BytesStreamOutput output = new BytesStreamOutput();
mQuery.writeTo(output);
StreamInput input = output.bytes().streamInput();
BoolQueryBuilder pQuery= new BoolQueryBuilder(input);

The above method gives me java.lang.UnsupportedOperationException: can't read named writeable from StreamInput

CodePudding user response:

There is no direct method given as known which will help to clone the existing object. However, here is a method you can use to copy your existing BoolQueryBuilder object.

public BoolQueryBuilder createCopy(BoolQueryBuilder oldQuery) {
    BoolQueryBuilder builder = new BoolQueryBuilder();
    oldQuery.must().forEach(builder::must);
    oldQuery.mustNot().forEach(builder::mustNot);
    oldQuery.filter().forEach(builder::filter);
    oldQuery.should().forEach(builder::should);
    builder.minimumShouldMatch(oldQuery.minimumShouldMatch());
    builder.adjustPureNegative(oldQuery.adjustPureNegative());
    return builder;
}
  • Related