Skip to content

Commit

Permalink
[Remove] Multiple Types from IndexTemplateMetadata
Browse files Browse the repository at this point in the history
Removes multi-type support from IndexTemplateMetadata so that instead of holding
a map of multiple types to mappings, it only returns a single mapping for a
single type.

Signed-off-by: Nicholas Walter Knize <[email protected]>
  • Loading branch information
nknize committed Mar 11, 2022
1 parent 9cfa395 commit 860e24e
Show file tree
Hide file tree
Showing 26 changed files with 306 additions and 437 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void testFromXContent() throws IOException {
.test();
}

public void testParsingFromEsResponse() throws IOException {
public void testParsingFromOpenSearchResponse() throws IOException {
for (int runs = 0; runs < 20; runs++) {
org.opensearch.action.admin.indices.template.get.GetIndexTemplatesResponse esResponse =
new org.opensearch.action.admin.indices.template.get.GetIndexTemplatesResponse(new ArrayList<>());
Expand Down Expand Up @@ -131,8 +131,7 @@ public void testParsingFromEsResponse() throws IOException {
assertThat(result.order(), equalTo(esIMD.order()));
assertThat(result.version(), equalTo(esIMD.version()));

assertThat(esIMD.mappings().size(), equalTo(1));
BytesReference mappingSource = esIMD.mappings().valuesIt().next().uncompressed();
BytesReference mappingSource = esIMD.mappings().uncompressed();
Map<String, Object> expectedMapping = XContentHelper.convertToMap(mappingSource, true, xContentBuilder.contentType())
.v2();
assertThat(result.mappings().sourceAsMap(), equalTo(expectedMapping.get("_doc")));
Expand Down Expand Up @@ -224,7 +223,10 @@ static void toXContent(GetIndexTemplatesResponse response, XContentBuilder build
serverTemplateBuilder.order(clientITMD.order());
serverTemplateBuilder.version(clientITMD.version());
if (clientITMD.mappings() != null) {
serverTemplateBuilder.putMapping(MapperService.SINGLE_MAPPING_NAME, clientITMD.mappings().source());
// The client-side mappings never include a wrapping type, but server-side mappings
// for index templates still do so we need to wrap things here
String mappings = "{\"" + MapperService.SINGLE_MAPPING_NAME + "\": " + clientITMD.mappings().source().string() + "}";
serverTemplateBuilder.putMapping(MapperService.SINGLE_MAPPING_NAME, mappings);
}
serverIndexTemplates.add(serverTemplateBuilder.build());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,11 @@
import org.opensearch.test.OpenSearchIntegTestCase.ClusterScope;
import org.opensearch.test.OpenSearchIntegTestCase.Scope;

import java.util.HashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiFunction;

import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_WAIT_FOR_ACTIVE_SHARDS;
import static org.opensearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked;
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertBlocked;
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertRequestBuilderThrows;
Expand Down Expand Up @@ -109,28 +107,6 @@ public void testCreationDateGenerated() {
assertThat(index.getCreationDate(), allOf(lessThanOrEqualTo(timeAfterRequest), greaterThanOrEqualTo(timeBeforeRequest)));
}

public void testDoubleAddMapping() throws Exception {
try {
prepareCreate("test").addMapping("type1", "date", "type=date").addMapping("type1", "num", "type=integer");
fail("did not hit expected exception");
} catch (IllegalStateException ise) {
// expected
}
try {
prepareCreate("test").addMapping("type1", new HashMap<String, Object>()).addMapping("type1", new HashMap<String, Object>());
fail("did not hit expected exception");
} catch (IllegalStateException ise) {
// expected
}
try {
prepareCreate("test").addMapping("type1", jsonBuilder().startObject().endObject())
.addMapping("type1", jsonBuilder().startObject().endObject());
fail("did not hit expected exception");
} catch (IllegalStateException ise) {
// expected
}
}

public void testNonNestedMappings() throws Exception {
assertAcked(
prepareCreate("test").addMapping(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.opensearch.common.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.index.mapper.MapperService;
import org.opensearch.test.OpenSearchIntegTestCase;
import org.opensearch.test.hamcrest.OpenSearchAssertions;

Expand Down Expand Up @@ -182,7 +183,7 @@ public void testIndexActions() throws Exception {
// test successful
SearchResponse countResponse = client().prepareSearch("test")
.setSize(0)
.setQuery(termQuery("_type", "type1"))
.setQuery(termQuery("_type", MapperService.SINGLE_MAPPING_NAME))
.execute()
.actionGet();
assertNoFailures(countResponse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ private XContentBuilder createTypeSource() throws IOException {
private XContentBuilder createPutMappingSource() throws IOException {
return XContentFactory.jsonBuilder()
.startObject()
.startObject("my-type")
.startObject("properties")
.startObject("title")
.field("type", "text")
Expand All @@ -220,7 +219,6 @@ private XContentBuilder createPutMappingSource() throws IOException {
.endObject()
.endObject()
.endObject()
.endObject()
.endObject();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.opensearch.common.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.index.mapper.MapperService;
import org.opensearch.plugins.Plugin;
import org.opensearch.test.OpenSearchIntegTestCase;
import org.opensearch.test.InternalSettingsPlugin;
Expand Down Expand Up @@ -112,48 +113,40 @@ public void testGetFieldMappings() throws Exception {
GetFieldMappingsResponse response = client().admin()
.indices()
.prepareGetFieldMappings("indexa")
.setTypes("typeA")
.setTypes(MapperService.SINGLE_MAPPING_NAME)
.setFields("field1", "obj.subfield")
.get();
assertThat(response.fieldMappings("indexa", "typeA", "field1").fullName(), equalTo("field1"));
assertThat(response.fieldMappings("indexa", "typeA", "field1").sourceAsMap(), hasKey("field1"));
assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").fullName(), equalTo("obj.subfield"));
assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").sourceAsMap(), hasKey("subfield"));
assertThat(response.fieldMappings("indexa", MapperService.SINGLE_MAPPING_NAME, "field1").fullName(), equalTo("field1"));
assertThat(response.fieldMappings("indexa", MapperService.SINGLE_MAPPING_NAME, "field1").sourceAsMap(), hasKey("field1"));
assertThat(response.fieldMappings("indexa", MapperService.SINGLE_MAPPING_NAME, "obj.subfield").fullName(), equalTo("obj.subfield"));
assertThat(response.fieldMappings("indexa", MapperService.SINGLE_MAPPING_NAME, "obj.subfield").sourceAsMap(), hasKey("subfield"));
assertThat(response.fieldMappings("indexb", "typeB", "field1"), nullValue());

// Get mappings by name
response = client().admin().indices().prepareGetFieldMappings("indexa").setTypes("typeA").setFields("field1", "obj.subfield").get();
assertThat(response.fieldMappings("indexa", "typeA", "field1").fullName(), equalTo("field1"));
assertThat(response.fieldMappings("indexa", "typeA", "field1").sourceAsMap(), hasKey("field1"));
assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").fullName(), equalTo("obj.subfield"));
assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").sourceAsMap(), hasKey("subfield"));
response = client().admin()
.indices()
.prepareGetFieldMappings("indexa")
.setTypes(MapperService.SINGLE_MAPPING_NAME)
.setFields("field1", "obj.subfield")
.get();
assertThat(response.fieldMappings("indexa", MapperService.SINGLE_MAPPING_NAME, "field1").fullName(), equalTo("field1"));
assertThat(response.fieldMappings("indexa", MapperService.SINGLE_MAPPING_NAME, "field1").sourceAsMap(), hasKey("field1"));
assertThat(response.fieldMappings("indexa", MapperService.SINGLE_MAPPING_NAME, "obj.subfield").fullName(), equalTo("obj.subfield"));
assertThat(response.fieldMappings("indexa", MapperService.SINGLE_MAPPING_NAME, "obj.subfield").sourceAsMap(), hasKey("subfield"));
assertThat(response.fieldMappings("indexa", "typeB", "field1"), nullValue());
assertThat(response.fieldMappings("indexb", "typeB", "field1"), nullValue());

// get mappings by name across multiple indices
response = client().admin().indices().prepareGetFieldMappings().setTypes("typeA").setFields("obj.subfield").get();
assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").fullName(), equalTo("obj.subfield"));
assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").sourceAsMap(), hasKey("subfield"));
response = client().admin()
.indices()
.prepareGetFieldMappings()
.setTypes(MapperService.SINGLE_MAPPING_NAME)
.setFields("obj.subfield")
.get();
assertThat(response.fieldMappings("indexa", MapperService.SINGLE_MAPPING_NAME, "obj.subfield").fullName(), equalTo("obj.subfield"));
assertThat(response.fieldMappings("indexa", MapperService.SINGLE_MAPPING_NAME, "obj.subfield").sourceAsMap(), hasKey("subfield"));
assertThat(response.fieldMappings("indexa", "typeB", "obj.subfield"), nullValue());
assertThat(response.fieldMappings("indexb", "typeB", "obj.subfield"), nullValue());

// get mappings by name across multiple types
response = client().admin().indices().prepareGetFieldMappings("indexa").setFields("obj.subfield").get();
assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").fullName(), equalTo("obj.subfield"));
assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").sourceAsMap(), hasKey("subfield"));
assertThat(response.fieldMappings("indexa", "typeA", "field1"), nullValue());
assertThat(response.fieldMappings("indexb", "typeB", "obj.subfield"), nullValue());
assertThat(response.fieldMappings("indexb", "typeB", "field1"), nullValue());

// get mappings by name across multiple types & indices
response = client().admin().indices().prepareGetFieldMappings().setFields("obj.subfield").get();
assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").fullName(), equalTo("obj.subfield"));
assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").sourceAsMap(), hasKey("subfield"));
assertThat(response.fieldMappings("indexa", "typeA", "field1"), nullValue());
assertThat(response.fieldMappings("indexb", "typeB", "field1"), nullValue());
assertThat(response.fieldMappings("indexb", "typeB", "obj.subfield").fullName(), equalTo("obj.subfield"));
assertThat(response.fieldMappings("indexb", "typeB", "obj.subfield").sourceAsMap(), hasKey("subfield"));
assertThat(response.fieldMappings("indexb", "typeB", "field1"), nullValue());
}

@SuppressWarnings("unchecked")
Expand All @@ -170,24 +163,29 @@ public void testSimpleGetFieldMappingsWithDefaults() throws Exception {
.get();

assertThat(
(Map<String, Object>) response.fieldMappings("test", "type", "num").sourceAsMap().get("num"),
(Map<String, Object>) response.fieldMappings("test", MapperService.SINGLE_MAPPING_NAME, "num").sourceAsMap().get("num"),
hasEntry("index", Boolean.TRUE)
);
assertThat((Map<String, Object>) response.fieldMappings("test", "type", "num").sourceAsMap().get("num"), hasEntry("type", "long"));
assertThat(
(Map<String, Object>) response.fieldMappings("test", "type", "field1").sourceAsMap().get("field1"),
(Map<String, Object>) response.fieldMappings("test", MapperService.SINGLE_MAPPING_NAME, "num").sourceAsMap().get("num"),
hasEntry("type", "long")
);
assertThat(
(Map<String, Object>) response.fieldMappings("test", MapperService.SINGLE_MAPPING_NAME, "field1").sourceAsMap().get("field1"),
hasEntry("index", Boolean.TRUE)
);
assertThat(
(Map<String, Object>) response.fieldMappings("test", "type", "field1").sourceAsMap().get("field1"),
(Map<String, Object>) response.fieldMappings("test", MapperService.SINGLE_MAPPING_NAME, "field1").sourceAsMap().get("field1"),
hasEntry("type", "text")
);
assertThat(
(Map<String, Object>) response.fieldMappings("test", "type", "field2").sourceAsMap().get("field2"),
(Map<String, Object>) response.fieldMappings("test", MapperService.SINGLE_MAPPING_NAME, "field2").sourceAsMap().get("field2"),
hasEntry("type", "text")
);
assertThat(
(Map<String, Object>) response.fieldMappings("test", "type", "obj.subfield").sourceAsMap().get("subfield"),
(Map<String, Object>) response.fieldMappings("test", MapperService.SINGLE_MAPPING_NAME, "obj.subfield")
.sourceAsMap()
.get("subfield"),
hasEntry("type", "keyword")
);
}
Expand All @@ -198,12 +196,12 @@ public void testGetFieldMappingsWithFieldAlias() throws Exception {

GetFieldMappingsResponse response = client().admin().indices().prepareGetFieldMappings().setFields("alias", "field1").get();

FieldMappingMetadata aliasMapping = response.fieldMappings("test", "type", "alias");
FieldMappingMetadata aliasMapping = response.fieldMappings("test", MapperService.SINGLE_MAPPING_NAME, "alias");
assertThat(aliasMapping.fullName(), equalTo("alias"));
assertThat(aliasMapping.sourceAsMap(), hasKey("alias"));
assertThat((Map<String, Object>) aliasMapping.sourceAsMap().get("alias"), hasEntry("type", "alias"));

FieldMappingMetadata field1Mapping = response.fieldMappings("test", "type", "field1");
FieldMappingMetadata field1Mapping = response.fieldMappings("test", MapperService.SINGLE_MAPPING_NAME, "field1");
assertThat(field1Mapping.fullName(), equalTo("field1"));
assertThat(field1Mapping.sourceAsMap(), hasKey("field1"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,10 @@ public void testUpdateMappingWithConflicts() {
client().admin()
.indices()
.preparePutMapping("test")
.setSource("{\"type\":{\"properties\":{\"body\":{\"type\":\"integer\"}}}}", XContentType.JSON)
.setSource(
"{\"" + MapperService.SINGLE_MAPPING_NAME + "\":{\"properties\":{\"body\":{\"type\":\"integer\"}}}}",
XContentType.JSON
)
.execute()
.actionGet();
fail("Expected MergeMappingException");
Expand All @@ -225,7 +228,10 @@ public void testUpdateMappingWithNormsConflicts() {
client().admin()
.indices()
.preparePutMapping("test")
.setSource("{\"type\":{\"properties\":{\"body\":{\"type\":\"text\", \"norms\": true }}}}", XContentType.JSON)
.setSource(
"{\"" + MapperService.SINGLE_MAPPING_NAME + "\":{\"properties\":{\"body\":{\"type\":\"text\", \"norms\": true }}}}",
XContentType.JSON
)
.execute()
.actionGet();
fail("Expected MergeMappingException");
Expand All @@ -242,7 +248,11 @@ public void testUpdateMappingNoChanges() {
.indices()
.prepareCreate("test")
.setSettings(Settings.builder().put("index.number_of_shards", 2).put("index.number_of_replicas", 0))
.addMapping("type", "{\"type\":{\"properties\":{\"body\":{\"type\":\"text\"}}}}", XContentType.JSON)
.addMapping(
"type",
"{\"" + MapperService.SINGLE_MAPPING_NAME + "\":{\"properties\":{\"body\":{\"type\":\"text\"}}}}",
XContentType.JSON
)
.execute()
.actionGet();
client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ public void testIdBasedScriptFields() throws Exception {
assertThat(response.getHits().getAt(i).getId(), equalTo(Integer.toString(i)));
Set<String> fields = new HashSet<>(response.getHits().getAt(i).getFields().keySet());
assertThat(fields, equalTo(singleton("type")));
assertThat(response.getHits().getAt(i).getFields().get("type").getValue(), equalTo("type1"));
assertThat(response.getHits().getAt(i).getFields().get("type").getValue(), equalTo(MapperService.SINGLE_MAPPING_NAME));
}

response = client().prepareSearch()
Expand All @@ -504,7 +504,7 @@ public void testIdBasedScriptFields() throws Exception {
assertThat(response.getHits().getAt(i).getId(), equalTo(Integer.toString(i)));
Set<String> fields = new HashSet<>(response.getHits().getAt(i).getFields().keySet());
assertThat(fields, equalTo(newHashSet("type", "id")));
assertThat(response.getHits().getAt(i).getFields().get("type").getValue(), equalTo("type1"));
assertThat(response.getHits().getAt(i).getFields().get("type").getValue(), equalTo(MapperService.SINGLE_MAPPING_NAME));
assertThat(response.getHits().getAt(i).getFields().get("id").getValue(), equalTo(Integer.toString(i)));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1987,41 +1987,6 @@ public void testRangeQueryRangeFields_24744() throws Exception {
assertHitCount(searchResponse, 1);
}

public void testRangeQueryTypeField_31476() throws Exception {
assertAcked(prepareCreate("test").addMapping("foo", "field", "type=keyword"));

client().prepareIndex("test").setId("1").setSource("field", "value").get();
refresh();

RangeQueryBuilder range = new RangeQueryBuilder("_type").from("ape").to("zebra");
SearchResponse searchResponse = client().prepareSearch("test").setQuery(range).get();
assertHitCount(searchResponse, 1);

range = new RangeQueryBuilder("_type").from("monkey").to("zebra");
searchResponse = client().prepareSearch("test").setQuery(range).get();
assertHitCount(searchResponse, 0);

range = new RangeQueryBuilder("_type").from("ape").to("donkey");
searchResponse = client().prepareSearch("test").setQuery(range).get();
assertHitCount(searchResponse, 0);

range = new RangeQueryBuilder("_type").from("ape").to("foo").includeUpper(false);
searchResponse = client().prepareSearch("test").setQuery(range).get();
assertHitCount(searchResponse, 0);

range = new RangeQueryBuilder("_type").from("ape").to("foo").includeUpper(true);
searchResponse = client().prepareSearch("test").setQuery(range).get();
assertHitCount(searchResponse, 1);

range = new RangeQueryBuilder("_type").from("foo").to("zebra").includeLower(false);
searchResponse = client().prepareSearch("test").setQuery(range).get();
assertHitCount(searchResponse, 0);

range = new RangeQueryBuilder("_type").from("foo").to("zebra").includeLower(true);
searchResponse = client().prepareSearch("test").setQuery(range).get();
assertHitCount(searchResponse, 1);
}

public void testNestedQueryWithFieldAlias() throws Exception {
XContentBuilder mapping = XContentFactory.jsonBuilder()
.startObject()
Expand Down
Loading

0 comments on commit 860e24e

Please sign in to comment.