[Remove] deprecated getMapping API from IndicesClient (#2262)

Removes the deprecated types based get, getMapping, getAsync, and
getMappingAsync methods from IndicesClient. It also removes extra nesting of
mappings belong the deprecated type named object and removes the types based
methods from the affected request classes.

Signed-off-by: Nicholas Walter Knize <nknize@apache.org>
This commit is contained in:
Nick Knize 2022-03-02 13:44:04 -06:00 committed by GitHub
parent 4b89410055
commit 897f4e7295
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
38 changed files with 348 additions and 1099 deletions

View File

@ -550,60 +550,6 @@ public final class IndicesClient {
); );
} }
/**
* Retrieves the mappings on an index or indices using the Get Mapping API.
*
* @param getMappingsRequest the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @return the response
* @throws IOException in case there is a problem sending the request or parsing back the response
*
* @deprecated This method uses old request and response objects which still refer to types, a deprecated
* feature. The method {@link #getMapping(GetMappingsRequest, RequestOptions)} should be used instead, which
* accepts a new request object.
*/
@Deprecated
public org.opensearch.action.admin.indices.mapping.get.GetMappingsResponse getMapping(
org.opensearch.action.admin.indices.mapping.get.GetMappingsRequest getMappingsRequest,
RequestOptions options
) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(
getMappingsRequest,
IndicesRequestConverters::getMappings,
options,
org.opensearch.action.admin.indices.mapping.get.GetMappingsResponse::fromXContent,
emptySet()
);
}
/**
* Asynchronously retrieves the mappings on an index on indices using the Get Mapping API.
*
* @param getMappingsRequest the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @param listener the listener to be notified upon request completion
*
* @deprecated This method uses old request and response objects which still refer to types, a deprecated feature.
* The method {@link #getMapping(GetMappingsRequest, RequestOptions)} should be used instead, which accepts a new
* request object.
* @return cancellable that may be used to cancel the request
*/
@Deprecated
public Cancellable getMappingAsync(
org.opensearch.action.admin.indices.mapping.get.GetMappingsRequest getMappingsRequest,
RequestOptions options,
ActionListener<org.opensearch.action.admin.indices.mapping.get.GetMappingsResponse> listener
) {
return restHighLevelClient.performRequestAsyncAndParseEntity(
getMappingsRequest,
IndicesRequestConverters::getMappings,
options,
org.opensearch.action.admin.indices.mapping.get.GetMappingsResponse::fromXContent,
listener,
emptySet()
);
}
/** /**
* Retrieves the field mappings on an index or indices using the Get Field Mapping API. * Retrieves the field mappings on an index or indices using the Get Field Mapping API.
* *
@ -1008,56 +954,6 @@ public final class IndicesClient {
); );
} }
/**
* Retrieve information about one or more indexes
*
* @param getIndexRequest the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @return the response
* @throws IOException in case there is a problem sending the request or parsing back the response
* @deprecated This method uses an old request object which still refers to types, a deprecated feature. The method
* {@link #get(GetIndexRequest, RequestOptions)} should be used instead, which accepts a new request object.
*/
@Deprecated
public org.opensearch.action.admin.indices.get.GetIndexResponse get(
org.opensearch.action.admin.indices.get.GetIndexRequest getIndexRequest,
RequestOptions options
) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(
getIndexRequest,
IndicesRequestConverters::getIndex,
options,
org.opensearch.action.admin.indices.get.GetIndexResponse::fromXContent,
emptySet()
);
}
/**
* Retrieve information about one or more indexes
*
* @param getIndexRequest the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @param listener the listener to be notified upon request completion
* @deprecated This method uses an old request object which still refers to types, a deprecated feature. The method
* {@link #getAsync(GetIndexRequest, RequestOptions, ActionListener)} should be used instead, which accepts a new request object.
* @return cancellable that may be used to cancel the request
*/
@Deprecated
public Cancellable getAsync(
org.opensearch.action.admin.indices.get.GetIndexRequest getIndexRequest,
RequestOptions options,
ActionListener<org.opensearch.action.admin.indices.get.GetIndexResponse> listener
) {
return restHighLevelClient.performRequestAsyncAndParseEntity(
getIndexRequest,
IndicesRequestConverters::getIndex,
options,
org.opensearch.action.admin.indices.get.GetIndexResponse::fromXContent,
listener,
emptySet()
);
}
/** /**
* Force merge one or more indices using the Force Merge API. * Force merge one or more indices using the Force Merge API.
* *

View File

@ -240,22 +240,6 @@ final class IndicesRequestConverters {
return request; return request;
} }
@Deprecated
static Request getMappings(org.opensearch.action.admin.indices.mapping.get.GetMappingsRequest getMappingsRequest) {
String[] indices = getMappingsRequest.indices() == null ? Strings.EMPTY_ARRAY : getMappingsRequest.indices();
String[] types = getMappingsRequest.types() == null ? Strings.EMPTY_ARRAY : getMappingsRequest.types();
Request request = new Request(HttpGet.METHOD_NAME, RequestConverters.endpoint(indices, "_mapping", types));
RequestConverters.Params parameters = new RequestConverters.Params();
parameters.withMasterTimeout(getMappingsRequest.masterNodeTimeout());
parameters.withIndicesOptions(getMappingsRequest.indicesOptions());
parameters.withLocal(getMappingsRequest.local());
parameters.putParam(INCLUDE_TYPE_NAME_PARAMETER, "true");
request.addParameters(parameters.asMap());
return request;
}
static Request getFieldMapping(GetFieldMappingsRequest getFieldMappingsRequest) { static Request getFieldMapping(GetFieldMappingsRequest getFieldMappingsRequest) {
String[] indices = getFieldMappingsRequest.indices() == null ? Strings.EMPTY_ARRAY : getFieldMappingsRequest.indices(); String[] indices = getFieldMappingsRequest.indices() == null ? Strings.EMPTY_ARRAY : getFieldMappingsRequest.indices();
String[] fields = getFieldMappingsRequest.fields() == null ? Strings.EMPTY_ARRAY : getFieldMappingsRequest.fields(); String[] fields = getFieldMappingsRequest.fields() == null ? Strings.EMPTY_ARRAY : getFieldMappingsRequest.fields();

View File

@ -32,6 +32,7 @@
package org.opensearch.client; package org.opensearch.client;
import org.opensearch.common.bytes.BytesReference; import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.collect.ImmutableOpenMap;
import org.opensearch.common.xcontent.LoggingDeprecationHandler; import org.opensearch.common.xcontent.LoggingDeprecationHandler;
import org.opensearch.common.xcontent.NamedXContentRegistry; import org.opensearch.common.xcontent.NamedXContentRegistry;
import org.opensearch.common.xcontent.ToXContent; import org.opensearch.common.xcontent.ToXContent;
@ -42,6 +43,10 @@ import org.opensearch.common.xcontent.XContentType;
import org.opensearch.test.OpenSearchTestCase; import org.opensearch.test.OpenSearchTestCase;
import java.io.IOException; import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/** /**
* Base class for HLRC response parsing tests. * Base class for HLRC response parsing tests.
@ -99,4 +104,16 @@ public abstract class AbstractResponseTestCase<S extends ToXContent, C> extends
return ToXContent.EMPTY_PARAMS; return ToXContent.EMPTY_PARAMS;
} }
protected static <T> void assertMapEquals(ImmutableOpenMap<String, T> expected, Map<String, T> actual) {
Set<String> expectedKeys = new HashSet<>();
Iterator<String> keysIt = expected.keysIt();
while (keysIt.hasNext()) {
expectedKeys.add(keysIt.next());
}
assertEquals(expectedKeys, actual.keySet());
for (String key : expectedKeys) {
assertEquals(expected.get(key), actual.get(key));
}
}
} }

View File

@ -128,7 +128,6 @@ import org.opensearch.index.query.QueryBuilders;
import org.opensearch.rest.RestStatus; import org.opensearch.rest.RestStatus;
import org.opensearch.rest.action.admin.indices.RestCreateIndexAction; import org.opensearch.rest.action.admin.indices.RestCreateIndexAction;
import org.opensearch.rest.action.admin.indices.RestGetIndexTemplateAction; import org.opensearch.rest.action.admin.indices.RestGetIndexTemplateAction;
import org.opensearch.rest.action.admin.indices.RestGetIndicesAction;
import org.opensearch.rest.action.admin.indices.RestPutIndexTemplateAction; import org.opensearch.rest.action.admin.indices.RestPutIndexTemplateAction;
import org.opensearch.rest.action.admin.indices.RestRolloverIndexAction; import org.opensearch.rest.action.admin.indices.RestRolloverIndexAction;
@ -487,33 +486,6 @@ public class IndicesClientIT extends OpenSearchRestHighLevelClientTestCase {
assertEquals("integer", fieldMapping.get("type")); assertEquals("integer", fieldMapping.get("type"));
} }
@SuppressWarnings("unchecked")
public void testGetIndexWithTypes() throws IOException {
String indexName = "get_index_test";
Settings basicSettings = Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, 0).build();
String mappings = "\"properties\":{\"field-1\":{\"type\":\"integer\"}}";
createIndex(indexName, basicSettings, mappings);
org.opensearch.action.admin.indices.get.GetIndexRequest getIndexRequest =
new org.opensearch.action.admin.indices.get.GetIndexRequest().indices(indexName).includeDefaults(false);
org.opensearch.action.admin.indices.get.GetIndexResponse getIndexResponse = execute(
getIndexRequest,
highLevelClient().indices()::get,
highLevelClient().indices()::getAsync,
expectWarningsOnce(RestGetIndicesAction.TYPES_DEPRECATION_MESSAGE)
);
// default settings should be null
assertNull(getIndexResponse.getSetting(indexName, "index.refresh_interval"));
assertEquals("1", getIndexResponse.getSetting(indexName, SETTING_NUMBER_OF_SHARDS));
assertEquals("0", getIndexResponse.getSetting(indexName, SETTING_NUMBER_OF_REPLICAS));
assertNotNull(getIndexResponse.getMappings().get(indexName));
MappingMetadata mappingMetadata = getIndexResponse.getMappings().get(indexName).get("_doc");
assertNotNull(mappingMetadata);
assertEquals("_doc", mappingMetadata.type());
assertEquals("{\"properties\":{\"field-1\":{\"type\":\"integer\"}}}", mappingMetadata.source().string());
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testGetIndexWithDefaults() throws IOException { public void testGetIndexWithDefaults() throws IOException {
String indexName = "get_index_test"; String indexName = "get_index_test";

View File

@ -318,53 +318,6 @@ public class IndicesRequestConvertersTests extends OpenSearchTestCase {
Assert.assertThat(HttpGet.METHOD_NAME, equalTo(request.getMethod())); Assert.assertThat(HttpGet.METHOD_NAME, equalTo(request.getMethod()));
} }
public void testGetMappingWithTypes() {
org.opensearch.action.admin.indices.mapping.get.GetMappingsRequest getMappingRequest =
new org.opensearch.action.admin.indices.mapping.get.GetMappingsRequest();
String[] indices = Strings.EMPTY_ARRAY;
if (randomBoolean()) {
indices = RequestConvertersTests.randomIndicesNames(0, 5);
getMappingRequest.indices(indices);
} else if (randomBoolean()) {
getMappingRequest.indices((String[]) null);
}
String type = null;
if (randomBoolean()) {
type = randomAlphaOfLengthBetween(3, 10);
getMappingRequest.types(type);
} else if (randomBoolean()) {
getMappingRequest.types((String[]) null);
}
Map<String, String> expectedParams = new HashMap<>();
RequestConvertersTests.setRandomIndicesOptions(
getMappingRequest::indicesOptions,
getMappingRequest::indicesOptions,
expectedParams
);
RequestConvertersTests.setRandomMasterTimeout(getMappingRequest, expectedParams);
RequestConvertersTests.setRandomLocal(getMappingRequest::local, expectedParams);
expectedParams.put(INCLUDE_TYPE_NAME_PARAMETER, "true");
Request request = IndicesRequestConverters.getMappings(getMappingRequest);
StringJoiner endpoint = new StringJoiner("/", "/", "");
String index = String.join(",", indices);
if (Strings.hasLength(index)) {
endpoint.add(index);
}
endpoint.add("_mapping");
if (type != null) {
endpoint.add(type);
}
Assert.assertThat(endpoint.toString(), equalTo(request.getEndpoint()));
Assert.assertThat(expectedParams, equalTo(request.getParameters()));
Assert.assertThat(HttpGet.METHOD_NAME, equalTo(request.getMethod()));
}
public void testGetFieldMapping() { public void testGetFieldMapping() {
GetFieldMappingsRequest getFieldMappingsRequest = new GetFieldMappingsRequest(); GetFieldMappingsRequest getFieldMappingsRequest = new GetFieldMappingsRequest();

View File

@ -33,19 +33,17 @@
package org.opensearch.client.indices; package org.opensearch.client.indices;
import org.apache.lucene.util.CollectionUtil; import org.apache.lucene.util.CollectionUtil;
import org.opensearch.client.AbstractResponseTestCase;
import org.opensearch.client.GetAliasesResponseTests; import org.opensearch.client.GetAliasesResponseTests;
import org.opensearch.cluster.metadata.AliasMetadata; import org.opensearch.cluster.metadata.AliasMetadata;
import org.opensearch.cluster.metadata.MappingMetadata; import org.opensearch.cluster.metadata.MappingMetadata;
import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.collect.ImmutableOpenMap;
import org.opensearch.common.settings.IndexScopedSettings; import org.opensearch.common.settings.IndexScopedSettings;
import org.opensearch.common.settings.Settings; import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.ToXContent; import org.opensearch.common.xcontent.XContentParser;
import org.opensearch.common.xcontent.ToXContent.Params; import org.opensearch.common.xcontent.XContentType;
import org.opensearch.common.xcontent.XContentBuilder;
import org.opensearch.index.RandomCreateIndexGenerator; import org.opensearch.index.RandomCreateIndexGenerator;
import org.opensearch.index.mapper.MapperService; import org.opensearch.index.mapper.MapperService;
import org.opensearch.rest.BaseRestHandler;
import org.opensearch.test.OpenSearchTestCase;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -57,40 +55,18 @@ import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import static org.opensearch.test.AbstractXContentTestCase.xContentTester; public class GetIndexResponseTests extends AbstractResponseTestCase<
org.opensearch.action.admin.indices.get.GetIndexResponse,
GetIndexResponse> {
public class GetIndexResponseTests extends OpenSearchTestCase { @Override
protected org.opensearch.action.admin.indices.get.GetIndexResponse createServerTestInstance(XContentType xContentType) {
// Because the client-side class does not have a toXContent method, we test xContent serialization by creating
// a random client object, converting it to a server object then serializing it to xContent, and finally
// parsing it back as a client object. We check equality between the original client object, and the parsed one.
public void testFromXContent() throws IOException {
xContentTester(
this::createParser,
GetIndexResponseTests::createTestInstance,
GetIndexResponseTests::toXContent,
GetIndexResponse::fromXContent
).supportsUnknownFields(false)
.assertToXContentEquivalence(false)
.assertEqualsConsumer(GetIndexResponseTests::assertEqualInstances)
.test();
}
private static void assertEqualInstances(GetIndexResponse expected, GetIndexResponse actual) {
assertArrayEquals(expected.getIndices(), actual.getIndices());
assertEquals(expected.getMappings(), actual.getMappings());
assertEquals(expected.getSettings(), actual.getSettings());
assertEquals(expected.getDefaultSettings(), actual.getDefaultSettings());
assertEquals(expected.getAliases(), actual.getAliases());
}
private static GetIndexResponse createTestInstance() {
String[] indices = generateRandomStringArray(5, 5, false, false); String[] indices = generateRandomStringArray(5, 5, false, false);
Map<String, MappingMetadata> mappings = new HashMap<>(); ImmutableOpenMap.Builder<String, MappingMetadata> mappings = ImmutableOpenMap.builder();
Map<String, List<AliasMetadata>> aliases = new HashMap<>(); ImmutableOpenMap.Builder<String, List<AliasMetadata>> aliases = ImmutableOpenMap.builder();
Map<String, Settings> settings = new HashMap<>(); ImmutableOpenMap.Builder<String, Settings> settings = ImmutableOpenMap.builder();
Map<String, Settings> defaultSettings = new HashMap<>(); ImmutableOpenMap.Builder<String, Settings> defaultSettings = ImmutableOpenMap.builder();
Map<String, String> dataStreams = new HashMap<>(); ImmutableOpenMap.Builder<String, String> dataStreams = ImmutableOpenMap.builder();
IndexScopedSettings indexScopedSettings = IndexScopedSettings.DEFAULT_SCOPED_SETTINGS; IndexScopedSettings indexScopedSettings = IndexScopedSettings.DEFAULT_SCOPED_SETTINGS;
boolean includeDefaults = randomBoolean(); boolean includeDefaults = randomBoolean();
for (String index : indices) { for (String index : indices) {
@ -116,17 +92,36 @@ public class GetIndexResponseTests extends OpenSearchTestCase {
dataStreams.put(index, randomAlphaOfLength(5).toLowerCase(Locale.ROOT)); dataStreams.put(index, randomAlphaOfLength(5).toLowerCase(Locale.ROOT));
} }
} }
return new GetIndexResponse(indices, mappings, aliases, settings, defaultSettings, dataStreams); return new org.opensearch.action.admin.indices.get.GetIndexResponse(
indices,
mappings.build(),
aliases.build(),
settings.build(),
defaultSettings.build(),
dataStreams.build()
);
}
@Override
protected GetIndexResponse doParseToClientInstance(XContentParser parser) throws IOException {
return GetIndexResponse.fromXContent(parser);
}
@Override
protected void assertInstances(
org.opensearch.action.admin.indices.get.GetIndexResponse serverTestInstance,
GetIndexResponse clientInstance
) {
assertArrayEquals(serverTestInstance.getIndices(), clientInstance.getIndices());
assertMapEquals(serverTestInstance.getMappings(), clientInstance.getMappings());
assertMapEquals(serverTestInstance.getSettings(), clientInstance.getSettings());
assertMapEquals(serverTestInstance.defaultSettings(), clientInstance.getDefaultSettings());
assertMapEquals(serverTestInstance.getAliases(), clientInstance.getAliases());
} }
private static MappingMetadata createMappingsForIndex() { private static MappingMetadata createMappingsForIndex() {
int typeCount = rarely() ? 0 : 1; int typeCount = rarely() ? 0 : 1;
MappingMetadata mmd; MappingMetadata mmd = new MappingMetadata(MapperService.SINGLE_MAPPING_NAME, Collections.emptyMap());
try {
mmd = new MappingMetadata(MapperService.SINGLE_MAPPING_NAME, Collections.emptyMap());
} catch (IOException e) {
throw new RuntimeException(e);
}
for (int i = 0; i < typeCount; i++) { for (int i = 0; i < typeCount; i++) {
if (rarely() == false) { // rarely have no fields if (rarely() == false) { // rarely have no fields
Map<String, Object> mappings = new HashMap<>(); Map<String, Object> mappings = new HashMap<>();
@ -135,12 +130,8 @@ public class GetIndexResponseTests extends OpenSearchTestCase {
mappings.put("field2-" + i, randomFieldMapping()); mappings.put("field2-" + i, randomFieldMapping());
} }
try { String typeName = MapperService.SINGLE_MAPPING_NAME;
String typeName = MapperService.SINGLE_MAPPING_NAME; mmd = new MappingMetadata(typeName, mappings);
mmd = new MappingMetadata(typeName, mappings);
} catch (IOException e) {
fail("shouldn't have failed " + e);
}
} }
} }
return mmd; return mmd;
@ -178,39 +169,4 @@ public class GetIndexResponseTests extends OpenSearchTestCase {
} }
return mappings; return mappings;
} }
private static void toXContent(GetIndexResponse response, XContentBuilder builder) throws IOException {
// first we need to repackage from GetIndexResponse to org.opensearch.action.admin.indices.get.GetIndexResponse
ImmutableOpenMap.Builder<String, ImmutableOpenMap<String, MappingMetadata>> allMappings = ImmutableOpenMap.builder();
ImmutableOpenMap.Builder<String, List<AliasMetadata>> aliases = ImmutableOpenMap.builder();
ImmutableOpenMap.Builder<String, Settings> settings = ImmutableOpenMap.builder();
ImmutableOpenMap.Builder<String, Settings> defaultSettings = ImmutableOpenMap.builder();
Map<String, MappingMetadata> indexMappings = response.getMappings();
for (String index : response.getIndices()) {
MappingMetadata mmd = indexMappings.get(index);
ImmutableOpenMap.Builder<String, MappingMetadata> typedMappings = ImmutableOpenMap.builder();
if (mmd != null) {
typedMappings.put(MapperService.SINGLE_MAPPING_NAME, mmd);
}
allMappings.put(index, typedMappings.build());
aliases.put(index, response.getAliases().get(index));
settings.put(index, response.getSettings().get(index));
defaultSettings.put(index, response.getDefaultSettings().get(index));
}
org.opensearch.action.admin.indices.get.GetIndexResponse serverResponse =
new org.opensearch.action.admin.indices.get.GetIndexResponse(
response.getIndices(),
allMappings.build(),
aliases.build(),
settings.build(),
defaultSettings.build(),
ImmutableOpenMap.<String, String>builder().build()
);
// then we can call its toXContent method, forcing no output of types
Params params = new ToXContent.MapParams(Collections.singletonMap(BaseRestHandler.INCLUDE_TYPE_NAME_PARAMETER, "false"));
serverResponse.toXContent(builder, params);
}
} }

View File

@ -50,7 +50,6 @@ import org.opensearch.index.mapper.MapperService;
import org.opensearch.test.OpenSearchTestCase; import org.opensearch.test.OpenSearchTestCase;
import java.io.IOException; import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
@ -196,13 +195,9 @@ public class GetIndexTemplatesResponseTests extends OpenSearchTestCase {
templateBuilder.version(between(0, 100)); templateBuilder.version(between(0, 100));
} }
if (randomBoolean()) { if (randomBoolean()) {
try { Map<String, Object> map = XContentHelper.convertToMap(new BytesArray(mappingString), true, XContentType.JSON).v2();
Map<String, Object> map = XContentHelper.convertToMap(new BytesArray(mappingString), true, XContentType.JSON).v2(); MappingMetadata mapping = new MappingMetadata(MapperService.SINGLE_MAPPING_NAME, map);
MappingMetadata mapping = new MappingMetadata(MapperService.SINGLE_MAPPING_NAME, map); templateBuilder.mapping(mapping);
templateBuilder.mapping(mapping);
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
} }
templates.add(templateBuilder.build()); templates.add(templateBuilder.build());
} }

View File

@ -32,70 +32,54 @@
package org.opensearch.client.indices; package org.opensearch.client.indices;
import org.opensearch.client.AbstractResponseTestCase;
import org.opensearch.cluster.metadata.MappingMetadata; import org.opensearch.cluster.metadata.MappingMetadata;
import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.collect.ImmutableOpenMap;
import org.opensearch.common.xcontent.ToXContent; import org.opensearch.common.xcontent.XContentParser;
import org.opensearch.common.xcontent.ToXContent.Params; import org.opensearch.common.xcontent.XContentType;
import org.opensearch.common.xcontent.XContentBuilder;
import org.opensearch.index.mapper.MapperService; import org.opensearch.index.mapper.MapperService;
import org.opensearch.rest.BaseRestHandler;
import org.opensearch.test.OpenSearchTestCase;
import java.io.IOException; import java.io.IOException;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.function.Predicate;
import static org.opensearch.client.indices.GetMappingsResponse.MAPPINGS; public class GetMappingsResponseTests extends AbstractResponseTestCase<
import static org.opensearch.test.AbstractXContentTestCase.xContentTester; org.opensearch.action.admin.indices.mapping.get.GetMappingsResponse,
GetMappingsResponse> {
public class GetMappingsResponseTests extends OpenSearchTestCase { @Override
protected org.opensearch.action.admin.indices.mapping.get.GetMappingsResponse createServerTestInstance(XContentType xContentType) {
// Because the client-side class does not have a toXContent method, we test xContent serialization by creating ImmutableOpenMap.Builder<String, MappingMetadata> mappings = ImmutableOpenMap.builder();
// a random client object, converting it to a server object then serializing it to xContent, and finally int numberOfIndexes = randomIntBetween(1, 5);
// parsing it back as a client object. We check equality between the original client object, and the parsed one. for (int i = 0; i < numberOfIndexes; i++) {
public void testFromXContent() throws IOException { mappings.put("index-" + randomAlphaOfLength(5), randomMappingMetadata());
xContentTester( }
this::createParser, return new org.opensearch.action.admin.indices.mapping.get.GetMappingsResponse(mappings.build());
GetMappingsResponseTests::createTestInstance,
GetMappingsResponseTests::toXContent,
GetMappingsResponse::fromXContent
).supportsUnknownFields(true)
.assertEqualsConsumer(GetMappingsResponseTests::assertEqualInstances)
.randomFieldsExcludeFilter(randomFieldsExcludeFilter())
.test();
} }
private static GetMappingsResponse createTestInstance() { @Override
Map<String, MappingMetadata> mappings = Collections.singletonMap("index-" + randomAlphaOfLength(5), randomMappingMetadata()); protected GetMappingsResponse doParseToClientInstance(XContentParser parser) throws IOException {
return new GetMappingsResponse(mappings); return GetMappingsResponse.fromXContent(parser);
} }
private static void assertEqualInstances(GetMappingsResponse expected, GetMappingsResponse actual) { @Override
assertEquals(expected.mappings(), actual.mappings()); protected void assertInstances(
} org.opensearch.action.admin.indices.mapping.get.GetMappingsResponse serverTestInstance,
GetMappingsResponse clientInstance
private Predicate<String> randomFieldsExcludeFilter() { ) {
return field -> !field.equals(MAPPINGS.getPreferredName()); assertMapEquals(serverTestInstance.getMappings(), clientInstance.mappings());
} }
public static MappingMetadata randomMappingMetadata() { public static MappingMetadata randomMappingMetadata() {
Map<String, Object> mappings = new HashMap<>(); Map<String, Object> mappings = new HashMap<>();
if (frequently()) { // rarely have no fields if (frequently()) { // rarely have no fields
mappings.put("field1", randomFieldMapping()); mappings.put("field1", randomFieldMapping());
if (randomBoolean()) { if (randomBoolean()) {
mappings.put("field2", randomFieldMapping()); mappings.put("field2", randomFieldMapping());
} }
} }
return new MappingMetadata(MapperService.SINGLE_MAPPING_NAME, mappings);
try {
return new MappingMetadata(MapperService.SINGLE_MAPPING_NAME, mappings);
} catch (IOException e) {
throw new RuntimeException(e);
}
} }
private static Map<String, Object> randomFieldMapping() { private static Map<String, Object> randomFieldMapping() {
@ -110,22 +94,4 @@ public class GetMappingsResponseTests extends OpenSearchTestCase {
} }
return mappings; return mappings;
} }
private static void toXContent(GetMappingsResponse response, XContentBuilder builder) throws IOException {
Params params = new ToXContent.MapParams(Collections.singletonMap(BaseRestHandler.INCLUDE_TYPE_NAME_PARAMETER, "false"));
ImmutableOpenMap.Builder<String, ImmutableOpenMap<String, MappingMetadata>> allMappings = ImmutableOpenMap.builder();
for (Map.Entry<String, MappingMetadata> indexEntry : response.mappings().entrySet()) {
ImmutableOpenMap.Builder<String, MappingMetadata> mappings = ImmutableOpenMap.builder();
mappings.put(MapperService.SINGLE_MAPPING_NAME, indexEntry.getValue());
allMappings.put(indexEntry.getKey(), mappings.build());
}
org.opensearch.action.admin.indices.mapping.get.GetMappingsResponse serverResponse =
new org.opensearch.action.admin.indices.mapping.get.GetMappingsResponse(allMappings.build());
builder.startObject();
serverResponse.toXContent(builder, params);
builder.endObject();
}
} }

View File

@ -68,7 +68,7 @@ public class SizeMappingIT extends OpenSearchIntegTestCase {
assertAcked(client().admin().indices().prepareCreate(index).addMapping(type, builder)); assertAcked(client().admin().indices().prepareCreate(index).addMapping(type, builder));
// check mapping again // check mapping again
assertSizeMappingEnabled(index, type, true); assertSizeMappingEnabled(index, true);
// update some field in the mapping // update some field in the mapping
XContentBuilder updateMappingBuilder = jsonBuilder().startObject() XContentBuilder updateMappingBuilder = jsonBuilder().startObject()
@ -87,7 +87,7 @@ public class SizeMappingIT extends OpenSearchIntegTestCase {
assertAcked(putMappingResponse); assertAcked(putMappingResponse);
// make sure size field is still in mapping // make sure size field is still in mapping
assertSizeMappingEnabled(index, type, true); assertSizeMappingEnabled(index, true);
} }
public void testThatSizeCanBeSwitchedOnAndOff() throws Exception { public void testThatSizeCanBeSwitchedOnAndOff() throws Exception {
@ -98,7 +98,7 @@ public class SizeMappingIT extends OpenSearchIntegTestCase {
assertAcked(client().admin().indices().prepareCreate(index).addMapping(type, builder)); assertAcked(client().admin().indices().prepareCreate(index).addMapping(type, builder));
// check mapping again // check mapping again
assertSizeMappingEnabled(index, type, true); assertSizeMappingEnabled(index, true);
// update some field in the mapping // update some field in the mapping
XContentBuilder updateMappingBuilder = jsonBuilder().startObject() XContentBuilder updateMappingBuilder = jsonBuilder().startObject()
@ -115,18 +115,17 @@ public class SizeMappingIT extends OpenSearchIntegTestCase {
assertAcked(putMappingResponse); assertAcked(putMappingResponse);
// make sure size field is still in mapping // make sure size field is still in mapping
assertSizeMappingEnabled(index, type, false); assertSizeMappingEnabled(index, false);
} }
private void assertSizeMappingEnabled(String index, String type, boolean enabled) throws IOException { private void assertSizeMappingEnabled(String index, boolean enabled) throws IOException {
String errMsg = String.format( String errMsg = String.format(
Locale.ROOT, Locale.ROOT,
"Expected size field mapping to be " + (enabled ? "enabled" : "disabled") + " for %s/%s", "Expected size field mapping to be " + (enabled ? "enabled" : "disabled") + " for %s",
index, index
type
); );
GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings(index).addTypes(type).get(); GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings(index).get();
Map<String, Object> mappingSource = getMappingsResponse.getMappings().get(index).get(type).getSourceAsMap(); Map<String, Object> mappingSource = getMappingsResponse.getMappings().get(index).getSourceAsMap();
assertThat(errMsg, mappingSource, hasKey("_size")); assertThat(errMsg, mappingSource, hasKey("_size"));
String sizeAsString = mappingSource.get("_size").toString(); String sizeAsString = mappingSource.get("_size").toString();
assertThat(sizeAsString, is(notNullValue())); assertThat(sizeAsString, is(notNullValue()));

View File

@ -1,70 +0,0 @@
---
setup:
- do:
indices.create:
include_type_name: true
index: test_index
body:
aliases:
test_alias: {}
test_blias: {}
mappings:
type_1: {}
settings:
number_of_shards: 1
number_of_replicas: 1
- do:
indices.create:
index: test_index_2
body:
settings:
number_of_shards: 1
number_of_replicas: 2
aliases:
test_alias: {}
test_blias: {}
- do:
indices.create:
index: test_index_3
body:
aliases:
test_alias: {}
test_blias: {}
- do:
indices.close:
index: test_index_3
- do:
cluster.health:
wait_for_status: yellow
---
"Test include_type_name":
- do:
indices.get:
include_type_name: true
index: test_index
- is_true: test_index.mappings
- is_true: test_index.mappings.type_1
- do:
indices.get:
include_type_name: false
index: test_index
- is_true: test_index.mappings
- is_false: test_index.mappings.type_1
---
"Test include_type_name dafaults to false":
- do:
indices.get:
index: test_index
- is_true: test_index.mappings
- is_false: test_index.mappings.type_1

View File

@ -148,12 +148,9 @@ public class CreateIndexIT extends OpenSearchIntegTestCase {
GetMappingsResponse response = client().admin().indices().prepareGetMappings("test").get(); GetMappingsResponse response = client().admin().indices().prepareGetMappings("test").get();
ImmutableOpenMap<String, MappingMetadata> mappings = response.mappings().get("test"); MappingMetadata mappings = response.mappings().get("test");
assertNotNull(mappings); assertNotNull(mappings);
assertFalse(mappings.sourceAsMap().isEmpty());
MappingMetadata metadata = mappings.get("_doc");
assertNotNull(metadata);
assertFalse(metadata.sourceAsMap().isEmpty());
} }
public void testEmptyNestedMappings() throws Exception { public void testEmptyNestedMappings() throws Exception {
@ -161,12 +158,10 @@ public class CreateIndexIT extends OpenSearchIntegTestCase {
GetMappingsResponse response = client().admin().indices().prepareGetMappings("test").get(); GetMappingsResponse response = client().admin().indices().prepareGetMappings("test").get();
ImmutableOpenMap<String, MappingMetadata> mappings = response.mappings().get("test"); MappingMetadata mappings = response.mappings().get("test");
assertNotNull(mappings); assertNotNull(mappings);
MappingMetadata metadata = mappings.get("_doc"); assertTrue(mappings.sourceAsMap().isEmpty());
assertNotNull(metadata);
assertTrue(metadata.sourceAsMap().isEmpty());
} }
public void testMappingParamAndNestedMismatch() throws Exception { public void testMappingParamAndNestedMismatch() throws Exception {
@ -190,12 +185,9 @@ public class CreateIndexIT extends OpenSearchIntegTestCase {
GetMappingsResponse response = client().admin().indices().prepareGetMappings("test").get(); GetMappingsResponse response = client().admin().indices().prepareGetMappings("test").get();
ImmutableOpenMap<String, MappingMetadata> mappings = response.mappings().get("test"); MappingMetadata mappings = response.mappings().get("test");
assertNotNull(mappings); assertNotNull(mappings);
assertTrue(mappings.sourceAsMap().isEmpty());
MappingMetadata metadata = mappings.get("_doc");
assertNotNull(metadata);
assertTrue(metadata.sourceAsMap().isEmpty());
} }
public void testInvalidShardCountSettings() throws Exception { public void testInvalidShardCountSettings() throws Exception {

View File

@ -35,6 +35,7 @@ package org.opensearch.action.admin.indices.get;
import com.carrotsearch.hppc.cursors.ObjectObjectCursor; import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
import org.opensearch.action.admin.indices.alias.Alias; import org.opensearch.action.admin.indices.alias.Alias;
import org.opensearch.action.admin.indices.get.GetIndexRequest.Feature; import org.opensearch.action.admin.indices.get.GetIndexRequest.Feature;
import org.opensearch.action.support.IndicesOptions;
import org.opensearch.cluster.metadata.AliasMetadata; import org.opensearch.cluster.metadata.AliasMetadata;
import org.opensearch.cluster.metadata.MappingMetadata; import org.opensearch.cluster.metadata.MappingMetadata;
import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.collect.ImmutableOpenMap;
@ -94,6 +95,19 @@ public class GetIndexIT extends OpenSearchIntegTestCase {
} }
} }
public void testUnknownIndexWithAllowNoIndices() {
GetIndexResponse response = client().admin()
.indices()
.prepareGetIndex()
.addIndices("missing_idx")
.setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN)
.get();
assertThat(response.indices(), notNullValue());
assertThat(response.indices().length, equalTo(0));
assertThat(response.mappings(), notNullValue());
assertThat(response.mappings().size(), equalTo(0));
}
public void testEmpty() { public void testEmpty() {
GetIndexResponse response = client().admin().indices().prepareGetIndex().addIndices("empty_idx").get(); GetIndexResponse response = client().admin().indices().prepareGetIndex().addIndices("empty_idx").get();
String[] indices = response.indices(); String[] indices = response.indices();
@ -263,24 +277,19 @@ public class GetIndexIT extends OpenSearchIntegTestCase {
} }
private void assertMappings(GetIndexResponse response, String indexName) { private void assertMappings(GetIndexResponse response, String indexName) {
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappings = response.mappings(); ImmutableOpenMap<String, MappingMetadata> mappings = response.mappings();
assertThat(mappings, notNullValue()); assertThat(mappings, notNullValue());
assertThat(mappings.size(), equalTo(1)); assertThat(mappings.size(), equalTo(1));
ImmutableOpenMap<String, MappingMetadata> indexMappings = mappings.get(indexName); MappingMetadata indexMappings = mappings.get(indexName);
assertThat(indexMappings, notNullValue()); assertThat(indexMappings, notNullValue());
assertThat(indexMappings.size(), equalTo(1));
MappingMetadata mapping = indexMappings.get("type1");
assertThat(mapping, notNullValue());
assertThat(mapping.type(), equalTo("type1"));
} }
private void assertEmptyOrOnlyDefaultMappings(GetIndexResponse response, String indexName) { private void assertEmptyOrOnlyDefaultMappings(GetIndexResponse response, String indexName) {
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappings = response.mappings(); ImmutableOpenMap<String, MappingMetadata> mappings = response.mappings();
assertThat(mappings, notNullValue()); assertThat(mappings, notNullValue());
assertThat(mappings.size(), equalTo(1)); assertThat(mappings.size(), equalTo(1));
ImmutableOpenMap<String, MappingMetadata> indexMappings = mappings.get(indexName); MappingMetadata indexMappings = mappings.get(indexName);
assertThat(indexMappings, notNullValue()); assertEquals(indexMappings, MappingMetadata.EMPTY_MAPPINGS);
assertThat(indexMappings.size(), equalTo(0));
} }
private void assertAliases(GetIndexResponse response, String indexName) { private void assertAliases(GetIndexResponse response, String indexName) {

View File

@ -35,12 +35,10 @@ package org.opensearch.client.documentation;
import org.opensearch.action.admin.indices.mapping.get.GetMappingsResponse; import org.opensearch.action.admin.indices.mapping.get.GetMappingsResponse;
import org.opensearch.client.Client; import org.opensearch.client.Client;
import org.opensearch.cluster.metadata.MappingMetadata; import org.opensearch.cluster.metadata.MappingMetadata;
import org.opensearch.common.collect.ImmutableOpenMap;
import org.opensearch.common.xcontent.XContentType; import org.opensearch.common.xcontent.XContentType;
import org.opensearch.test.OpenSearchIntegTestCase; import org.opensearch.test.OpenSearchIntegTestCase;
import static java.util.Collections.singletonMap; import static java.util.Collections.singletonMap;
import static org.hamcrest.Matchers.instanceOf;
/** /**
* This class is used to generate the Java indices administration documentation. * This class is used to generate the Java indices administration documentation.
@ -73,8 +71,6 @@ public class IndicesDocumentationIT extends OpenSearchIntegTestCase {
// end::index-with-mapping // end::index-with-mapping
GetMappingsResponse getMappingsResponse = client.admin().indices().prepareGetMappings("twitter").get(); GetMappingsResponse getMappingsResponse = client.admin().indices().prepareGetMappings("twitter").get();
assertEquals(1, getMappingsResponse.getMappings().size()); assertEquals(1, getMappingsResponse.getMappings().size());
ImmutableOpenMap<String, MappingMetadata> indexMapping = getMappingsResponse.getMappings().get("twitter");
assertThat(indexMapping.get("_doc"), instanceOf(MappingMetadata.class));
// we need to delete in order to create a fresh new index with another type // we need to delete in order to create a fresh new index with another type
client.admin().indices().prepareDelete("twitter").get(); client.admin().indices().prepareDelete("twitter").get();
@ -108,11 +104,8 @@ public class IndicesDocumentationIT extends OpenSearchIntegTestCase {
// end::putMapping-request-source // end::putMapping-request-source
getMappingsResponse = client.admin().indices().prepareGetMappings("twitter").get(); getMappingsResponse = client.admin().indices().prepareGetMappings("twitter").get();
assertEquals(1, getMappingsResponse.getMappings().size()); assertEquals(1, getMappingsResponse.getMappings().size());
indexMapping = getMappingsResponse.getMappings().get("twitter"); MappingMetadata indexMapping = getMappingsResponse.getMappings().get("twitter");
assertEquals( assertEquals(singletonMap("properties", singletonMap("name", singletonMap("type", "text"))), indexMapping.getSourceAsMap());
singletonMap("properties", singletonMap("name", singletonMap("type", "text"))),
indexMapping.get("_doc").getSourceAsMap()
);
} }
} }

View File

@ -292,24 +292,15 @@ public class SimpleClusterStateIT extends OpenSearchIntegTestCase {
.get() .get()
); );
ensureGreen(); // wait for green state, so its both green, and there are no more pending events ensureGreen(); // wait for green state, so its both green, and there are no more pending events
MappingMetadata masterMappingMetadata = client().admin() MappingMetadata masterMappingMetadata = client().admin().indices().prepareGetMappings("test").get().getMappings().get("test");
.indices()
.prepareGetMappings("test")
.setTypes("type")
.get()
.getMappings()
.get("test")
.get("type");
for (Client client : clients()) { for (Client client : clients()) {
MappingMetadata mappingMetadata = client.admin() MappingMetadata mappingMetadata = client.admin()
.indices() .indices()
.prepareGetMappings("test") .prepareGetMappings("test")
.setTypes("type")
.setLocal(true) .setLocal(true)
.get() .get()
.getMappings() .getMappings()
.get("test") .get("test");
.get("type");
assertThat(mappingMetadata.source().string(), equalTo(masterMappingMetadata.source().string())); assertThat(mappingMetadata.source().string(), equalTo(masterMappingMetadata.source().string()));
assertThat(mappingMetadata, equalTo(masterMappingMetadata)); assertThat(mappingMetadata, equalTo(masterMappingMetadata));
} }

View File

@ -53,7 +53,6 @@ import org.opensearch.cluster.routing.RoutingTable;
import org.opensearch.cluster.routing.ShardRouting; import org.opensearch.cluster.routing.ShardRouting;
import org.opensearch.cluster.routing.allocation.AllocationService; import org.opensearch.cluster.routing.allocation.AllocationService;
import org.opensearch.cluster.service.ClusterService; import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.collect.ImmutableOpenMap;
import org.opensearch.common.settings.Settings; import org.opensearch.common.settings.Settings;
import org.opensearch.common.unit.TimeValue; import org.opensearch.common.unit.TimeValue;
import org.opensearch.discovery.Discovery; import org.opensearch.discovery.Discovery;
@ -269,14 +268,7 @@ public class RareClusterStateIT extends OpenSearchIntegTestCase {
// ...and wait for mappings to be available on master // ...and wait for mappings to be available on master
assertBusy(() -> { assertBusy(() -> {
ImmutableOpenMap<String, MappingMetadata> indexMappings = client().admin() MappingMetadata typeMappings = client().admin().indices().prepareGetMappings("index").get().getMappings().get("index");
.indices()
.prepareGetMappings("index")
.get()
.getMappings()
.get("index");
assertNotNull(indexMappings);
MappingMetadata typeMappings = indexMappings.get("type");
assertNotNull(typeMappings); assertNotNull(typeMappings);
Object properties; Object properties;
try { try {

View File

@ -147,11 +147,9 @@ public class MetadataNodesIT extends OpenSearchIntegTestCase {
) )
.get(); .get();
GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings(index).addTypes("_doc").get(); GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings(index).get();
assertNotNull( assertNotNull(
((Map<String, ?>) (getMappingsResponse.getMappings().get(index).get("_doc").getSourceAsMap().get("properties"))).get( ((Map<String, ?>) (getMappingsResponse.getMappings().get(index).getSourceAsMap().get("properties"))).get("integer_field")
"integer_field"
)
); );
// make sure it was also written on red node although index is closed // make sure it was also written on red node although index is closed
@ -187,11 +185,9 @@ public class MetadataNodesIT extends OpenSearchIntegTestCase {
) )
.get(); .get();
getMappingsResponse = client().admin().indices().prepareGetMappings(index).addTypes("_doc").get(); getMappingsResponse = client().admin().indices().prepareGetMappings(index).get();
assertNotNull( assertNotNull(
((Map<String, ?>) (getMappingsResponse.getMappings().get(index).get("_doc").getSourceAsMap().get("properties"))).get( ((Map<String, ?>) (getMappingsResponse.getMappings().get(index).getSourceAsMap().get("properties"))).get("float_field")
"float_field"
)
); );
// make sure it was also written on red node although index is closed // make sure it was also written on red node although index is closed

View File

@ -160,7 +160,7 @@ public class HiddenIndexIT extends OpenSearchIntegTestCase {
GetMappingsResponse mappingsResponse = client().admin().indices().prepareGetMappings("a_hidden_index").get(); GetMappingsResponse mappingsResponse = client().admin().indices().prepareGetMappings("a_hidden_index").get();
assertThat(mappingsResponse.mappings().size(), is(1)); assertThat(mappingsResponse.mappings().size(), is(1));
MappingMetadata mappingMetadata = mappingsResponse.mappings().get("a_hidden_index").get("_doc"); MappingMetadata mappingMetadata = mappingsResponse.mappings().get("a_hidden_index");
assertNotNull(mappingMetadata); assertNotNull(mappingMetadata);
Map<String, Object> propertiesMap = (Map<String, Object>) mappingMetadata.getSourceAsMap().get("properties"); Map<String, Object> propertiesMap = (Map<String, Object>) mappingMetadata.getSourceAsMap().get("properties");
assertNotNull(propertiesMap); assertNotNull(propertiesMap);

View File

@ -38,7 +38,6 @@ import org.opensearch.action.index.IndexRequestBuilder;
import org.opensearch.cluster.ClusterState; import org.opensearch.cluster.ClusterState;
import org.opensearch.cluster.ClusterStateUpdateTask; import org.opensearch.cluster.ClusterStateUpdateTask;
import org.opensearch.cluster.metadata.MappingMetadata; import org.opensearch.cluster.metadata.MappingMetadata;
import org.opensearch.common.collect.ImmutableOpenMap;
import org.opensearch.cluster.service.ClusterService; import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.settings.Settings; import org.opensearch.common.settings.Settings;
import org.opensearch.common.unit.TimeValue; import org.opensearch.common.unit.TimeValue;
@ -94,11 +93,9 @@ public class DynamicMappingIT extends OpenSearchIntegTestCase {
} }
private static void assertMappingsHaveField(GetMappingsResponse mappings, String index, String field) throws IOException { private static void assertMappingsHaveField(GetMappingsResponse mappings, String index, String field) throws IOException {
ImmutableOpenMap<String, MappingMetadata> indexMappings = mappings.getMappings().get("index"); MappingMetadata indexMappings = mappings.getMappings().get("index");
assertNotNull(indexMappings); assertNotNull(indexMappings);
MappingMetadata typeMappings = indexMappings.get(MapperService.SINGLE_MAPPING_NAME); Map<String, Object> typeMappingsMap = indexMappings.getSourceAsMap();
assertNotNull(typeMappings);
Map<String, Object> typeMappingsMap = typeMappings.getSourceAsMap();
Map<String, Object> properties = (Map<String, Object>) typeMappingsMap.get("properties"); Map<String, Object> properties = (Map<String, Object>) typeMappingsMap.get("properties");
assertTrue("Could not find [" + field + "] in " + typeMappingsMap.toString(), properties.containsKey(field)); assertTrue("Could not find [" + field + "] in " + typeMappingsMap.toString(), properties.containsKey(field));
} }

View File

@ -61,7 +61,7 @@ public class MultiFieldsIntegrationIT extends OpenSearchIntegTestCase {
assertAcked(client().admin().indices().prepareCreate("my-index").addMapping("my-type", createTypeSource())); assertAcked(client().admin().indices().prepareCreate("my-index").addMapping("my-type", createTypeSource()));
GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("my-index").get(); GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("my-index").get();
MappingMetadata mappingMetadata = getMappingsResponse.mappings().get("my-index").get("my-type"); MappingMetadata mappingMetadata = getMappingsResponse.mappings().get("my-index");
assertThat(mappingMetadata, not(nullValue())); assertThat(mappingMetadata, not(nullValue()));
Map<String, Object> mappingSource = mappingMetadata.sourceAsMap(); Map<String, Object> mappingSource = mappingMetadata.sourceAsMap();
Map<String, Object> titleFields = ((Map<String, Object>) XContentMapValues.extractValue("properties.title.fields", mappingSource)); Map<String, Object> titleFields = ((Map<String, Object>) XContentMapValues.extractValue("properties.title.fields", mappingSource));
@ -79,7 +79,7 @@ public class MultiFieldsIntegrationIT extends OpenSearchIntegTestCase {
assertAcked(client().admin().indices().preparePutMapping("my-index").setType("my-type").setSource(createPutMappingSource())); assertAcked(client().admin().indices().preparePutMapping("my-index").setType("my-type").setSource(createPutMappingSource()));
getMappingsResponse = client().admin().indices().prepareGetMappings("my-index").get(); getMappingsResponse = client().admin().indices().prepareGetMappings("my-index").get();
mappingMetadata = getMappingsResponse.mappings().get("my-index").get("my-type"); mappingMetadata = getMappingsResponse.mappings().get("my-index");
assertThat(mappingMetadata, not(nullValue())); assertThat(mappingMetadata, not(nullValue()));
mappingSource = mappingMetadata.sourceAsMap(); mappingSource = mappingMetadata.sourceAsMap();
assertThat(((Map<String, Object>) XContentMapValues.extractValue("properties.title", mappingSource)).size(), equalTo(2)); assertThat(((Map<String, Object>) XContentMapValues.extractValue("properties.title", mappingSource)).size(), equalTo(2));
@ -101,7 +101,7 @@ public class MultiFieldsIntegrationIT extends OpenSearchIntegTestCase {
assertAcked(client().admin().indices().prepareCreate("my-index").addMapping("my-type", createMappingSource("geo_point"))); assertAcked(client().admin().indices().prepareCreate("my-index").addMapping("my-type", createMappingSource("geo_point")));
GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("my-index").get(); GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("my-index").get();
MappingMetadata mappingMetadata = getMappingsResponse.mappings().get("my-index").get("my-type"); MappingMetadata mappingMetadata = getMappingsResponse.mappings().get("my-index");
assertThat(mappingMetadata, not(nullValue())); assertThat(mappingMetadata, not(nullValue()));
Map<String, Object> mappingSource = mappingMetadata.sourceAsMap(); Map<String, Object> mappingSource = mappingMetadata.sourceAsMap();
Map<String, Object> aField = ((Map<String, Object>) XContentMapValues.extractValue("properties.a", mappingSource)); Map<String, Object> aField = ((Map<String, Object>) XContentMapValues.extractValue("properties.a", mappingSource));
@ -130,7 +130,7 @@ public class MultiFieldsIntegrationIT extends OpenSearchIntegTestCase {
assertAcked(client().admin().indices().prepareCreate("my-index").addMapping("my-type", createMappingSource("completion"))); assertAcked(client().admin().indices().prepareCreate("my-index").addMapping("my-type", createMappingSource("completion")));
GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("my-index").get(); GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("my-index").get();
MappingMetadata mappingMetadata = getMappingsResponse.mappings().get("my-index").get("my-type"); MappingMetadata mappingMetadata = getMappingsResponse.mappings().get("my-index");
assertThat(mappingMetadata, not(nullValue())); assertThat(mappingMetadata, not(nullValue()));
Map<String, Object> mappingSource = mappingMetadata.sourceAsMap(); Map<String, Object> mappingSource = mappingMetadata.sourceAsMap();
Map<String, Object> aField = ((Map<String, Object>) XContentMapValues.extractValue("properties.a", mappingSource)); Map<String, Object> aField = ((Map<String, Object>) XContentMapValues.extractValue("properties.a", mappingSource));
@ -152,7 +152,7 @@ public class MultiFieldsIntegrationIT extends OpenSearchIntegTestCase {
assertAcked(client().admin().indices().prepareCreate("my-index").addMapping("my-type", createMappingSource("ip"))); assertAcked(client().admin().indices().prepareCreate("my-index").addMapping("my-type", createMappingSource("ip")));
GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("my-index").get(); GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("my-index").get();
MappingMetadata mappingMetadata = getMappingsResponse.mappings().get("my-index").get("my-type"); MappingMetadata mappingMetadata = getMappingsResponse.mappings().get("my-index");
assertThat(mappingMetadata, not(nullValue())); assertThat(mappingMetadata, not(nullValue()));
Map<String, Object> mappingSource = mappingMetadata.sourceAsMap(); Map<String, Object> mappingSource = mappingMetadata.sourceAsMap();
Map<String, Object> aField = ((Map<String, Object>) XContentMapValues.extractValue("properties.a", mappingSource)); Map<String, Object> aField = ((Map<String, Object>) XContentMapValues.extractValue("properties.a", mappingSource));

View File

@ -603,26 +603,26 @@ public class IndicesOptionsIntegrationIT extends OpenSearchIntegTestCase {
} }
verify(client().admin().indices().preparePutMapping("foo").setType("type").setSource("field", "type=text"), false); verify(client().admin().indices().preparePutMapping("foo").setType("type").setSource("field", "type=text"), false);
assertThat(client().admin().indices().prepareGetMappings("foo").get().mappings().get("foo").get("type"), notNullValue()); assertThat(client().admin().indices().prepareGetMappings("foo").get().mappings().get("foo"), notNullValue());
verify(client().admin().indices().preparePutMapping("b*").setType("type").setSource("field", "type=text"), false); verify(client().admin().indices().preparePutMapping("b*").setType("type").setSource("field", "type=text"), false);
assertThat(client().admin().indices().prepareGetMappings("bar").get().mappings().get("bar").get("type"), notNullValue()); assertThat(client().admin().indices().prepareGetMappings("bar").get().mappings().get("bar"), notNullValue());
assertThat(client().admin().indices().prepareGetMappings("barbaz").get().mappings().get("barbaz").get("type"), notNullValue()); assertThat(client().admin().indices().prepareGetMappings("barbaz").get().mappings().get("barbaz"), notNullValue());
verify(client().admin().indices().preparePutMapping("_all").setType("type").setSource("field", "type=text"), false); verify(client().admin().indices().preparePutMapping("_all").setType("type").setSource("field", "type=text"), false);
assertThat(client().admin().indices().prepareGetMappings("foo").get().mappings().get("foo").get("type"), notNullValue()); assertThat(client().admin().indices().prepareGetMappings("foo").get().mappings().get("foo"), notNullValue());
assertThat(client().admin().indices().prepareGetMappings("foobar").get().mappings().get("foobar").get("type"), notNullValue()); assertThat(client().admin().indices().prepareGetMappings("foobar").get().mappings().get("foobar"), notNullValue());
assertThat(client().admin().indices().prepareGetMappings("bar").get().mappings().get("bar").get("type"), notNullValue()); assertThat(client().admin().indices().prepareGetMappings("bar").get().mappings().get("bar"), notNullValue());
assertThat(client().admin().indices().prepareGetMappings("barbaz").get().mappings().get("barbaz").get("type"), notNullValue()); assertThat(client().admin().indices().prepareGetMappings("barbaz").get().mappings().get("barbaz"), notNullValue());
verify(client().admin().indices().preparePutMapping().setType("type").setSource("field", "type=text"), false); verify(client().admin().indices().preparePutMapping().setType("type").setSource("field", "type=text"), false);
assertThat(client().admin().indices().prepareGetMappings("foo").get().mappings().get("foo").get("type"), notNullValue()); assertThat(client().admin().indices().prepareGetMappings("foo").get().mappings().get("foo"), notNullValue());
assertThat(client().admin().indices().prepareGetMappings("foobar").get().mappings().get("foobar").get("type"), notNullValue()); assertThat(client().admin().indices().prepareGetMappings("foobar").get().mappings().get("foobar"), notNullValue());
assertThat(client().admin().indices().prepareGetMappings("bar").get().mappings().get("bar").get("type"), notNullValue()); assertThat(client().admin().indices().prepareGetMappings("bar").get().mappings().get("bar"), notNullValue());
assertThat(client().admin().indices().prepareGetMappings("barbaz").get().mappings().get("barbaz").get("type"), notNullValue()); assertThat(client().admin().indices().prepareGetMappings("barbaz").get().mappings().get("barbaz"), notNullValue());
verify(client().admin().indices().preparePutMapping("c*").setType("type").setSource("field", "type=text"), true); verify(client().admin().indices().preparePutMapping("c*").setType("type").setSource("field", "type=text"), true);
assertAcked(client().admin().indices().prepareClose("barbaz").get()); assertAcked(client().admin().indices().prepareClose("barbaz").get());
verify(client().admin().indices().preparePutMapping("barbaz").setType("type").setSource("field", "type=text"), false); verify(client().admin().indices().preparePutMapping("barbaz").setType("type").setSource("field", "type=text"), false);
assertThat(client().admin().indices().prepareGetMappings("barbaz").get().mappings().get("barbaz").get("type"), notNullValue()); assertThat(client().admin().indices().prepareGetMappings("barbaz").get().mappings().get("barbaz"), notNullValue());
} }
public static final class TestPlugin extends Plugin { public static final class TestPlugin extends Plugin {

View File

@ -34,6 +34,7 @@ package org.opensearch.indices.mapping;
import org.opensearch.action.admin.cluster.health.ClusterHealthResponse; import org.opensearch.action.admin.cluster.health.ClusterHealthResponse;
import org.opensearch.action.admin.indices.mapping.get.GetMappingsResponse; import org.opensearch.action.admin.indices.mapping.get.GetMappingsResponse;
import org.opensearch.cluster.metadata.MappingMetadata;
import org.opensearch.common.Priority; import org.opensearch.common.Priority;
import org.opensearch.common.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentBuilder;
import org.opensearch.plugins.Plugin; import org.opensearch.plugins.Plugin;
@ -66,7 +67,7 @@ public class SimpleGetMappingsIT extends OpenSearchIntegTestCase {
createIndex("index"); createIndex("index");
GetMappingsResponse response = client().admin().indices().prepareGetMappings().execute().actionGet(); GetMappingsResponse response = client().admin().indices().prepareGetMappings().execute().actionGet();
assertThat(response.mappings().containsKey("index"), equalTo(true)); assertThat(response.mappings().containsKey("index"), equalTo(true));
assertThat(response.mappings().get("index").size(), equalTo(0)); assertEquals(MappingMetadata.EMPTY_MAPPINGS, response.mappings().get("index"));
} }
private XContentBuilder getMappingForType(String type) throws IOException { private XContentBuilder getMappingForType(String type) throws IOException {
@ -97,50 +98,19 @@ public class SimpleGetMappingsIT extends OpenSearchIntegTestCase {
// Get all mappings // Get all mappings
GetMappingsResponse response = client().admin().indices().prepareGetMappings().execute().actionGet(); GetMappingsResponse response = client().admin().indices().prepareGetMappings().execute().actionGet();
assertThat(response.mappings().size(), equalTo(2)); assertThat(response.mappings().size(), equalTo(2));
assertThat(response.mappings().get("indexa").size(), equalTo(1)); assertThat(response.mappings().get("indexa"), notNullValue());
assertThat(response.mappings().get("indexa").get("typeA"), notNullValue()); assertThat(response.mappings().get("indexb"), notNullValue());
assertThat(response.mappings().get("indexb").size(), equalTo(1));
assertThat(response.mappings().get("indexb").get("typeA"), notNullValue());
// Get all mappings, via wildcard support // Get all mappings, via wildcard support
response = client().admin().indices().prepareGetMappings("*").setTypes("*").execute().actionGet(); response = client().admin().indices().prepareGetMappings("*").execute().actionGet();
assertThat(response.mappings().size(), equalTo(2)); assertThat(response.mappings().size(), equalTo(2));
assertThat(response.mappings().get("indexa").size(), equalTo(1)); assertThat(response.mappings().get("indexa"), notNullValue());
assertThat(response.mappings().get("indexa").get("typeA"), notNullValue()); assertThat(response.mappings().get("indexb"), notNullValue());
assertThat(response.mappings().get("indexb").size(), equalTo(1));
assertThat(response.mappings().get("indexb").get("typeA"), notNullValue());
// Get all typeA mappings in all indices // Get mappings in indexa
response = client().admin().indices().prepareGetMappings("*").setTypes("typeA").execute().actionGet();
assertThat(response.mappings().size(), equalTo(2));
assertThat(response.mappings().get("indexa").size(), equalTo(1));
assertThat(response.mappings().get("indexa").get("typeA"), notNullValue());
assertThat(response.mappings().get("indexb").size(), equalTo(1));
assertThat(response.mappings().get("indexb").get("typeA"), notNullValue());
// Get all mappings in indexa
response = client().admin().indices().prepareGetMappings("indexa").execute().actionGet(); response = client().admin().indices().prepareGetMappings("indexa").execute().actionGet();
assertThat(response.mappings().size(), equalTo(1)); assertThat(response.mappings().size(), equalTo(1));
assertThat(response.mappings().get("indexa").size(), equalTo(1)); assertThat(response.mappings().get("indexa"), notNullValue());
assertThat(response.mappings().get("indexa").get("typeA"), notNullValue());
// Get all mappings beginning with A* in indexa
response = client().admin().indices().prepareGetMappings("indexa").setTypes("*A").execute().actionGet();
assertThat(response.mappings().size(), equalTo(1));
assertThat(response.mappings().get("indexa").size(), equalTo(1));
assertThat(response.mappings().get("indexa").get("typeA"), notNullValue());
// Get all mappings beginning with B* in all indices
response = client().admin().indices().prepareGetMappings().setTypes("B*").execute().actionGet();
assertThat(response.mappings().size(), equalTo(0));
// Get all mappings beginning with B* and A* in all indices
response = client().admin().indices().prepareGetMappings().setTypes("B*", "*A").execute().actionGet();
assertThat(response.mappings().size(), equalTo(2));
assertThat(response.mappings().get("indexa").size(), equalTo(1));
assertThat(response.mappings().get("indexa").get("typeA"), notNullValue());
assertThat(response.mappings().get("indexb").size(), equalTo(1));
assertThat(response.mappings().get("indexb").get("typeA"), notNullValue());
} }
public void testGetMappingsWithBlocks() throws IOException { public void testGetMappingsWithBlocks() throws IOException {
@ -152,7 +122,7 @@ public class SimpleGetMappingsIT extends OpenSearchIntegTestCase {
enableIndexBlock("test", block); enableIndexBlock("test", block);
GetMappingsResponse response = client().admin().indices().prepareGetMappings().execute().actionGet(); GetMappingsResponse response = client().admin().indices().prepareGetMappings().execute().actionGet();
assertThat(response.mappings().size(), equalTo(1)); assertThat(response.mappings().size(), equalTo(1));
assertThat(response.mappings().get("test").size(), equalTo(1)); assertNotNull(response.mappings().get("test"));
} finally { } finally {
disableIndexBlock("test", block); disableIndexBlock("test", block);
} }

View File

@ -41,7 +41,6 @@ import org.opensearch.client.Client;
import org.opensearch.cluster.action.index.MappingUpdatedAction; import org.opensearch.cluster.action.index.MappingUpdatedAction;
import org.opensearch.cluster.metadata.MappingMetadata; import org.opensearch.cluster.metadata.MappingMetadata;
import org.opensearch.common.Priority; import org.opensearch.common.Priority;
import org.opensearch.common.collect.ImmutableOpenMap;
import org.opensearch.common.settings.Settings; import org.opensearch.common.settings.Settings;
import org.opensearch.common.unit.TimeValue; import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.xcontent.XContentType; import org.opensearch.common.xcontent.XContentType;
@ -163,7 +162,7 @@ public class UpdateMappingIntegrationIT extends OpenSearchIntegTestCase {
GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("test").execute().actionGet(); GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("test").execute().actionGet();
assertThat( assertThat(
getMappingsResponse.mappings().get("test").get("_doc").source().toString(), getMappingsResponse.mappings().get("test").source().toString(),
equalTo("{\"_doc\":{\"properties\":{\"body\":{\"type\":\"text\"},\"date\":{\"type\":\"integer\"}}}}") equalTo("{\"_doc\":{\"properties\":{\"body\":{\"type\":\"text\"},\"date\":{\"type\":\"integer\"}}}}")
); );
} }
@ -189,7 +188,7 @@ public class UpdateMappingIntegrationIT extends OpenSearchIntegTestCase {
GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("test").execute().actionGet(); GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("test").execute().actionGet();
assertThat( assertThat(
getMappingsResponse.mappings().get("test").get("_doc").source().toString(), getMappingsResponse.mappings().get("test").source().toString(),
equalTo("{\"_doc\":{\"properties\":{\"date\":{\"type\":\"integer\"}}}}") equalTo("{\"_doc\":{\"properties\":{\"date\":{\"type\":\"integer\"}}}}")
); );
} }
@ -313,10 +312,9 @@ public class UpdateMappingIntegrationIT extends OpenSearchIntegTestCase {
assertThat(response.isAcknowledged(), equalTo(true)); assertThat(response.isAcknowledged(), equalTo(true));
GetMappingsResponse getMappingResponse = client2.admin().indices().prepareGetMappings(indexName).get(); GetMappingsResponse getMappingResponse = client2.admin().indices().prepareGetMappings(indexName).get();
ImmutableOpenMap<String, MappingMetadata> mappings = getMappingResponse.getMappings().get(indexName); MappingMetadata mappings = getMappingResponse.getMappings().get(indexName);
assertThat(mappings.containsKey(typeName), equalTo(true));
assertThat( assertThat(
((Map<String, Object>) mappings.get(typeName).getSourceAsMap().get("properties")).keySet(), ((Map<String, Object>) mappings.getSourceAsMap().get("properties")).keySet(),
Matchers.hasItem(fieldName) Matchers.hasItem(fieldName)
); );
} }
@ -399,12 +397,10 @@ public class UpdateMappingIntegrationIT extends OpenSearchIntegTestCase {
*/ */
private void assertMappingOnMaster(final String index, final String... fieldNames) { private void assertMappingOnMaster(final String index, final String... fieldNames) {
GetMappingsResponse response = client().admin().indices().prepareGetMappings(index).get(); GetMappingsResponse response = client().admin().indices().prepareGetMappings(index).get();
ImmutableOpenMap<String, MappingMetadata> mappings = response.getMappings().get(index); MappingMetadata mappings = response.getMappings().get(index);
assertThat(mappings, notNullValue()); assertThat(mappings, notNullValue());
MappingMetadata mappingMetadata = mappings.get(MapperService.SINGLE_MAPPING_NAME); Map<String, Object> mappingSource = mappings.getSourceAsMap();
assertThat(mappingMetadata, notNullValue());
Map<String, Object> mappingSource = mappingMetadata.getSourceAsMap();
assertFalse(mappingSource.isEmpty()); assertFalse(mappingSource.isEmpty());
assertTrue(mappingSource.containsKey("properties")); assertTrue(mappingSource.containsKey("properties"));
@ -414,7 +410,7 @@ public class UpdateMappingIntegrationIT extends OpenSearchIntegTestCase {
fieldName = fieldName.replace(".", ".properties."); fieldName = fieldName.replace(".", ".properties.");
} }
assertThat( assertThat(
"field " + fieldName + " doesn't exists in mapping " + mappingMetadata.source().string(), "field " + fieldName + " doesn't exists in mapping " + mappings.source().string(),
XContentMapValues.extractValue(fieldName, mappingProperties), XContentMapValues.extractValue(fieldName, mappingProperties),
notNullValue() notNullValue()
); );

View File

@ -33,8 +33,8 @@
package org.opensearch.action.admin.indices.get; package org.opensearch.action.admin.indices.get;
import com.carrotsearch.hppc.cursors.ObjectObjectCursor; import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
import org.apache.lucene.util.CollectionUtil;
import org.opensearch.LegacyESVersion; import org.opensearch.LegacyESVersion;
import org.opensearch.Version;
import org.opensearch.action.ActionResponse; import org.opensearch.action.ActionResponse;
import org.opensearch.cluster.metadata.AliasMetadata; import org.opensearch.cluster.metadata.AliasMetadata;
import org.opensearch.cluster.metadata.MappingMetadata; import org.opensearch.cluster.metadata.MappingMetadata;
@ -45,27 +45,21 @@ import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.settings.Settings; import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.ToXContentObject; import org.opensearch.common.xcontent.ToXContentObject;
import org.opensearch.common.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentParser; import org.opensearch.index.mapper.MapperService;
import org.opensearch.common.xcontent.XContentParser.Token;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import static org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.opensearch.rest.BaseRestHandler.DEFAULT_INCLUDE_TYPE_NAME_POLICY;
import static org.opensearch.rest.BaseRestHandler.INCLUDE_TYPE_NAME_PARAMETER;
/** /**
* A response for a get index action. * A response for a get index action.
*/ */
public class GetIndexResponse extends ActionResponse implements ToXContentObject { public class GetIndexResponse extends ActionResponse implements ToXContentObject {
private ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappings = ImmutableOpenMap.of(); private ImmutableOpenMap<String, MappingMetadata> mappings = ImmutableOpenMap.of();
private ImmutableOpenMap<String, List<AliasMetadata>> aliases = ImmutableOpenMap.of(); private ImmutableOpenMap<String, List<AliasMetadata>> aliases = ImmutableOpenMap.of();
private ImmutableOpenMap<String, Settings> settings = ImmutableOpenMap.of(); private ImmutableOpenMap<String, Settings> settings = ImmutableOpenMap.of();
private ImmutableOpenMap<String, Settings> defaultSettings = ImmutableOpenMap.of(); private ImmutableOpenMap<String, Settings> defaultSettings = ImmutableOpenMap.of();
@ -74,7 +68,7 @@ public class GetIndexResponse extends ActionResponse implements ToXContentObject
public GetIndexResponse( public GetIndexResponse(
String[] indices, String[] indices,
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappings, ImmutableOpenMap<String, MappingMetadata> mappings,
ImmutableOpenMap<String, List<AliasMetadata>> aliases, ImmutableOpenMap<String, List<AliasMetadata>> aliases,
ImmutableOpenMap<String, Settings> settings, ImmutableOpenMap<String, Settings> settings,
ImmutableOpenMap<String, Settings> defaultSettings, ImmutableOpenMap<String, Settings> defaultSettings,
@ -105,15 +99,26 @@ public class GetIndexResponse extends ActionResponse implements ToXContentObject
this.indices = in.readStringArray(); this.indices = in.readStringArray();
int mappingsSize = in.readVInt(); int mappingsSize = in.readVInt();
ImmutableOpenMap.Builder<String, ImmutableOpenMap<String, MappingMetadata>> mappingsMapBuilder = ImmutableOpenMap.builder(); ImmutableOpenMap.Builder<String, MappingMetadata> mappingsMapBuilder = ImmutableOpenMap.builder();
for (int i = 0; i < mappingsSize; i++) { for (int i = 0; i < mappingsSize; i++) {
String key = in.readString(); String index = in.readString();
int valueSize = in.readVInt(); if (in.getVersion().before(Version.V_2_0_0)) {
ImmutableOpenMap.Builder<String, MappingMetadata> mappingEntryBuilder = ImmutableOpenMap.builder(); int numMappings = in.readVInt();
for (int j = 0; j < valueSize; j++) { if (numMappings == 0) {
mappingEntryBuilder.put(in.readString(), new MappingMetadata(in)); mappingsMapBuilder.put(index, MappingMetadata.EMPTY_MAPPINGS);
} else if (numMappings == 1) {
String type = in.readString();
if (MapperService.SINGLE_MAPPING_NAME.equals(type) == false) {
throw new IllegalStateException("Expected " + MapperService.SINGLE_MAPPING_NAME + " but got [" + type + "]");
}
mappingsMapBuilder.put(index, new MappingMetadata(in));
} else {
throw new IllegalStateException("Expected 0 or 1 mappings but got: " + numMappings);
}
} else {
final MappingMetadata metadata = in.readOptionalWriteable(MappingMetadata::new);
mappingsMapBuilder.put(index, metadata != null ? metadata : MappingMetadata.EMPTY_MAPPINGS);
} }
mappingsMapBuilder.put(key, mappingEntryBuilder.build());
} }
mappings = mappingsMapBuilder.build(); mappings = mappingsMapBuilder.build();
@ -163,11 +168,11 @@ public class GetIndexResponse extends ActionResponse implements ToXContentObject
return indices(); return indices();
} }
public ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappings() { public ImmutableOpenMap<String, MappingMetadata> mappings() {
return mappings; return mappings;
} }
public ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> getMappings() { public ImmutableOpenMap<String, MappingMetadata> getMappings() {
return mappings(); return mappings();
} }
@ -235,12 +240,16 @@ public class GetIndexResponse extends ActionResponse implements ToXContentObject
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
out.writeStringArray(indices); out.writeStringArray(indices);
out.writeVInt(mappings.size()); out.writeVInt(mappings.size());
for (ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetadata>> indexEntry : mappings) { for (ObjectObjectCursor<String, MappingMetadata> indexEntry : mappings) {
out.writeString(indexEntry.key); out.writeString(indexEntry.key);
out.writeVInt(indexEntry.value.size()); if (out.getVersion().before(Version.V_2_0_0)) {
for (ObjectObjectCursor<String, MappingMetadata> mappingEntry : indexEntry.value) { out.writeVInt(indexEntry.value == MappingMetadata.EMPTY_MAPPINGS ? 0 : 1);
out.writeString(mappingEntry.key); if (indexEntry.value != MappingMetadata.EMPTY_MAPPINGS) {
mappingEntry.value.writeTo(out); out.writeString(MapperService.SINGLE_MAPPING_NAME);
indexEntry.value.writeTo(out);
}
} else {
out.writeOptionalWriteable(indexEntry.value);
} }
} }
out.writeVInt(aliases.size()); out.writeVInt(aliases.size());
@ -286,29 +295,11 @@ public class GetIndexResponse extends ActionResponse implements ToXContentObject
} }
builder.endObject(); builder.endObject();
ImmutableOpenMap<String, MappingMetadata> indexMappings = mappings.get(index); MappingMetadata indexMappings = mappings.get(index);
boolean includeTypeName = params.paramAsBoolean(INCLUDE_TYPE_NAME_PARAMETER, DEFAULT_INCLUDE_TYPE_NAME_POLICY); if (indexMappings == null) {
if (includeTypeName) { builder.startObject("mappings").endObject();
builder.startObject("mappings");
if (indexMappings != null) {
for (final ObjectObjectCursor<String, MappingMetadata> typeEntry : indexMappings) {
builder.field(typeEntry.key);
builder.map(typeEntry.value.sourceAsMap());
}
}
builder.endObject();
} else { } else {
MappingMetadata mappings = null; builder.field("mappings", indexMappings.sourceAsMap());
for (final ObjectObjectCursor<String, MappingMetadata> typeEntry : indexMappings) {
assert mappings == null;
mappings = typeEntry.value;
}
if (mappings == null) {
// no mappings yet
builder.startObject("mappings").endObject();
} else {
builder.field("mappings", mappings.sourceAsMap());
}
} }
builder.startObject("settings"); builder.startObject("settings");
@ -337,141 +328,6 @@ public class GetIndexResponse extends ActionResponse implements ToXContentObject
return builder; return builder;
} }
private static List<AliasMetadata> parseAliases(XContentParser parser) throws IOException {
List<AliasMetadata> indexAliases = new ArrayList<>();
// We start at START_OBJECT since parseIndexEntry ensures that
while (parser.nextToken() != Token.END_OBJECT) {
ensureExpectedToken(Token.FIELD_NAME, parser.currentToken(), parser);
indexAliases.add(AliasMetadata.Builder.fromXContent(parser));
}
return indexAliases;
}
private static ImmutableOpenMap<String, MappingMetadata> parseMappings(XContentParser parser) throws IOException {
ImmutableOpenMap.Builder<String, MappingMetadata> indexMappings = ImmutableOpenMap.builder();
// We start at START_OBJECT since parseIndexEntry ensures that
while (parser.nextToken() != Token.END_OBJECT) {
ensureExpectedToken(Token.FIELD_NAME, parser.currentToken(), parser);
parser.nextToken();
if (parser.currentToken() == Token.START_OBJECT) {
String mappingType = parser.currentName();
indexMappings.put(mappingType, new MappingMetadata(mappingType, parser.map()));
} else if (parser.currentToken() == Token.START_ARRAY) {
parser.skipChildren();
}
}
return indexMappings.build();
}
private static IndexEntry parseIndexEntry(XContentParser parser) throws IOException {
List<AliasMetadata> indexAliases = null;
ImmutableOpenMap<String, MappingMetadata> indexMappings = null;
Settings indexSettings = null;
Settings indexDefaultSettings = null;
String dataStream = null;
// We start at START_OBJECT since fromXContent ensures that
while (parser.nextToken() != Token.END_OBJECT) {
ensureExpectedToken(Token.FIELD_NAME, parser.currentToken(), parser);
parser.nextToken();
if (parser.currentToken() == Token.START_OBJECT) {
switch (parser.currentName()) {
case "aliases":
indexAliases = parseAliases(parser);
break;
case "mappings":
indexMappings = parseMappings(parser);
break;
case "settings":
indexSettings = Settings.fromXContent(parser);
break;
case "defaults":
indexDefaultSettings = Settings.fromXContent(parser);
break;
default:
parser.skipChildren();
}
} else if (parser.currentToken() == Token.VALUE_STRING) {
if (parser.currentName().equals("data_stream")) {
dataStream = parser.text();
}
parser.skipChildren();
} else if (parser.currentToken() == Token.START_ARRAY) {
parser.skipChildren();
}
}
return new IndexEntry(indexAliases, indexMappings, indexSettings, indexDefaultSettings, dataStream);
}
// This is just an internal container to make stuff easier for returning
private static class IndexEntry {
List<AliasMetadata> indexAliases = new ArrayList<>();
ImmutableOpenMap<String, MappingMetadata> indexMappings = ImmutableOpenMap.of();
Settings indexSettings = Settings.EMPTY;
Settings indexDefaultSettings = Settings.EMPTY;
String dataStream;
IndexEntry(
List<AliasMetadata> indexAliases,
ImmutableOpenMap<String, MappingMetadata> indexMappings,
Settings indexSettings,
Settings indexDefaultSettings,
String dataStream
) {
if (indexAliases != null) this.indexAliases = indexAliases;
if (indexMappings != null) this.indexMappings = indexMappings;
if (indexSettings != null) this.indexSettings = indexSettings;
if (indexDefaultSettings != null) this.indexDefaultSettings = indexDefaultSettings;
if (dataStream != null) this.dataStream = dataStream;
}
}
public static GetIndexResponse fromXContent(XContentParser parser) throws IOException {
ImmutableOpenMap.Builder<String, List<AliasMetadata>> aliases = ImmutableOpenMap.builder();
ImmutableOpenMap.Builder<String, ImmutableOpenMap<String, MappingMetadata>> mappings = ImmutableOpenMap.builder();
ImmutableOpenMap.Builder<String, Settings> settings = ImmutableOpenMap.builder();
ImmutableOpenMap.Builder<String, Settings> defaultSettings = ImmutableOpenMap.builder();
ImmutableOpenMap.Builder<String, String> dataStreams = ImmutableOpenMap.builder();
List<String> indices = new ArrayList<>();
if (parser.currentToken() == null) {
parser.nextToken();
}
ensureExpectedToken(Token.START_OBJECT, parser.currentToken(), parser);
parser.nextToken();
while (!parser.isClosed()) {
if (parser.currentToken() == Token.START_OBJECT) {
// we assume this is an index entry
String indexName = parser.currentName();
indices.add(indexName);
IndexEntry indexEntry = parseIndexEntry(parser);
// make the order deterministic
CollectionUtil.timSort(indexEntry.indexAliases, Comparator.comparing(AliasMetadata::alias));
aliases.put(indexName, Collections.unmodifiableList(indexEntry.indexAliases));
mappings.put(indexName, indexEntry.indexMappings);
settings.put(indexName, indexEntry.indexSettings);
if (indexEntry.indexDefaultSettings.isEmpty() == false) {
defaultSettings.put(indexName, indexEntry.indexDefaultSettings);
}
if (indexEntry.dataStream != null) {
dataStreams.put(indexName, indexEntry.dataStream);
}
} else if (parser.currentToken() == Token.START_ARRAY) {
parser.skipChildren();
} else {
parser.nextToken();
}
}
return new GetIndexResponse(
indices.toArray(new String[0]),
mappings.build(),
aliases.build(),
settings.build(),
defaultSettings.build(),
dataStreams.build()
);
}
@Override @Override
public String toString() { public String toString() {
return Strings.toString(this); return Strings.toString(this);

View File

@ -102,7 +102,7 @@ public class TransportGetIndexAction extends TransportClusterInfoAction<GetIndex
final ClusterState state, final ClusterState state,
final ActionListener<GetIndexResponse> listener final ActionListener<GetIndexResponse> listener
) { ) {
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappingsResult = ImmutableOpenMap.of(); ImmutableOpenMap<String, MappingMetadata> mappingsResult = ImmutableOpenMap.of();
ImmutableOpenMap<String, List<AliasMetadata>> aliasesResult = ImmutableOpenMap.of(); ImmutableOpenMap<String, List<AliasMetadata>> aliasesResult = ImmutableOpenMap.of();
ImmutableOpenMap<String, Settings> settings = ImmutableOpenMap.of(); ImmutableOpenMap<String, Settings> settings = ImmutableOpenMap.of();
ImmutableOpenMap<String, Settings> defaultSettings = ImmutableOpenMap.of(); ImmutableOpenMap<String, Settings> defaultSettings = ImmutableOpenMap.of();
@ -121,8 +121,7 @@ public class TransportGetIndexAction extends TransportClusterInfoAction<GetIndex
case MAPPINGS: case MAPPINGS:
if (!doneMappings) { if (!doneMappings) {
try { try {
mappingsResult = state.metadata() mappingsResult = state.metadata().findMappings(concreteIndices, indicesService.getFieldFilter());
.findMappings(concreteIndices, request.types(), indicesService.getFieldFilter());
doneMappings = true; doneMappings = true;
} catch (IOException e) { } catch (IOException e) {
listener.onFailure(e); listener.onFailure(e);

View File

@ -33,6 +33,7 @@
package org.opensearch.action.admin.indices.mapping.get; package org.opensearch.action.admin.indices.mapping.get;
import com.carrotsearch.hppc.cursors.ObjectObjectCursor; import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
import org.opensearch.Version;
import org.opensearch.action.ActionResponse; import org.opensearch.action.ActionResponse;
import org.opensearch.cluster.metadata.MappingMetadata; import org.opensearch.cluster.metadata.MappingMetadata;
import org.opensearch.common.ParseField; import org.opensearch.common.ParseField;
@ -42,119 +43,82 @@ import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput; import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.xcontent.ToXContentFragment; import org.opensearch.common.xcontent.ToXContentFragment;
import org.opensearch.common.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentParser; import org.opensearch.index.mapper.MapperService;
import org.opensearch.rest.BaseRestHandler;
import java.io.IOException; import java.io.IOException;
import java.util.Map;
import static org.opensearch.rest.BaseRestHandler.DEFAULT_INCLUDE_TYPE_NAME_POLICY;
public class GetMappingsResponse extends ActionResponse implements ToXContentFragment { public class GetMappingsResponse extends ActionResponse implements ToXContentFragment {
private static final ParseField MAPPINGS = new ParseField("mappings"); private static final ParseField MAPPINGS = new ParseField("mappings");
private ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappings = ImmutableOpenMap.of(); private final ImmutableOpenMap<String, MappingMetadata> mappings;
public GetMappingsResponse(ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappings) { public GetMappingsResponse(ImmutableOpenMap<String, MappingMetadata> mappings) {
this.mappings = mappings; this.mappings = mappings;
} }
GetMappingsResponse(StreamInput in) throws IOException { GetMappingsResponse(StreamInput in) throws IOException {
super(in); super(in);
int size = in.readVInt(); int size = in.readVInt();
ImmutableOpenMap.Builder<String, ImmutableOpenMap<String, MappingMetadata>> indexMapBuilder = ImmutableOpenMap.builder(); ImmutableOpenMap.Builder<String, MappingMetadata> indexMapBuilder = ImmutableOpenMap.builder();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
String key = in.readString(); String index = in.readString();
int valueSize = in.readVInt(); if (in.getVersion().before(Version.V_2_0_0)) {
ImmutableOpenMap.Builder<String, MappingMetadata> typeMapBuilder = ImmutableOpenMap.builder(); int mappingCount = in.readVInt();
for (int j = 0; j < valueSize; j++) { if (mappingCount == 0) {
typeMapBuilder.put(in.readString(), new MappingMetadata(in)); indexMapBuilder.put(index, MappingMetadata.EMPTY_MAPPINGS);
} else if (mappingCount == 1) {
String type = in.readString();
if (MapperService.SINGLE_MAPPING_NAME.equals(type) == false) {
throw new IllegalStateException("Expected " + MapperService.SINGLE_MAPPING_NAME + " but got [" + type + "]");
}
indexMapBuilder.put(index, new MappingMetadata(in));
} else {
throw new IllegalStateException("Expected 0 or 1 mappings but got: " + mappingCount);
}
} else {
boolean hasMapping = in.readBoolean();
indexMapBuilder.put(index, hasMapping ? new MappingMetadata(in) : MappingMetadata.EMPTY_MAPPINGS);
} }
indexMapBuilder.put(key, typeMapBuilder.build());
} }
mappings = indexMapBuilder.build(); mappings = indexMapBuilder.build();
} }
public ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappings() { public ImmutableOpenMap<String, MappingMetadata> mappings() {
return mappings; return mappings;
} }
public ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> getMappings() { public ImmutableOpenMap<String, MappingMetadata> getMappings() {
return mappings(); return mappings();
} }
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(mappings.size()); out.writeVInt(mappings.size());
for (ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetadata>> indexEntry : mappings) { for (ObjectObjectCursor<String, MappingMetadata> indexEntry : mappings) {
out.writeString(indexEntry.key); out.writeString(indexEntry.key);
out.writeVInt(indexEntry.value.size()); if (out.getVersion().before(Version.V_2_0_0)) {
for (ObjectObjectCursor<String, MappingMetadata> typeEntry : indexEntry.value) { out.writeVInt(indexEntry.value == MappingMetadata.EMPTY_MAPPINGS ? 0 : 1);
out.writeString(typeEntry.key); if (indexEntry.value != MappingMetadata.EMPTY_MAPPINGS) {
typeEntry.value.writeTo(out); out.writeString(MapperService.SINGLE_MAPPING_NAME);
indexEntry.value.writeTo(out);
}
} else {
out.writeOptionalWriteable(indexEntry.value);
} }
} }
} }
public static GetMappingsResponse fromXContent(XContentParser parser) throws IOException {
if (parser.currentToken() == null) {
parser.nextToken();
}
assert parser.currentToken() == XContentParser.Token.START_OBJECT;
Map<String, Object> parts = parser.map();
ImmutableOpenMap.Builder<String, ImmutableOpenMap<String, MappingMetadata>> builder = new ImmutableOpenMap.Builder<>();
for (Map.Entry<String, Object> entry : parts.entrySet()) {
final String indexName = entry.getKey();
assert entry.getValue() instanceof Map : "expected a map as type mapping, but got: " + entry.getValue().getClass();
final Map<String, Object> mapping = (Map<String, Object>) ((Map) entry.getValue()).get(MAPPINGS.getPreferredName());
ImmutableOpenMap.Builder<String, MappingMetadata> typeBuilder = new ImmutableOpenMap.Builder<>();
for (Map.Entry<String, Object> typeEntry : mapping.entrySet()) {
final String typeName = typeEntry.getKey();
assert typeEntry.getValue() instanceof Map : "expected a map as inner type mapping, but got: "
+ typeEntry.getValue().getClass();
final Map<String, Object> fieldMappings = (Map<String, Object>) typeEntry.getValue();
MappingMetadata mmd = new MappingMetadata(typeName, fieldMappings);
typeBuilder.put(typeName, mmd);
}
builder.put(indexName, typeBuilder.build());
}
return new GetMappingsResponse(builder.build());
}
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
boolean includeTypeName = params.paramAsBoolean(BaseRestHandler.INCLUDE_TYPE_NAME_PARAMETER, DEFAULT_INCLUDE_TYPE_NAME_POLICY); for (final ObjectObjectCursor<String, MappingMetadata> indexEntry : getMappings()) {
if (indexEntry.value != null) {
for (final ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetadata>> indexEntry : getMappings()) { builder.startObject(indexEntry.key);
builder.startObject(indexEntry.key); builder.field(MAPPINGS.getPreferredName(), indexEntry.value.sourceAsMap());
{ builder.endObject();
if (includeTypeName == false) { } else {
MappingMetadata mappings = null; builder.startObject(MAPPINGS.getPreferredName()).endObject();
for (final ObjectObjectCursor<String, MappingMetadata> typeEntry : indexEntry.value) {
assert mappings == null;
mappings = typeEntry.value;
}
if (mappings == null) {
// no mappings yet
builder.startObject(MAPPINGS.getPreferredName()).endObject();
} else {
builder.field(MAPPINGS.getPreferredName(), mappings.sourceAsMap());
}
} else {
builder.startObject(MAPPINGS.getPreferredName());
{
for (final ObjectObjectCursor<String, MappingMetadata> typeEntry : indexEntry.value) {
builder.field(typeEntry.key, typeEntry.value.sourceAsMap());
}
}
builder.endObject();
}
} }
builder.endObject();
} }
return builder; return builder;
} }

View File

@ -91,8 +91,8 @@ public class TransportGetMappingsAction extends TransportClusterInfoAction<GetMa
) { ) {
logger.trace("serving getMapping request based on version {}", state.version()); logger.trace("serving getMapping request based on version {}", state.version());
try { try {
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> result = state.metadata() ImmutableOpenMap<String, MappingMetadata> result = state.metadata()
.findMappings(concreteIndices, request.types(), indicesService.getFieldFilter()); .findMappings(concreteIndices, indicesService.getFieldFilter());
listener.onResponse(new GetMappingsResponse(result)); listener.onResponse(new GetMappingsResponse(result));
} catch (IOException e) { } catch (IOException e) {
listener.onFailure(e); listener.onFailure(e);

View File

@ -371,7 +371,7 @@ public class TransportShardBulkAction extends TransportWriteAction<BulkShardRequ
primary.mapperService() primary.mapperService()
.merge( .merge(
MapperService.SINGLE_MAPPING_NAME, MapperService.SINGLE_MAPPING_NAME,
new CompressedXContent(result.getRequiredMappingUpdate(), XContentType.JSON, ToXContent.EMPTY_PARAMS), new CompressedXContent(result.getRequiredMappingUpdate(), ToXContent.EMPTY_PARAMS),
MapperService.MergeReason.MAPPING_UPDATE_PREFLIGHT MapperService.MergeReason.MAPPING_UPDATE_PREFLIGHT
); );
} catch (Exception e) { } catch (Exception e) {

View File

@ -32,6 +32,7 @@
package org.opensearch.action.support.master.info; package org.opensearch.action.support.master.info;
import org.opensearch.Version;
import org.opensearch.action.IndicesRequest; import org.opensearch.action.IndicesRequest;
import org.opensearch.action.support.IndicesOptions; import org.opensearch.action.support.IndicesOptions;
import org.opensearch.action.support.master.MasterNodeReadRequest; import org.opensearch.action.support.master.MasterNodeReadRequest;
@ -46,7 +47,6 @@ public abstract class ClusterInfoRequest<Request extends ClusterInfoRequest<Requ
IndicesRequest.Replaceable { IndicesRequest.Replaceable {
private String[] indices = Strings.EMPTY_ARRAY; private String[] indices = Strings.EMPTY_ARRAY;
private String[] types = Strings.EMPTY_ARRAY;
private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpen(); private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpen();
@ -55,7 +55,9 @@ public abstract class ClusterInfoRequest<Request extends ClusterInfoRequest<Requ
public ClusterInfoRequest(StreamInput in) throws IOException { public ClusterInfoRequest(StreamInput in) throws IOException {
super(in); super(in);
indices = in.readStringArray(); indices = in.readStringArray();
types = in.readStringArray(); if (in.getVersion().before(Version.V_2_0_0)) {
in.readStringArray();
}
indicesOptions = IndicesOptions.readIndicesOptions(in); indicesOptions = IndicesOptions.readIndicesOptions(in);
} }
@ -63,7 +65,9 @@ public abstract class ClusterInfoRequest<Request extends ClusterInfoRequest<Requ
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out); super.writeTo(out);
out.writeStringArray(indices); out.writeStringArray(indices);
out.writeStringArray(types); if (out.getVersion().before(Version.V_2_0_0)) {
out.writeStringArray(Strings.EMPTY_ARRAY);
}
indicesOptions.writeIndicesOptions(out); indicesOptions.writeIndicesOptions(out);
} }
@ -74,12 +78,6 @@ public abstract class ClusterInfoRequest<Request extends ClusterInfoRequest<Requ
return (Request) this; return (Request) this;
} }
@SuppressWarnings("unchecked")
public Request types(String... types) {
this.types = types;
return (Request) this;
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Request indicesOptions(IndicesOptions indicesOptions) { public Request indicesOptions(IndicesOptions indicesOptions) {
this.indicesOptions = indicesOptions; this.indicesOptions = indicesOptions;
@ -91,10 +89,6 @@ public abstract class ClusterInfoRequest<Request extends ClusterInfoRequest<Requ
return indices; return indices;
} }
public String[] types() {
return types;
}
@Override @Override
public IndicesOptions indicesOptions() { public IndicesOptions indicesOptions() {
return indicesOptions; return indicesOptions;

View File

@ -62,18 +62,6 @@ public abstract class ClusterInfoRequestBuilder<
return (Builder) this; return (Builder) this;
} }
@SuppressWarnings("unchecked")
public Builder setTypes(String... types) {
request.types(types);
return (Builder) this;
}
@SuppressWarnings("unchecked")
public Builder addTypes(String... types) {
request.types(ArrayUtils.concat(request.types(), types));
return (Builder) this;
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Builder setIndicesOptions(IndicesOptions indicesOptions) { public Builder setIndicesOptions(IndicesOptions indicesOptions) {
request.indicesOptions(indicesOptions); request.indicesOptions(indicesOptions);

View File

@ -36,15 +36,19 @@ import org.opensearch.LegacyESVersion;
import org.opensearch.OpenSearchParseException; import org.opensearch.OpenSearchParseException;
import org.opensearch.cluster.AbstractDiffable; import org.opensearch.cluster.AbstractDiffable;
import org.opensearch.cluster.Diff; import org.opensearch.cluster.Diff;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.compress.CompressedXContent; import org.opensearch.common.compress.CompressedXContent;
import org.opensearch.common.io.stream.StreamInput; import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput; import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.xcontent.ToXContent; import org.opensearch.common.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentHelper; import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.index.mapper.DocumentMapper; import org.opensearch.index.mapper.DocumentMapper;
import org.opensearch.index.mapper.MapperService;
import java.io.IOException; import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Collections;
import java.util.Map; import java.util.Map;
import static org.opensearch.common.xcontent.support.XContentMapValues.nodeBooleanValue; import static org.opensearch.common.xcontent.support.XContentMapValues.nodeBooleanValue;
@ -53,6 +57,7 @@ import static org.opensearch.common.xcontent.support.XContentMapValues.nodeBoole
* Mapping configuration for a type. * Mapping configuration for a type.
*/ */
public class MappingMetadata extends AbstractDiffable<MappingMetadata> { public class MappingMetadata extends AbstractDiffable<MappingMetadata> {
public static final MappingMetadata EMPTY_MAPPINGS = new MappingMetadata(MapperService.SINGLE_MAPPING_NAME, Collections.emptyMap());
public static class Routing { public static class Routing {
@ -88,7 +93,7 @@ public class MappingMetadata extends AbstractDiffable<MappingMetadata> {
private final CompressedXContent source; private final CompressedXContent source;
private Routing routing; private final Routing routing;
public MappingMetadata(DocumentMapper docMapper) { public MappingMetadata(DocumentMapper docMapper) {
this.type = docMapper.type(); this.type = docMapper.type();
@ -96,6 +101,7 @@ public class MappingMetadata extends AbstractDiffable<MappingMetadata> {
this.routing = new Routing(docMapper.routingFieldMapper().required()); this.routing = new Routing(docMapper.routingFieldMapper().required());
} }
@SuppressWarnings("unchecked")
public MappingMetadata(CompressedXContent mapping) { public MappingMetadata(CompressedXContent mapping) {
this.source = mapping; this.source = mapping;
Map<String, Object> mappingMap = XContentHelper.convertToMap(mapping.compressedReference(), true).v2(); Map<String, Object> mappingMap = XContentHelper.convertToMap(mapping.compressedReference(), true).v2();
@ -103,20 +109,27 @@ public class MappingMetadata extends AbstractDiffable<MappingMetadata> {
throw new IllegalStateException("Can't derive type from mapping, no root type: " + mapping.string()); throw new IllegalStateException("Can't derive type from mapping, no root type: " + mapping.string());
} }
this.type = mappingMap.keySet().iterator().next(); this.type = mappingMap.keySet().iterator().next();
initMappers((Map<String, Object>) mappingMap.get(this.type)); this.routing = initRouting((Map<String, Object>) mappingMap.get(this.type));
} }
public MappingMetadata(String type, Map<String, Object> mapping) throws IOException { @SuppressWarnings("unchecked")
public MappingMetadata(String type, Map<String, Object> mapping) {
this.type = type; this.type = type;
this.source = new CompressedXContent((builder, params) -> builder.mapContents(mapping), XContentType.JSON, ToXContent.EMPTY_PARAMS); try {
XContentBuilder mappingBuilder = XContentFactory.jsonBuilder().map(mapping);
this.source = new CompressedXContent(BytesReference.bytes(mappingBuilder));
} catch (IOException e) {
throw new UncheckedIOException(e); // XContent exception, should never happen
}
Map<String, Object> withoutType = mapping; Map<String, Object> withoutType = mapping;
if (mapping.size() == 1 && mapping.containsKey(type)) { if (mapping.size() == 1 && mapping.containsKey(type)) {
withoutType = (Map<String, Object>) mapping.get(type); withoutType = (Map<String, Object>) mapping.get(type);
} }
initMappers(withoutType); this.routing = initRouting(withoutType);
} }
private void initMappers(Map<String, Object> withoutType) { @SuppressWarnings("unchecked")
private Routing initRouting(Map<String, Object> withoutType) {
if (withoutType.containsKey("_routing")) { if (withoutType.containsKey("_routing")) {
boolean required = false; boolean required = false;
Map<String, Object> routingNode = (Map<String, Object>) withoutType.get("_routing"); Map<String, Object> routingNode = (Map<String, Object>) withoutType.get("_routing");
@ -134,9 +147,9 @@ public class MappingMetadata extends AbstractDiffable<MappingMetadata> {
} }
} }
} }
this.routing = new Routing(required); return new Routing(required);
} else { } else {
this.routing = Routing.EMPTY; return Routing.EMPTY;
} }
} }

View File

@ -454,44 +454,26 @@ public class Metadata implements Iterable<IndexMetadata>, Diffable<Metadata>, To
} }
/** /**
* Finds all mappings for types and concrete indices. Types are expanded to include all types that match the glob * Finds all mappings for concrete indices. Only fields that match the provided field
* patterns in the types array. Empty types array, null or {"_all"} will be expanded to all types available for * filter will be returned (default is a predicate that always returns true, which can be
* the given indices. Only fields that match the provided field filter will be returned (default is a predicate * overridden via plugins)
* that always returns true, which can be overridden via plugins)
* *
* @see MapperPlugin#getFieldFilter() * @see MapperPlugin#getFieldFilter()
* *
*/ */
public ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> findMappings( public ImmutableOpenMap<String, MappingMetadata> findMappings(String[] concreteIndices, Function<String, Predicate<String>> fieldFilter)
String[] concreteIndices, throws IOException {
final String[] types,
Function<String, Predicate<String>> fieldFilter
) throws IOException {
assert types != null;
assert concreteIndices != null; assert concreteIndices != null;
if (concreteIndices.length == 0) { if (concreteIndices.length == 0) {
return ImmutableOpenMap.of(); return ImmutableOpenMap.of();
} }
boolean isAllTypes = isAllTypes(types); ImmutableOpenMap.Builder<String, MappingMetadata> indexMapBuilder = ImmutableOpenMap.builder();
ImmutableOpenMap.Builder<String, ImmutableOpenMap<String, MappingMetadata>> indexMapBuilder = ImmutableOpenMap.builder();
Iterable<String> intersection = HppcMaps.intersection(ObjectHashSet.from(concreteIndices), indices.keys()); Iterable<String> intersection = HppcMaps.intersection(ObjectHashSet.from(concreteIndices), indices.keys());
for (String index : intersection) { for (String index : intersection) {
IndexMetadata indexMetadata = indices.get(index); IndexMetadata indexMetadata = indices.get(index);
Predicate<String> fieldPredicate = fieldFilter.apply(index); Predicate<String> fieldPredicate = fieldFilter.apply(index);
if (isAllTypes) { indexMapBuilder.put(index, filterFields(indexMetadata.mapping(), fieldPredicate));
indexMapBuilder.put(index, filterFields(indexMetadata.getMappings(), fieldPredicate));
} else {
ImmutableOpenMap.Builder<String, MappingMetadata> filteredMappings = ImmutableOpenMap.builder();
for (ObjectObjectCursor<String, MappingMetadata> cursor : indexMetadata.getMappings()) {
if (Regex.simpleMatch(types, cursor.key)) {
filteredMappings.put(cursor.key, filterFields(cursor.value, fieldPredicate));
}
}
if (!filteredMappings.isEmpty()) {
indexMapBuilder.put(index, filteredMappings.build());
}
}
} }
return indexMapBuilder.build(); return indexMapBuilder.build();
} }
@ -514,22 +496,11 @@ public class Metadata implements Iterable<IndexMetadata>, Diffable<Metadata>, To
return builder.build(); return builder.build();
} }
private static ImmutableOpenMap<String, MappingMetadata> filterFields(
ImmutableOpenMap<String, MappingMetadata> mappings,
Predicate<String> fieldPredicate
) throws IOException {
if (fieldPredicate == MapperPlugin.NOOP_FIELD_PREDICATE) {
return mappings;
}
ImmutableOpenMap.Builder<String, MappingMetadata> builder = ImmutableOpenMap.builder(mappings.size());
for (ObjectObjectCursor<String, MappingMetadata> cursor : mappings) {
builder.put(cursor.key, filterFields(cursor.value, fieldPredicate));
}
return builder.build(); // No types specified means return them all
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private static MappingMetadata filterFields(MappingMetadata mappingMetadata, Predicate<String> fieldPredicate) throws IOException { private static MappingMetadata filterFields(MappingMetadata mappingMetadata, Predicate<String> fieldPredicate) {
if (mappingMetadata == null) {
return MappingMetadata.EMPTY_MAPPINGS;
}
if (fieldPredicate == MapperPlugin.NOOP_FIELD_PREDICATE) { if (fieldPredicate == MapperPlugin.NOOP_FIELD_PREDICATE) {
return mappingMetadata; return mappingMetadata;
} }

View File

@ -41,7 +41,6 @@ import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.xcontent.ToXContent; import org.opensearch.common.xcontent.ToXContent;
import org.opensearch.common.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentFactory; import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentType;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
@ -82,15 +81,19 @@ public final class CompressedXContent {
/** /**
* Create a {@link CompressedXContent} out of a {@link ToXContent} instance. * Create a {@link CompressedXContent} out of a {@link ToXContent} instance.
*/ */
public CompressedXContent(ToXContent xcontent, XContentType type, ToXContent.Params params) throws IOException { public CompressedXContent(ToXContent xcontent, ToXContent.Params params) throws IOException {
BytesStreamOutput bStream = new BytesStreamOutput(); BytesStreamOutput bStream = new BytesStreamOutput();
OutputStream compressedStream = CompressorFactory.COMPRESSOR.threadLocalOutputStream(bStream); OutputStream compressedStream = CompressorFactory.COMPRESSOR.threadLocalOutputStream(bStream);
CRC32 crc32 = new CRC32(); CRC32 crc32 = new CRC32();
OutputStream checkedStream = new CheckedOutputStream(compressedStream, crc32); OutputStream checkedStream = new CheckedOutputStream(compressedStream, crc32);
try (XContentBuilder builder = XContentFactory.contentBuilder(type, checkedStream)) { try (XContentBuilder builder = XContentFactory.jsonBuilder(checkedStream)) {
builder.startObject(); if (xcontent.isFragment()) {
builder.startObject();
}
xcontent.toXContent(builder, params); xcontent.toXContent(builder, params);
builder.endObject(); if (xcontent.isFragment()) {
builder.endObject();
}
} }
this.bytes = BytesReference.toBytes(bStream.bytes()); this.bytes = BytesReference.toBytes(bStream.bytes());
this.crc32 = (int) crc32.getValue(); this.crc32 = (int) crc32.getValue();

View File

@ -153,7 +153,7 @@ public class DocumentMapper implements ToXContentFragment {
this.fieldMappers = MappingLookup.fromMapping(this.mapping, indexAnalyzers.getDefaultIndexAnalyzer()); this.fieldMappers = MappingLookup.fromMapping(this.mapping, indexAnalyzers.getDefaultIndexAnalyzer());
try { try {
mappingSource = new CompressedXContent(this, XContentType.JSON, ToXContent.EMPTY_PARAMS); mappingSource = new CompressedXContent(this, ToXContent.EMPTY_PARAMS);
} catch (Exception e) { } catch (Exception e) {
throw new OpenSearchGenerationException("failed to serialize source for type [" + type + "]", e); throw new OpenSearchGenerationException("failed to serialize source for type [" + type + "]", e);
} }

View File

@ -41,26 +41,16 @@ import org.opensearch.common.collect.ImmutableOpenMap;
import org.opensearch.common.io.stream.Writeable; import org.opensearch.common.io.stream.Writeable;
import org.opensearch.common.settings.IndexScopedSettings; import org.opensearch.common.settings.IndexScopedSettings;
import org.opensearch.common.settings.Settings; import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.ToXContent;
import org.opensearch.common.xcontent.XContentParser;
import org.opensearch.index.RandomCreateIndexGenerator; import org.opensearch.index.RandomCreateIndexGenerator;
import org.opensearch.rest.BaseRestHandler; import org.opensearch.test.AbstractWireSerializingTestCase;
import org.opensearch.test.AbstractSerializingTestCase;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.function.Predicate;
public class GetIndexResponseTests extends AbstractSerializingTestCase<GetIndexResponse> { public class GetIndexResponseTests extends AbstractWireSerializingTestCase<GetIndexResponse> {
@Override
protected GetIndexResponse doParseInstance(XContentParser parser) throws IOException {
return GetIndexResponse.fromXContent(parser);
}
@Override @Override
protected Writeable.Reader<GetIndexResponse> instanceReader() { protected Writeable.Reader<GetIndexResponse> instanceReader() {
@ -70,7 +60,7 @@ public class GetIndexResponseTests extends AbstractSerializingTestCase<GetIndexR
@Override @Override
protected GetIndexResponse createTestInstance() { protected GetIndexResponse createTestInstance() {
String[] indices = generateRandomStringArray(5, 5, false, false); String[] indices = generateRandomStringArray(5, 5, false, false);
ImmutableOpenMap.Builder<String, ImmutableOpenMap<String, MappingMetadata>> mappings = ImmutableOpenMap.builder(); ImmutableOpenMap.Builder<String, MappingMetadata> mappings = ImmutableOpenMap.builder();
ImmutableOpenMap.Builder<String, List<AliasMetadata>> aliases = ImmutableOpenMap.builder(); ImmutableOpenMap.Builder<String, List<AliasMetadata>> aliases = ImmutableOpenMap.builder();
ImmutableOpenMap.Builder<String, Settings> settings = ImmutableOpenMap.builder(); ImmutableOpenMap.Builder<String, Settings> settings = ImmutableOpenMap.builder();
ImmutableOpenMap.Builder<String, Settings> defaultSettings = ImmutableOpenMap.builder(); ImmutableOpenMap.Builder<String, Settings> defaultSettings = ImmutableOpenMap.builder();
@ -78,9 +68,7 @@ public class GetIndexResponseTests extends AbstractSerializingTestCase<GetIndexR
IndexScopedSettings indexScopedSettings = IndexScopedSettings.DEFAULT_SCOPED_SETTINGS; IndexScopedSettings indexScopedSettings = IndexScopedSettings.DEFAULT_SCOPED_SETTINGS;
boolean includeDefaults = randomBoolean(); boolean includeDefaults = randomBoolean();
for (String index : indices) { for (String index : indices) {
// rarely have no types mappings.put(index, GetMappingsResponseTests.createMappingsForIndex());
int typeCount = rarely() ? 0 : 1;
mappings.put(index, GetMappingsResponseTests.createMappingsForIndex(typeCount, true));
List<AliasMetadata> aliasMetadataList = new ArrayList<>(); List<AliasMetadata> aliasMetadataList = new ArrayList<>();
int aliasesNum = randomIntBetween(0, 3); int aliasesNum = randomIntBetween(0, 3);
@ -111,19 +99,4 @@ public class GetIndexResponseTests extends AbstractSerializingTestCase<GetIndexR
dataStreams.build() dataStreams.build()
); );
} }
@Override
protected Predicate<String> getRandomFieldsExcludeFilter() {
// we do not want to add new fields at the root (index-level), or inside the blocks
return f -> f.equals("") || f.contains(".settings") || f.contains(".defaults") || f.contains(".mappings") || f.contains(".aliases");
}
/**
* For xContent roundtrip testing we force the xContent output to still contain types because the parser still expects them.
* The new typeless parsing is implemented in the client side GetIndexResponse.
*/
@Override
protected ToXContent.Params getToXContentParams() {
return new ToXContent.MapParams(Collections.singletonMap(BaseRestHandler.INCLUDE_TYPE_NAME_PARAMETER, "true"));
}
} }

View File

@ -32,65 +32,35 @@
package org.opensearch.action.admin.indices.mapping.get; package org.opensearch.action.admin.indices.mapping.get;
import com.carrotsearch.hppc.cursors.ObjectCursor;
import org.opensearch.cluster.metadata.MappingMetadata; import org.opensearch.cluster.metadata.MappingMetadata;
import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.collect.ImmutableOpenMap;
import org.opensearch.common.io.stream.Writeable; import org.opensearch.common.io.stream.Writeable;
import org.opensearch.common.xcontent.ToXContent;
import org.opensearch.common.xcontent.ToXContent.Params;
import org.opensearch.common.xcontent.XContentParser;
import org.opensearch.index.mapper.MapperService; import org.opensearch.index.mapper.MapperService;
import org.opensearch.rest.BaseRestHandler; import org.opensearch.test.AbstractWireSerializingTestCase;
import org.opensearch.test.AbstractSerializingTestCase;
import org.opensearch.test.EqualsHashCodeTestUtils; import org.opensearch.test.EqualsHashCodeTestUtils;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
public class GetMappingsResponseTests extends AbstractSerializingTestCase<GetMappingsResponse> { public class GetMappingsResponseTests extends AbstractWireSerializingTestCase<GetMappingsResponse> {
@Override
protected boolean supportsUnknownFields() {
return false;
}
public void testCheckEqualsAndHashCode() { public void testCheckEqualsAndHashCode() {
GetMappingsResponse resp = createTestInstance(); GetMappingsResponse resp = createTestInstance();
EqualsHashCodeTestUtils.checkEqualsAndHashCode(resp, r -> new GetMappingsResponse(r.mappings()), GetMappingsResponseTests::mutate); EqualsHashCodeTestUtils.checkEqualsAndHashCode(resp, r -> new GetMappingsResponse(r.mappings()), GetMappingsResponseTests::mutate);
} }
@Override
protected GetMappingsResponse doParseInstance(XContentParser parser) throws IOException {
return GetMappingsResponse.fromXContent(parser);
}
@Override @Override
protected Writeable.Reader<GetMappingsResponse> instanceReader() { protected Writeable.Reader<GetMappingsResponse> instanceReader() {
return GetMappingsResponse::new; return GetMappingsResponse::new;
} }
private static GetMappingsResponse mutate(GetMappingsResponse original) throws IOException { private static GetMappingsResponse mutate(GetMappingsResponse original) {
ImmutableOpenMap.Builder<String, ImmutableOpenMap<String, MappingMetadata>> builder = ImmutableOpenMap.builder(original.mappings()); ImmutableOpenMap.Builder<String, MappingMetadata> builder = ImmutableOpenMap.builder(original.mappings());
String indexKey = original.mappings().keys().iterator().next().value; String indexKey = original.mappings().keys().iterator().next().value;
builder.put(indexKey + "1", createMappingsForIndex());
ImmutableOpenMap.Builder<String, MappingMetadata> typeBuilder = ImmutableOpenMap.builder(original.mappings().get(indexKey));
final String typeKey;
Iterator<ObjectCursor<String>> iter = original.mappings().get(indexKey).keys().iterator();
if (iter.hasNext()) {
typeKey = iter.next().value;
} else {
typeKey = "new-type";
}
typeBuilder.put(typeKey, new MappingMetadata("type-" + randomAlphaOfLength(6), randomFieldMapping()));
builder.put(indexKey, typeBuilder.build());
return new GetMappingsResponse(builder.build()); return new GetMappingsResponse(builder.build());
} }
@ -99,48 +69,23 @@ public class GetMappingsResponseTests extends AbstractSerializingTestCase<GetMap
return mutate(instance); return mutate(instance);
} }
public static ImmutableOpenMap<String, MappingMetadata> createMappingsForIndex(int typeCount, boolean randomTypeName) { public static MappingMetadata createMappingsForIndex() {
List<MappingMetadata> typeMappings = new ArrayList<>(typeCount); Map<String, Object> mappings = new HashMap<>();
if (rarely() == false) { // rarely have no fields
for (int i = 0; i < typeCount; i++) { mappings.put("field", randomFieldMapping());
if (rarely() == false) { // rarely have no fields if (randomBoolean()) {
Map<String, Object> mappings = new HashMap<>(); mappings.put("field2", randomFieldMapping());
mappings.put("field-" + i, randomFieldMapping());
if (randomBoolean()) {
mappings.put("field2-" + i, randomFieldMapping());
}
try {
String typeName = MapperService.SINGLE_MAPPING_NAME;
if (randomTypeName) {
typeName = "type-" + randomAlphaOfLength(5);
}
MappingMetadata mmd = new MappingMetadata(typeName, mappings);
typeMappings.add(mmd);
} catch (IOException e) {
fail("shouldn't have failed " + e);
}
} }
String typeName = MapperService.SINGLE_MAPPING_NAME;
return new MappingMetadata(typeName, mappings);
} }
ImmutableOpenMap.Builder<String, MappingMetadata> typeBuilder = ImmutableOpenMap.builder(); return new MappingMetadata(MapperService.SINGLE_MAPPING_NAME, mappings);
typeMappings.forEach(mmd -> typeBuilder.put(mmd.type(), mmd));
return typeBuilder.build();
}
/**
* For xContent roundtrip testing we force the xContent output to still contain types because the parser
* still expects them. The new typeless parsing is implemented in the client side GetMappingsResponse.
*/
@Override
protected Params getToXContentParams() {
return new ToXContent.MapParams(Collections.singletonMap(BaseRestHandler.INCLUDE_TYPE_NAME_PARAMETER, "true"));
} }
@Override @Override
protected GetMappingsResponse createTestInstance() { protected GetMappingsResponse createTestInstance() {
ImmutableOpenMap.Builder<String, ImmutableOpenMap<String, MappingMetadata>> indexBuilder = ImmutableOpenMap.builder(); ImmutableOpenMap.Builder<String, MappingMetadata> indexBuilder = ImmutableOpenMap.builder();
int typeCount = rarely() ? 0 : 1; indexBuilder.put("index-" + randomAlphaOfLength(5), createMappingsForIndex());
indexBuilder.put("index-" + randomAlphaOfLength(5), createMappingsForIndex(typeCount, randomBoolean()));
GetMappingsResponse resp = new GetMappingsResponse(indexBuilder.build()); GetMappingsResponse resp = new GetMappingsResponse(indexBuilder.build());
logger.debug("--> created: {}", resp); logger.debug("--> created: {}", resp);
return resp; return resp;

View File

@ -659,34 +659,20 @@ public class MetadataTests extends OpenSearchTestCase {
.build(); .build();
{ {
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappings = metadata.findMappings( ImmutableOpenMap<String, MappingMetadata> mappings = metadata.findMappings(Strings.EMPTY_ARRAY, MapperPlugin.NOOP_FIELD_FILTER);
Strings.EMPTY_ARRAY,
Strings.EMPTY_ARRAY,
MapperPlugin.NOOP_FIELD_FILTER
);
assertEquals(0, mappings.size()); assertEquals(0, mappings.size());
} }
{ {
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappings = metadata.findMappings( ImmutableOpenMap<String, MappingMetadata> mappings = metadata.findMappings(
new String[] { "index1" }, new String[] { "index1" },
new String[] { "notfound" },
MapperPlugin.NOOP_FIELD_FILTER
);
assertEquals(0, mappings.size());
}
{
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappings = metadata.findMappings(
new String[] { "index1" },
Strings.EMPTY_ARRAY,
MapperPlugin.NOOP_FIELD_FILTER MapperPlugin.NOOP_FIELD_FILTER
); );
assertEquals(1, mappings.size()); assertEquals(1, mappings.size());
assertIndexMappingsNotFiltered(mappings, "index1"); assertIndexMappingsNotFiltered(mappings, "index1");
} }
{ {
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappings = metadata.findMappings( ImmutableOpenMap<String, MappingMetadata> mappings = metadata.findMappings(
new String[] { "index1", "index2" }, new String[] { "index1", "index2" },
new String[] { randomBoolean() ? "_doc" : "_all" },
MapperPlugin.NOOP_FIELD_FILTER MapperPlugin.NOOP_FIELD_FILTER
); );
assertEquals(2, mappings.size()); assertEquals(2, mappings.size());
@ -715,43 +701,19 @@ public class MetadataTests extends OpenSearchTestCase {
.build(); .build();
{ {
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappings = metadata.findMappings( ImmutableOpenMap<String, MappingMetadata> mappings = metadata.findMappings(
new String[] { "index1" }, new String[] { "index1" },
randomBoolean() ? Strings.EMPTY_ARRAY : new String[] { "_all" },
MapperPlugin.NOOP_FIELD_FILTER MapperPlugin.NOOP_FIELD_FILTER
); );
ImmutableOpenMap<String, MappingMetadata> index1 = mappings.get("index1"); MappingMetadata mappingMetadata = mappings.get("index1");
MappingMetadata mappingMetadata = index1.get("_doc");
assertSame(originalMappingMetadata, mappingMetadata); assertSame(originalMappingMetadata, mappingMetadata);
} }
{ {
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappings = metadata.findMappings( ImmutableOpenMap<String, MappingMetadata> mappings = metadata.findMappings(
new String[] { "index1" }, new String[] { "index1" },
randomBoolean() ? Strings.EMPTY_ARRAY : new String[] { "_all" },
index -> field -> randomBoolean() index -> field -> randomBoolean()
); );
ImmutableOpenMap<String, MappingMetadata> index1 = mappings.get("index1"); MappingMetadata mappingMetadata = mappings.get("index1");
MappingMetadata mappingMetadata = index1.get("_doc");
assertNotSame(originalMappingMetadata, mappingMetadata);
}
{
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappings = metadata.findMappings(
new String[] { "index1" },
new String[] { "_doc" },
MapperPlugin.NOOP_FIELD_FILTER
);
ImmutableOpenMap<String, MappingMetadata> index1 = mappings.get("index1");
MappingMetadata mappingMetadata = index1.get("_doc");
assertSame(originalMappingMetadata, mappingMetadata);
}
{
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappings = metadata.findMappings(
new String[] { "index1" },
new String[] { "_doc" },
index -> field -> randomBoolean()
);
ImmutableOpenMap<String, MappingMetadata> index1 = mappings.get("index1");
MappingMetadata mappingMetadata = index1.get("_doc");
assertNotSame(originalMappingMetadata, mappingMetadata); assertNotSame(originalMappingMetadata, mappingMetadata);
} }
} }
@ -802,9 +764,8 @@ public class MetadataTests extends OpenSearchTestCase {
.build(); .build();
{ {
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappings = metadata.findMappings( ImmutableOpenMap<String, MappingMetadata> mappings = metadata.findMappings(
new String[] { "index1", "index2", "index3" }, new String[] { "index1", "index2", "index3" },
new String[] { "_doc" },
index -> { index -> {
if (index.equals("index1")) { if (index.equals("index1")) {
return field -> field.startsWith("name.") == false return field -> field.startsWith("name.") == false
@ -822,11 +783,7 @@ public class MetadataTests extends OpenSearchTestCase {
assertIndexMappingsNoFields(mappings, "index2"); assertIndexMappingsNoFields(mappings, "index2");
assertIndexMappingsNotFiltered(mappings, "index3"); assertIndexMappingsNotFiltered(mappings, "index3");
ImmutableOpenMap<String, MappingMetadata> index1Mappings = mappings.get("index1"); MappingMetadata docMapping = mappings.get("index1");
assertNotNull(index1Mappings);
assertEquals(1, index1Mappings.size());
MappingMetadata docMapping = index1Mappings.get("_doc");
assertNotNull(docMapping); assertNotNull(docMapping);
Map<String, Object> sourceAsMap = docMapping.getSourceAsMap(); Map<String, Object> sourceAsMap = docMapping.getSourceAsMap();
@ -868,17 +825,14 @@ public class MetadataTests extends OpenSearchTestCase {
} }
{ {
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappings = metadata.findMappings( ImmutableOpenMap<String, MappingMetadata> mappings = metadata.findMappings(
new String[] { "index1", "index2", "index3" }, new String[] { "index1", "index2", "index3" },
new String[] { "_doc" },
index -> field -> (index.equals("index3") && field.endsWith("keyword")) index -> field -> (index.equals("index3") && field.endsWith("keyword"))
); );
assertIndexMappingsNoFields(mappings, "index1"); assertIndexMappingsNoFields(mappings, "index1");
assertIndexMappingsNoFields(mappings, "index2"); assertIndexMappingsNoFields(mappings, "index2");
ImmutableOpenMap<String, MappingMetadata> index3 = mappings.get("index3"); MappingMetadata mappingMetadata = mappings.get("index3");
assertEquals(1, index3.size());
MappingMetadata mappingMetadata = index3.get("_doc");
Map<String, Object> sourceAsMap = mappingMetadata.getSourceAsMap(); Map<String, Object> sourceAsMap = mappingMetadata.getSourceAsMap();
assertEquals(3, sourceAsMap.size()); assertEquals(3, sourceAsMap.size());
assertTrue(sourceAsMap.containsKey("_routing")); assertTrue(sourceAsMap.containsKey("_routing"));
@ -906,9 +860,8 @@ public class MetadataTests extends OpenSearchTestCase {
} }
{ {
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappings = metadata.findMappings( ImmutableOpenMap<String, MappingMetadata> mappings = metadata.findMappings(
new String[] { "index1", "index2", "index3" }, new String[] { "index1", "index2", "index3" },
new String[] { "_doc" },
index -> field -> (index.equals("index2")) index -> field -> (index.equals("index2"))
); );
@ -928,14 +881,8 @@ public class MetadataTests extends OpenSearchTestCase {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private static void assertIndexMappingsNoFields( private static void assertIndexMappingsNoFields(ImmutableOpenMap<String, MappingMetadata> mappings, String index) {
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappings, MappingMetadata docMapping = mappings.get(index);
String index
) {
ImmutableOpenMap<String, MappingMetadata> indexMappings = mappings.get(index);
assertNotNull(indexMappings);
assertEquals(1, indexMappings.size());
MappingMetadata docMapping = indexMappings.get("_doc");
assertNotNull(docMapping); assertNotNull(docMapping);
Map<String, Object> sourceAsMap = docMapping.getSourceAsMap(); Map<String, Object> sourceAsMap = docMapping.getSourceAsMap();
assertEquals(3, sourceAsMap.size()); assertEquals(3, sourceAsMap.size());
@ -946,15 +893,8 @@ public class MetadataTests extends OpenSearchTestCase {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private static void assertIndexMappingsNotFiltered( private static void assertIndexMappingsNotFiltered(ImmutableOpenMap<String, MappingMetadata> mappings, String index) {
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappings, MappingMetadata docMapping = mappings.get(index);
String index
) {
ImmutableOpenMap<String, MappingMetadata> indexMappings = mappings.get(index);
assertNotNull(indexMappings);
assertEquals(1, indexMappings.size());
MappingMetadata docMapping = indexMappings.get("_doc");
assertNotNull(docMapping); assertNotNull(docMapping);
Map<String, Object> sourceAsMap = docMapping.getSourceAsMap(); Map<String, Object> sourceAsMap = docMapping.getSourceAsMap();

View File

@ -102,8 +102,8 @@ public class FieldFilterMapperPluginTests extends OpenSearchSingleNodeTestCase {
// double check that submitting the filtered mappings to an unfiltered index leads to the same get field mappings output // double check that submitting the filtered mappings to an unfiltered index leads to the same get field mappings output
// as the one coming from a filtered index with same mappings // as the one coming from a filtered index with same mappings
GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("filtered").get(); GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("filtered").get();
ImmutableOpenMap<String, MappingMetadata> filtered = getMappingsResponse.getMappings().get("filtered"); MappingMetadata filtered = getMappingsResponse.getMappings().get("filtered");
assertAcked(client().admin().indices().prepareCreate("test").addMapping("_doc", filtered.get("_doc").getSourceAsMap())); assertAcked(client().admin().indices().prepareCreate("test").addMapping("_doc", filtered.getSourceAsMap()));
GetFieldMappingsResponse response = client().admin().indices().prepareGetFieldMappings("test").setFields("*").get(); GetFieldMappingsResponse response = client().admin().indices().prepareGetFieldMappings("test").setFields("*").get();
assertEquals(1, response.mappings().size()); assertEquals(1, response.mappings().size());
assertFieldMappings(response.mappings().get("test"), FILTERED_FLAT_FIELDS); assertFieldMappings(response.mappings().get("test"), FILTERED_FLAT_FIELDS);
@ -121,8 +121,8 @@ public class FieldFilterMapperPluginTests extends OpenSearchSingleNodeTestCase {
// double check that submitting the filtered mappings to an unfiltered index leads to the same field_caps output // double check that submitting the filtered mappings to an unfiltered index leads to the same field_caps output
// as the one coming from a filtered index with same mappings // as the one coming from a filtered index with same mappings
GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("filtered").get(); GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("filtered").get();
ImmutableOpenMap<String, MappingMetadata> filteredMapping = getMappingsResponse.getMappings().get("filtered"); MappingMetadata filteredMapping = getMappingsResponse.getMappings().get("filtered");
assertAcked(client().admin().indices().prepareCreate("test").addMapping("_doc", filteredMapping.get("_doc").getSourceAsMap())); assertAcked(client().admin().indices().prepareCreate("test").addMapping("_doc", filteredMapping.getSourceAsMap()));
FieldCapabilitiesResponse test = client().fieldCaps(new FieldCapabilitiesRequest().fields("*").indices("test")).actionGet(); FieldCapabilitiesResponse test = client().fieldCaps(new FieldCapabilitiesRequest().fields("*").indices("test")).actionGet();
// properties.value is an object field in the new mapping // properties.value is an object field in the new mapping
filteredFields.add("properties.value"); filteredFields.add("properties.value");
@ -161,12 +161,12 @@ public class FieldFilterMapperPluginTests extends OpenSearchSingleNodeTestCase {
assertEquals("Some unexpected fields were returned: " + fields.keySet(), 0, fields.size()); assertEquals("Some unexpected fields were returned: " + fields.keySet(), 0, fields.size());
} }
private void assertExpectedMappings(ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappings) { private void assertExpectedMappings(ImmutableOpenMap<String, MappingMetadata> mappings) {
assertEquals(2, mappings.size()); assertEquals(2, mappings.size());
assertNotFiltered(mappings.get("index1")); assertNotFiltered(mappings.get("index1"));
ImmutableOpenMap<String, MappingMetadata> filtered = mappings.get("filtered"); MappingMetadata filtered = mappings.get("filtered");
assertFiltered(filtered); assertFiltered(filtered);
assertMappingsAreValid(filtered.get("_doc").getSourceAsMap()); assertMappingsAreValid(filtered.getSourceAsMap());
} }
private void assertMappingsAreValid(Map<String, Object> sourceAsMap) { private void assertMappingsAreValid(Map<String, Object> sourceAsMap) {
@ -179,9 +179,7 @@ public class FieldFilterMapperPluginTests extends OpenSearchSingleNodeTestCase {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private static void assertFiltered(ImmutableOpenMap<String, MappingMetadata> mappings) { private static void assertFiltered(MappingMetadata mappingMetadata) {
assertEquals(1, mappings.size());
MappingMetadata mappingMetadata = mappings.get("_doc");
assertNotNull(mappingMetadata); assertNotNull(mappingMetadata);
Map<String, Object> sourceAsMap = mappingMetadata.getSourceAsMap(); Map<String, Object> sourceAsMap = mappingMetadata.getSourceAsMap();
assertEquals(4, sourceAsMap.size()); assertEquals(4, sourceAsMap.size());
@ -226,9 +224,7 @@ public class FieldFilterMapperPluginTests extends OpenSearchSingleNodeTestCase {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private static void assertNotFiltered(ImmutableOpenMap<String, MappingMetadata> mappings) { private static void assertNotFiltered(MappingMetadata mappingMetadata) {
assertEquals(1, mappings.size());
MappingMetadata mappingMetadata = mappings.get("_doc");
assertNotNull(mappingMetadata); assertNotNull(mappingMetadata);
Map<String, Object> sourceAsMap = mappingMetadata.getSourceAsMap(); Map<String, Object> sourceAsMap = mappingMetadata.getSourceAsMap();
assertEquals(4, sourceAsMap.size()); assertEquals(4, sourceAsMap.size());

View File

@ -1096,7 +1096,7 @@ public abstract class OpenSearchIntegTestCase extends OpenSearchTestCase {
// remove local node reference // remove local node reference
masterClusterState = ClusterState.Builder.fromBytes(masterClusterStateBytes, null, namedWriteableRegistry); masterClusterState = ClusterState.Builder.fromBytes(masterClusterStateBytes, null, namedWriteableRegistry);
Map<String, Object> masterStateMap = convertToMap(masterClusterState); Map<String, Object> masterStateMap = convertToMap(masterClusterState);
int masterClusterStateSize = ClusterState.Builder.toBytes(masterClusterState).length; int masterClusterStateSize = masterClusterState.toString().length();
String masterId = masterClusterState.nodes().getMasterNodeId(); String masterId = masterClusterState.nodes().getMasterNodeId();
for (Client client : cluster().getClients()) { for (Client client : cluster().getClients()) {
ClusterState localClusterState = client.admin().cluster().prepareState().all().setLocal(true).get().getState(); ClusterState localClusterState = client.admin().cluster().prepareState().all().setLocal(true).get().getState();
@ -1104,7 +1104,7 @@ public abstract class OpenSearchIntegTestCase extends OpenSearchTestCase {
// remove local node reference // remove local node reference
localClusterState = ClusterState.Builder.fromBytes(localClusterStateBytes, null, namedWriteableRegistry); localClusterState = ClusterState.Builder.fromBytes(localClusterStateBytes, null, namedWriteableRegistry);
final Map<String, Object> localStateMap = convertToMap(localClusterState); final Map<String, Object> localStateMap = convertToMap(localClusterState);
final int localClusterStateSize = ClusterState.Builder.toBytes(localClusterState).length; final int localClusterStateSize = localClusterState.toString().length();
// Check that the non-master node has the same version of the cluster state as the master and // Check that the non-master node has the same version of the cluster state as the master and
// that the master node matches the master (otherwise there is no requirement for the cluster state to match) // that the master node matches the master (otherwise there is no requirement for the cluster state to match)
if (masterClusterState.version() == localClusterState.version() if (masterClusterState.version() == localClusterState.version()
@ -1112,7 +1112,10 @@ public abstract class OpenSearchIntegTestCase extends OpenSearchTestCase {
try { try {
assertEquals("cluster state UUID does not match", masterClusterState.stateUUID(), localClusterState.stateUUID()); assertEquals("cluster state UUID does not match", masterClusterState.stateUUID(), localClusterState.stateUUID());
// We cannot compare serialization bytes since serialization order of maps is not guaranteed // We cannot compare serialization bytes since serialization order of maps is not guaranteed
// but we can compare serialization sizes - they should be the same // We also cannot compare byte array size because CompressedXContent's DeflateCompressor uses
// a synced flush that can affect the size of the compressed byte array
// (see: DeflateCompressedXContentTests#testDifferentCompressedRepresentation for an example)
// instead we compare the string length of cluster state - they should be the same
assertEquals("cluster state size does not match", masterClusterStateSize, localClusterStateSize); assertEquals("cluster state size does not match", masterClusterStateSize, localClusterStateSize);
// Compare JSON serialization // Compare JSON serialization
assertNull( assertNull(