Skip to content

Commit 87ee045

Browse files
authored
Avoid duplicate _type fields in v7 compat layer (#83239)
Relates #83195 (comment)
1 parent 878a445 commit 87ee045

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

server/src/main/java/org/elasticsearch/search/SearchHit.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ public XContentBuilder toInnerXContent(XContentBuilder builder, Params params) t
624624
if (index != null) {
625625
builder.field(Fields._INDEX, RemoteClusterAware.buildRemoteIndexName(clusterAlias, index));
626626
}
627-
if (builder.getRestApiVersion() == RestApiVersion.V_7) {
627+
if (builder.getRestApiVersion() == RestApiVersion.V_7 && metaFields.containsKey(MapperService.TYPE_FIELD_NAME) == false) {
628628
builder.field(MapperService.TYPE_FIELD_NAME, MapperService.SINGLE_MAPPING_NAME);
629629
}
630630
if (id != null) {

x-pack/qa/repository-old-versions/src/test/java/org/elasticsearch/oldrepos/OldRepositoryAccessIT.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,14 @@ private void assertDocs(
395395
boolean sourceOnlyRepository,
396396
Version oldVersion
397397
) throws IOException {
398+
RequestOptions v7RequestOptions = RequestOptions.DEFAULT.toBuilder()
399+
.addHeader("Content-Type", "application/vnd.elasticsearch+json;compatible-with=7")
400+
.addHeader("Accept", "application/vnd.elasticsearch+json;compatible-with=7")
401+
.build();
402+
RequestOptions randomRequestOptions = randomBoolean() ? RequestOptions.DEFAULT : v7RequestOptions;
403+
398404
// run a search against the index
399-
SearchResponse searchResponse = client.search(new SearchRequest(index), RequestOptions.DEFAULT);
405+
SearchResponse searchResponse = client.search(new SearchRequest(index), randomRequestOptions);
400406
logger.info(searchResponse);
401407
// check hit count
402408
assertEquals(numDocs, searchResponse.getHits().getTotalHits().value);
@@ -420,7 +426,7 @@ private void assertDocs(
420426
.query(QueryBuilders.matchQuery("val", num))
421427
.runtimeMappings(Map.of("val", Map.of("type", "long")))
422428
),
423-
RequestOptions.DEFAULT
429+
randomRequestOptions
424430
);
425431
logger.info(searchResponse);
426432
assertEquals(1, searchResponse.getHits().getTotalHits().value);
@@ -445,7 +451,7 @@ private void assertDocs(
445451
.query(QueryBuilders.matchAllQuery())
446452
.sort(SortBuilders.fieldSort("val").order(SortOrder.DESC))
447453
),
448-
RequestOptions.DEFAULT
454+
randomRequestOptions
449455
);
450456
logger.info(searchResponse);
451457
// check sort order
@@ -460,7 +466,7 @@ private void assertDocs(
460466
long typeCount = expectedIds.stream().filter(idd -> getType(oldVersion, idd).equals(randomType)).count();
461467
searchResponse = client.search(
462468
new SearchRequest(index).source(SearchSourceBuilder.searchSource().query(QueryBuilders.termQuery("_type", randomType))),
463-
RequestOptions.DEFAULT
469+
randomRequestOptions
464470
);
465471
logger.info(searchResponse);
466472
assertEquals(typeCount, searchResponse.getHits().getTotalHits().value);

0 commit comments

Comments
 (0)