Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.common.xcontent.support.XContentMapValues;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus;
Expand Down Expand Up @@ -209,7 +210,7 @@ public void testCreateIndex() throws IOException {
mappingBuilder.startObject().startObject("properties").startObject("field");
mappingBuilder.field("type", "text");
mappingBuilder.endObject().endObject().endObject();
createIndexRequest.mapping("type_name", mappingBuilder);
createIndexRequest.mapping(MapperService.SINGLE_MAPPING_NAME, mappingBuilder);

CreateIndexResponse createIndexResponse =
execute(createIndexRequest, highLevelClient().indices()::create, highLevelClient().indices()::createAsync);
Expand All @@ -226,7 +227,7 @@ public void testCreateIndex() throws IOException {
Map<String, Object> term = (Map) filter.get("term");
assertEquals(2016, term.get("year"));

assertEquals("text", XContentMapValues.extractValue(indexName + ".mappings.type_name.properties.field.type", getIndexResponse));
assertEquals("text", XContentMapValues.extractValue(indexName + ".mappings.properties.field.type", getIndexResponse));
}
}

Expand Down Expand Up @@ -340,7 +341,7 @@ public void testGetIndex() throws IOException {
.put(SETTING_NUMBER_OF_SHARDS, 1)
.put(SETTING_NUMBER_OF_REPLICAS, 0)
.build();
String mappings = "\"type-1\":{\"properties\":{\"field-1\":{\"type\":\"integer\"}}}";
String mappings = "\"_doc\":{\"properties\":{\"field-1\":{\"type\":\"integer\"}}}";
createIndex(indexName, basicSettings, mappings);

GetIndexRequest getIndexRequest = new GetIndexRequest()
Expand All @@ -353,8 +354,8 @@ public void testGetIndex() throws IOException {
assertEquals("1", getIndexResponse.getSetting(indexName, SETTING_NUMBER_OF_SHARDS));
assertEquals("0", getIndexResponse.getSetting(indexName, SETTING_NUMBER_OF_REPLICAS));
assertNotNull(getIndexResponse.getMappings().get(indexName));
assertNotNull(getIndexResponse.getMappings().get(indexName).get("type-1"));
Object o = getIndexResponse.getMappings().get(indexName).get("type-1").getSourceAsMap().get("properties");
assertNotNull(getIndexResponse.getMappings().get(indexName).get("_doc"));
Object o = getIndexResponse.getMappings().get(indexName).get("_doc").getSourceAsMap().get("properties");
assertThat(o, instanceOf(Map.class));
//noinspection unchecked
assertThat(((Map<String, Object>) o).get("field-1"), instanceOf(Map.class));
Expand All @@ -370,7 +371,7 @@ public void testGetIndexWithDefaults() throws IOException {
.put(SETTING_NUMBER_OF_SHARDS, 1)
.put(SETTING_NUMBER_OF_REPLICAS, 0)
.build();
String mappings = "\"type-1\":{\"properties\":{\"field-1\":{\"type\":\"integer\"}}}";
String mappings = "\"_doc\":{\"properties\":{\"field-1\":{\"type\":\"integer\"}}}";
createIndex(indexName, basicSettings, mappings);

GetIndexRequest getIndexRequest = new GetIndexRequest()
Expand All @@ -384,8 +385,8 @@ public void testGetIndexWithDefaults() throws IOException {
assertEquals("1", getIndexResponse.getSetting(indexName, SETTING_NUMBER_OF_SHARDS));
assertEquals("0", getIndexResponse.getSetting(indexName, SETTING_NUMBER_OF_REPLICAS));
assertNotNull(getIndexResponse.getMappings().get(indexName));
assertNotNull(getIndexResponse.getMappings().get(indexName).get("type-1"));
Object o = getIndexResponse.getMappings().get(indexName).get("type-1").getSourceAsMap().get("properties");
assertNotNull(getIndexResponse.getMappings().get(indexName).get("_doc"));
Object o = getIndexResponse.getMappings().get(indexName).get("_doc").getSourceAsMap().get("properties");
assertThat(o, instanceOf(Map.class));
assertThat(((Map<String, Object>) o).get("field-1"), instanceOf(Map.class));
Map<String, Object> fieldMapping = (Map<String, Object>) ((Map<String, Object>) o).get("field-1");
Expand All @@ -408,7 +409,7 @@ public void testPutMapping() throws IOException {
createIndex(indexName, Settings.EMPTY);

PutMappingRequest putMappingRequest = new PutMappingRequest(indexName);
putMappingRequest.type("type_name");
putMappingRequest.type("_doc");
XContentBuilder mappingBuilder = JsonXContent.contentBuilder();
mappingBuilder.startObject().startObject("properties").startObject("field");
mappingBuilder.field("type", "text");
Expand All @@ -420,7 +421,7 @@ public void testPutMapping() throws IOException {
assertTrue(putMappingResponse.isAcknowledged());

Map<String, Object> getIndexResponse = getAsMap(indexName);
assertEquals("text", XContentMapValues.extractValue(indexName + ".mappings.type_name.properties.field.type", getIndexResponse));
assertEquals("text", XContentMapValues.extractValue(indexName + ".mappings.properties.field.type", getIndexResponse));
}

public void testGetMapping() throws IOException {
Expand All @@ -440,7 +441,7 @@ public void testGetMapping() throws IOException {
assertTrue(putMappingResponse.isAcknowledged());

Map<String, Object> getIndexResponse = getAsMap(indexName);
assertEquals("text", XContentMapValues.extractValue(indexName + ".mappings._doc.properties.field.type", getIndexResponse));
assertEquals("text", XContentMapValues.extractValue(indexName + ".mappings.properties.field.type", getIndexResponse));

GetMappingsRequest request = new GetMappingsRequest()
.indices(indexName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.ESRestHighLevelClientTestCase;
import org.elasticsearch.client.indices.FreezeIndexRequest;
import org.elasticsearch.client.GetAliasesResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.SyncedFlushResponse;
import org.elasticsearch.client.core.ShardsAcknowledgedResponse;
import org.elasticsearch.client.indices.FreezeIndexRequest;
import org.elasticsearch.client.indices.GetIndexTemplatesRequest;
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
import org.elasticsearch.client.indices.UnfreezeIndexRequest;
import org.elasticsearch.client.core.ShardsAcknowledgedResponse;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.elasticsearch.cluster.metadata.IndexTemplateMetaData;
import org.elasticsearch.cluster.metadata.MappingMetaData;
Expand Down Expand Up @@ -1249,7 +1249,7 @@ public void testGetIndex() throws Exception {
Settings settings = Settings.builder().put("number_of_shards", 3).build();
String mappings = "{\"properties\":{\"field-1\":{\"type\":\"integer\"}}}";
CreateIndexResponse createIndexResponse = client.indices().create(
new CreateIndexRequest("index", settings).mapping("doc", mappings, XContentType.JSON),
new CreateIndexRequest("index", settings).mapping("_doc", mappings, XContentType.JSON),
RequestOptions.DEFAULT);
assertTrue(createIndexResponse.isAcknowledged());
}
Expand All @@ -1272,7 +1272,7 @@ public void testGetIndex() throws Exception {

// tag::get-index-response
ImmutableOpenMap<String, MappingMetaData> indexMappings = getIndexResponse.getMappings().get("index"); // <1>
Map<String, Object> indexTypeMappings = indexMappings.get("doc").getSourceAsMap(); // <2>
Map<String, Object> indexTypeMappings = indexMappings.get("_doc").getSourceAsMap(); // <2>
List<AliasMetaData> indexAliases = getIndexResponse.getAliases().get("index"); // <3>
String numberOfShardsString = getIndexResponse.getSetting("index", "index.number_of_shards"); // <4>
Settings indexSettings = getIndexResponse.getSettings().get("index"); // <5>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
}
},
"params":{
"include_type_name": {
"type" : "boolean",
"description" : "Whether to add the type name to the response (default: false)"
},
"local":{
"type":"boolean",
"description":"Return local information, do not retrieve the state from master node (default: false)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,35 @@ setup:
- is_true: test_index.settings
- is_true: test_index.mappings

---
"Test include_type_name":
- skip:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this can be removed after the parameter support has been backported to 6.x

version: " - 6.99.99"
reason: the include_type_name parameter is not backported to pre 7.0 versions yet

- 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

- do:
indices.get:
index: test_index

- is_true: test_index.mappings
- is_false: test_index.mappings.type_1

---
"Get index infos should work for wildcards":

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.action.admin.indices.get;

import com.carrotsearch.hppc.cursors.ObjectObjectCursor;

import org.apache.lucene.util.CollectionUtil;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionResponse;
Expand All @@ -34,13 +35,15 @@
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentParser.Token;
import org.elasticsearch.index.mapper.MapperService;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
Expand Down Expand Up @@ -249,15 +252,29 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
}
builder.endObject();

builder.startObject("mappings");
ImmutableOpenMap<String, MappingMetaData> indexMappings = mappings.get(index);
if (indexMappings != null) {
for (final ObjectObjectCursor<String, MappingMetaData> typeEntry : indexMappings) {
builder.field(typeEntry.key);
builder.map(typeEntry.value.sourceAsMap());
boolean includeTypeName = params.paramAsBoolean("include_type_name", false);
if (includeTypeName) {
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 {
if (indexMappings != null && indexMappings.size() > 0) {
builder.field("mappings");
for (final ObjectObjectCursor<String, MappingMetaData> typeEntry : indexMappings) {
builder.map(typeEntry.value.sourceAsMap());
}
} else {
// we always want to output a mappings object, even if empty
builder.startObject("mappings");
builder.endObject();
}
}
builder.endObject();

builder.startObject("settings");
Settings indexSettings = settings.get(index);
Expand Down Expand Up @@ -291,6 +308,16 @@ private static List<AliasMetaData> parseAliases(XContentParser parser) throws IO
}

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
Map<String, Object> map = parser.map();
if (map.isEmpty() == false) {
indexMappings.put(MapperService.SINGLE_MAPPING_NAME, new MappingMetaData(MapperService.SINGLE_MAPPING_NAME, map));
}
return indexMappings.build();
}

private static ImmutableOpenMap<String, MappingMetaData> parseMappingsWithTypes(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) {
Expand All @@ -306,7 +333,7 @@ private static ImmutableOpenMap<String, MappingMetaData> parseMappings(XContentP
return indexMappings.build();
}

private static IndexEntry parseIndexEntry(XContentParser parser) throws IOException {
private static IndexEntry parseIndexEntry(XContentParser parser, boolean legacyWithTypes) throws IOException {
List<AliasMetaData> indexAliases = null;
ImmutableOpenMap<String, MappingMetaData> indexMappings = null;
Settings indexSettings = null;
Expand All @@ -321,7 +348,11 @@ private static IndexEntry parseIndexEntry(XContentParser parser) throws IOExcept
indexAliases = parseAliases(parser);
break;
case "mappings":
indexMappings = parseMappings(parser);
if (legacyWithTypes) {
indexMappings = parseMappingsWithTypes(parser);
} else {
indexMappings = parseMappings(parser);
}
break;
case "settings":
indexSettings = Settings.fromXContent(parser);
Expand Down Expand Up @@ -355,6 +386,10 @@ private static class IndexEntry {
}

public static GetIndexResponse fromXContent(XContentParser parser) throws IOException {
return fromXContent(parser, false);
}

public static GetIndexResponse fromXContent(XContentParser parser, boolean legacyWithTypes) 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();
Expand All @@ -372,7 +407,7 @@ public static GetIndexResponse fromXContent(XContentParser parser) throws IOExce
// we assume this is an index entry
String indexName = parser.currentName();
indices.add(indexName);
IndexEntry indexEntry = parseIndexEntry(parser);
IndexEntry indexEntry = parseIndexEntry(parser, legacyWithTypes);
// make the order deterministic
CollectionUtil.timSort(indexEntry.indexAliases, Comparator.comparing(AliasMetaData::alias));
aliases.put(indexName, Collections.unmodifiableList(indexEntry.indexAliases));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
package org.elasticsearch.rest.action.admin.indices;


import org.apache.logging.log4j.LogManager;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestController;
Expand All @@ -41,6 +43,9 @@
*/
public class RestGetIndicesAction extends BaseRestHandler {

private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(RestGetIndicesAction.class));
static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Using `include_type_name` in get indices requests is deprecated. "
+ "The parameter will be removed in the next major version.";

public RestGetIndicesAction(
final Settings settings,
Expand All @@ -58,6 +63,10 @@ public String getName() {
@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
// starting with 7.0 we don't include types by default in the response
if (request.hasParam("include_type_name")) {
deprecationLogger.deprecatedAndMaybeLog("get_indices_with_types", TYPES_DEPRECATION_MESSAGE);
}
final GetIndexRequest getIndexRequest = new GetIndexRequest();
getIndexRequest.indices(indices);
getIndexRequest.indicesOptions(IndicesOptions.fromRequest(request, getIndexRequest.indicesOptions()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
package org.elasticsearch.rest.action.admin.indices;

import com.carrotsearch.hppc.cursors.ObjectCursor;
import org.apache.logging.log4j.Logger;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
import org.elasticsearch.action.support.IndicesOptions;
Expand Down
Loading