Skip to content

Commit 9d4602d

Browse files
authored
[8.x] Two empty mappings now are created equally (#107936) (#112794)
1 parent 8d87b1c commit 9d4602d

File tree

8 files changed

+78
-50
lines changed

8 files changed

+78
-50
lines changed

docs/changelog/107936.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 107936
2+
summary: Two empty mappings now are created equally
3+
area: Mapping
4+
type: bug
5+
issues:
6+
- 107031

rest-api-spec/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ tasks.named("yamlRestTestV7CompatTransform").configure { task ->
4747
task.skipTestsByFilePattern("**/indices.upgrade/*.yml", "upgrade api will only get a dummy endpoint returning an exception suggesting to use _reindex")
4848
task.skipTestsByFilePattern("**/indices.stats/60_field_usage/*/*.yml", "field usage results will be different between lucene versions")
4949
task.skipTestsByFilePattern("**/search.aggregation/*.yml", "run by the aggregation module")
50+
task.skipTestsByFilePattern("**/indices.get_mapping/11_basic_with_types.yml", "Empty mapping creation has changed")
5051

5152
task.skipTest("bulk/11_dynamic_templates/Dynamic templates", "Error message has changed")
5253
task.skipTest("index/80_date_nanos/date_nanos requires dates after 1970 and before 2262", "Error message has changed")
@@ -84,6 +85,7 @@ tasks.named("yamlRestTestV7CompatTransform").configure { task ->
8485
task.skipTest("search/370_profile/fetch nested source", "profile output has changed")
8586
task.skipTest("search/240_date_nanos/doc value fields are working as expected across date and date_nanos fields", "Fetching docvalues field multiple times is no longer allowed")
8687
task.skipTest("search/110_field_collapsing/field collapsing and rescore", "#107779 Field collapsing is compatible with rescore in 8.15")
88+
task.skipTest("indices.create/11_basic_with_types/Create index with mappings", "Empty mapping creation has changed")
8789

8890
task.replaceValueInMatch("_type", "_doc")
8991
task.addAllowedWarningRegex("\\[types removal\\].*")
Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,85 @@
11
---
22
setup:
33
- do:
4-
indices.create:
5-
index: test_1
6-
body:
7-
mappings: {}
4+
indices.create:
5+
index: test_1
6+
body:
7+
mappings: {}
88
- do:
9-
indices.create:
10-
index: test_2
11-
body:
12-
mappings: {}
9+
indices.create:
10+
index: test_2
11+
body:
12+
mappings: {}
1313
---
1414
"Get /{index}/_mapping with empty mappings":
1515

16-
- do:
17-
indices.create:
18-
index: t
16+
- do:
17+
indices.create:
18+
index: t
1919

20-
- do:
21-
indices.get_mapping:
22-
index: t
20+
- do:
21+
indices.get_mapping:
22+
index: t
2323

24-
- match: { t.mappings: {}}
24+
- match: { t.mappings: {}}
2525

2626
---
2727
"Get /_mapping":
2828

29-
- do:
30-
indices.get_mapping: {}
29+
- do:
30+
indices.get_mapping: {}
3131

32-
- is_true: test_1.mappings
33-
- is_true: test_2.mappings
32+
- is_true: test_1.mappings
33+
- is_true: test_2.mappings
3434

3535
---
3636
"Get /{index}/_mapping":
3737

38-
- do:
39-
indices.get_mapping:
40-
index: test_1
38+
- do:
39+
indices.get_mapping:
40+
index: test_1
4141

42-
- is_true: test_1.mappings
43-
- is_false: test_2
42+
- is_true: test_1.mappings
43+
- is_false: test_2
4444

4545

4646

4747
---
4848
"Get /_all/_mapping":
4949

50-
- do:
51-
indices.get_mapping:
52-
index: _all
50+
- do:
51+
indices.get_mapping:
52+
index: _all
5353

54-
- is_true: test_1.mappings
55-
- is_true: test_2.mappings
54+
- is_true: test_1.mappings
55+
- is_true: test_2.mappings
5656

5757
---
5858
"Get /*/_mapping":
5959

60-
- do:
61-
indices.get_mapping:
62-
index: '*'
60+
- do:
61+
indices.get_mapping:
62+
index: '*'
6363

64-
- is_true: test_1.mappings
65-
- is_true: test_2.mappings
64+
- is_true: test_1.mappings
65+
- is_true: test_2.mappings
6666

6767
---
6868
"Get /index,index/_mapping":
6969

70-
- do:
71-
indices.get_mapping:
72-
index: test_1,test_2
70+
- do:
71+
indices.get_mapping:
72+
index: test_1,test_2
7373

74-
- is_true: test_1.mappings
75-
- is_true: test_2.mappings
74+
- is_true: test_1.mappings
75+
- is_true: test_2.mappings
7676

7777
---
7878
"Get /index*/_mapping/":
7979

80-
- do:
81-
indices.get_mapping:
82-
index: '*2'
80+
- do:
81+
indices.get_mapping:
82+
index: '*2'
8383

84-
- is_true: test_2.mappings
85-
- is_false: test_1
84+
- is_true: test_2.mappings
85+
- is_false: test_1

server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/create/CreateIndexIT.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
1717
import org.elasticsearch.action.admin.indices.alias.Alias;
1818
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
19+
import org.elasticsearch.action.fieldcaps.FieldCapabilitiesRequest;
20+
import org.elasticsearch.action.fieldcaps.FieldCapabilitiesResponse;
1921
import org.elasticsearch.action.support.ActionTestUtils;
2022
import org.elasticsearch.action.support.ActiveShardCount;
2123
import org.elasticsearch.action.support.IndicesOptions;
@@ -154,6 +156,20 @@ public void testEmptyMappings() throws Exception {
154156
assertTrue(mappings.sourceAsMap().isEmpty());
155157
}
156158

159+
public void testTwoEmptyEqualMappings() throws Exception {
160+
assertAcked(prepareCreate("test1"));
161+
assertAcked(prepareCreate("test2").setMapping(XContentFactory.jsonBuilder().startObject().endObject()));
162+
FieldCapabilitiesRequest fieldCapsReq1 = new FieldCapabilitiesRequest();
163+
fieldCapsReq1.indices("test1");
164+
fieldCapsReq1.fields("*");
165+
FieldCapabilitiesResponse fieldCapsResp1 = internalCluster().coordOnlyNodeClient().fieldCaps(fieldCapsReq1).actionGet();
166+
FieldCapabilitiesRequest fieldCapsReq2 = new FieldCapabilitiesRequest();
167+
fieldCapsReq2.indices("test2");
168+
fieldCapsReq2.fields("*");
169+
FieldCapabilitiesResponse fieldCapsResp2 = internalCluster().coordOnlyNodeClient().fieldCaps(fieldCapsReq2).actionGet();
170+
assertEquals(fieldCapsResp1.get(), fieldCapsResp2.get());
171+
}
172+
157173
public void testInvalidShardCountSettings() throws Exception {
158174
int value = randomIntBetween(-10, 0);
159175
try {

server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ public class CreateIndexRequest extends AcknowledgedRequest<CreateIndexRequest>
6767
private boolean initializeFailureStore;
6868

6969
private Settings settings = Settings.EMPTY;
70+
public static final String EMPTY_MAPPINGS = "{}";
7071

71-
private String mappings = "{}";
72+
private String mappings = EMPTY_MAPPINGS;
7273

7374
private final Set<Alias> aliases = new HashSet<>();
7475

@@ -284,8 +285,11 @@ private CreateIndexRequest mapping(BytesReference source, XContentType xContentT
284285
}
285286

286287
private CreateIndexRequest mapping(String type, Map<String, ?> source) {
287-
// wrap it in a type map if its not
288-
if (source.size() != 1 || source.containsKey(type) == false) {
288+
if (source.isEmpty()) {
289+
// If no source is provided we return empty mappings
290+
return mapping(EMPTY_MAPPINGS);
291+
} else if (source.size() != 1 || source.containsKey(type) == false) {
292+
// wrap it in a type map if its not
289293
source = Map.of(MapperService.SINGLE_MAPPING_NAME, source);
290294
} else if (MapperService.SINGLE_MAPPING_NAME.equals(type) == false) {
291295
// if it has a different type name, then unwrap and rewrap with _doc

server/src/main/java/org/elasticsearch/action/admin/indices/rollover/MetadataRolloverService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ static void validate(
660660
}
661661
if ((request.settings().equals(Settings.EMPTY) == false)
662662
|| (request.aliases().size() > 0)
663-
|| (request.mappings().equals("{}") == false)) {
663+
|| (request.mappings().equals(CreateIndexRequest.EMPTY_MAPPINGS) == false)) {
664664
throw new IllegalArgumentException(
665665
"aliases, mappings, and index settings may not be specified when rolling over a data stream"
666666
);

server/src/main/java/org/elasticsearch/cluster/metadata/MetadataMigrateToDataStreamService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ static void prepareBackingIndex(
224224
Settings nodeSettings
225225
) throws IOException {
226226
MappingMetadata mm = im.mapping();
227-
if (mm == null) {
227+
if (mm == null || mm.equals(MappingMetadata.EMPTY_MAPPINGS)) {
228228
throw new IllegalArgumentException("backing index [" + im.getIndex().getName() + "] must have mappings for a timestamp field");
229229
}
230230

x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/EnrichPolicyRunner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ private static List<Map<String, Object>> toMappings(GetIndexResponse response) {
172172
private Map<String, Object> getMappings(final GetIndexResponse getIndexResponse, final String sourceIndexName) {
173173
Map<String, MappingMetadata> mappings = getIndexResponse.mappings();
174174
MappingMetadata indexMapping = mappings.get(sourceIndexName);
175-
if (indexMapping == MappingMetadata.EMPTY_MAPPINGS) {
175+
if (MappingMetadata.EMPTY_MAPPINGS.equals(indexMapping)) {
176176
throw new ElasticsearchException(
177177
"Enrich policy execution for [{}] failed. No mapping available on source [{}] included in [{}]",
178178
policyName,

0 commit comments

Comments
 (0)