Skip to content

Commit

Permalink
Fix queries losing inherited data when toBuilder is called. (opensear…
Browse files Browse the repository at this point in the history
…ch-project#1181)

* Fixes opensearch-project#1172

Calling `toBuilder` on classes which implement `QueryBase` will now preserve `boost` and `queryName`.

Signed-off-by: Chris Danisch <[email protected]>

* Updated changelog

Signed-off-by: Chris Danisch <[email protected]>

* Added toBuilder to QueryBase instead.

Signed-off-by: Chris Danisch <[email protected]>

---------

Signed-off-by: Chris Danisch <[email protected]>
(cherry picked from commit 020e0b0)
  • Loading branch information
PiSoup authored and Xtansia committed Sep 4, 2024
1 parent 13fa4bf commit c93672c
Show file tree
Hide file tree
Showing 58 changed files with 70 additions and 56 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Removed

### Fixed
- Fix queries not preserving boost and name when converted to builders ([#1181](https://github.com/opensearch-project/opensearch-java/pull/1181))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().filter(filter).minimumShouldMatch(minimumShouldMatch).must(must).mustNot(mustNot).should(should);
return toBuilder(new Builder()).filter(filter).minimumShouldMatch(minimumShouldMatch).must(must).mustNot(mustNot).should(should);
}

// ---------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().negativeBoost(negativeBoost).negative(negative).positive(positive);
return toBuilder(new Builder()).negativeBoost(negativeBoost).negative(negative).positive(positive);
}

// ---------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().fields(fields)
return toBuilder(new Builder()).fields(fields)
.query(query)
.autoGenerateSynonymsPhraseQuery(autoGenerateSynonymsPhraseQuery)
.operator(operator)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().field(field)
return toBuilder(new Builder()).field(field)
.analyzer(analyzer)
.cutoffFrequency(cutoffFrequency)
.highFreqOperator(highFreqOperator)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().filter(filter);
return toBuilder(new Builder()).filter(filter);
}

// ---------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().queries(queries).tieBreaker(tieBreaker);
return toBuilder(new Builder()).queries(queries).tieBreaker(tieBreaker);
}

// ---------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().origin(origin).pivot(pivot).field(field);
return toBuilder(new Builder()).origin(origin).pivot(pivot).field(field);
}

// ---------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().field(field);
return toBuilder(new Builder()).field(field);
}

// ---------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().boostMode(boostMode)
return toBuilder(new Builder()).boostMode(boostMode)
.functions(functions)
.maxBoost(maxBoost)
.minScore(minScore)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().field(field)
return toBuilder(new Builder()).field(field)
.value(value)
.maxExpansions(maxExpansions)
.prefixLength(prefixLength)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().field(field)
return toBuilder(new Builder()).field(field)
.boundingBox(boundingBox)
.type(type)
.validationMethod(validationMethod)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().field(field).location(location);
return toBuilder(new Builder()).field(field).location(location);
}

// ---------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().field(field).shape(shape);
return toBuilder(new Builder()).field(field).shape(shape);
}

// ---------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().ignoreUnmapped(ignoreUnmapped)
return toBuilder(new Builder()).ignoreUnmapped(ignoreUnmapped)
.innerHits(innerHits)
.maxChildren(maxChildren)
.minChildren(minChildren)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().ignoreUnmapped(ignoreUnmapped).innerHits(innerHits).parentType(parentType).query(query).score(score);
return toBuilder(new Builder()).ignoreUnmapped(ignoreUnmapped)
.innerHits(innerHits)
.parentType(parentType)
.query(query)
.score(score);
}

// ---------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public Query.Kind _queryKind() {
}

public HybridQuery.Builder toBuilder() {
return new HybridQuery.Builder().queries(queries);
return toBuilder(new Builder()).queries(queries);
}

public static class Builder extends QueryBase.AbstractBuilder<HybridQuery.Builder> implements ObjectBuilder<HybridQuery> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().values(values);
return toBuilder(new Builder()).values(values);
}

// ---------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public void serialize(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder()._kind(_kind)._value(_value).field(field);
return toBuilder(new Builder())._kind(_kind)._value(_value).field(field);
}

public static class Builder extends QueryBase.AbstractBuilder<Builder> implements ObjectBuilder<IntervalsQuery> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().field(field).vector(vector).k(k).filter(filter);
return toBuilder(new Builder()).field(field).vector(vector).k(k).filter(filter);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().field(field)
return toBuilder(new Builder()).field(field)
.analyzer(analyzer)
.fuzziness(fuzziness)
.fuzzyRewrite(fuzzyRewrite)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().field(field)
return toBuilder(new Builder()).field(field)
.analyzer(analyzer)
.maxExpansions(maxExpansions)
.query(query)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().field(field).analyzer(analyzer).query(query).slop(slop).zeroTermsQuery(zeroTermsQuery);
return toBuilder(new Builder()).field(field).analyzer(analyzer).query(query).slop(slop).zeroTermsQuery(zeroTermsQuery);
}

// ---------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().field(field)
return toBuilder(new Builder()).field(field)
.analyzer(analyzer)
.autoGenerateSynonymsPhraseQuery(autoGenerateSynonymsPhraseQuery)
.cutoffFrequency(cutoffFrequency)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().analyzer(analyzer)
return toBuilder(new Builder()).analyzer(analyzer)
.boostTerms(boostTerms)
.failOnUnsupportedField(failOnUnsupportedField)
.fields(fields)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().analyzer(analyzer)
return toBuilder(new Builder()).analyzer(analyzer)
.autoGenerateSynonymsPhraseQuery(autoGenerateSynonymsPhraseQuery)
.cutoffFrequency(cutoffFrequency)
.fields(fields)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().ignoreUnmapped(ignoreUnmapped).innerHits(innerHits).path(path).query(query).scoreMode(scoreMode);
return toBuilder(new Builder()).ignoreUnmapped(ignoreUnmapped).innerHits(innerHits).path(path).query(query).scoreMode(scoreMode);
}

// ---------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().field(field).queryText(queryText).queryImage(queryImage).k(k).modelId(modelId).filter(filter);
return toBuilder(new Builder()).field(field).queryText(queryText).queryImage(queryImage).k(k).modelId(modelId).filter(filter);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().id(id).ignoreUnmapped(ignoreUnmapped).type(type);
return toBuilder(new Builder()).id(id).ignoreUnmapped(ignoreUnmapped).type(type);
}

// ---------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().document(document)
return toBuilder(new Builder()).document(document)
.documents(documents)
.field(field)
.id(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public void serialize(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder()._kind(_kind)._value(_value).organic(organic);
return toBuilder(new Builder())._kind(_kind)._value(_value).organic(organic);
}

public static class Builder extends QueryBase.AbstractBuilder<Builder> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().field(field).value(value).rewrite(rewrite).value(value).caseInsensitive(caseInsensitive);
return toBuilder(new Builder()).field(field).value(value).rewrite(rewrite).value(value).caseInsensitive(caseInsensitive);
}

// ---------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {

}

protected <BuilderT extends AbstractBuilder<BuilderT>> BuilderT toBuilder(BuilderT builder) {
return builder.queryName(queryName).boost(boost);
}

protected abstract static class AbstractBuilder<BuilderT extends AbstractBuilder<BuilderT>> extends ObjectBuilderBase {
@Nullable
private Float boost;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().allowLeadingWildcard(allowLeadingWildcard)
return toBuilder(new Builder()).allowLeadingWildcard(allowLeadingWildcard)
.analyzer(analyzer)
.analyzeWildcard(analyzeWildcard)
.autoGenerateSynonymsPhraseQuery(autoGenerateSynonymsPhraseQuery)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().field(field).gt(gt).gte(gte).lt(lt).lte(lte).from(from).to(to).format(format).timeZone(timeZone);
return toBuilder(new Builder()).field(field).gt(gt).gte(gte).lt(lt).lte(lte).from(from).to(to).format(format).timeZone(timeZone);
}

// ---------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().field(field).saturation(saturation).log(log).linear(linear).sigmoid(sigmoid);
return toBuilder(new Builder()).field(field).saturation(saturation).log(log).linear(linear).sigmoid(sigmoid);
}

// ---------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().field(field)
return toBuilder(new Builder()).field(field)
.value(value)
.caseInsensitive(caseInsensitive)
.flags(flags)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().script(script);
return toBuilder(new Builder()).script(script);
}

// ---------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().minScore(minScore).query(query).script(script);
return toBuilder(new Builder()).minScore(minScore).query(query).script(script);
}

// ---------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().analyzer(analyzer)
return toBuilder(new Builder()).analyzer(analyzer)
.analyzeWildcard(analyzeWildcard)
.autoGenerateSynonymsPhraseQuery(autoGenerateSynonymsPhraseQuery)
.defaultOperator(defaultOperator)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().big(big).little(little);
return toBuilder(new Builder()).big(big).little(little);
}

// ---------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().field(field).query(query);
return toBuilder(new Builder()).field(field).query(query);
}

// ---------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
}

public Builder toBuilder() {
return new Builder().end(end).match(match);
return toBuilder(new Builder()).end(end).match(match);
}

// ---------------------------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit c93672c

Please sign in to comment.