Skip to content

Commit 8e87113

Browse files
committed
[Rest Api Compatibility] Indicies boost in object format
Previouslly removed elastic#55078 indices_boost field is available when used with rest api compatibility relates elastic#51816
1 parent c412aae commit 8e87113

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

rest-api-spec/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ tasks.named("yamlRestCompatTest").configure {
105105
'mtermvectors/21_deprecated_with_types/Deprecated camel case and _ parameters should fail in Term Vectors query',
106106
'mtermvectors/30_mix_typeless_typeful/mtermvectors without types on an index that has types',
107107
'search/10_source_filtering/docvalue_fields with default format', //use_field_mapping change
108-
'search/40_indices_boost/Indices boost using object', //indices_boost
109108
'search/150_rewrite_on_coordinator/Ensure that we fetch the document only once', //terms_lookup
110109
'search/171_terms_query_with_types/Terms Query with No.of terms exceeding index.max_terms_count should FAIL', //bulk
111110
'search/260_parameter_validation/test size=-1 is deprecated', //size=-1 change

server/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import org.elasticsearch.ElasticsearchException;
1212
import org.elasticsearch.Version;
13+
import org.elasticsearch.common.logging.DeprecationLogger;
1314
import org.elasticsearch.core.Booleans;
1415
import org.elasticsearch.core.Nullable;
1516
import org.elasticsearch.common.xcontent.ParseField;
@@ -18,6 +19,7 @@
1819
import org.elasticsearch.common.io.stream.StreamInput;
1920
import org.elasticsearch.common.io.stream.StreamOutput;
2021
import org.elasticsearch.common.io.stream.Writeable;
22+
import org.elasticsearch.core.RestApiVersion;
2123
import org.elasticsearch.core.TimeValue;
2224
import org.elasticsearch.common.xcontent.ToXContentFragment;
2325
import org.elasticsearch.common.xcontent.ToXContentObject;
@@ -68,6 +70,8 @@
6870
* @see org.elasticsearch.action.search.SearchRequest#source(SearchSourceBuilder)
6971
*/
7072
public final class SearchSourceBuilder implements Writeable, ToXContentObject, Rewriteable<SearchSourceBuilder> {
73+
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(SearchSourceBuilder.class);
74+
7175
public static final ParseField FROM_FIELD = new ParseField("from");
7276
public static final ParseField SIZE_FIELD = new ParseField("size");
7377
public static final ParseField TIMEOUT_FIELD = new ParseField("timeout");
@@ -1164,6 +1168,20 @@ public void parseXContent(XContentParser parser, boolean checkTrailingTokens) th
11641168
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
11651169
scriptFields.add(new ScriptField(parser));
11661170
}
1171+
} else if (parser.getRestApiVersion() == RestApiVersion.V_7 &&
1172+
INDICES_BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
1173+
deprecationLogger.compatibleApiWarning("indices_boost_object_format",
1174+
"Object format in indices_boost is deprecated, please use array format instead");
1175+
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
1176+
if (token == XContentParser.Token.FIELD_NAME) {
1177+
currentFieldName = parser.currentName();
1178+
} else if (token.isValue()) {
1179+
indexBoosts.add(new IndexBoost(currentFieldName, parser.floatValue()));
1180+
} else {
1181+
throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token +
1182+
" in [" + currentFieldName + "].", parser.getTokenLocation());
1183+
}
1184+
}
11671185
} else if (AGGREGATIONS_FIELD.match(currentFieldName, parser.getDeprecationHandler())
11681186
|| AGGS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
11691187
aggregations = AggregatorFactories.parseAggregators(parser);

server/src/test/java/org/elasticsearch/search/builder/SearchSourceBuilderTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,17 @@ public void testToXContentWithPointInTime() throws IOException {
400400
}
401401

402402
public void testParseIndicesBoost() throws IOException {
403+
{
404+
String restContent = " { \"indices_boost\": {\"foo\": 1.0, \"bar\": 2.0}}";
405+
try (XContentParser parser = createParser(JsonXContent.jsonXContent, restContent)) {
406+
SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.fromXContent(parser);
407+
assertEquals(2, searchSourceBuilder.indexBoosts().size());
408+
assertEquals(new SearchSourceBuilder.IndexBoost("foo", 1.0f), searchSourceBuilder.indexBoosts().get(0));
409+
assertEquals(new SearchSourceBuilder.IndexBoost("bar", 2.0f), searchSourceBuilder.indexBoosts().get(1));
410+
assertWarnings("Object format in indices_boost is deprecated, please use array format instead");
411+
}
412+
}
413+
403414
{
404415
String restContent = "{" +
405416
" \"indices_boost\" : [\n" +

0 commit comments

Comments
 (0)