Skip to content

Commit

Permalink
Increase test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Gao Binlong <gbinlong@amazon.com>
  • Loading branch information
gaobinlong committed Aug 13, 2024
1 parent 456e8ba commit 9495dae
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 87 deletions.
10 changes: 10 additions & 0 deletions server/src/test/java/org/opensearch/index/IndexSettingsTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,16 @@ public void testUnmapFieldsBeyondTotalFieldsLimitSetting() {
);
settings = new IndexSettings(metadata, Settings.EMPTY);
assertTrue(settings.getUnmapFieldsBeyondTotalFieldsLimit());

Settings.Builder newSettings = Settings.builder()
.put(
Settings.builder()
.put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)
.put(INDEX_MAPPING_TOTAL_FIELDS_UNMAP_FIELDS_BEYONGD_LIMIT_SETTING.getKey(), false)
.build()
);
settings.updateIndexMetadata(newIndexMeta("index", newSettings.build()));
assertFalse(settings.getUnmapFieldsBeyondTotalFieldsLimit());
}

public void testRemoteTranslogStoreDefaultSetting() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1201,85 +1201,166 @@ public void testDynamicStrictAllowTemplatesValue() throws Exception {
}

public void testDynamicValueWithUnmapFieldsBeyondTotalLimit() throws Exception {
Settings settings = Settings.builder()
.put(MapperService.INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING.getKey(), 3)
.put(MapperService.INDEX_MAPPING_TOTAL_FIELDS_UNMAP_FIELDS_BEYONGD_LIMIT_SETTING.getKey(), true)
.build();
DocumentMapper mapper = createDocumentMapper(topMapping(b -> {
b.field("dynamic", "true");
b.startObject("properties");
{
b.startObject("foo");
b.field("type", "keyword");
b.endObject();
b.startObject("bar");
b.field("type", "keyword");
b.endObject();
b.startObject("zoo");
b.field("type", "keyword");
{
Settings settings = Settings.builder()
.put(MapperService.INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING.getKey(), 3)
.put(MapperService.INDEX_MAPPING_TOTAL_FIELDS_UNMAP_FIELDS_BEYONGD_LIMIT_SETTING.getKey(), true)
.build();
DocumentMapper mapper = createDocumentMapper(topMapping(b -> {
b.field("dynamic", "true");
b.startObject("properties");
{
b.startObject("foo");
b.field("type", "keyword");
b.endObject();
b.startObject("bar");
b.field("type", "keyword");
b.endObject();
b.startObject("zoo");
b.field("type", "keyword");
b.endObject();
}
b.endObject();
}
b.endObject();

), settings);

ParsedDocument doc = mapper.parse(source(b -> b.field("test1", "baz")));
assertEquals(0, doc.rootDoc().getFields("test1").length);
}

), settings);
{
Settings settings = Settings.builder()
.put(MapperService.INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING.getKey(), 3)
.put(MapperService.INDEX_MAPPING_TOTAL_FIELDS_UNMAP_FIELDS_BEYONGD_LIMIT_SETTING.getKey(), true)
.build();
DocumentMapper mapper = createDocumentMapper(topMapping(b -> {
b.field("dynamic", "true");
b.startObject("properties");
{
b.startObject("foo");
b.field("type", "keyword");
b.endObject();
b.startObject("bar");
b.field("type", "keyword");
b.endObject();
}
b.endObject();
}

ParsedDocument doc = mapper.parse(source(b -> b.field("test1", "baz")));
assertEquals(0, doc.rootDoc().getFields("test1").length);
), settings);

// Add a string type field will add two fields into the mapping(text+keyword), so the field `test`
// will not be added to the mapping because of the total fields limit
ParsedDocument doc = mapper.parse(source(b -> b.field("test", "baz")));
assertEquals(0, doc.rootDoc().getFields("test").length);
}
}

public void testDynamicLongArrayWithUnmapFieldsBeyondTotalLimit() throws Exception {
Settings settings = Settings.builder()
.put(MapperService.INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING.getKey(), 3)
.put(MapperService.INDEX_MAPPING_TOTAL_FIELDS_UNMAP_FIELDS_BEYONGD_LIMIT_SETTING.getKey(), true)
.build();
DocumentMapper mapper = createDocumentMapper(topMapping(b -> {
b.field("dynamic", "true");
b.startObject("properties");
{
b.startObject("foo");
b.field("type", "keyword");
b.endObject();
b.startObject("bar");
b.field("type", "keyword");
{
Settings settings = Settings.builder()
.put(MapperService.INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING.getKey(), 3)
.put(MapperService.INDEX_MAPPING_TOTAL_FIELDS_UNMAP_FIELDS_BEYONGD_LIMIT_SETTING.getKey(), true)
.build();
DocumentMapper mapper = createDocumentMapper(topMapping(b -> {
b.field("dynamic", "true");
b.startObject("properties");
{
b.startObject("foo");
b.field("type", "keyword");
b.endObject();
b.startObject("bar");
b.field("type", "keyword");
b.endObject();
b.startObject("zoo");
b.field("type", "keyword");
b.endObject();
}
b.endObject();
b.startObject("zoo");
b.field("type", "keyword");
}), settings);

ParsedDocument doc = mapper.parse(source(b -> b.startArray("test").value(0).value(1).endArray()));
assertEquals(0, doc.rootDoc().getFields("test").length);
}

{
Settings settings = Settings.builder()
.put(MapperService.INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING.getKey(), 3)
.put(MapperService.INDEX_MAPPING_TOTAL_FIELDS_UNMAP_FIELDS_BEYONGD_LIMIT_SETTING.getKey(), true)
.build();
DocumentMapper mapper = createDocumentMapper(topMapping(b -> {
b.field("dynamic", "true");
b.startObject("properties");
{
b.startObject("foo");
b.field("type", "keyword");
b.endObject();
b.startObject("bar");
b.field("type", "keyword");
b.endObject();
}
b.endObject();
}
b.endObject();
}), settings);
}), settings);

ParsedDocument doc = mapper.parse(source(b -> b.startArray("test").value(0).value(1).endArray()));
assertEquals(0, doc.rootDoc().getFields("test").length);
ParsedDocument doc = mapper.parse(source(b -> b.startArray("test").value(0).value(1).endArray()));
assertEquals(2, doc.rootDoc().getFields("test").length);
}
}

public void testDynamicObjectWithUnmapFieldsBeyondTotalLimit() throws Exception {
Settings settings = Settings.builder()
.put(MapperService.INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING.getKey(), 3)
.put(MapperService.INDEX_MAPPING_TOTAL_FIELDS_UNMAP_FIELDS_BEYONGD_LIMIT_SETTING.getKey(), true)
.build();
DocumentMapper mapper = createDocumentMapper(topMapping(b -> {
b.field("dynamic", "true");
b.startObject("properties");
{
b.startObject("foo");
b.field("type", "keyword");
b.endObject();
b.startObject("bar");
b.field("type", "keyword");
b.endObject();
b.startObject("zoo");
b.field("type", "keyword");
{
Settings settings = Settings.builder()
.put(MapperService.INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING.getKey(), 3)
.put(MapperService.INDEX_MAPPING_TOTAL_FIELDS_UNMAP_FIELDS_BEYONGD_LIMIT_SETTING.getKey(), true)
.build();
DocumentMapper mapper = createDocumentMapper(topMapping(b -> {
b.field("dynamic", "true");
b.startObject("properties");
{
b.startObject("foo");
b.field("type", "keyword");
b.endObject();
b.startObject("bar");
b.field("type", "keyword");
b.endObject();
b.startObject("zoo");
b.field("type", "keyword");
b.endObject();
}
b.endObject();
}
b.endObject();

), settings);

ParsedDocument doc = mapper.parse(source(b -> b.startObject("test").field("test1", "baz").endObject()));
assertEquals(0, doc.rootDoc().getFields("test.test1").length);
}

), settings);
{
Settings settings = Settings.builder()
.put(MapperService.INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING.getKey(), 3)
.put(MapperService.INDEX_MAPPING_TOTAL_FIELDS_UNMAP_FIELDS_BEYONGD_LIMIT_SETTING.getKey(), true)
.build();
DocumentMapper mapper = createDocumentMapper(topMapping(b -> {
b.field("dynamic", "true");
b.startObject("properties");
{
b.startObject("foo");
b.field("type", "keyword");
b.endObject();
b.startObject("bar");
b.field("type", "keyword");
b.endObject();
}
b.endObject();
}

ParsedDocument doc = mapper.parse(source(b -> b.startObject("test").field("test1", "baz").endObject()));
assertEquals(0, doc.rootDoc().getFields("test.test1").length);
), settings);

ParsedDocument doc = mapper.parse(source(b -> b.startObject("test").field("test1", "baz").endObject()));
assertEquals(0, doc.rootDoc().getFields("test.test1").length);
}
}

public void testDynamicStrictAllowTemplatesValueWithUnmapFieldsBeyondTotalLimit() throws Exception {
Expand Down Expand Up @@ -1408,33 +1489,6 @@ public void testDynamicStrictAllowTemplatesObjectWithUnmapFieldsBeyondTotalLimit
assertEquals(0, doc.rootDoc().getFields("test.test1").length);
}

public void testCannotAddNewFieldWithUnmapFieldsBeyondTotalLimit() throws Exception {
Settings settings = Settings.builder()
.put(MapperService.INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING.getKey(), 3)
.put(MapperService.INDEX_MAPPING_TOTAL_FIELDS_UNMAP_FIELDS_BEYONGD_LIMIT_SETTING.getKey(), true)
.build();
DocumentMapper mapper = createDocumentMapper(topMapping(b -> {
b.field("dynamic", "true");
b.startObject("properties");
{
b.startObject("foo");
b.field("type", "keyword");
b.endObject();
b.startObject("bar");
b.field("type", "keyword");
b.endObject();
}
b.endObject();
}

), settings);

// Add a string type field will add two fields into the mapping(text+keyword), so the field `test`
// will not be added to the mapping because of the total fields limit
ParsedDocument doc = mapper.parse(source(b -> b.field("test", "baz")));
assertEquals(0, doc.rootDoc().getFields("test").length);
}

public void testDynamicStrictAllowTemplatesNull() throws Exception {
DocumentMapper mapper = createDocumentMapper(topMapping(b -> b.field("dynamic", "strict_allow_templates")));
StrictDynamicMappingException exception = expectThrows(
Expand Down

0 comments on commit 9495dae

Please sign in to comment.