Skip to content

Commit 6d914a2

Browse files
authored
Restore V8 REST compatibility around highlight force_source parameter (#124873)
While that was an approved breaking change, the change should have ensured v8 compatibility, so that under v8 compatibility mode the parameter is still accepted, but rejected in v9 compatibility mode (default). This commit ensures compatibility and adds a test for it.
1 parent cbfc100 commit 6d914a2

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

docs/changelog/124873.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 124873
2+
summary: Restore V8 REST compatibility around highlight `force_source` parameter
3+
area: Highlighting
4+
type: bug
5+
issues: []

server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/AbstractHighlighterBuilder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.elasticsearch.common.io.stream.StreamInput;
1818
import org.elasticsearch.common.io.stream.StreamOutput;
1919
import org.elasticsearch.common.io.stream.Writeable;
20+
import org.elasticsearch.core.RestApiVersion;
2021
import org.elasticsearch.index.query.QueryBuilder;
2122
import org.elasticsearch.index.query.Rewriteable;
2223
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.BoundaryScannerType;
@@ -64,6 +65,8 @@ public abstract class AbstractHighlighterBuilder<HB extends AbstractHighlighterB
6465
public static final ParseField TYPE_FIELD = new ParseField("type");
6566
public static final ParseField FRAGMENTER_FIELD = new ParseField("fragmenter");
6667
public static final ParseField NO_MATCH_SIZE_FIELD = new ParseField("no_match_size");
68+
public static final ParseField FORCE_SOURCE_FIELD = new ParseField("force_source").withAllDeprecated()
69+
.forRestApiVersion(restApiVersion -> restApiVersion == RestApiVersion.V_8);
6770
public static final ParseField PHRASE_LIMIT_FIELD = new ParseField("phrase_limit");
6871
public static final ParseField OPTIONS_FIELD = new ParseField("options");
6972
public static final ParseField HIGHLIGHT_QUERY_FIELD = new ParseField("highlight_query");
@@ -666,6 +669,7 @@ static <HB extends AbstractHighlighterBuilder<HB>> BiFunction<XContentParser, HB
666669
parser.declareString(HB::highlighterType, TYPE_FIELD);
667670
parser.declareString(HB::fragmenter, FRAGMENTER_FIELD);
668671
parser.declareInt(HB::noMatchSize, NO_MATCH_SIZE_FIELD);
672+
parser.declareBoolean((builder, value) -> {}, FORCE_SOURCE_FIELD); // force_source is ignored
669673
parser.declareInt(HB::phraseLimit, PHRASE_LIMIT_FIELD);
670674
parser.declareInt(HB::maxAnalyzedOffset, MAX_ANALYZED_OFFSET_FIELD);
671675
parser.declareObject(HB::options, (XContentParser p, Void c) -> {

server/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightBuilderTests.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import org.elasticsearch.common.settings.Settings;
1919
import org.elasticsearch.common.util.Maps;
2020
import org.elasticsearch.common.util.set.Sets;
21+
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
22+
import org.elasticsearch.core.RestApiVersion;
2123
import org.elasticsearch.index.Index;
2224
import org.elasticsearch.index.IndexSettings;
2325
import org.elasticsearch.index.IndexVersion;
@@ -46,6 +48,7 @@
4648
import org.elasticsearch.xcontent.XContentFactory;
4749
import org.elasticsearch.xcontent.XContentParseException;
4850
import org.elasticsearch.xcontent.XContentParser;
51+
import org.elasticsearch.xcontent.XContentParserConfiguration;
4952
import org.elasticsearch.xcontent.XContentType;
5053
import org.elasticsearch.xcontent.json.JsonXContent;
5154
import org.junit.AfterClass;
@@ -607,6 +610,36 @@ public void testOrderSerialization() throws Exception {
607610
}
608611
}
609612

613+
public void testForceSourceRemovedInV9() throws IOException {
614+
String highlightJson = """
615+
{ "fields" : { }, "force_source" : true }
616+
""";
617+
618+
XContentParserConfiguration config = XContentParserConfiguration.EMPTY.withRegistry(xContentRegistry())
619+
.withDeprecationHandler(LoggingDeprecationHandler.INSTANCE)
620+
.withRestApiVersion(RestApiVersion.V_9);
621+
try (XContentParser parser = JsonXContent.jsonXContent.createParser(config, highlightJson)) {
622+
XContentParseException xContentParseException = expectThrows(
623+
XContentParseException.class,
624+
() -> HighlightBuilder.fromXContent(parser)
625+
);
626+
assertThat(xContentParseException.getMessage(), containsString("unknown field [force_source]"));
627+
}
628+
}
629+
630+
public void testForceSourceV8Comp() throws IOException {
631+
String highlightJson = """
632+
{ "fields" : { }, "force_source" : true }
633+
""";
634+
XContentParserConfiguration config = XContentParserConfiguration.EMPTY.withRegistry(xContentRegistry())
635+
.withDeprecationHandler(LoggingDeprecationHandler.INSTANCE)
636+
.withRestApiVersion(RestApiVersion.V_8);
637+
try (XContentParser parser = JsonXContent.jsonXContent.createParser(config, highlightJson)) {
638+
HighlightBuilder.fromXContent(parser);
639+
assertWarnings("Deprecated field [force_source] used, this field is unused and will be removed entirely");
640+
}
641+
}
642+
610643
protected static XContentBuilder toXContent(HighlightBuilder highlight, XContentType contentType) throws IOException {
611644
XContentBuilder builder = XContentFactory.contentBuilder(contentType);
612645
if (randomBoolean()) {

0 commit comments

Comments
 (0)