Skip to content

Commit

Permalink
Two empty mappings now are created equally (elastic#107936)
Browse files Browse the repository at this point in the history
* Two empty mappings reported equally in field_caps

* empty mapping now is {}

* iter

(cherry picked from commit cd8d37b)
  • Loading branch information
piergm committed Sep 12, 2024
1 parent 6067464 commit e9c4d81
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 50 deletions.
6 changes: 6 additions & 0 deletions docs/changelog/107936.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 107936
summary: Two empty mappings now are created equally
area: Mapping
type: bug
issues:
- 107031
Original file line number Diff line number Diff line change
@@ -1,85 +1,85 @@
---
setup:
- do:
indices.create:
index: test_1
body:
mappings: {}
indices.create:
index: test_1
body:
mappings: {}
- do:
indices.create:
index: test_2
body:
mappings: {}
indices.create:
index: test_2
body:
mappings: {}
---
"Get /{index}/_mapping with empty mappings":

- do:
indices.create:
index: t
- do:
indices.create:
index: t

- do:
indices.get_mapping:
index: t
- do:
indices.get_mapping:
index: t

- match: { t.mappings: {}}
- match: { t.mappings: {}}

---
"Get /_mapping":

- do:
indices.get_mapping: {}
- do:
indices.get_mapping: {}

- is_true: test_1.mappings
- is_true: test_2.mappings
- is_true: test_1.mappings
- is_true: test_2.mappings

---
"Get /{index}/_mapping":

- do:
indices.get_mapping:
index: test_1
- do:
indices.get_mapping:
index: test_1

- is_true: test_1.mappings
- is_false: test_2
- is_true: test_1.mappings
- is_false: test_2



---
"Get /_all/_mapping":

- do:
indices.get_mapping:
index: _all
- do:
indices.get_mapping:
index: _all

- is_true: test_1.mappings
- is_true: test_2.mappings
- is_true: test_1.mappings
- is_true: test_2.mappings

---
"Get /*/_mapping":

- do:
indices.get_mapping:
index: '*'
- do:
indices.get_mapping:
index: '*'

- is_true: test_1.mappings
- is_true: test_2.mappings
- is_true: test_1.mappings
- is_true: test_2.mappings

---
"Get /index,index/_mapping":

- do:
indices.get_mapping:
index: test_1,test_2
- do:
indices.get_mapping:
index: test_1,test_2

- is_true: test_1.mappings
- is_true: test_2.mappings
- is_true: test_1.mappings
- is_true: test_2.mappings

---
"Get /index*/_mapping/":

- do:
indices.get_mapping:
index: '*2'
- do:
indices.get_mapping:
index: '*2'

- is_true: test_2.mappings
- is_false: test_1
- is_true: test_2.mappings
- is_false: test_1
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
import org.elasticsearch.action.fieldcaps.FieldCapabilitiesRequest;
import org.elasticsearch.action.fieldcaps.FieldCapabilitiesResponse;
import org.elasticsearch.action.support.ActionTestUtils;
import org.elasticsearch.action.support.ActiveShardCount;
import org.elasticsearch.action.support.IndicesOptions;
Expand Down Expand Up @@ -154,6 +156,20 @@ public void testEmptyMappings() throws Exception {
assertTrue(mappings.sourceAsMap().isEmpty());
}

public void testTwoEmptyEqualMappings() throws Exception {
assertAcked(prepareCreate("test1"));
assertAcked(prepareCreate("test2").setMapping(XContentFactory.jsonBuilder().startObject().endObject()));
FieldCapabilitiesRequest fieldCapsReq1 = new FieldCapabilitiesRequest();
fieldCapsReq1.indices("test1");
fieldCapsReq1.fields("*");
FieldCapabilitiesResponse fieldCapsResp1 = internalCluster().coordOnlyNodeClient().fieldCaps(fieldCapsReq1).actionGet();
FieldCapabilitiesRequest fieldCapsReq2 = new FieldCapabilitiesRequest();
fieldCapsReq2.indices("test2");
fieldCapsReq2.fields("*");
FieldCapabilitiesResponse fieldCapsResp2 = internalCluster().coordOnlyNodeClient().fieldCaps(fieldCapsReq2).actionGet();
assertEquals(fieldCapsResp1.get(), fieldCapsResp2.get());
}

public void testInvalidShardCountSettings() throws Exception {
int value = randomIntBetween(-10, 0);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ public class CreateIndexRequest extends AcknowledgedRequest<CreateIndexRequest>
private boolean initializeFailureStore;

private Settings settings = Settings.EMPTY;
public static final String EMPTY_MAPPINGS = "{}";

private String mappings = "{}";
private String mappings = EMPTY_MAPPINGS;

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

Expand Down Expand Up @@ -284,8 +285,11 @@ private CreateIndexRequest mapping(BytesReference source, XContentType xContentT
}

private CreateIndexRequest mapping(String type, Map<String, ?> source) {
// wrap it in a type map if its not
if (source.size() != 1 || source.containsKey(type) == false) {
if (source.isEmpty()) {
// If no source is provided we return empty mappings
return mapping(EMPTY_MAPPINGS);
} else if (source.size() != 1 || source.containsKey(type) == false) {
// wrap it in a type map if its not
source = Map.of(MapperService.SINGLE_MAPPING_NAME, source);
} else if (MapperService.SINGLE_MAPPING_NAME.equals(type) == false) {
// if it has a different type name, then unwrap and rewrap with _doc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ static void validate(
}
if ((request.settings().equals(Settings.EMPTY) == false)
|| (request.aliases().size() > 0)
|| (request.mappings().equals("{}") == false)) {
|| (request.mappings().equals(CreateIndexRequest.EMPTY_MAPPINGS) == false)) {
throw new IllegalArgumentException(
"aliases, mappings, and index settings may not be specified when rolling over a data stream"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ static void prepareBackingIndex(
Settings nodeSettings
) throws IOException {
MappingMetadata mm = im.mapping();
if (mm == null) {
if (mm == null || mm.equals(MappingMetadata.EMPTY_MAPPINGS)) {
throw new IllegalArgumentException("backing index [" + im.getIndex().getName() + "] must have mappings for a timestamp field");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private static List<Map<String, Object>> toMappings(GetIndexResponse response) {
private Map<String, Object> getMappings(final GetIndexResponse getIndexResponse, final String sourceIndexName) {
Map<String, MappingMetadata> mappings = getIndexResponse.mappings();
MappingMetadata indexMapping = mappings.get(sourceIndexName);
if (indexMapping == MappingMetadata.EMPTY_MAPPINGS) {
if (MappingMetadata.EMPTY_MAPPINGS.equals(indexMapping)) {
throw new ElasticsearchException(
"Enrich policy execution for [{}] failed. No mapping available on source [{}] included in [{}]",
policyName,
Expand Down

0 comments on commit e9c4d81

Please sign in to comment.