[Remove] Multiple Types from IndexTemplateMetadata (#2400)

Removes multi-type support from IndexTemplateMetadata so that instead of holding
a map of multiple types to mappings, it only returns a single mapping for a
single type. Also removes type from documentMapper() method to avoid any
accidental NullPointerExceptions in the internal mapping retrieval.

Signed-off-by: Nicholas Walter Knize <nknize@apache.org>
This commit is contained in:
Nick Knize 2022-03-10 23:36:08 -05:00 committed by GitHub
parent 9cfa395128
commit b00b3ce2b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
56 changed files with 530 additions and 919 deletions

View File

@ -83,7 +83,7 @@ public class GetIndexTemplatesResponseTests extends OpenSearchTestCase {
.test(); .test();
} }
public void testParsingFromEsResponse() throws IOException { public void testParsingFromOpenSearchResponse() throws IOException {
for (int runs = 0; runs < 20; runs++) { for (int runs = 0; runs < 20; runs++) {
org.opensearch.action.admin.indices.template.get.GetIndexTemplatesResponse esResponse = org.opensearch.action.admin.indices.template.get.GetIndexTemplatesResponse esResponse =
new org.opensearch.action.admin.indices.template.get.GetIndexTemplatesResponse(new ArrayList<>()); new org.opensearch.action.admin.indices.template.get.GetIndexTemplatesResponse(new ArrayList<>());
@ -131,8 +131,7 @@ public class GetIndexTemplatesResponseTests extends OpenSearchTestCase {
assertThat(result.order(), equalTo(esIMD.order())); assertThat(result.order(), equalTo(esIMD.order()));
assertThat(result.version(), equalTo(esIMD.version())); assertThat(result.version(), equalTo(esIMD.version()));
assertThat(esIMD.mappings().size(), equalTo(1)); BytesReference mappingSource = esIMD.mappings().uncompressed();
BytesReference mappingSource = esIMD.mappings().valuesIt().next().uncompressed();
Map<String, Object> expectedMapping = XContentHelper.convertToMap(mappingSource, true, xContentBuilder.contentType()) Map<String, Object> expectedMapping = XContentHelper.convertToMap(mappingSource, true, xContentBuilder.contentType())
.v2(); .v2();
assertThat(result.mappings().sourceAsMap(), equalTo(expectedMapping.get("_doc"))); assertThat(result.mappings().sourceAsMap(), equalTo(expectedMapping.get("_doc")));
@ -224,7 +223,10 @@ public class GetIndexTemplatesResponseTests extends OpenSearchTestCase {
serverTemplateBuilder.order(clientITMD.order()); serverTemplateBuilder.order(clientITMD.order());
serverTemplateBuilder.version(clientITMD.version()); serverTemplateBuilder.version(clientITMD.version());
if (clientITMD.mappings() != null) { if (clientITMD.mappings() != null) {
serverTemplateBuilder.putMapping(MapperService.SINGLE_MAPPING_NAME, clientITMD.mappings().source()); // The client-side mappings never include a wrapping type, but server-side mappings
// for index templates still do so we need to wrap things here
String mappings = "{\"" + MapperService.SINGLE_MAPPING_NAME + "\": " + clientITMD.mappings().source().string() + "}";
serverTemplateBuilder.putMapping(MapperService.SINGLE_MAPPING_NAME, mappings);
} }
serverIndexTemplates.add(serverTemplateBuilder.build()); serverIndexTemplates.add(serverTemplateBuilder.build());

View File

@ -584,7 +584,7 @@ public class PercolateQueryBuilder extends AbstractQueryBuilder<PercolateQueryBu
); );
} }
} }
docMapper = mapperService.documentMapper(type); docMapper = mapperService.documentMapper();
for (BytesReference document : documents) { for (BytesReference document : documents) {
docs.add(docMapper.parse(new SourceToParse(context.index().getName(), type, "_temp_id", document, documentXContentType))); docs.add(docMapper.parse(new SourceToParse(context.index().getName(), type, "_temp_id", document, documentXContentType)));
} }

View File

@ -209,7 +209,7 @@ public class CandidateQueryTests extends OpenSearchSingleNodeTestCase {
.endObject() .endObject()
); );
mapperService.merge("type", new CompressedXContent(percolatorMapper), MapperService.MergeReason.MAPPING_UPDATE); mapperService.merge("type", new CompressedXContent(percolatorMapper), MapperService.MergeReason.MAPPING_UPDATE);
fieldMapper = (PercolatorFieldMapper) mapperService.documentMapper("type").mappers().getMapper(queryField); fieldMapper = (PercolatorFieldMapper) mapperService.documentMapper().mappers().getMapper(queryField);
fieldType = (PercolatorFieldMapper.PercolatorFieldType) fieldMapper.fieldType(); fieldType = (PercolatorFieldMapper.PercolatorFieldType) fieldMapper.fieldType();
queries = new ArrayList<>(); queries = new ArrayList<>();

View File

@ -161,7 +161,6 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase {
String mapper = Strings.toString( String mapper = Strings.toString(
XContentFactory.jsonBuilder() XContentFactory.jsonBuilder()
.startObject() .startObject()
.startObject("doc")
.startObject("properties") .startObject("properties")
.startObject("field") .startObject("field")
.field("type", "text") .field("type", "text")
@ -204,9 +203,8 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase {
.endObject() .endObject()
.endObject() .endObject()
.endObject() .endObject()
.endObject()
); );
mapperService.merge("doc", new CompressedXContent(mapper), MapperService.MergeReason.MAPPING_UPDATE); mapperService.merge(MapperService.SINGLE_MAPPING_NAME, new CompressedXContent(mapper), MapperService.MergeReason.MAPPING_UPDATE);
} }
private void addQueryFieldMappings() throws Exception { private void addQueryFieldMappings() throws Exception {
@ -214,16 +212,18 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase {
String percolatorMapper = Strings.toString( String percolatorMapper = Strings.toString(
XContentFactory.jsonBuilder() XContentFactory.jsonBuilder()
.startObject() .startObject()
.startObject("doc")
.startObject("properties") .startObject("properties")
.startObject(fieldName) .startObject(fieldName)
.field("type", "percolator") .field("type", "percolator")
.endObject() .endObject()
.endObject() .endObject()
.endObject() .endObject()
.endObject()
); );
mapperService.merge("doc", new CompressedXContent(percolatorMapper), MapperService.MergeReason.MAPPING_UPDATE); mapperService.merge(
MapperService.SINGLE_MAPPING_NAME,
new CompressedXContent(percolatorMapper),
MapperService.MergeReason.MAPPING_UPDATE
);
fieldType = (PercolatorFieldMapper.PercolatorFieldType) mapperService.fieldType(fieldName); fieldType = (PercolatorFieldMapper.PercolatorFieldType) mapperService.fieldType(fieldName);
} }
@ -235,7 +235,7 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase {
TermQuery termQuery2 = new TermQuery(new Term("field", "term2")); TermQuery termQuery2 = new TermQuery(new Term("field", "term2"));
bq.add(termQuery2, Occur.SHOULD); bq.add(termQuery2, Occur.SHOULD);
DocumentMapper documentMapper = mapperService.documentMapper("doc"); DocumentMapper documentMapper = mapperService.documentMapper();
PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName); PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName);
IndexMetadata build = IndexMetadata.builder("") IndexMetadata build = IndexMetadata.builder("")
.settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)) .settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT))
@ -295,7 +295,7 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase {
Query rangeQuery2 = mapperService.fieldType("number_field1").rangeQuery(15, 20, true, true, null, null, null, context); Query rangeQuery2 = mapperService.fieldType("number_field1").rangeQuery(15, 20, true, true, null, null, null, context);
bq.add(rangeQuery2, Occur.MUST); bq.add(rangeQuery2, Occur.MUST);
DocumentMapper documentMapper = mapperService.documentMapper("doc"); DocumentMapper documentMapper = mapperService.documentMapper();
IndexMetadata build = IndexMetadata.builder("") IndexMetadata build = IndexMetadata.builder("")
.settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)) .settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT))
.numberOfShards(1) .numberOfShards(1)
@ -354,7 +354,7 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase {
public void testExtractTermsAndRanges_failed() throws Exception { public void testExtractTermsAndRanges_failed() throws Exception {
addQueryFieldMappings(); addQueryFieldMappings();
TermRangeQuery query = new TermRangeQuery("field1", new BytesRef("a"), new BytesRef("z"), true, true); TermRangeQuery query = new TermRangeQuery("field1", new BytesRef("a"), new BytesRef("z"), true, true);
DocumentMapper documentMapper = mapperService.documentMapper("doc"); DocumentMapper documentMapper = mapperService.documentMapper();
PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName); PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName);
IndexMetadata build = IndexMetadata.builder("") IndexMetadata build = IndexMetadata.builder("")
.settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)) .settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT))
@ -380,7 +380,7 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase {
public void testExtractTermsAndRanges_partial() throws Exception { public void testExtractTermsAndRanges_partial() throws Exception {
addQueryFieldMappings(); addQueryFieldMappings();
PhraseQuery phraseQuery = new PhraseQuery("field", "term"); PhraseQuery phraseQuery = new PhraseQuery("field", "term");
DocumentMapper documentMapper = mapperService.documentMapper("doc"); DocumentMapper documentMapper = mapperService.documentMapper();
PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName); PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName);
IndexMetadata build = IndexMetadata.builder("") IndexMetadata build = IndexMetadata.builder("")
.settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)) .settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT))
@ -549,11 +549,11 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase {
public void testPercolatorFieldMapper() throws Exception { public void testPercolatorFieldMapper() throws Exception {
addQueryFieldMappings(); addQueryFieldMappings();
QueryBuilder queryBuilder = termQuery("field", "value"); QueryBuilder queryBuilder = termQuery("field", "value");
ParsedDocument doc = mapperService.documentMapper("doc") ParsedDocument doc = mapperService.documentMapper()
.parse( .parse(
new SourceToParse( new SourceToParse(
"test", "test",
"doc", MapperService.SINGLE_MAPPING_NAME,
"1", "1",
BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, queryBuilder).endObject()), BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, queryBuilder).endObject()),
XContentType.JSON XContentType.JSON
@ -570,11 +570,11 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase {
// add an query for which we don't extract terms from // add an query for which we don't extract terms from
queryBuilder = rangeQuery("field").from("a").to("z"); queryBuilder = rangeQuery("field").from("a").to("z");
doc = mapperService.documentMapper("doc") doc = mapperService.documentMapper()
.parse( .parse(
new SourceToParse( new SourceToParse(
"test", "test",
"doc", MapperService.SINGLE_MAPPING_NAME,
"1", "1",
BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, queryBuilder).endObject()), BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, queryBuilder).endObject()),
XContentType.JSON XContentType.JSON
@ -592,7 +592,7 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase {
.parse( .parse(
new SourceToParse( new SourceToParse(
"test", "test",
"doc", MapperService.SINGLE_MAPPING_NAME,
"1", "1",
BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, queryBuilder).endObject()), BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, queryBuilder).endObject()),
XContentType.JSON XContentType.JSON
@ -617,11 +617,11 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase {
// (it can't use shard data for rewriting purposes, because percolator queries run on MemoryIndex) // (it can't use shard data for rewriting purposes, because percolator queries run on MemoryIndex)
for (QueryBuilder query : queries) { for (QueryBuilder query : queries) {
ParsedDocument doc = mapperService.documentMapper("doc") ParsedDocument doc = mapperService.documentMapper()
.parse( .parse(
new SourceToParse( new SourceToParse(
"test", "test",
"doc", MapperService.SINGLE_MAPPING_NAME,
"1", "1",
BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, query).endObject()), BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, query).endObject()),
XContentType.JSON XContentType.JSON
@ -636,11 +636,11 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase {
addQueryFieldMappings(); addQueryFieldMappings();
client().prepareIndex("remote").setId("1").setSource("field", "value").get(); client().prepareIndex("remote").setId("1").setSource("field", "value").get();
QueryBuilder queryBuilder = termsLookupQuery("field", new TermsLookup("remote", "1", "field")); QueryBuilder queryBuilder = termsLookupQuery("field", new TermsLookup("remote", "1", "field"));
ParsedDocument doc = mapperService.documentMapper("doc") ParsedDocument doc = mapperService.documentMapper()
.parse( .parse(
new SourceToParse( new SourceToParse(
"test", "test",
"doc", MapperService.SINGLE_MAPPING_NAME,
"1", "1",
BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, queryBuilder).endObject()), BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, queryBuilder).endObject()),
XContentType.JSON XContentType.JSON
@ -661,11 +661,11 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase {
public void testPercolatorFieldMapperUnMappedField() throws Exception { public void testPercolatorFieldMapperUnMappedField() throws Exception {
addQueryFieldMappings(); addQueryFieldMappings();
MapperParsingException exception = expectThrows(MapperParsingException.class, () -> { MapperParsingException exception = expectThrows(MapperParsingException.class, () -> {
mapperService.documentMapper("doc") mapperService.documentMapper()
.parse( .parse(
new SourceToParse( new SourceToParse(
"test", "test",
"doc", MapperService.SINGLE_MAPPING_NAME,
"1", "1",
BytesReference.bytes( BytesReference.bytes(
XContentFactory.jsonBuilder().startObject().field(fieldName, termQuery("unmapped_field", "value")).endObject() XContentFactory.jsonBuilder().startObject().field(fieldName, termQuery("unmapped_field", "value")).endObject()
@ -680,11 +680,11 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase {
public void testPercolatorFieldMapper_noQuery() throws Exception { public void testPercolatorFieldMapper_noQuery() throws Exception {
addQueryFieldMappings(); addQueryFieldMappings();
ParsedDocument doc = mapperService.documentMapper("doc") ParsedDocument doc = mapperService.documentMapper()
.parse( .parse(
new SourceToParse( new SourceToParse(
"test", "test",
"doc", MapperService.SINGLE_MAPPING_NAME,
"1", "1",
BytesReference.bytes(XContentFactory.jsonBuilder().startObject().endObject()), BytesReference.bytes(XContentFactory.jsonBuilder().startObject().endObject()),
XContentType.JSON XContentType.JSON
@ -693,11 +693,11 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase {
assertThat(doc.rootDoc().getFields(fieldType.queryBuilderField.name()).length, equalTo(0)); assertThat(doc.rootDoc().getFields(fieldType.queryBuilderField.name()).length, equalTo(0));
try { try {
mapperService.documentMapper("doc") mapperService.documentMapper()
.parse( .parse(
new SourceToParse( new SourceToParse(
"test", "test",
"doc", MapperService.SINGLE_MAPPING_NAME,
"1", "1",
BytesReference.bytes(XContentFactory.jsonBuilder().startObject().nullField(fieldName).endObject()), BytesReference.bytes(XContentFactory.jsonBuilder().startObject().nullField(fieldName).endObject()),
XContentType.JSON XContentType.JSON
@ -716,7 +716,6 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase {
String percolatorMapper = Strings.toString( String percolatorMapper = Strings.toString(
XContentFactory.jsonBuilder() XContentFactory.jsonBuilder()
.startObject() .startObject()
.startObject("doc")
.startObject("properties") .startObject("properties")
.startObject(fieldName) .startObject(fieldName)
.field("type", "percolator") .field("type", "percolator")
@ -724,18 +723,21 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase {
.endObject() .endObject()
.endObject() .endObject()
.endObject() .endObject()
.endObject()
); );
MapperParsingException e = expectThrows( MapperParsingException e = expectThrows(
MapperParsingException.class, MapperParsingException.class,
() -> mapperService.merge("doc", new CompressedXContent(percolatorMapper), MapperService.MergeReason.MAPPING_UPDATE) () -> mapperService.merge(
MapperService.SINGLE_MAPPING_NAME,
new CompressedXContent(percolatorMapper),
MapperService.MergeReason.MAPPING_UPDATE
)
); );
assertThat(e.getMessage(), containsString("Mapping definition for [" + fieldName + "] has unsupported parameters: [index : no]")); assertThat(e.getMessage(), containsString("Mapping definition for [" + fieldName + "] has unsupported parameters: [index : no]"));
} }
// multiple percolator fields are allowed in the mapping, but only one field can be used at index time. // multiple percolator fields are allowed in the mapping, but only one field can be used at index time.
public void testMultiplePercolatorFields() throws Exception { public void testMultiplePercolatorFields() throws Exception {
String typeName = "doc"; String typeName = MapperService.SINGLE_MAPPING_NAME;
String percolatorMapper = Strings.toString( String percolatorMapper = Strings.toString(
XContentFactory.jsonBuilder() XContentFactory.jsonBuilder()
.startObject() .startObject()
@ -754,7 +756,7 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase {
mapperService.merge(typeName, new CompressedXContent(percolatorMapper), MapperService.MergeReason.MAPPING_UPDATE); mapperService.merge(typeName, new CompressedXContent(percolatorMapper), MapperService.MergeReason.MAPPING_UPDATE);
QueryBuilder queryBuilder = matchQuery("field", "value"); QueryBuilder queryBuilder = matchQuery("field", "value");
ParsedDocument doc = mapperService.documentMapper(typeName) ParsedDocument doc = mapperService.documentMapper()
.parse( .parse(
new SourceToParse( new SourceToParse(
"test", "test",
@ -776,7 +778,7 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase {
// percolator field can be nested under an object field, but only one query can be specified per document // percolator field can be nested under an object field, but only one query can be specified per document
public void testNestedPercolatorField() throws Exception { public void testNestedPercolatorField() throws Exception {
String typeName = "doc"; String typeName = MapperService.SINGLE_MAPPING_NAME;
String percolatorMapper = Strings.toString( String percolatorMapper = Strings.toString(
XContentFactory.jsonBuilder() XContentFactory.jsonBuilder()
.startObject() .startObject()
@ -797,7 +799,7 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase {
mapperService.merge(typeName, new CompressedXContent(percolatorMapper), MapperService.MergeReason.MAPPING_UPDATE); mapperService.merge(typeName, new CompressedXContent(percolatorMapper), MapperService.MergeReason.MAPPING_UPDATE);
QueryBuilder queryBuilder = matchQuery("field", "value"); QueryBuilder queryBuilder = matchQuery("field", "value");
ParsedDocument doc = mapperService.documentMapper(typeName) ParsedDocument doc = mapperService.documentMapper()
.parse( .parse(
new SourceToParse( new SourceToParse(
"test", "test",
@ -817,7 +819,7 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase {
BytesRef queryBuilderAsBytes = queryBuilderField.binaryValue(); BytesRef queryBuilderAsBytes = queryBuilderField.binaryValue();
assertQueryBuilder(queryBuilderAsBytes, queryBuilder); assertQueryBuilder(queryBuilderAsBytes, queryBuilder);
doc = mapperService.documentMapper(typeName) doc = mapperService.documentMapper()
.parse( .parse(
new SourceToParse( new SourceToParse(
"test", "test",
@ -840,7 +842,7 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase {
assertQueryBuilder(queryBuilderAsBytes, queryBuilder); assertQueryBuilder(queryBuilderAsBytes, queryBuilder);
MapperParsingException e = expectThrows(MapperParsingException.class, () -> { MapperParsingException e = expectThrows(MapperParsingException.class, () -> {
mapperService.documentMapper(typeName) mapperService.documentMapper()
.parse( .parse(
new SourceToParse( new SourceToParse(
"test", "test",
@ -948,11 +950,11 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase {
query.endObject(); query.endObject();
query.endObject(); query.endObject();
ParsedDocument doc = mapperService.documentMapper("doc") ParsedDocument doc = mapperService.documentMapper()
.parse( .parse(
new SourceToParse( new SourceToParse(
"test", "test",
"doc", MapperService.SINGLE_MAPPING_NAME,
"1", "1",
BytesReference.bytes( BytesReference.bytes(
XContentFactory.jsonBuilder() XContentFactory.jsonBuilder()
@ -996,11 +998,11 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase {
query.endObject(); query.endObject();
query.endObject(); query.endObject();
doc = mapperService.documentMapper("doc") doc = mapperService.documentMapper()
.parse( .parse(
new SourceToParse( new SourceToParse(
"test", "test",
"doc", MapperService.SINGLE_MAPPING_NAME,
"1", "1",
BytesReference.bytes( BytesReference.bytes(
XContentFactory.jsonBuilder() XContentFactory.jsonBuilder()
@ -1091,11 +1093,11 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase {
QueryBuilder qb = boolQuery().must(boolQuery().must(termQuery("field", "value1")).must(termQuery("field", "value2"))) QueryBuilder qb = boolQuery().must(boolQuery().must(termQuery("field", "value1")).must(termQuery("field", "value2")))
.must(boolQuery().must(termQuery("field", "value2")).must(termQuery("field", "value3"))); .must(boolQuery().must(termQuery("field", "value2")).must(termQuery("field", "value3")));
ParsedDocument doc = mapperService.documentMapper("doc") ParsedDocument doc = mapperService.documentMapper()
.parse( .parse(
new SourceToParse( new SourceToParse(
"test", "test",
"doc", MapperService.SINGLE_MAPPING_NAME,
"1", "1",
BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, qb).endObject()), BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, qb).endObject()),
XContentType.JSON XContentType.JSON
@ -1117,11 +1119,11 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase {
.must(boolQuery().must(termQuery("field", "value2")).must(termQuery("field", "value3"))) .must(boolQuery().must(termQuery("field", "value2")).must(termQuery("field", "value3")))
.must(boolQuery().must(termQuery("field", "value3")).must(termQuery("field", "value4"))) .must(boolQuery().must(termQuery("field", "value3")).must(termQuery("field", "value4")))
.must(boolQuery().should(termQuery("field", "value4")).should(termQuery("field", "value5"))); .must(boolQuery().should(termQuery("field", "value4")).should(termQuery("field", "value5")));
doc = mapperService.documentMapper("doc") doc = mapperService.documentMapper()
.parse( .parse(
new SourceToParse( new SourceToParse(
"test", "test",
"doc", MapperService.SINGLE_MAPPING_NAME,
"1", "1",
BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, qb).endObject()), BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, qb).endObject()),
XContentType.JSON XContentType.JSON
@ -1146,11 +1148,11 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase {
.should(boolQuery().should(termQuery("field", "value2")).should(termQuery("field", "value3"))) .should(boolQuery().should(termQuery("field", "value2")).should(termQuery("field", "value3")))
.should(boolQuery().should(termQuery("field", "value3")).should(termQuery("field", "value4"))) .should(boolQuery().should(termQuery("field", "value3")).should(termQuery("field", "value4")))
.should(boolQuery().should(termQuery("field", "value4")).should(termQuery("field", "value5"))); .should(boolQuery().should(termQuery("field", "value4")).should(termQuery("field", "value5")));
doc = mapperService.documentMapper("doc") doc = mapperService.documentMapper()
.parse( .parse(
new SourceToParse( new SourceToParse(
"test", "test",
"doc", MapperService.SINGLE_MAPPING_NAME,
"1", "1",
BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, qb).endObject()), BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, qb).endObject()),
XContentType.JSON XContentType.JSON

View File

@ -63,10 +63,10 @@ public class SizeMappingTests extends OpenSearchSingleNodeTestCase {
public void testSizeEnabled() throws Exception { public void testSizeEnabled() throws Exception {
IndexService service = createIndex("test", Settings.EMPTY, "type", "_size", "enabled=true"); IndexService service = createIndex("test", Settings.EMPTY, "type", "_size", "enabled=true");
DocumentMapper docMapper = service.mapperService().documentMapper("type"); DocumentMapper docMapper = service.mapperService().documentMapper();
BytesReference source = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("field", "value").endObject()); BytesReference source = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("field", "value").endObject());
ParsedDocument doc = docMapper.parse(new SourceToParse("test", "type", "1", source, XContentType.JSON)); ParsedDocument doc = docMapper.parse(new SourceToParse("test", MapperService.SINGLE_MAPPING_NAME, "1", source, XContentType.JSON));
boolean stored = false; boolean stored = false;
boolean points = false; boolean points = false;
@ -80,27 +80,27 @@ public class SizeMappingTests extends OpenSearchSingleNodeTestCase {
public void testSizeDisabled() throws Exception { public void testSizeDisabled() throws Exception {
IndexService service = createIndex("test", Settings.EMPTY, "type", "_size", "enabled=false"); IndexService service = createIndex("test", Settings.EMPTY, "type", "_size", "enabled=false");
DocumentMapper docMapper = service.mapperService().documentMapper("type"); DocumentMapper docMapper = service.mapperService().documentMapper();
BytesReference source = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("field", "value").endObject()); BytesReference source = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("field", "value").endObject());
ParsedDocument doc = docMapper.parse(new SourceToParse("test", "type", "1", source, XContentType.JSON)); ParsedDocument doc = docMapper.parse(new SourceToParse("test", MapperService.SINGLE_MAPPING_NAME, "1", source, XContentType.JSON));
assertThat(doc.rootDoc().getField("_size"), nullValue()); assertThat(doc.rootDoc().getField("_size"), nullValue());
} }
public void testSizeNotSet() throws Exception { public void testSizeNotSet() throws Exception {
IndexService service = createIndex("test", Settings.EMPTY, "type"); IndexService service = createIndex("test", Settings.EMPTY, MapperService.SINGLE_MAPPING_NAME);
DocumentMapper docMapper = service.mapperService().documentMapper("type"); DocumentMapper docMapper = service.mapperService().documentMapper();
BytesReference source = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("field", "value").endObject()); BytesReference source = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("field", "value").endObject());
ParsedDocument doc = docMapper.parse(new SourceToParse("test", "type", "1", source, XContentType.JSON)); ParsedDocument doc = docMapper.parse(new SourceToParse("test", MapperService.SINGLE_MAPPING_NAME, "1", source, XContentType.JSON));
assertThat(doc.rootDoc().getField("_size"), nullValue()); assertThat(doc.rootDoc().getField("_size"), nullValue());
} }
public void testThatDisablingWorksWhenMerging() throws Exception { public void testThatDisablingWorksWhenMerging() throws Exception {
IndexService service = createIndex("test", Settings.EMPTY, "type", "_size", "enabled=true"); IndexService service = createIndex("test", Settings.EMPTY, "type", "_size", "enabled=true");
DocumentMapper docMapper = service.mapperService().documentMapper("type"); DocumentMapper docMapper = service.mapperService().documentMapper();
assertThat(docMapper.metadataMapper(SizeFieldMapper.class).enabled(), is(true)); assertThat(docMapper.metadataMapper(SizeFieldMapper.class).enabled(), is(true));
String disabledMapping = Strings.toString( String disabledMapping = Strings.toString(

View File

@ -664,9 +664,6 @@ public class FullClusterRestartIT extends AbstractFullClusterRestartTestCase {
// before timing out // before timing out
.put(INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), "100ms") .put(INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), "100ms")
.put(SETTING_ALLOCATION_MAX_RETRY.getKey(), "0"); // fail faster .put(SETTING_ALLOCATION_MAX_RETRY.getKey(), "0"); // fail faster
if (randomBoolean()) {
settings.put(IndexSettings.INDEX_TRANSLOG_RETENTION_SIZE_SETTING.getKey(), "-1");
}
createIndex(index, settings.build()); createIndex(index, settings.build());
} }
ensureGreen(index); ensureGreen(index);

View File

@ -1,5 +1,5 @@
--- ---
"Return empty object if field doesn't exist, but type and index do": "Return empty object if field doesn't exist, but index does":
- do: - do:
indices.create: indices.create:
index: test_index index: test_index

View File

@ -52,19 +52,18 @@ import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.index.IndexNotFoundException; import org.opensearch.index.IndexNotFoundException;
import org.opensearch.index.IndexService; import org.opensearch.index.IndexService;
import org.opensearch.index.mapper.MapperParsingException; import org.opensearch.index.mapper.MapperParsingException;
import org.opensearch.index.mapper.MapperService;
import org.opensearch.index.query.RangeQueryBuilder; import org.opensearch.index.query.RangeQueryBuilder;
import org.opensearch.indices.IndicesService; import org.opensearch.indices.IndicesService;
import org.opensearch.test.OpenSearchIntegTestCase; import org.opensearch.test.OpenSearchIntegTestCase;
import org.opensearch.test.OpenSearchIntegTestCase.ClusterScope; import org.opensearch.test.OpenSearchIntegTestCase.ClusterScope;
import org.opensearch.test.OpenSearchIntegTestCase.Scope; import org.opensearch.test.OpenSearchIntegTestCase.Scope;
import java.util.HashMap;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_WAIT_FOR_ACTIVE_SHARDS; import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_WAIT_FOR_ACTIVE_SHARDS;
import static org.opensearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked; import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked;
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertBlocked; import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertBlocked;
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertRequestBuilderThrows; import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertRequestBuilderThrows;
@ -109,28 +108,6 @@ public class CreateIndexIT extends OpenSearchIntegTestCase {
assertThat(index.getCreationDate(), allOf(lessThanOrEqualTo(timeAfterRequest), greaterThanOrEqualTo(timeBeforeRequest))); assertThat(index.getCreationDate(), allOf(lessThanOrEqualTo(timeAfterRequest), greaterThanOrEqualTo(timeBeforeRequest)));
} }
public void testDoubleAddMapping() throws Exception {
try {
prepareCreate("test").addMapping("type1", "date", "type=date").addMapping("type1", "num", "type=integer");
fail("did not hit expected exception");
} catch (IllegalStateException ise) {
// expected
}
try {
prepareCreate("test").addMapping("type1", new HashMap<String, Object>()).addMapping("type1", new HashMap<String, Object>());
fail("did not hit expected exception");
} catch (IllegalStateException ise) {
// expected
}
try {
prepareCreate("test").addMapping("type1", jsonBuilder().startObject().endObject())
.addMapping("type1", jsonBuilder().startObject().endObject());
fail("did not hit expected exception");
} catch (IllegalStateException ise) {
// expected
}
}
public void testNonNestedMappings() throws Exception { public void testNonNestedMappings() throws Exception {
assertAcked( assertAcked(
prepareCreate("test").addMapping( prepareCreate("test").addMapping(
@ -168,11 +145,16 @@ public class CreateIndexIT extends OpenSearchIntegTestCase {
MapperParsingException e = expectThrows( MapperParsingException e = expectThrows(
MapperParsingException.class, MapperParsingException.class,
() -> prepareCreate("test").addMapping( () -> prepareCreate("test").addMapping(
"type1", MapperService.SINGLE_MAPPING_NAME,
XContentFactory.jsonBuilder().startObject().startObject("type2").endObject().endObject() XContentFactory.jsonBuilder().startObject().startObject("type2").endObject().endObject()
).get() ).get()
); );
assertThat(e.getMessage(), startsWith("Failed to parse mapping [type1]: Root mapping definition has unsupported parameters")); assertThat(
e.getMessage(),
startsWith(
"Failed to parse mapping [" + MapperService.SINGLE_MAPPING_NAME + "]: Root mapping definition has unsupported parameters"
)
);
} }
public void testEmptyMappings() throws Exception { public void testEmptyMappings() throws Exception {

View File

@ -365,7 +365,7 @@ public class RareClusterStateIT extends OpenSearchIntegTestCase {
final IndexService indexService = indicesService.indexServiceSafe(index); final IndexService indexService = indicesService.indexServiceSafe(index);
assertNotNull(indexService); assertNotNull(indexService);
final MapperService mapperService = indexService.mapperService(); final MapperService mapperService = indexService.mapperService();
DocumentMapper mapper = mapperService.documentMapper(MapperService.SINGLE_MAPPING_NAME); DocumentMapper mapper = mapperService.documentMapper();
assertNotNull(mapper); assertNotNull(mapper);
assertNotNull(mapper.mappers().getMapper("field")); assertNotNull(mapper.mappers().getMapper("field"));
}); });
@ -389,7 +389,7 @@ public class RareClusterStateIT extends OpenSearchIntegTestCase {
final IndexService indexService = indicesService.indexServiceSafe(index); final IndexService indexService = indicesService.indexServiceSafe(index);
assertNotNull(indexService); assertNotNull(indexService);
final MapperService mapperService = indexService.mapperService(); final MapperService mapperService = indexService.mapperService();
DocumentMapper mapper = mapperService.documentMapper(MapperService.SINGLE_MAPPING_NAME); DocumentMapper mapper = mapperService.documentMapper();
assertNotNull(mapper); assertNotNull(mapper);
assertNotNull(mapper.mappers().getMapper("field2")); assertNotNull(mapper.mappers().getMapper("field2"));
}); });

View File

@ -47,6 +47,7 @@ import org.opensearch.common.Strings;
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 org.opensearch.common.xcontent.XContentType;
import org.opensearch.index.mapper.MapperService;
import org.opensearch.test.OpenSearchIntegTestCase; import org.opensearch.test.OpenSearchIntegTestCase;
import org.opensearch.test.hamcrest.OpenSearchAssertions; import org.opensearch.test.hamcrest.OpenSearchAssertions;
@ -182,7 +183,7 @@ public class DocumentActionsIT extends OpenSearchIntegTestCase {
// test successful // test successful
SearchResponse countResponse = client().prepareSearch("test") SearchResponse countResponse = client().prepareSearch("test")
.setSize(0) .setSize(0)
.setQuery(termQuery("_type", "type1")) .setQuery(termQuery("_type", MapperService.SINGLE_MAPPING_NAME))
.execute() .execute()
.actionGet(); .actionGet();
assertNoFailures(countResponse); assertNoFailures(countResponse);

View File

@ -60,6 +60,7 @@ import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentType; import org.opensearch.common.xcontent.XContentType;
import org.opensearch.env.NodeEnvironment; import org.opensearch.env.NodeEnvironment;
import org.opensearch.index.mapper.MapperParsingException; import org.opensearch.index.mapper.MapperParsingException;
import org.opensearch.index.mapper.MapperService;
import org.opensearch.indices.IndexClosedException; import org.opensearch.indices.IndexClosedException;
import org.opensearch.indices.ShardLimitValidator; import org.opensearch.indices.ShardLimitValidator;
import org.opensearch.test.OpenSearchIntegTestCase; import org.opensearch.test.OpenSearchIntegTestCase;
@ -108,14 +109,7 @@ public class GatewayIndexStateIT extends OpenSearchIntegTestCase {
.prepareCreate("test") .prepareCreate("test")
.addMapping( .addMapping(
"type1", "type1",
XContentFactory.jsonBuilder() XContentFactory.jsonBuilder().startObject().startObject("_routing").field("required", true).endObject().endObject()
.startObject()
.startObject("type1")
.startObject("_routing")
.field("required", true)
.endObject()
.endObject()
.endObject()
) )
.execute() .execute()
.actionGet(); .actionGet();
@ -130,7 +124,7 @@ public class GatewayIndexStateIT extends OpenSearchIntegTestCase {
.metadata() .metadata()
.index("test") .index("test")
.getMappings() .getMappings()
.get("type1"); .get(MapperService.SINGLE_MAPPING_NAME);
assertThat(mappingMd.routing().required(), equalTo(true)); assertThat(mappingMd.routing().required(), equalTo(true));
logger.info("--> restarting nodes..."); logger.info("--> restarting nodes...");
@ -149,7 +143,7 @@ public class GatewayIndexStateIT extends OpenSearchIntegTestCase {
.metadata() .metadata()
.index("test") .index("test")
.getMappings() .getMappings()
.get("type1"); .get(MapperService.SINGLE_MAPPING_NAME);
assertThat(mappingMd.routing().required(), equalTo(true)); assertThat(mappingMd.routing().required(), equalTo(true));
} }

View File

@ -208,7 +208,6 @@ public class MultiFieldsIntegrationIT extends OpenSearchIntegTestCase {
private XContentBuilder createPutMappingSource() throws IOException { private XContentBuilder createPutMappingSource() throws IOException {
return XContentFactory.jsonBuilder() return XContentFactory.jsonBuilder()
.startObject() .startObject()
.startObject("my-type")
.startObject("properties") .startObject("properties")
.startObject("title") .startObject("title")
.field("type", "text") .field("type", "text")
@ -220,7 +219,6 @@ public class MultiFieldsIntegrationIT extends OpenSearchIntegTestCase {
.endObject() .endObject()
.endObject() .endObject()
.endObject() .endObject()
.endObject()
.endObject(); .endObject();
} }

View File

@ -74,6 +74,7 @@ import org.opensearch.index.engine.CommitStats;
import org.opensearch.index.engine.Engine; import org.opensearch.index.engine.Engine;
import org.opensearch.index.engine.NoOpEngine; import org.opensearch.index.engine.NoOpEngine;
import org.opensearch.index.flush.FlushStats; import org.opensearch.index.flush.FlushStats;
import org.opensearch.index.mapper.MapperService;
import org.opensearch.index.mapper.SourceToParse; import org.opensearch.index.mapper.SourceToParse;
import org.opensearch.index.seqno.RetentionLeaseSyncer; import org.opensearch.index.seqno.RetentionLeaseSyncer;
import org.opensearch.index.seqno.SequenceNumbers; import org.opensearch.index.seqno.SequenceNumbers;
@ -445,7 +446,7 @@ public class IndexShardIT extends OpenSearchSingleNodeTestCase {
.put("index.number_of_shards", 1) .put("index.number_of_shards", 1)
.put("index.translog.generation_threshold_size", generationThreshold + "b") .put("index.translog.generation_threshold_size", generationThreshold + "b")
.build(); .build();
createIndex("test", settings, "test"); createIndex("test", settings, MapperService.SINGLE_MAPPING_NAME);
ensureGreen("test"); ensureGreen("test");
final IndicesService indicesService = getInstanceFromNode(IndicesService.class); final IndicesService indicesService = getInstanceFromNode(IndicesService.class);
final IndexService test = indicesService.indexService(resolveIndex("test")); final IndexService test = indicesService.indexService(resolveIndex("test"));
@ -459,7 +460,7 @@ public class IndexShardIT extends OpenSearchSingleNodeTestCase {
final Engine.IndexResult result = shard.applyIndexOperationOnPrimary( final Engine.IndexResult result = shard.applyIndexOperationOnPrimary(
Versions.MATCH_ANY, Versions.MATCH_ANY,
VersionType.INTERNAL, VersionType.INTERNAL,
new SourceToParse("test", "test", "1", new BytesArray("{}"), XContentType.JSON), new SourceToParse("test", MapperService.SINGLE_MAPPING_NAME, "1", new BytesArray("{}"), XContentType.JSON),
SequenceNumbers.UNASSIGNED_SEQ_NO, SequenceNumbers.UNASSIGNED_SEQ_NO,
0, 0,
IndexRequest.UNSET_AUTO_GENERATED_TIMESTAMP, IndexRequest.UNSET_AUTO_GENERATED_TIMESTAMP,

View File

@ -1,128 +0,0 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
package org.opensearch.indices.exists.types;
import org.opensearch.action.admin.indices.create.CreateIndexResponse;
import org.opensearch.action.admin.indices.exists.types.TypesExistsResponse;
import org.opensearch.client.Client;
import org.opensearch.cluster.metadata.IndexMetadata;
import org.opensearch.index.IndexNotFoundException;
import org.opensearch.plugins.Plugin;
import org.opensearch.test.OpenSearchIntegTestCase;
import org.opensearch.test.InternalSettingsPlugin;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_BLOCKS_READ;
import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_BLOCKS_WRITE;
import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_READ_ONLY;
import static org.opensearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked;
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertBlocked;
import static org.hamcrest.Matchers.equalTo;
public class TypesExistsIT extends OpenSearchIntegTestCase {
@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
return Collections.singleton(InternalSettingsPlugin.class);
}
public void testSimple() throws Exception {
Client client = client();
CreateIndexResponse response1 = client.admin()
.indices()
.prepareCreate("test1")
.addMapping("type1", jsonBuilder().startObject().startObject("type1").endObject().endObject())
.execute()
.actionGet();
CreateIndexResponse response2 = client.admin()
.indices()
.prepareCreate("test2")
.addMapping("type2", jsonBuilder().startObject().startObject("type2").endObject().endObject())
.execute()
.actionGet();
client.admin().indices().prepareAliases().addAlias("test1", "alias1").execute().actionGet();
assertAcked(response1);
assertAcked(response2);
TypesExistsResponse response = client.admin().indices().prepareTypesExists("test1").setTypes("type1").execute().actionGet();
assertThat(response.isExists(), equalTo(true));
response = client.admin().indices().prepareTypesExists("test1").setTypes("type2").execute().actionGet();
assertThat(response.isExists(), equalTo(false));
try {
client.admin().indices().prepareTypesExists("notExist").setTypes("type1").execute().actionGet();
fail("Exception should have been thrown");
} catch (IndexNotFoundException e) {}
try {
client.admin().indices().prepareTypesExists("notExist").setTypes("type0").execute().actionGet();
fail("Exception should have been thrown");
} catch (IndexNotFoundException e) {}
response = client.admin().indices().prepareTypesExists("alias1").setTypes("type1").execute().actionGet();
assertThat(response.isExists(), equalTo(true));
response = client.admin().indices().prepareTypesExists("*").setTypes("type1").execute().actionGet();
assertThat(response.isExists(), equalTo(false));
response = client.admin().indices().prepareTypesExists("test1", "test2").setTypes("type1").execute().actionGet();
assertThat(response.isExists(), equalTo(false));
response = client.admin().indices().prepareTypesExists("test1", "test2").setTypes("type2").execute().actionGet();
assertThat(response.isExists(), equalTo(false));
}
public void testTypesExistsWithBlocks() throws IOException {
assertAcked(prepareCreate("ro").addMapping("type1", jsonBuilder().startObject().startObject("type1").endObject().endObject()));
ensureGreen("ro");
// Request is not blocked
for (String block : Arrays.asList(SETTING_BLOCKS_READ, SETTING_BLOCKS_WRITE, SETTING_READ_ONLY)) {
try {
enableIndexBlock("ro", block);
assertThat(
client().admin().indices().prepareTypesExists("ro").setTypes("type1").execute().actionGet().isExists(),
equalTo(true)
);
} finally {
disableIndexBlock("ro", block);
}
}
// Request is blocked
try {
enableIndexBlock("ro", IndexMetadata.SETTING_BLOCKS_METADATA);
assertBlocked(client().admin().indices().prepareTypesExists("ro").setTypes("type1"));
} finally {
disableIndexBlock("ro", IndexMetadata.SETTING_BLOCKS_METADATA);
}
}
}

View File

@ -77,7 +77,7 @@ public class SimpleGetFieldMappingsIT extends OpenSearchIntegTestCase {
assertThat(response.mappings().size(), equalTo(1)); assertThat(response.mappings().size(), equalTo(1));
assertThat(response.mappings().get("index").size(), equalTo(0)); assertThat(response.mappings().get("index").size(), equalTo(0));
assertThat(response.fieldMappings("index", "type", "field"), nullValue()); assertThat(response.fieldMappings("index", "field"), nullValue());
} }
private XContentBuilder getMappingForType(String type) throws IOException { private XContentBuilder getMappingForType(String type) throws IOException {
@ -112,48 +112,26 @@ public class SimpleGetFieldMappingsIT extends OpenSearchIntegTestCase {
GetFieldMappingsResponse response = client().admin() GetFieldMappingsResponse response = client().admin()
.indices() .indices()
.prepareGetFieldMappings("indexa") .prepareGetFieldMappings("indexa")
.setTypes("typeA")
.setFields("field1", "obj.subfield") .setFields("field1", "obj.subfield")
.get(); .get();
assertThat(response.fieldMappings("indexa", "typeA", "field1").fullName(), equalTo("field1")); assertThat(response.fieldMappings("indexa", "field1").fullName(), equalTo("field1"));
assertThat(response.fieldMappings("indexa", "typeA", "field1").sourceAsMap(), hasKey("field1")); assertThat(response.fieldMappings("indexa", "field1").sourceAsMap(), hasKey("field1"));
assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").fullName(), equalTo("obj.subfield")); assertThat(response.fieldMappings("indexa", "obj.subfield").fullName(), equalTo("obj.subfield"));
assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").sourceAsMap(), hasKey("subfield")); assertThat(response.fieldMappings("indexa", "obj.subfield").sourceAsMap(), hasKey("subfield"));
assertThat(response.fieldMappings("indexb", "typeB", "field1"), nullValue());
// Get mappings by name // Get mappings by name
response = client().admin().indices().prepareGetFieldMappings("indexa").setTypes("typeA").setFields("field1", "obj.subfield").get(); response = client().admin().indices().prepareGetFieldMappings("indexa").setFields("field1", "obj.subfield").get();
assertThat(response.fieldMappings("indexa", "typeA", "field1").fullName(), equalTo("field1")); assertThat(response.fieldMappings("indexa", "field1").fullName(), equalTo("field1"));
assertThat(response.fieldMappings("indexa", "typeA", "field1").sourceAsMap(), hasKey("field1")); assertThat(response.fieldMappings("indexa", "field1").sourceAsMap(), hasKey("field1"));
assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").fullName(), equalTo("obj.subfield")); assertThat(response.fieldMappings("indexa", "obj.subfield").fullName(), equalTo("obj.subfield"));
assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").sourceAsMap(), hasKey("subfield")); assertThat(response.fieldMappings("indexa", "obj.subfield").sourceAsMap(), hasKey("subfield"));
assertThat(response.fieldMappings("indexa", "typeB", "field1"), nullValue());
assertThat(response.fieldMappings("indexb", "typeB", "field1"), nullValue());
// get mappings by name across multiple indices // get mappings by name across multiple indices
response = client().admin().indices().prepareGetFieldMappings().setTypes("typeA").setFields("obj.subfield").get();
assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").fullName(), equalTo("obj.subfield"));
assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").sourceAsMap(), hasKey("subfield"));
assertThat(response.fieldMappings("indexa", "typeB", "obj.subfield"), nullValue());
assertThat(response.fieldMappings("indexb", "typeB", "obj.subfield"), nullValue());
// get mappings by name across multiple types
response = client().admin().indices().prepareGetFieldMappings("indexa").setFields("obj.subfield").get();
assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").fullName(), equalTo("obj.subfield"));
assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").sourceAsMap(), hasKey("subfield"));
assertThat(response.fieldMappings("indexa", "typeA", "field1"), nullValue());
assertThat(response.fieldMappings("indexb", "typeB", "obj.subfield"), nullValue());
assertThat(response.fieldMappings("indexb", "typeB", "field1"), nullValue());
// get mappings by name across multiple types & indices
response = client().admin().indices().prepareGetFieldMappings().setFields("obj.subfield").get(); response = client().admin().indices().prepareGetFieldMappings().setFields("obj.subfield").get();
assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").fullName(), equalTo("obj.subfield")); assertThat(response.fieldMappings("indexa", "obj.subfield").fullName(), equalTo("obj.subfield"));
assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").sourceAsMap(), hasKey("subfield")); assertThat(response.fieldMappings("indexa", "obj.subfield").sourceAsMap(), hasKey("subfield"));
assertThat(response.fieldMappings("indexa", "typeA", "field1"), nullValue()); assertThat(response.fieldMappings("indexb", "obj.subfield").fullName(), equalTo("obj.subfield"));
assertThat(response.fieldMappings("indexb", "typeB", "field1"), nullValue()); assertThat(response.fieldMappings("indexb", "obj.subfield").sourceAsMap(), hasKey("subfield"));
assertThat(response.fieldMappings("indexb", "typeB", "obj.subfield").fullName(), equalTo("obj.subfield"));
assertThat(response.fieldMappings("indexb", "typeB", "obj.subfield").sourceAsMap(), hasKey("subfield"));
assertThat(response.fieldMappings("indexb", "typeB", "field1"), nullValue());
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -169,25 +147,16 @@ public class SimpleGetFieldMappingsIT extends OpenSearchIntegTestCase {
.includeDefaults(true) .includeDefaults(true)
.get(); .get();
assertThat((Map<String, Object>) response.fieldMappings("test", "num").sourceAsMap().get("num"), hasEntry("index", Boolean.TRUE));
assertThat((Map<String, Object>) response.fieldMappings("test", "num").sourceAsMap().get("num"), hasEntry("type", "long"));
assertThat( assertThat(
(Map<String, Object>) response.fieldMappings("test", "type", "num").sourceAsMap().get("num"), (Map<String, Object>) response.fieldMappings("test", "field1").sourceAsMap().get("field1"),
hasEntry("index", Boolean.TRUE) hasEntry("index", Boolean.TRUE)
); );
assertThat((Map<String, Object>) response.fieldMappings("test", "type", "num").sourceAsMap().get("num"), hasEntry("type", "long")); assertThat((Map<String, Object>) response.fieldMappings("test", "field1").sourceAsMap().get("field1"), hasEntry("type", "text"));
assertThat((Map<String, Object>) response.fieldMappings("test", "field2").sourceAsMap().get("field2"), hasEntry("type", "text"));
assertThat( assertThat(
(Map<String, Object>) response.fieldMappings("test", "type", "field1").sourceAsMap().get("field1"), (Map<String, Object>) response.fieldMappings("test", "obj.subfield").sourceAsMap().get("subfield"),
hasEntry("index", Boolean.TRUE)
);
assertThat(
(Map<String, Object>) response.fieldMappings("test", "type", "field1").sourceAsMap().get("field1"),
hasEntry("type", "text")
);
assertThat(
(Map<String, Object>) response.fieldMappings("test", "type", "field2").sourceAsMap().get("field2"),
hasEntry("type", "text")
);
assertThat(
(Map<String, Object>) response.fieldMappings("test", "type", "obj.subfield").sourceAsMap().get("subfield"),
hasEntry("type", "keyword") hasEntry("type", "keyword")
); );
} }
@ -198,12 +167,12 @@ public class SimpleGetFieldMappingsIT extends OpenSearchIntegTestCase {
GetFieldMappingsResponse response = client().admin().indices().prepareGetFieldMappings().setFields("alias", "field1").get(); GetFieldMappingsResponse response = client().admin().indices().prepareGetFieldMappings().setFields("alias", "field1").get();
FieldMappingMetadata aliasMapping = response.fieldMappings("test", "type", "alias"); FieldMappingMetadata aliasMapping = response.fieldMappings("test", "alias");
assertThat(aliasMapping.fullName(), equalTo("alias")); assertThat(aliasMapping.fullName(), equalTo("alias"));
assertThat(aliasMapping.sourceAsMap(), hasKey("alias")); assertThat(aliasMapping.sourceAsMap(), hasKey("alias"));
assertThat((Map<String, Object>) aliasMapping.sourceAsMap().get("alias"), hasEntry("type", "alias")); assertThat((Map<String, Object>) aliasMapping.sourceAsMap().get("alias"), hasEntry("type", "alias"));
FieldMappingMetadata field1Mapping = response.fieldMappings("test", "type", "field1"); FieldMappingMetadata field1Mapping = response.fieldMappings("test", "field1");
assertThat(field1Mapping.fullName(), equalTo("field1")); assertThat(field1Mapping.fullName(), equalTo("field1"));
assertThat(field1Mapping.sourceAsMap(), hasKey("field1")); assertThat(field1Mapping.sourceAsMap(), hasKey("field1"));
} }
@ -216,7 +185,6 @@ public class SimpleGetFieldMappingsIT extends OpenSearchIntegTestCase {
GetFieldMappingsResponse response = client().admin() GetFieldMappingsResponse response = client().admin()
.indices() .indices()
.prepareGetFieldMappings("index") .prepareGetFieldMappings("index")
.setTypes("type")
.setFields("field1", "obj.subfield") .setFields("field1", "obj.subfield")
.get(); .get();
XContentBuilder responseBuilder = XContentFactory.jsonBuilder().prettyPrint(); XContentBuilder responseBuilder = XContentFactory.jsonBuilder().prettyPrint();
@ -229,7 +197,7 @@ public class SimpleGetFieldMappingsIT extends OpenSearchIntegTestCase {
params.put("pretty", "false"); params.put("pretty", "false");
response = client().admin().indices().prepareGetFieldMappings("index").setTypes("type").setFields("field1", "obj.subfield").get(); response = client().admin().indices().prepareGetFieldMappings("index").setFields("field1", "obj.subfield").get();
responseBuilder = XContentFactory.jsonBuilder().prettyPrint().lfAtEnd(); responseBuilder = XContentFactory.jsonBuilder().prettyPrint().lfAtEnd();
response.toXContent(responseBuilder, new ToXContent.MapParams(params)); response.toXContent(responseBuilder, new ToXContent.MapParams(params));
responseStrings = Strings.toString(responseBuilder); responseStrings = Strings.toString(responseBuilder);
@ -249,10 +217,9 @@ public class SimpleGetFieldMappingsIT extends OpenSearchIntegTestCase {
GetFieldMappingsResponse response = client().admin() GetFieldMappingsResponse response = client().admin()
.indices() .indices()
.prepareGetFieldMappings("test") .prepareGetFieldMappings("test")
.setTypes("_doc")
.setFields("field1", "obj.subfield") .setFields("field1", "obj.subfield")
.get(); .get();
assertThat(response.fieldMappings("test", "_doc", "field1").fullName(), equalTo("field1")); assertThat(response.fieldMappings("test", "field1").fullName(), equalTo("field1"));
} finally { } finally {
disableIndexBlock("test", block); disableIndexBlock("test", block);
} }

View File

@ -205,7 +205,10 @@ public class UpdateMappingIntegrationIT extends OpenSearchIntegTestCase {
client().admin() client().admin()
.indices() .indices()
.preparePutMapping("test") .preparePutMapping("test")
.setSource("{\"type\":{\"properties\":{\"body\":{\"type\":\"integer\"}}}}", XContentType.JSON) .setSource(
"{\"" + MapperService.SINGLE_MAPPING_NAME + "\":{\"properties\":{\"body\":{\"type\":\"integer\"}}}}",
XContentType.JSON
)
.execute() .execute()
.actionGet(); .actionGet();
fail("Expected MergeMappingException"); fail("Expected MergeMappingException");
@ -225,7 +228,10 @@ public class UpdateMappingIntegrationIT extends OpenSearchIntegTestCase {
client().admin() client().admin()
.indices() .indices()
.preparePutMapping("test") .preparePutMapping("test")
.setSource("{\"type\":{\"properties\":{\"body\":{\"type\":\"text\", \"norms\": true }}}}", XContentType.JSON) .setSource(
"{\"" + MapperService.SINGLE_MAPPING_NAME + "\":{\"properties\":{\"body\":{\"type\":\"text\", \"norms\": true }}}}",
XContentType.JSON
)
.execute() .execute()
.actionGet(); .actionGet();
fail("Expected MergeMappingException"); fail("Expected MergeMappingException");
@ -242,7 +248,11 @@ public class UpdateMappingIntegrationIT extends OpenSearchIntegTestCase {
.indices() .indices()
.prepareCreate("test") .prepareCreate("test")
.setSettings(Settings.builder().put("index.number_of_shards", 2).put("index.number_of_replicas", 0)) .setSettings(Settings.builder().put("index.number_of_shards", 2).put("index.number_of_replicas", 0))
.addMapping("type", "{\"type\":{\"properties\":{\"body\":{\"type\":\"text\"}}}}", XContentType.JSON) .addMapping(
MapperService.SINGLE_MAPPING_NAME,
"{\"" + MapperService.SINGLE_MAPPING_NAME + "\":{\"properties\":{\"body\":{\"type\":\"text\"}}}}",
XContentType.JSON
)
.execute() .execute()
.actionGet(); .actionGet();
client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet(); client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();
@ -250,7 +260,7 @@ public class UpdateMappingIntegrationIT extends OpenSearchIntegTestCase {
AcknowledgedResponse putMappingResponse = client().admin() AcknowledgedResponse putMappingResponse = client().admin()
.indices() .indices()
.preparePutMapping("test") .preparePutMapping("test")
.setSource("{\"type\":{\"properties\":{\"body\":{\"type\":\"text\"}}}}", XContentType.JSON) .setSource("{\"properties\":{\"body\":{\"type\":\"text\"}}}", XContentType.JSON)
.execute() .execute()
.actionGet(); .actionGet();

View File

@ -486,7 +486,7 @@ public class SearchFieldsIT extends OpenSearchIntegTestCase {
assertThat(response.getHits().getAt(i).getId(), equalTo(Integer.toString(i))); assertThat(response.getHits().getAt(i).getId(), equalTo(Integer.toString(i)));
Set<String> fields = new HashSet<>(response.getHits().getAt(i).getFields().keySet()); Set<String> fields = new HashSet<>(response.getHits().getAt(i).getFields().keySet());
assertThat(fields, equalTo(singleton("type"))); assertThat(fields, equalTo(singleton("type")));
assertThat(response.getHits().getAt(i).getFields().get("type").getValue(), equalTo("type1")); assertThat(response.getHits().getAt(i).getFields().get("type").getValue(), equalTo(MapperService.SINGLE_MAPPING_NAME));
} }
response = client().prepareSearch() response = client().prepareSearch()
@ -504,7 +504,7 @@ public class SearchFieldsIT extends OpenSearchIntegTestCase {
assertThat(response.getHits().getAt(i).getId(), equalTo(Integer.toString(i))); assertThat(response.getHits().getAt(i).getId(), equalTo(Integer.toString(i)));
Set<String> fields = new HashSet<>(response.getHits().getAt(i).getFields().keySet()); Set<String> fields = new HashSet<>(response.getHits().getAt(i).getFields().keySet());
assertThat(fields, equalTo(newHashSet("type", "id"))); assertThat(fields, equalTo(newHashSet("type", "id")));
assertThat(response.getHits().getAt(i).getFields().get("type").getValue(), equalTo("type1")); assertThat(response.getHits().getAt(i).getFields().get("type").getValue(), equalTo(MapperService.SINGLE_MAPPING_NAME));
assertThat(response.getHits().getAt(i).getFields().get("id").getValue(), equalTo(Integer.toString(i))); assertThat(response.getHits().getAt(i).getFields().get("id").getValue(), equalTo(Integer.toString(i)));
} }
} }

View File

@ -1987,41 +1987,6 @@ public class SearchQueryIT extends OpenSearchIntegTestCase {
assertHitCount(searchResponse, 1); assertHitCount(searchResponse, 1);
} }
public void testRangeQueryTypeField_31476() throws Exception {
assertAcked(prepareCreate("test").addMapping("foo", "field", "type=keyword"));
client().prepareIndex("test").setId("1").setSource("field", "value").get();
refresh();
RangeQueryBuilder range = new RangeQueryBuilder("_type").from("ape").to("zebra");
SearchResponse searchResponse = client().prepareSearch("test").setQuery(range).get();
assertHitCount(searchResponse, 1);
range = new RangeQueryBuilder("_type").from("monkey").to("zebra");
searchResponse = client().prepareSearch("test").setQuery(range).get();
assertHitCount(searchResponse, 0);
range = new RangeQueryBuilder("_type").from("ape").to("donkey");
searchResponse = client().prepareSearch("test").setQuery(range).get();
assertHitCount(searchResponse, 0);
range = new RangeQueryBuilder("_type").from("ape").to("foo").includeUpper(false);
searchResponse = client().prepareSearch("test").setQuery(range).get();
assertHitCount(searchResponse, 0);
range = new RangeQueryBuilder("_type").from("ape").to("foo").includeUpper(true);
searchResponse = client().prepareSearch("test").setQuery(range).get();
assertHitCount(searchResponse, 1);
range = new RangeQueryBuilder("_type").from("foo").to("zebra").includeLower(false);
searchResponse = client().prepareSearch("test").setQuery(range).get();
assertHitCount(searchResponse, 0);
range = new RangeQueryBuilder("_type").from("foo").to("zebra").includeLower(true);
searchResponse = client().prepareSearch("test").setQuery(range).get();
assertHitCount(searchResponse, 1);
}
public void testNestedQueryWithFieldAlias() throws Exception { public void testNestedQueryWithFieldAlias() throws Exception {
XContentBuilder mapping = XContentFactory.jsonBuilder() XContentBuilder mapping = XContentFactory.jsonBuilder()
.startObject() .startObject()

View File

@ -41,9 +41,7 @@ import org.opensearch.cluster.metadata.IndexMetadata;
import org.opensearch.common.settings.Settings; import org.opensearch.common.settings.Settings;
import org.opensearch.index.Index; import org.opensearch.index.Index;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map;
import java.util.Set; import java.util.Set;
/** /**
@ -61,7 +59,7 @@ public class CreateIndexClusterStateUpdateRequest extends ClusterStateUpdateRequ
private Settings settings = Settings.Builder.EMPTY_SETTINGS; private Settings settings = Settings.Builder.EMPTY_SETTINGS;
private final Map<String, String> mappings = new HashMap<>(); private String mappings = "{}";
private final Set<Alias> aliases = new HashSet<>(); private final Set<Alias> aliases = new HashSet<>();
@ -80,8 +78,8 @@ public class CreateIndexClusterStateUpdateRequest extends ClusterStateUpdateRequ
return this; return this;
} }
public CreateIndexClusterStateUpdateRequest mappings(Map<String, String> mappings) { public CreateIndexClusterStateUpdateRequest mappings(String mappings) {
this.mappings.putAll(mappings); this.mappings = mappings;
return this; return this;
} }
@ -122,7 +120,7 @@ public class CreateIndexClusterStateUpdateRequest extends ClusterStateUpdateRequ
return settings; return settings;
} }
public Map<String, String> mappings() { public String mappings() {
return mappings; return mappings;
} }

View File

@ -35,6 +35,7 @@ package org.opensearch.action.admin.indices.create;
import org.opensearch.LegacyESVersion; import org.opensearch.LegacyESVersion;
import org.opensearch.OpenSearchGenerationException; import org.opensearch.OpenSearchGenerationException;
import org.opensearch.OpenSearchParseException; import org.opensearch.OpenSearchParseException;
import org.opensearch.Version;
import org.opensearch.action.ActionRequestValidationException; import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.action.IndicesRequest; import org.opensearch.action.IndicesRequest;
import org.opensearch.action.admin.indices.alias.Alias; import org.opensearch.action.admin.indices.alias.Alias;
@ -46,7 +47,6 @@ import org.opensearch.common.ParseField;
import org.opensearch.common.Strings; import org.opensearch.common.Strings;
import org.opensearch.common.bytes.BytesArray; import org.opensearch.common.bytes.BytesArray;
import org.opensearch.common.bytes.BytesReference; import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.collect.MapBuilder;
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.settings.Settings; import org.opensearch.common.settings.Settings;
@ -58,9 +58,10 @@ import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentHelper; import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.common.xcontent.XContentParser; import org.opensearch.common.xcontent.XContentParser;
import org.opensearch.common.xcontent.XContentType; import org.opensearch.common.xcontent.XContentType;
import org.opensearch.index.mapper.MapperService;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@ -92,7 +93,7 @@ public class CreateIndexRequest extends AcknowledgedRequest<CreateIndexRequest>
private Settings settings = EMPTY_SETTINGS; private Settings settings = EMPTY_SETTINGS;
private final Map<String, String> mappings = new HashMap<>(); private String mappings = "{}";
private final Set<Alias> aliases = new HashSet<>(); private final Set<Alias> aliases = new HashSet<>();
@ -103,11 +104,21 @@ public class CreateIndexRequest extends AcknowledgedRequest<CreateIndexRequest>
cause = in.readString(); cause = in.readString();
index = in.readString(); index = in.readString();
settings = readSettingsFromStream(in); settings = readSettingsFromStream(in);
int size = in.readVInt(); if (in.getVersion().before(Version.V_2_0_0)) {
for (int i = 0; i < size; i++) { int size = in.readVInt();
final String type = in.readString(); if (size == 1) {
String source = in.readString(); String type = in.readString();
mappings.put(type, source); if (MapperService.SINGLE_MAPPING_NAME.equals(type) == false) {
throw new IllegalArgumentException(
"Expected to receive mapping type of [" + MapperService.SINGLE_MAPPING_NAME + "] but got [" + type + "]"
);
}
mappings = in.readString();
} else if (size != 0) {
throw new IllegalStateException("Expected to read 0 or 1 mappings, but received " + size);
}
} else {
mappings = in.readString();
} }
int aliasesSize = in.readVInt(); int aliasesSize = in.readVInt();
for (int i = 0; i < aliasesSize; i++) { for (int i = 0; i < aliasesSize; i++) {
@ -221,6 +232,19 @@ public class CreateIndexRequest extends AcknowledgedRequest<CreateIndexRequest>
return this; return this;
} }
/**
* Set the mapping for this index
*
* The mapping should be in the form of a JSON string, with an outer _doc key
* <pre>
* .mapping("{\"_doc\":{\"properties\": ... }}")
* </pre>
*/
public CreateIndexRequest mapping(String mapping) {
this.mappings = mapping;
return this;
}
/** /**
* Adds mapping that will be added when the index gets created. * Adds mapping that will be added when the index gets created.
* *
@ -249,14 +273,6 @@ public class CreateIndexRequest extends AcknowledgedRequest<CreateIndexRequest>
return mapping(type, mappingAsMap); return mapping(type, mappingAsMap);
} }
/**
* The cause for this index creation.
*/
public CreateIndexRequest cause(String cause) {
this.cause = cause;
return this;
}
/** /**
* Adds mapping that will be added when the index gets created. * Adds mapping that will be added when the index gets created.
* *
@ -278,18 +294,17 @@ public class CreateIndexRequest extends AcknowledgedRequest<CreateIndexRequest>
*/ */
@Deprecated @Deprecated
public CreateIndexRequest mapping(String type, Map<String, ?> source) { public CreateIndexRequest mapping(String type, Map<String, ?> source) {
if (mappings.containsKey(type)) {
throw new IllegalStateException("mappings for type \"" + type + "\" were already defined");
}
// wrap it in a type map if its not // wrap it in a type map if its not
if (source.size() != 1 || !source.containsKey(type)) { if (source.size() != 1 || !source.containsKey(type)) {
source = MapBuilder.<String, Object>newMapBuilder().put(type, source).map(); source = Collections.singletonMap(MapperService.SINGLE_MAPPING_NAME, source);
} else if (MapperService.SINGLE_MAPPING_NAME.equals(type) == false) {
// if it has a different type name, then unwrap and rewrap with _doc
source = Collections.singletonMap(MapperService.SINGLE_MAPPING_NAME, source.get(type));
} }
try { try {
XContentBuilder builder = XContentFactory.jsonBuilder(); XContentBuilder builder = XContentFactory.jsonBuilder();
builder.map(source); builder.map(source);
mappings.put(type, Strings.toString(builder)); return mapping(Strings.toString(builder));
return this;
} catch (IOException e) { } catch (IOException e) {
throw new OpenSearchGenerationException("Failed to generate [" + source + "]", e); throw new OpenSearchGenerationException("Failed to generate [" + source + "]", e);
} }
@ -306,6 +321,14 @@ public class CreateIndexRequest extends AcknowledgedRequest<CreateIndexRequest>
return this; return this;
} }
/**
* The cause for this index creation.
*/
public CreateIndexRequest cause(String cause) {
this.cause = cause;
return this;
}
/** /**
* Sets the aliases that will be associated with the index when it gets created * Sets the aliases that will be associated with the index when it gets created
*/ */
@ -421,7 +444,7 @@ public class CreateIndexRequest extends AcknowledgedRequest<CreateIndexRequest>
return this; return this;
} }
public Map<String, String> mappings() { public String mappings() {
return this.mappings; return this.mappings;
} }
@ -467,10 +490,16 @@ public class CreateIndexRequest extends AcknowledgedRequest<CreateIndexRequest>
out.writeString(cause); out.writeString(cause);
out.writeString(index); out.writeString(index);
writeSettingsToStream(settings, out); writeSettingsToStream(settings, out);
out.writeVInt(mappings.size()); if (out.getVersion().before(Version.V_2_0_0)) {
for (Map.Entry<String, String> entry : mappings.entrySet()) { if ("{}".equals(mappings)) {
out.writeString(entry.getKey()); out.writeVInt(0);
out.writeString(entry.getValue()); } else {
out.writeVInt(1);
out.writeString(MapperService.SINGLE_MAPPING_NAME);
out.writeString(mappings);
}
} else {
out.writeString(mappings);
} }
out.writeVInt(aliases.size()); out.writeVInt(aliases.size());
for (Alias alias : aliases) { for (Alias alias : aliases) {

View File

@ -32,10 +32,12 @@
package org.opensearch.action.admin.indices.mapping.get; package org.opensearch.action.admin.indices.mapping.get;
import org.opensearch.Version;
import org.opensearch.action.ActionRequestValidationException; import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.action.OriginalIndices; import org.opensearch.action.OriginalIndices;
import org.opensearch.action.support.IndicesOptions; import org.opensearch.action.support.IndicesOptions;
import org.opensearch.action.support.single.shard.SingleShardRequest; import org.opensearch.action.support.single.shard.SingleShardRequest;
import org.opensearch.common.Strings;
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;
@ -43,26 +45,26 @@ import java.io.IOException;
public class GetFieldMappingsIndexRequest extends SingleShardRequest<GetFieldMappingsIndexRequest> { public class GetFieldMappingsIndexRequest extends SingleShardRequest<GetFieldMappingsIndexRequest> {
private final boolean probablySingleFieldRequest;
private final boolean includeDefaults; private final boolean includeDefaults;
private final String[] fields; private final String[] fields;
private final String[] types;
private OriginalIndices originalIndices; private final OriginalIndices originalIndices;
GetFieldMappingsIndexRequest(StreamInput in) throws IOException { GetFieldMappingsIndexRequest(StreamInput in) throws IOException {
super(in); super(in);
types = in.readStringArray(); if (in.getVersion().before(Version.V_2_0_0)) {
in.readStringArray(); // removed types array
}
fields = in.readStringArray(); fields = in.readStringArray();
includeDefaults = in.readBoolean(); includeDefaults = in.readBoolean();
probablySingleFieldRequest = in.readBoolean(); if (in.getVersion().before(Version.V_2_0_0)) {
in.readBoolean(); // removed probablySingleField boolean
}
originalIndices = OriginalIndices.readOriginalIndices(in); originalIndices = OriginalIndices.readOriginalIndices(in);
} }
GetFieldMappingsIndexRequest(GetFieldMappingsRequest other, String index, boolean probablySingleFieldRequest) { GetFieldMappingsIndexRequest(GetFieldMappingsRequest other, String index) {
this.probablySingleFieldRequest = probablySingleFieldRequest;
this.includeDefaults = other.includeDefaults(); this.includeDefaults = other.includeDefaults();
this.types = other.types();
this.fields = other.fields(); this.fields = other.fields();
assert index != null; assert index != null;
this.index(index); this.index(index);
@ -74,18 +76,10 @@ public class GetFieldMappingsIndexRequest extends SingleShardRequest<GetFieldMap
return null; return null;
} }
public String[] types() {
return types;
}
public String[] fields() { public String[] fields() {
return fields; return fields;
} }
public boolean probablySingleFieldRequest() {
return probablySingleFieldRequest;
}
public boolean includeDefaults() { public boolean includeDefaults() {
return includeDefaults; return includeDefaults;
} }
@ -103,10 +97,14 @@ public class GetFieldMappingsIndexRequest extends SingleShardRequest<GetFieldMap
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out); super.writeTo(out);
out.writeStringArray(types); if (out.getVersion().before(Version.V_2_0_0)) {
out.writeStringArray(Strings.EMPTY_ARRAY);
}
out.writeStringArray(fields); out.writeStringArray(fields);
out.writeBoolean(includeDefaults); out.writeBoolean(includeDefaults);
out.writeBoolean(probablySingleFieldRequest); if (out.getVersion().before(Version.V_2_0_0)) {
out.writeBoolean(false);
}
OriginalIndices.writeOriginalIndices(originalIndices, out); OriginalIndices.writeOriginalIndices(originalIndices, out);
} }

View File

@ -32,6 +32,7 @@
package org.opensearch.action.admin.indices.mapping.get; package org.opensearch.action.admin.indices.mapping.get;
import org.opensearch.Version;
import org.opensearch.action.ActionRequest; import org.opensearch.action.ActionRequest;
import org.opensearch.action.ActionRequestValidationException; import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.action.IndicesRequest; import org.opensearch.action.IndicesRequest;
@ -41,6 +42,7 @@ import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput; import org.opensearch.common.io.stream.StreamOutput;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
/** /**
* Request the mappings of specific fields * Request the mappings of specific fields
@ -57,7 +59,6 @@ public class GetFieldMappingsRequest extends ActionRequest implements IndicesReq
private boolean includeDefaults = false; private boolean includeDefaults = false;
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();
@ -66,7 +67,12 @@ public class GetFieldMappingsRequest extends ActionRequest implements IndicesReq
public GetFieldMappingsRequest(StreamInput in) throws IOException { public GetFieldMappingsRequest(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)) {
String[] types = in.readStringArray();
if (types != Strings.EMPTY_ARRAY) {
throw new IllegalArgumentException("Expected empty type array but received [" + Arrays.toString(types) + "]");
}
}
indicesOptions = IndicesOptions.readIndicesOptions(in); indicesOptions = IndicesOptions.readIndicesOptions(in);
local = in.readBoolean(); local = in.readBoolean();
fields = in.readStringArray(); fields = in.readStringArray();
@ -92,11 +98,6 @@ public class GetFieldMappingsRequest extends ActionRequest implements IndicesReq
return this; return this;
} }
public GetFieldMappingsRequest types(String... types) {
this.types = types;
return this;
}
public GetFieldMappingsRequest indicesOptions(IndicesOptions indicesOptions) { public GetFieldMappingsRequest indicesOptions(IndicesOptions indicesOptions) {
this.indicesOptions = indicesOptions; this.indicesOptions = indicesOptions;
return this; return this;
@ -107,10 +108,6 @@ public class GetFieldMappingsRequest extends ActionRequest implements IndicesReq
return indices; return indices;
} }
public String[] types() {
return types;
}
@Override @Override
public IndicesOptions indicesOptions() { public IndicesOptions indicesOptions() {
return indicesOptions; return indicesOptions;
@ -150,7 +147,9 @@ public class GetFieldMappingsRequest extends ActionRequest implements IndicesReq
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);
out.writeBoolean(local); out.writeBoolean(local);
out.writeStringArray(fields); out.writeStringArray(fields);

View File

@ -54,16 +54,6 @@ public class GetFieldMappingsRequestBuilder extends ActionRequestBuilder<GetFiel
return this; return this;
} }
public GetFieldMappingsRequestBuilder setTypes(String... types) {
request.types(types);
return this;
}
public GetFieldMappingsRequestBuilder addTypes(String... types) {
request.types(ArrayUtils.concat(request.types(), types));
return this;
}
public GetFieldMappingsRequestBuilder setIndicesOptions(IndicesOptions indicesOptions) { public GetFieldMappingsRequestBuilder setIndicesOptions(IndicesOptions indicesOptions) {
request.indicesOptions(indicesOptions); request.indicesOptions(indicesOptions);
return this; return this;

View File

@ -32,9 +32,9 @@
package org.opensearch.action.admin.indices.mapping.get; package org.opensearch.action.admin.indices.mapping.get;
import org.opensearch.Version;
import org.opensearch.action.ActionResponse; import org.opensearch.action.ActionResponse;
import org.opensearch.common.ParseField; import org.opensearch.common.ParseField;
import org.opensearch.common.bytes.BytesArray;
import org.opensearch.common.bytes.BytesReference; import org.opensearch.common.bytes.BytesReference;
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;
@ -47,6 +47,7 @@ import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.common.xcontent.XContentParser; import org.opensearch.common.xcontent.XContentParser;
import org.opensearch.common.xcontent.XContentType; import org.opensearch.common.xcontent.XContentType;
import org.opensearch.index.mapper.Mapper; import org.opensearch.index.mapper.Mapper;
import org.opensearch.index.mapper.MapperService;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -97,38 +98,37 @@ public class GetFieldMappingsResponse extends ActionResponse implements ToXConte
}, MAPPINGS, ObjectParser.ValueType.OBJECT); }, MAPPINGS, ObjectParser.ValueType.OBJECT);
} }
// todo remove middle `type` level private final Map<String, Map<String, FieldMappingMetadata>> mappings;
private final Map<String, Map<String, Map<String, FieldMappingMetadata>>> mappings;
GetFieldMappingsResponse(Map<String, Map<String, Map<String, FieldMappingMetadata>>> mappings) { GetFieldMappingsResponse(Map<String, Map<String, FieldMappingMetadata>> mappings) {
this.mappings = mappings; this.mappings = mappings;
} }
GetFieldMappingsResponse(StreamInput in) throws IOException { GetFieldMappingsResponse(StreamInput in) throws IOException {
super(in); super(in);
int size = in.readVInt(); int size = in.readVInt();
Map<String, Map<String, Map<String, FieldMappingMetadata>>> indexMapBuilder = new HashMap<>(size); Map<String, Map<String, FieldMappingMetadata>> indexMapBuilder = new HashMap<>(size);
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
String index = in.readString(); String index = in.readString();
int typesSize = in.readVInt(); if (in.getVersion().before(Version.V_2_0_0)) {
Map<String, Map<String, FieldMappingMetadata>> typeMapBuilder = new HashMap<>(typesSize); int typesSize = in.readVInt();
for (int j = 0; j < typesSize; j++) { if (typesSize != 1) {
String type = in.readString(); throw new IllegalStateException("Expected single type but received [" + typesSize + "]");
int fieldSize = in.readVInt();
Map<String, FieldMappingMetadata> fieldMapBuilder = new HashMap<>(fieldSize);
for (int k = 0; k < fieldSize; k++) {
fieldMapBuilder.put(in.readString(), new FieldMappingMetadata(in.readString(), in.readBytesReference()));
} }
typeMapBuilder.put(type, unmodifiableMap(fieldMapBuilder)); in.readString(); // type
} }
indexMapBuilder.put(index, unmodifiableMap(typeMapBuilder)); int fieldSize = in.readVInt();
Map<String, FieldMappingMetadata> fieldMapBuilder = new HashMap<>(fieldSize);
for (int k = 0; k < fieldSize; k++) {
fieldMapBuilder.put(in.readString(), new FieldMappingMetadata(in.readString(), in.readBytesReference()));
}
indexMapBuilder.put(index, unmodifiableMap(fieldMapBuilder));
} }
mappings = unmodifiableMap(indexMapBuilder); mappings = unmodifiableMap(indexMapBuilder);
} }
/** returns the retrieved field mapping. The return map keys are index, type, field (as specified in the request). */ /** returns the retrieved field mapping. The return map keys are index, type, field (as specified in the request). */
public Map<String, Map<String, Map<String, FieldMappingMetadata>>> mappings() { public Map<String, Map<String, FieldMappingMetadata>> mappings() {
return mappings; return mappings;
} }
@ -138,32 +138,23 @@ public class GetFieldMappingsResponse extends ActionResponse implements ToXConte
* @param field field name as specified in the {@link GetFieldMappingsRequest} * @param field field name as specified in the {@link GetFieldMappingsRequest}
* @return FieldMappingMetadata for the requested field or null if not found. * @return FieldMappingMetadata for the requested field or null if not found.
*/ */
public FieldMappingMetadata fieldMappings(String index, String type, String field) { public FieldMappingMetadata fieldMappings(String index, String field) {
Map<String, Map<String, FieldMappingMetadata>> indexMapping = mappings.get(index); Map<String, FieldMappingMetadata> indexMapping = mappings.get(index);
if (indexMapping == null) { if (indexMapping == null) {
return null; return null;
} }
Map<String, FieldMappingMetadata> typeMapping = indexMapping.get(type); return indexMapping.get(field);
if (typeMapping == null) {
return null;
}
return typeMapping.get(field);
} }
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(); builder.startObject();
for (Map.Entry<String, Map<String, Map<String, FieldMappingMetadata>>> indexEntry : mappings.entrySet()) { for (Map.Entry<String, Map<String, FieldMappingMetadata>> indexEntry : mappings.entrySet()) {
builder.startObject(indexEntry.getKey()); builder.startObject(indexEntry.getKey());
builder.startObject(MAPPINGS.getPreferredName()); builder.startObject(MAPPINGS.getPreferredName());
Map<String, FieldMappingMetadata> mappings = null;
for (Map.Entry<String, Map<String, FieldMappingMetadata>> typeEntry : indexEntry.getValue().entrySet()) {
assert mappings == null;
mappings = typeEntry.getValue();
}
if (mappings != null) { if (mappings != null) {
addFieldMappingsToBuilder(builder, params, mappings); addFieldMappingsToBuilder(builder, params, indexEntry.getValue());
} }
builder.endObject(); builder.endObject();
@ -183,7 +174,6 @@ public class GetFieldMappingsResponse extends ActionResponse implements ToXConte
} }
public static class FieldMappingMetadata implements ToXContentFragment { public static class FieldMappingMetadata implements ToXContentFragment {
public static final FieldMappingMetadata NULL = new FieldMappingMetadata("", BytesArray.EMPTY);
private static final ParseField FULL_NAME = new ParseField("full_name"); private static final ParseField FULL_NAME = new ParseField("full_name");
private static final ParseField MAPPING = new ParseField("mapping"); private static final ParseField MAPPING = new ParseField("mapping");
@ -220,10 +210,6 @@ public class GetFieldMappingsResponse extends ActionResponse implements ToXConte
return XContentHelper.convertToMap(source, true, XContentType.JSON).v2(); return XContentHelper.convertToMap(source, true, XContentType.JSON).v2();
} }
public boolean isNull() {
return NULL.fullName().equals(fullName) && NULL.source.length() == source.length();
}
// pkg-private for testing // pkg-private for testing
BytesReference getSource() { BytesReference getSource() {
return source; return source;
@ -268,18 +254,18 @@ public class GetFieldMappingsResponse extends ActionResponse implements ToXConte
@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 (Map.Entry<String, Map<String, Map<String, FieldMappingMetadata>>> indexEntry : mappings.entrySet()) { for (Map.Entry<String, Map<String, FieldMappingMetadata>> indexEntry : mappings.entrySet()) {
out.writeString(indexEntry.getKey()); out.writeString(indexEntry.getKey());
if (out.getVersion().before(Version.V_2_0_0)) {
out.writeVInt(1);
out.writeString(MapperService.SINGLE_MAPPING_NAME);
}
out.writeVInt(indexEntry.getValue().size()); out.writeVInt(indexEntry.getValue().size());
for (Map.Entry<String, Map<String, FieldMappingMetadata>> typeEntry : indexEntry.getValue().entrySet()) { for (Map.Entry<String, FieldMappingMetadata> fieldEntry : indexEntry.getValue().entrySet()) {
out.writeString(typeEntry.getKey()); out.writeString(fieldEntry.getKey());
out.writeVInt(typeEntry.getValue().size()); FieldMappingMetadata fieldMapping = fieldEntry.getValue();
for (Map.Entry<String, FieldMappingMetadata> fieldEntry : typeEntry.getValue().entrySet()) { out.writeString(fieldMapping.fullName());
out.writeString(fieldEntry.getKey()); out.writeBytesReference(fieldMapping.source);
FieldMappingMetadata fieldMapping = fieldEntry.getValue();
out.writeString(fieldMapping.fullName());
out.writeBytesReference(fieldMapping.source);
}
} }
} }
} }

View File

@ -112,13 +112,13 @@ public class GetMappingsResponse extends ActionResponse implements ToXContentFra
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
for (final ObjectObjectCursor<String, MappingMetadata> indexEntry : getMappings()) { for (final ObjectObjectCursor<String, MappingMetadata> indexEntry : getMappings()) {
builder.startObject(indexEntry.key);
if (indexEntry.value != null) { if (indexEntry.value != null) {
builder.startObject(indexEntry.key);
builder.field(MAPPINGS.getPreferredName(), indexEntry.value.sourceAsMap()); builder.field(MAPPINGS.getPreferredName(), indexEntry.value.sourceAsMap());
builder.endObject();
} else { } else {
builder.startObject(MAPPINGS.getPreferredName()).endObject(); builder.startObject(MAPPINGS.getPreferredName()).endObject();
} }
builder.endObject();
} }
return builder; return builder;
} }

View File

@ -81,9 +81,8 @@ public class TransportGetFieldMappingsAction extends HandledTransportAction<GetF
if (concreteIndices.length == 0) { if (concreteIndices.length == 0) {
listener.onResponse(new GetFieldMappingsResponse(emptyMap())); listener.onResponse(new GetFieldMappingsResponse(emptyMap()));
} else { } else {
boolean probablySingleFieldRequest = concreteIndices.length == 1 && request.types().length == 1 && request.fields().length == 1;
for (final String index : concreteIndices) { for (final String index : concreteIndices) {
GetFieldMappingsIndexRequest shardRequest = new GetFieldMappingsIndexRequest(request, index, probablySingleFieldRequest); GetFieldMappingsIndexRequest shardRequest = new GetFieldMappingsIndexRequest(request, index);
shardAction.execute(shardRequest, new ActionListener<GetFieldMappingsResponse>() { shardAction.execute(shardRequest, new ActionListener<GetFieldMappingsResponse>() {
@Override @Override
public void onResponse(GetFieldMappingsResponse result) { public void onResponse(GetFieldMappingsResponse result) {
@ -107,7 +106,7 @@ public class TransportGetFieldMappingsAction extends HandledTransportAction<GetF
} }
private GetFieldMappingsResponse merge(AtomicReferenceArray<Object> indexResponses) { private GetFieldMappingsResponse merge(AtomicReferenceArray<Object> indexResponses) {
Map<String, Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetadata>>> mergedResponses = new HashMap<>(); Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetadata>> mergedResponses = new HashMap<>();
for (int i = 0; i < indexResponses.length(); i++) { for (int i = 0; i < indexResponses.length(); i++) {
Object element = indexResponses.get(i); Object element = indexResponses.get(i);
if (element instanceof GetFieldMappingsResponse) { if (element instanceof GetFieldMappingsResponse) {

View File

@ -56,12 +56,10 @@ import org.opensearch.index.mapper.DocumentMapper;
import org.opensearch.index.mapper.Mapper; import org.opensearch.index.mapper.Mapper;
import org.opensearch.index.shard.ShardId; import org.opensearch.index.shard.ShardId;
import org.opensearch.indices.IndicesService; import org.opensearch.indices.IndicesService;
import org.opensearch.indices.TypeMissingException;
import org.opensearch.threadpool.ThreadPool; import org.opensearch.threadpool.ThreadPool;
import org.opensearch.transport.TransportService; import org.opensearch.transport.TransportService;
import java.io.IOException; import java.io.IOException;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -124,28 +122,9 @@ public class TransportGetFieldMappingsIndexAction extends TransportSingleShardAc
Predicate<String> metadataFieldPredicate = (f) -> indicesService.isMetadataField(indexCreatedVersion, f); Predicate<String> metadataFieldPredicate = (f) -> indicesService.isMetadataField(indexCreatedVersion, f);
Predicate<String> fieldPredicate = metadataFieldPredicate.or(indicesService.getFieldFilter().apply(shardId.getIndexName())); Predicate<String> fieldPredicate = metadataFieldPredicate.or(indicesService.getFieldFilter().apply(shardId.getIndexName()));
DocumentMapper mapper = indexService.mapperService().documentMapper(); DocumentMapper documentMapper = indexService.mapperService().documentMapper();
Collection<String> typeIntersection; Map<String, FieldMappingMetadata> fieldMapping = findFieldMappings(fieldPredicate, documentMapper, request);
if (request.types().length == 0) { return new GetFieldMappingsResponse(singletonMap(shardId.getIndexName(), fieldMapping));
typeIntersection = mapper == null ? Collections.emptySet() : Collections.singleton(mapper.type());
} else {
typeIntersection = mapper != null && Regex.simpleMatch(request.types(), mapper.type())
? Collections.singleton(mapper.type())
: Collections.emptySet();
if (typeIntersection.isEmpty()) {
throw new TypeMissingException(shardId.getIndex(), request.types());
}
}
Map<String, Map<String, FieldMappingMetadata>> typeMappings = new HashMap<>();
for (String type : typeIntersection) {
DocumentMapper documentMapper = indexService.mapperService().documentMapper(type);
Map<String, FieldMappingMetadata> fieldMapping = findFieldMappingsByType(fieldPredicate, documentMapper, request);
if (!fieldMapping.isEmpty()) {
typeMappings.put(type, fieldMapping);
}
}
return new GetFieldMappingsResponse(singletonMap(shardId.getIndexName(), Collections.unmodifiableMap(typeMappings)));
} }
@Override @Override
@ -195,11 +174,14 @@ public class TransportGetFieldMappingsIndexAction extends TransportSingleShardAc
} }
}; };
private static Map<String, FieldMappingMetadata> findFieldMappingsByType( private static Map<String, FieldMappingMetadata> findFieldMappings(
Predicate<String> fieldPredicate, Predicate<String> fieldPredicate,
DocumentMapper documentMapper, DocumentMapper documentMapper,
GetFieldMappingsIndexRequest request GetFieldMappingsIndexRequest request
) { ) {
if (documentMapper == null) {
return Collections.emptyMap();
}
Map<String, FieldMappingMetadata> fieldMappings = new HashMap<>(); Map<String, FieldMappingMetadata> fieldMappings = new HashMap<>();
final MappingLookup allFieldMappers = documentMapper.mappers(); final MappingLookup allFieldMappers = documentMapper.mappers();
for (String field : request.fields()) { for (String field : request.fields()) {
@ -218,8 +200,6 @@ public class TransportGetFieldMappingsIndexAction extends TransportSingleShardAc
Mapper fieldMapper = allFieldMappers.getMapper(field); Mapper fieldMapper = allFieldMappers.getMapper(field);
if (fieldMapper != null) { if (fieldMapper != null) {
addFieldMapper(fieldPredicate, field, fieldMapper, fieldMappings, request.includeDefaults()); addFieldMapper(fieldPredicate, field, fieldMapper, fieldMappings, request.includeDefaults());
} else if (request.probablySingleFieldRequest()) {
fieldMappings.put(field, FieldMappingMetadata.NULL);
} }
} }
} }

View File

@ -390,7 +390,9 @@ public class MetadataRolloverService {
if (Strings.isNullOrEmpty(newIndexName) == false) { if (Strings.isNullOrEmpty(newIndexName) == false) {
throw new IllegalArgumentException("new index name may not be specified when rolling over a data stream"); throw new IllegalArgumentException("new index name may not be specified when rolling over a data stream");
} }
if ((request.settings().equals(Settings.EMPTY) == false) || (request.aliases().size() > 0) || (request.mappings().size() > 0)) { if ((request.settings().equals(Settings.EMPTY) == false)
|| (request.aliases().size() > 0)
|| (request.mappings().equals("{}") == false)) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"aliases, mappings, and index settings may not be specified when rolling over a data stream" "aliases, mappings, and index settings may not be specified when rolling over a data stream"
); );

View File

@ -252,8 +252,8 @@ public class TransportSimulateIndexTemplateAction extends TransportMasterNodeRea
Map<String, AliasMetadata> aliasesByName = aliases.stream().collect(Collectors.toMap(AliasMetadata::getAlias, Function.identity())); Map<String, AliasMetadata> aliasesByName = aliases.stream().collect(Collectors.toMap(AliasMetadata::getAlias, Function.identity()));
// empty request mapping as the user can't specify any explicit mappings via the simulate api // empty request mapping as the user can't specify any explicit mappings via the simulate api
List<Map<String, Map<String, Object>>> mappings = MetadataCreateIndexService.collectV2Mappings( List<Map<String, Object>> mappings = MetadataCreateIndexService.collectV2Mappings(
Collections.emptyMap(), "{}",
simulatedState, simulatedState,
matchingTemplate, matchingTemplate,
xContentRegistry, xContentRegistry,
@ -264,11 +264,9 @@ public class TransportSimulateIndexTemplateAction extends TransportMasterNodeRea
indexMetadata, indexMetadata,
tempIndexService -> { tempIndexService -> {
MapperService mapperService = tempIndexService.mapperService(); MapperService mapperService = tempIndexService.mapperService();
for (Map<String, Map<String, Object>> mapping : mappings) { for (Map<String, Object> mapping : mappings) {
if (!mapping.isEmpty()) { if (mapping.isEmpty() == false) {
assert mapping.size() == 1 : mapping; mapperService.merge(MapperService.SINGLE_MAPPING_NAME, mapping, MapperService.MergeReason.INDEX_TEMPLATE);
Map.Entry<String, Map<String, Object>> entry = mapping.entrySet().iterator().next();
mapperService.merge(entry.getKey(), entry.getValue(), MapperService.MergeReason.INDEX_TEMPLATE);
} }
} }

View File

@ -51,17 +51,17 @@ import org.opensearch.common.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentFactory; import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentHelper; import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.common.xcontent.XContentParser; import org.opensearch.common.xcontent.XContentParser;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.index.mapper.MapperService;
import java.io.IOException; import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import static org.opensearch.cluster.metadata.Metadata.CONTEXT_MODE_PARAM;
public class IndexTemplateMetadata extends AbstractDiffable<IndexTemplateMetadata> { public class IndexTemplateMetadata extends AbstractDiffable<IndexTemplateMetadata> {
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(IndexTemplateMetadata.class); private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(IndexTemplateMetadata.class);
@ -161,12 +161,15 @@ public class IndexTemplateMetadata extends AbstractDiffable<IndexTemplateMetadat
return this.settings; return this.settings;
} }
public ImmutableOpenMap<String, CompressedXContent> mappings() { public CompressedXContent mappings() {
return this.mappings; if (this.mappings.isEmpty()) {
return null;
}
return this.mappings.iterator().next().value;
} }
public ImmutableOpenMap<String, CompressedXContent> getMappings() { public CompressedXContent getMappings() {
return this.mappings; return this.mappings();
} }
public ImmutableOpenMap<String, AliasMetadata> aliases() { public ImmutableOpenMap<String, AliasMetadata> aliases() {
@ -194,7 +197,7 @@ public class IndexTemplateMetadata extends AbstractDiffable<IndexTemplateMetadat
if (!settings.equals(that.settings)) return false; if (!settings.equals(that.settings)) return false;
if (!patterns.equals(that.patterns)) return false; if (!patterns.equals(that.patterns)) return false;
return Objects.equals(version, that.version); return Objects.equals(aliases, that.aliases) && Objects.equals(version, that.version);
} }
@Override @Override
@ -205,6 +208,7 @@ public class IndexTemplateMetadata extends AbstractDiffable<IndexTemplateMetadat
result = 31 * result + patterns.hashCode(); result = 31 * result + patterns.hashCode();
result = 31 * result + settings.hashCode(); result = 31 * result + settings.hashCode();
result = 31 * result + mappings.hashCode(); result = 31 * result + mappings.hashCode();
result = 31 * result + aliases.hashCode();
return result; return result;
} }
@ -248,6 +252,19 @@ public class IndexTemplateMetadata extends AbstractDiffable<IndexTemplateMetadat
out.writeOptionalVInt(version); out.writeOptionalVInt(version);
} }
@Override
public String toString() {
try {
XContentBuilder builder = JsonXContent.contentBuilder();
builder.startObject();
Builder.toXContentWithTypes(this, builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
return Strings.toString(builder);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public static class Builder { public static class Builder {
private static final Set<String> VALID_FIELDS = Sets.newHashSet( private static final Set<String> VALID_FIELDS = Sets.newHashSet(
@ -286,7 +303,7 @@ public class IndexTemplateMetadata extends AbstractDiffable<IndexTemplateMetadat
patterns(indexTemplateMetadata.patterns()); patterns(indexTemplateMetadata.patterns());
settings(indexTemplateMetadata.settings()); settings(indexTemplateMetadata.settings());
mappings = ImmutableOpenMap.builder(indexTemplateMetadata.mappings()); mappings = ImmutableOpenMap.builder(indexTemplateMetadata.mappings);
aliases = ImmutableOpenMap.builder(indexTemplateMetadata.aliases()); aliases = ImmutableOpenMap.builder(indexTemplateMetadata.aliases());
} }
@ -356,23 +373,6 @@ public class IndexTemplateMetadata extends AbstractDiffable<IndexTemplateMetadat
builder.endObject(); builder.endObject();
} }
/**
* Removes the nested type in the xContent representation of {@link IndexTemplateMetadata}.
*
* This method is useful to help bridge the gap between an the internal representation which still uses (the legacy format) a
* nested type in the mapping, and the external representation which does not use a nested type in the mapping.
*/
public static void removeType(IndexTemplateMetadata indexTemplateMetadata, XContentBuilder builder) throws IOException {
builder.startObject();
toInnerXContent(
indexTemplateMetadata,
builder,
new ToXContent.MapParams(Collections.singletonMap("reduce_mappings", "true")),
false
);
builder.endObject();
}
/** /**
* Serializes the template to xContent, making sure not to nest mappings under the * Serializes the template to xContent, making sure not to nest mappings under the
* type name. * type name.
@ -399,10 +399,6 @@ public class IndexTemplateMetadata extends AbstractDiffable<IndexTemplateMetadat
ToXContent.Params params, ToXContent.Params params,
boolean includeTypeName boolean includeTypeName
) throws IOException { ) throws IOException {
Metadata.XContentContext context = params.param(CONTEXT_MODE_PARAM) != null
? Metadata.XContentContext.valueOf(params.param(CONTEXT_MODE_PARAM))
: null;
builder.field("order", indexTemplateMetadata.order()); builder.field("order", indexTemplateMetadata.order());
if (indexTemplateMetadata.version() != null) { if (indexTemplateMetadata.version() != null) {
builder.field("version", indexTemplateMetadata.version()); builder.field("version", indexTemplateMetadata.version());
@ -413,50 +409,19 @@ public class IndexTemplateMetadata extends AbstractDiffable<IndexTemplateMetadat
indexTemplateMetadata.settings().toXContent(builder, params); indexTemplateMetadata.settings().toXContent(builder, params);
builder.endObject(); builder.endObject();
if (context == Metadata.XContentContext.API) { includeTypeName &= (params.paramAsBoolean("reduce_mappings", false) == false);
builder.startObject("mappings"); CompressedXContent m = indexTemplateMetadata.mappings();
for (ObjectObjectCursor<String, CompressedXContent> cursor1 : indexTemplateMetadata.mappings()) { if (m != null) {
Map<String, Object> mapping = XContentHelper.convertToMap(cursor1.value.uncompressed(), false).v2(); Map<String, Object> documentMapping = XContentHelper.convertToMap(m.uncompressed(), true).v2();
if (mapping.size() == 1 && mapping.containsKey(cursor1.key)) {
// the type name is the root value, reduce it
mapping = (Map<String, Object>) mapping.get(cursor1.key);
}
builder.field(cursor1.key);
builder.map(mapping);
}
builder.endObject();
} else if (params.paramAsBoolean("reduce_mappings", false)) {
// The parameter include_type_name is only ever used in the REST API, where reduce_mappings is
// always set to true. We therefore only check for include_type_name in this branch.
if (includeTypeName == false) { if (includeTypeName == false) {
Map<String, Object> documentMapping = null; documentMapping = reduceMapping(documentMapping);
for (ObjectObjectCursor<String, CompressedXContent> cursor : indexTemplateMetadata.mappings()) {
assert documentMapping == null;
Map<String, Object> mapping = XContentHelper.convertToMap(cursor.value.uncompressed(), true).v2();
documentMapping = reduceMapping(cursor.key, mapping);
}
if (documentMapping != null) {
builder.field("mappings", documentMapping);
} else {
builder.startObject("mappings").endObject();
}
} else { } else {
builder.startObject("mappings"); documentMapping = reduceEmptyMapping(documentMapping);
for (ObjectObjectCursor<String, CompressedXContent> cursor : indexTemplateMetadata.mappings()) {
Map<String, Object> mapping = XContentHelper.convertToMap(cursor.value.uncompressed(), true).v2();
mapping = reduceMapping(cursor.key, mapping);
builder.field(cursor.key);
builder.map(mapping);
}
builder.endObject();
} }
builder.field("mappings");
builder.map(documentMapping);
} else { } else {
builder.startArray("mappings"); builder.startObject("mappings").endObject();
for (ObjectObjectCursor<String, CompressedXContent> cursor : indexTemplateMetadata.mappings()) {
builder.map(XContentHelper.convertToMap(cursor.value.uncompressed(), true).v2());
}
builder.endArray();
} }
builder.startObject("aliases"); builder.startObject("aliases");
@ -467,15 +432,22 @@ public class IndexTemplateMetadata extends AbstractDiffable<IndexTemplateMetadat
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private static Map<String, Object> reduceMapping(String type, Map<String, Object> mapping) { private static Map<String, Object> reduceEmptyMapping(Map<String, Object> mapping) {
if (mapping.size() == 1 && mapping.containsKey(type)) { if (mapping.keySet().size() == 1
// the type name is the root value, reduce it && mapping.containsKey(MapperService.SINGLE_MAPPING_NAME)
return (Map<String, Object>) mapping.get(type); && ((Map<String, Object>) mapping.get(MapperService.SINGLE_MAPPING_NAME)).size() == 0) {
return (Map<String, Object>) mapping.values().iterator().next();
} else { } else {
return mapping; return mapping;
} }
} }
@SuppressWarnings("unchecked")
private static Map<String, Object> reduceMapping(Map<String, Object> mapping) {
assert mapping.keySet().size() == 1 : mapping.keySet();
return (Map<String, Object>) mapping.values().iterator().next();
}
public static IndexTemplateMetadata fromXContent(XContentParser parser, String templateName) throws IOException { public static IndexTemplateMetadata fromXContent(XContentParser parser, String templateName) throws IOException {
Builder builder = new Builder(templateName); Builder builder = new Builder(templateName);

View File

@ -32,7 +32,6 @@
package org.opensearch.cluster.metadata; package org.opensearch.cluster.metadata;
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -114,7 +113,6 @@ import java.util.function.Supplier;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import static java.util.Collections.singletonMap;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
import static org.opensearch.cluster.metadata.IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING; import static org.opensearch.cluster.metadata.IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING;
import static org.opensearch.cluster.metadata.IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING; import static org.opensearch.cluster.metadata.IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING;
@ -451,7 +449,7 @@ public class MetadataCreateIndexService {
final boolean silent, final boolean silent,
final IndexMetadata sourceMetadata, final IndexMetadata sourceMetadata,
final IndexMetadata temporaryIndexMeta, final IndexMetadata temporaryIndexMeta,
final List<Map<String, Map<String, Object>>> mappings, final List<Map<String, Object>> mappings,
final Function<IndexService, List<AliasMetadata>> aliasSupplier, final Function<IndexService, List<AliasMetadata>> aliasSupplier,
final List<String> templatesApplied, final List<String> templatesApplied,
final BiConsumer<Metadata.Builder, IndexMetadata> metadataTransformer final BiConsumer<Metadata.Builder, IndexMetadata> metadataTransformer
@ -541,20 +539,10 @@ public class MetadataCreateIndexService {
templates.stream().map(IndexTemplateMetadata::name).collect(Collectors.toList()) templates.stream().map(IndexTemplateMetadata::name).collect(Collectors.toList())
); );
final Map<String, Map<String, Object>> mappings = Collections.unmodifiableMap( final Map<String, Object> mappings = Collections.unmodifiableMap(
parseV1Mappings( parseV1Mappings(
request.mappings(), request.mappings(),
templates.stream() templates.stream().map(IndexTemplateMetadata::getMappings).collect(toList()),
.map(IndexTemplateMetadata::getMappings)
// Converts the ImmutableOpenMap into a non-terrible HashMap
.map(iom -> {
Map<String, CompressedXContent> converted = new HashMap<>(iom.size());
for (ObjectObjectCursor<String, CompressedXContent> cursor : iom) {
converted.put(cursor.key, cursor.value);
}
return converted;
})
.collect(toList()),
xContentRegistry xContentRegistry
) )
); );
@ -616,7 +604,7 @@ public class MetadataCreateIndexService {
); );
} }
final List<Map<String, Map<String, Object>>> mappings = collectV2Mappings( final List<Map<String, Object>> mappings = collectV2Mappings(
request.mappings(), request.mappings(),
currentState, currentState,
templateName, templateName,
@ -659,29 +647,31 @@ public class MetadataCreateIndexService {
); );
} }
public static List<Map<String, Map<String, Object>>> collectV2Mappings( public static List<Map<String, Object>> collectV2Mappings(
final Map<String, String> requestMappings, final String requestMappings,
final ClusterState currentState, final ClusterState currentState,
final String templateName, final String templateName,
final NamedXContentRegistry xContentRegistry, final NamedXContentRegistry xContentRegistry,
final String indexName final String indexName
) throws Exception { ) throws Exception {
List<Map<String, Map<String, Object>>> result = new ArrayList<>();
List<CompressedXContent> templateMappings = MetadataIndexTemplateService.collectMappings(currentState, templateName, indexName); List<CompressedXContent> templateMappings = MetadataIndexTemplateService.collectMappings(currentState, templateName, indexName);
return collectV2Mappings(requestMappings, templateMappings, xContentRegistry);
}
public static List<Map<String, Object>> collectV2Mappings(
final String requestMappings,
final List<CompressedXContent> templateMappings,
final NamedXContentRegistry xContentRegistry
) throws Exception {
List<Map<String, Object>> result = new ArrayList<>();
for (CompressedXContent templateMapping : templateMappings) { for (CompressedXContent templateMapping : templateMappings) {
Map<String, Object> parsedTemplateMapping = MapperService.parseMapping(xContentRegistry, templateMapping.string()); Map<String, Object> parsedTemplateMapping = MapperService.parseMapping(xContentRegistry, templateMapping.string());
result.add(singletonMap(MapperService.SINGLE_MAPPING_NAME, parsedTemplateMapping)); result.add(parsedTemplateMapping);
} }
if (requestMappings.size() > 0) { Map<String, Object> parsedRequestMappings = MapperService.parseMapping(xContentRegistry, requestMappings);
assert requestMappings.size() == 1 : "expected request metadata mappings to have 1 type but it had: " + requestMappings; result.add(parsedRequestMappings);
Map.Entry<String, String> entry = requestMappings.entrySet().iterator().next();
String type = entry.getKey();
Map<String, Object> parsedMappings = MapperService.parseMapping(xContentRegistry, entry.getValue());
result.add(singletonMap(type, parsedMappings));
}
return result; return result;
} }
@ -694,7 +684,8 @@ public class MetadataCreateIndexService {
) throws Exception { ) throws Exception {
logger.info("applying create index request using existing index [{}] metadata", sourceMetadata.getIndex().getName()); logger.info("applying create index request using existing index [{}] metadata", sourceMetadata.getIndex().getName());
if (request.mappings().size() > 0) { final Map<String, Object> mappings = MapperService.parseMapping(xContentRegistry, request.mappings());
if (mappings.isEmpty() == false) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"mappings are not allowed when creating an index from a source index, " + "all mappings are copied from the source index" "mappings are not allowed when creating an index from a source index, " + "all mappings are copied from the source index"
); );
@ -719,7 +710,7 @@ public class MetadataCreateIndexService {
silent, silent,
sourceMetadata, sourceMetadata,
tmpImd, tmpImd,
Collections.emptyList(), Collections.singletonList(mappings),
indexService -> resolveAndValidateAliases( indexService -> resolveAndValidateAliases(
request.index(), request.index(),
request.aliases(), request.aliases(),
@ -745,55 +736,28 @@ public class MetadataCreateIndexService {
* {@link IndexTemplateMetadata#order()}). This merging makes no distinction between field * {@link IndexTemplateMetadata#order()}). This merging makes no distinction between field
* definitions, as may result in an invalid field definition * definitions, as may result in an invalid field definition
*/ */
static Map<String, Map<String, Object>> parseV1Mappings( static Map<String, Object> parseV1Mappings(
Map<String, String> requestMappings, String requestMappings,
List<Map<String, CompressedXContent>> templateMappings, List<CompressedXContent> templateMappings,
NamedXContentRegistry xContentRegistry NamedXContentRegistry xContentRegistry
) throws Exception { ) throws Exception {
Map<String, Map<String, Object>> mappings = new HashMap<>(); Map<String, Object> mappings = MapperService.parseMapping(xContentRegistry, requestMappings);
for (Map.Entry<String, String> entry : requestMappings.entrySet()) {
Map<String, Object> mapping = MapperService.parseMapping(xContentRegistry, entry.getValue());
if (mapping.isEmpty()) {
// Someone provided an empty '{}' for mappings, which is okay, but to avoid
// tripping the below assertion, we can safely ignore it
continue;
}
assert mapping.size() == 1 : mapping;
assert entry.getKey().equals(mapping.keySet().iterator().next()) : entry.getKey() + " != " + mapping;
mappings.put(entry.getKey(), mapping);
}
// apply templates, merging the mappings into the request mapping if exists // apply templates, merging the mappings into the request mapping if exists
for (Map<String, CompressedXContent> tMapping : templateMappings) { for (CompressedXContent mapping : templateMappings) {
for (Map.Entry<String, CompressedXContent> cursor : tMapping.entrySet()) { if (mapping != null) {
String mappingString = cursor.getValue().string(); Map<String, Object> templateMapping = MapperService.parseMapping(xContentRegistry, mapping.string());
String type = cursor.getKey(); if (templateMapping.isEmpty()) {
if (mappings.containsKey(type)) { // Someone provided an empty '{}' for mappings, which is okay, but to avoid
XContentHelper.mergeDefaults(mappings.get(type), MapperService.parseMapping(xContentRegistry, mappingString)); // tripping the below assertion, we can safely ignore it
} else if (mappings.size() == 1 && type.equals(MapperService.SINGLE_MAPPING_NAME)) { continue;
// Typeless template with typed mapping }
Map<String, Object> templateMapping = MapperService.parseMapping(xContentRegistry, mappingString); assert templateMapping.size() == 1 : "expected exactly one mapping value, got: " + templateMapping;
assert templateMapping.size() == 1 : templateMapping; // pre-8x templates may have a wrapper type other than _doc, so we re-wrap things here
assert type.equals(templateMapping.keySet().iterator().next()) : type + " != " + templateMapping; templateMapping = Collections.singletonMap(MapperService.SINGLE_MAPPING_NAME, templateMapping.values().iterator().next());
Map.Entry<String, Map<String, Object>> mappingEntry = mappings.entrySet().iterator().next(); if (mappings.isEmpty()) {
templateMapping = singletonMap( mappings = templateMapping;
mappingEntry.getKey(), // reuse type name from the mapping
templateMapping.values().iterator().next()
); // but actual mappings from the template
XContentHelper.mergeDefaults(mappingEntry.getValue(), templateMapping);
} else if (tMapping.size() == 1 && mappings.containsKey(MapperService.SINGLE_MAPPING_NAME)) {
// Typed template with typeless mapping
Map<String, Object> templateMapping = MapperService.parseMapping(xContentRegistry, mappingString);
assert templateMapping.size() == 1 : templateMapping;
assert type.equals(templateMapping.keySet().iterator().next()) : type + " != " + templateMapping;
Map<String, Object> mapping = mappings.get(MapperService.SINGLE_MAPPING_NAME);
templateMapping = singletonMap(
MapperService.SINGLE_MAPPING_NAME, // make template mapping typeless
templateMapping.values().iterator().next()
);
XContentHelper.mergeDefaults(mapping, templateMapping);
} else { } else {
mappings.put(type, MapperService.parseMapping(xContentRegistry, mappingString)); XContentHelper.mergeDefaults(mappings, templateMapping);
} }
} }
} }
@ -1170,15 +1134,13 @@ public class MetadataCreateIndexService {
private static void updateIndexMappingsAndBuildSortOrder( private static void updateIndexMappingsAndBuildSortOrder(
IndexService indexService, IndexService indexService,
CreateIndexClusterStateUpdateRequest request, CreateIndexClusterStateUpdateRequest request,
List<Map<String, Map<String, Object>>> mappings, List<Map<String, Object>> mappings,
@Nullable IndexMetadata sourceMetadata @Nullable IndexMetadata sourceMetadata
) throws IOException { ) throws IOException {
MapperService mapperService = indexService.mapperService(); MapperService mapperService = indexService.mapperService();
for (Map<String, Map<String, Object>> mapping : mappings) { for (Map<String, Object> mapping : mappings) {
if (!mapping.isEmpty()) { if (mapping.isEmpty() == false) {
assert mapping.size() == 1 : mapping; mapperService.merge(MapperService.SINGLE_MAPPING_NAME, mapping, MergeReason.INDEX_TEMPLATE);
Map.Entry<String, Map<String, Object>> entry = mapping.entrySet().iterator().next();
mapperService.merge(entry.getKey(), entry.getValue(), MergeReason.INDEX_TEMPLATE);
} }
} }

View File

@ -932,6 +932,11 @@ public class MetadataIndexTemplateService {
templateBuilder.putAlias(aliasMetadata); templateBuilder.putAlias(aliasMetadata);
} }
IndexTemplateMetadata template = templateBuilder.build(); IndexTemplateMetadata template = templateBuilder.build();
IndexTemplateMetadata existingTemplate = currentState.metadata().templates().get(request.name);
if (template.equals(existingTemplate)) {
// The template is unchanged, therefore there is no need for a cluster state update
return currentState;
}
Metadata.Builder builder = Metadata.builder(currentState.metadata()).put(template); Metadata.Builder builder = Metadata.builder(currentState.metadata()).put(template);

View File

@ -81,7 +81,7 @@ public class DocumentMapper implements ToXContentFragment {
this.rootObjectMapper = builder.build(builderContext); this.rootObjectMapper = builder.build(builderContext);
final String type = rootObjectMapper.name(); final String type = rootObjectMapper.name();
final DocumentMapper existingMapper = mapperService.documentMapper(type); final DocumentMapper existingMapper = mapperService.documentMapper();
final Version indexCreatedVersion = mapperService.getIndexSettings().getIndexVersionCreated(); final Version indexCreatedVersion = mapperService.getIndexSettings().getIndexVersionCreated();
final Map<String, TypeParser> metadataMapperParsers = mapperService.mapperRegistry.getMetadataMapperParsers( final Map<String, TypeParser> metadataMapperParsers = mapperService.mapperRegistry.getMetadataMapperParsers(
indexCreatedVersion indexCreatedVersion

View File

@ -306,13 +306,13 @@ public class MapperService extends AbstractIndexComponent implements Closeable {
// refresh mapping can happen when the parsing/merging of the mapping from the metadata doesn't result in the same // refresh mapping can happen when the parsing/merging of the mapping from the metadata doesn't result in the same
// mapping, in this case, we send to the master to refresh its own version of the mappings (to conform with the // mapping, in this case, we send to the master to refresh its own version of the mappings (to conform with the
// merge version of it, which it does when refreshing the mappings), and warn log it. // merge version of it, which it does when refreshing the mappings), and warn log it.
if (documentMapper(mappingType).mappingSource().equals(incomingMappingSource) == false) { if (documentMapper().mappingSource().equals(incomingMappingSource) == false) {
logger.debug( logger.debug(
"[{}] parsed mapping [{}], and got different sources\noriginal:\n{}\nparsed:\n{}", "[{}] parsed mapping [{}], and got different sources\noriginal:\n{}\nparsed:\n{}",
index(), index(),
mappingType, mappingType,
incomingMappingSource, incomingMappingSource,
documentMapper(mappingType).mappingSource() documentMapper().mappingSource()
); );
requireRefresh = true; requireRefresh = true;
@ -530,16 +530,6 @@ public class MapperService extends AbstractIndexComponent implements Closeable {
return mapper; return mapper;
} }
/**
* Return the {@link DocumentMapper} for the given type.
*/
public DocumentMapper documentMapper(String type) {
if (mapper != null && type.equals(mapper.type())) {
return mapper;
}
return null;
}
/** /**
* Returns {@code true} if the given {@code mappingSource} includes a type * Returns {@code true} if the given {@code mappingSource} includes a type
* as a top-level object. * as a top-level object.
@ -574,12 +564,12 @@ public class MapperService extends AbstractIndexComponent implements Closeable {
* Returns the document mapper created, including a mapping update if the * Returns the document mapper created, including a mapping update if the
* type has been dynamically created. * type has been dynamically created.
*/ */
public DocumentMapperForType documentMapperWithAutoCreate(String type) { public DocumentMapperForType documentMapperWithAutoCreate() {
DocumentMapper mapper = documentMapper(type); DocumentMapper mapper = documentMapper();
if (mapper != null) { if (mapper != null) {
return new DocumentMapperForType(mapper, null); return new DocumentMapperForType(mapper, null);
} }
mapper = parse(type, null); mapper = parse(SINGLE_MAPPING_NAME, null);
return new DocumentMapperForType(mapper, mapper.mapping()); return new DocumentMapperForType(mapper, mapper.mapping());
} }

View File

@ -312,11 +312,11 @@ public class QueryShardContext extends QueryRewriteContext {
} }
/** /**
* Returns s {@link DocumentMapper} instance for the given type. * Returns s {@link DocumentMapper} instance.
* Delegates to {@link MapperService#documentMapper(String)} * Delegates to {@link MapperService#documentMapper()}
*/ */
public DocumentMapper documentMapper(String type) { public DocumentMapper documentMapper(String type) {
return mapperService.documentMapper(type); return mapperService.documentMapper();
} }
/** /**

View File

@ -137,7 +137,7 @@ public class TypeQueryBuilder extends AbstractQueryBuilder<TypeQueryBuilder> {
protected Query doToQuery(QueryShardContext context) throws IOException { protected Query doToQuery(QueryShardContext context) throws IOException {
deprecationLogger.deprecate("type_query", TYPES_DEPRECATION_MESSAGE); deprecationLogger.deprecate("type_query", TYPES_DEPRECATION_MESSAGE);
// LUCENE 4 UPGRADE document mapper should use bytesref as well? // LUCENE 4 UPGRADE document mapper should use bytesref as well?
DocumentMapper documentMapper = context.getMapperService().documentMapper(type); DocumentMapper documentMapper = context.getMapperService().documentMapper();
if (documentMapper == null) { if (documentMapper == null) {
// no type means no documents // no type means no documents
return new MatchNoDocsQuery(); return new MatchNoDocsQuery();

View File

@ -882,7 +882,7 @@ public class IndexShard extends AbstractIndexShardComponent implements IndicesCl
); );
} }
operation = prepareIndex( operation = prepareIndex(
docMapper(resolvedType), docMapper(),
sourceWithResolvedType, sourceWithResolvedType,
seqNo, seqNo,
opPrimaryTerm, opPrimaryTerm,
@ -1102,7 +1102,7 @@ public class IndexShard extends AbstractIndexShardComponent implements IndicesCl
// fail if index and delete operations do not use the same type. // fail if index and delete operations do not use the same type.
// TODO: clean this up when types are gone // TODO: clean this up when types are gone
try { try {
Mapping update = docMapper(type).getMapping(); Mapping update = docMapper().getMapping();
if (update != null) { if (update != null) {
return new Engine.DeleteResult(update); return new Engine.DeleteResult(update);
} }
@ -1249,7 +1249,7 @@ public class IndexShard extends AbstractIndexShardComponent implements IndicesCl
return getEngine().getSeqNoStats(replicationTracker.getGlobalCheckpoint()); return getEngine().getSeqNoStats(replicationTracker.getGlobalCheckpoint());
} }
public IndexingStats indexingStats(String... types) { public IndexingStats indexingStats() {
Engine engine = getEngineOrNull(); Engine engine = getEngineOrNull();
final boolean throttled; final boolean throttled;
final long throttleTimeInMillis; final long throttleTimeInMillis;
@ -3143,8 +3143,8 @@ public class IndexShard extends AbstractIndexShardComponent implements IndicesCl
} }
} }
private DocumentMapperForType docMapper(String type) { private DocumentMapperForType docMapper() {
return mapperService.documentMapperWithAutoCreate(mapperService.resolveDocumentType(type)); return mapperService.documentMapperWithAutoCreate();
} }
private EngineConfig newEngineConfig(LongSupplier globalCheckpointSupplier) { private EngineConfig newEngineConfig(LongSupplier globalCheckpointSupplier) {
@ -3874,7 +3874,7 @@ public class IndexShard extends AbstractIndexShardComponent implements IndicesCl
return new EngineConfig.TombstoneDocSupplier() { return new EngineConfig.TombstoneDocSupplier() {
@Override @Override
public ParsedDocument newDeleteTombstoneDoc(String type, String id) { public ParsedDocument newDeleteTombstoneDoc(String type, String id) {
return docMapper(type).getDocumentMapper().createDeleteTombstoneDoc(shardId.getIndexName(), type, id); return docMapper().getDocumentMapper().createDeleteTombstoneDoc(shardId.getIndexName(), type, id);
} }
@Override @Override

View File

@ -389,7 +389,7 @@ public class TermVectorsService {
String routing String routing
) { ) {
MapperService mapperService = indexShard.mapperService(); MapperService mapperService = indexShard.mapperService();
DocumentMapperForType docMapper = mapperService.documentMapperWithAutoCreate(MapperService.SINGLE_MAPPING_NAME); DocumentMapperForType docMapper = mapperService.documentMapperWithAutoCreate();
ParsedDocument parsedDocument = docMapper.getDocumentMapper() ParsedDocument parsedDocument = docMapper.getDocumentMapper()
.parse(new SourceToParse(index, MapperService.SINGLE_MAPPING_NAME, "_id_for_tv_api", doc, xContentType, routing)); .parse(new SourceToParse(index, MapperService.SINGLE_MAPPING_NAME, "_id_for_tv_api", doc, xContentType, routing));
if (docMapper.getMapping() != null) { if (docMapper.getMapping() != null) {

View File

@ -94,12 +94,7 @@ public class RestGetFieldMappingAction extends BaseRestHandler {
.getFieldMappings(getMappingsRequest, new RestBuilderListener<GetFieldMappingsResponse>(channel) { .getFieldMappings(getMappingsRequest, new RestBuilderListener<GetFieldMappingsResponse>(channel) {
@Override @Override
public RestResponse buildResponse(GetFieldMappingsResponse response, XContentBuilder builder) throws Exception { public RestResponse buildResponse(GetFieldMappingsResponse response, XContentBuilder builder) throws Exception {
Map<String, Map<String, Map<String, FieldMappingMetadata>>> mappingsByIndex = response.mappings(); Map<String, Map<String, FieldMappingMetadata>> mappingsByIndex = response.mappings();
boolean isPossibleSingleFieldRequest = indices.length == 1 && fields.length == 1;
if (isPossibleSingleFieldRequest && isFieldMappingMissingField(mappingsByIndex)) {
return new BytesRestResponse(OK, builder.startObject().endObject());
}
RestStatus status = OK; RestStatus status = OK;
if (mappingsByIndex.isEmpty() && fields.length > 0) { if (mappingsByIndex.isEmpty() && fields.length > 0) {
@ -111,24 +106,4 @@ public class RestGetFieldMappingAction extends BaseRestHandler {
}); });
} }
/**
* Helper method to find out if the only included fieldmapping metadata is typed NULL, which means
* that type and index exist, but the field did not
*/
private boolean isFieldMappingMissingField(Map<String, Map<String, Map<String, FieldMappingMetadata>>> mappingsByIndex) {
if (mappingsByIndex.size() != 1) {
return false;
}
for (Map<String, Map<String, FieldMappingMetadata>> value : mappingsByIndex.values()) {
for (Map<String, FieldMappingMetadata> fieldValue : value.values()) {
for (Map.Entry<String, FieldMappingMetadata> fieldMappingMetadataEntry : fieldValue.entrySet()) {
if (fieldMappingMetadataEntry.getValue().isNull()) {
return true;
}
}
}
}
return false;
}
} }

View File

@ -66,7 +66,7 @@ public class CreateIndexRequestTests extends OpenSearchTestCase {
try (StreamInput in = output.bytes().streamInput()) { try (StreamInput in = output.bytes().streamInput()) {
CreateIndexRequest serialized = new CreateIndexRequest(in); CreateIndexRequest serialized = new CreateIndexRequest(in);
assertEquals(request.index(), serialized.index()); assertEquals(request.index(), serialized.index());
assertEquals(mapping, serialized.mappings().get("my_type")); assertEquals("{\"_doc\":{}}", serialized.mappings());
} }
} }
} }

View File

@ -33,6 +33,7 @@
package org.opensearch.action.admin.indices.mapping.get; package org.opensearch.action.admin.indices.mapping.get;
import org.opensearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetadata; import org.opensearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetadata;
import org.opensearch.common.Strings;
import org.opensearch.common.bytes.BytesArray; import org.opensearch.common.bytes.BytesArray;
import org.opensearch.common.io.stream.BytesStreamOutput; import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.io.stream.StreamInput; import org.opensearch.common.io.stream.StreamInput;
@ -47,22 +48,29 @@ import java.util.Map;
public class GetFieldMappingsResponseTests extends AbstractWireSerializingTestCase<GetFieldMappingsResponse> { public class GetFieldMappingsResponseTests extends AbstractWireSerializingTestCase<GetFieldMappingsResponse> {
public void testManualSerialization() throws IOException { public void testManualSerialization() throws IOException {
Map<String, Map<String, Map<String, FieldMappingMetadata>>> mappings = new HashMap<>(); Map<String, Map<String, FieldMappingMetadata>> mappings = new HashMap<>();
FieldMappingMetadata fieldMappingMetadata = new FieldMappingMetadata("my field", new BytesArray("{}")); FieldMappingMetadata fieldMappingMetadata = new FieldMappingMetadata("my field", new BytesArray("{}"));
mappings.put("index", Collections.singletonMap("type", Collections.singletonMap("field", fieldMappingMetadata))); mappings.put("index", Collections.singletonMap("field", fieldMappingMetadata));
GetFieldMappingsResponse response = new GetFieldMappingsResponse(mappings); GetFieldMappingsResponse response = new GetFieldMappingsResponse(mappings);
try (BytesStreamOutput out = new BytesStreamOutput()) { try (BytesStreamOutput out = new BytesStreamOutput()) {
response.writeTo(out); response.writeTo(out);
try (StreamInput in = StreamInput.wrap(out.bytes().toBytesRef().bytes)) { try (StreamInput in = StreamInput.wrap(out.bytes().toBytesRef().bytes)) {
GetFieldMappingsResponse serialized = new GetFieldMappingsResponse(in); GetFieldMappingsResponse serialized = new GetFieldMappingsResponse(in);
FieldMappingMetadata metadata = serialized.fieldMappings("index", "type", "field"); FieldMappingMetadata metadata = serialized.fieldMappings("index", "field");
assertNotNull(metadata); assertNotNull(metadata);
assertEquals(new BytesArray("{}"), metadata.getSource()); assertEquals(new BytesArray("{}"), metadata.getSource());
} }
} }
} }
public void testNullFieldMappingToXContent() {
Map<String, Map<String, FieldMappingMetadata>> mappings = new HashMap<>();
mappings.put("index", Collections.emptyMap());
GetFieldMappingsResponse response = new GetFieldMappingsResponse(mappings);
assertEquals("{\"index\":{\"mappings\":{}}}", Strings.toString(response));
}
@Override @Override
protected GetFieldMappingsResponse createTestInstance() { protected GetFieldMappingsResponse createTestInstance() {
return new GetFieldMappingsResponse(randomMapping()); return new GetFieldMappingsResponse(randomMapping());
@ -73,24 +81,18 @@ public class GetFieldMappingsResponseTests extends AbstractWireSerializingTestCa
return GetFieldMappingsResponse::new; return GetFieldMappingsResponse::new;
} }
private Map<String, Map<String, Map<String, FieldMappingMetadata>>> randomMapping() { private Map<String, Map<String, FieldMappingMetadata>> randomMapping() {
Map<String, Map<String, Map<String, FieldMappingMetadata>>> mappings = new HashMap<>(); Map<String, Map<String, FieldMappingMetadata>> mappings = new HashMap<>();
int indices = randomInt(10); int indices = randomInt(10);
for (int i = 0; i < indices; i++) { for (int i = 0; i < indices; i++) {
final Map<String, Map<String, FieldMappingMetadata>> doctypesMappings = new HashMap<>(); Map<String, FieldMappingMetadata> fieldMappings = new HashMap<>();
int doctypes = randomInt(10); int fields = randomInt(10);
for (int j = 0; j < doctypes; j++) { for (int k = 0; k < fields; k++) {
Map<String, FieldMappingMetadata> fieldMappings = new HashMap<>(); final String mapping = randomBoolean() ? "{\"type\":\"string\"}" : "{\"type\":\"keyword\"}";
int fields = randomInt(10); FieldMappingMetadata metaData = new FieldMappingMetadata("my field", new BytesArray(mapping));
for (int k = 0; k < fields; k++) { fieldMappings.put("field" + k, metaData);
final String mapping = randomBoolean() ? "{\"type\":\"string\"}" : "{\"type\":\"keyword\"}";
FieldMappingMetadata metadata = new FieldMappingMetadata("my field", new BytesArray(mapping));
fieldMappings.put("field" + k, metadata);
}
doctypesMappings.put("doctype" + j, fieldMappings);
} }
mappings.put("index" + i, doctypesMappings);
} }
return mappings; return mappings;
} }

View File

@ -62,6 +62,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
public class RolloverRequestTests extends OpenSearchTestCase { public class RolloverRequestTests extends OpenSearchTestCase {
@ -122,7 +123,7 @@ public class RolloverRequestTests extends OpenSearchTestCase {
request.fromXContent(createParser(builder)); request.fromXContent(createParser(builder));
Map<String, Condition<?>> conditions = request.getConditions(); Map<String, Condition<?>> conditions = request.getConditions();
assertThat(conditions.size(), equalTo(2)); assertThat(conditions.size(), equalTo(2));
assertThat(request.getCreateIndexRequest().mappings().size(), equalTo(1)); assertThat(request.getCreateIndexRequest().mappings(), containsString("not_analyzed"));
assertThat(request.getCreateIndexRequest().aliases().size(), equalTo(1)); assertThat(request.getCreateIndexRequest().aliases().size(), equalTo(1));
assertThat(request.getCreateIndexRequest().settings().getAsInt("number_of_shards", 0), equalTo(10)); assertThat(request.getCreateIndexRequest().settings().getAsInt("number_of_shards", 0), equalTo(10));
} }
@ -143,7 +144,7 @@ public class RolloverRequestTests extends OpenSearchTestCase {
request.fromXContent(createParser(builder)); request.fromXContent(createParser(builder));
CreateIndexRequest createIndexRequest = request.getCreateIndexRequest(); CreateIndexRequest createIndexRequest = request.getCreateIndexRequest();
String mapping = createIndexRequest.mappings().get(MapperService.SINGLE_MAPPING_NAME); String mapping = createIndexRequest.mappings();
assertNotNull(mapping); assertNotNull(mapping);
Map<String, Object> parsedMapping = XContentHelper.convertToMap(new BytesArray(mapping), false, XContentType.JSON).v2(); Map<String, Object> parsedMapping = XContentHelper.convertToMap(new BytesArray(mapping), false, XContentType.JSON).v2();

View File

@ -221,9 +221,7 @@ public class ClusterStateTests extends OpenSearchTestCase {
+ " }\n" + " }\n"
+ " },\n" + " },\n"
+ " \"mappings\" : {\n" + " \"mappings\" : {\n"
+ " \"type\" : {\n" + " \"key1\" : { }\n"
+ " \"key1\" : { }\n"
+ " }\n"
+ " },\n" + " },\n"
+ " \"aliases\" : { }\n" + " \"aliases\" : { }\n"
+ " }\n" + " }\n"
@ -424,9 +422,7 @@ public class ClusterStateTests extends OpenSearchTestCase {
+ "\"\n" + "\"\n"
+ " },\n" + " },\n"
+ " \"mappings\" : {\n" + " \"mappings\" : {\n"
+ " \"type\" : {\n" + " \"key1\" : { }\n"
+ " \"key1\" : { }\n"
+ " }\n"
+ " },\n" + " },\n"
+ " \"aliases\" : { }\n" + " \"aliases\" : { }\n"
+ " }\n" + " }\n"
@ -627,11 +623,7 @@ public class ClusterStateTests extends OpenSearchTestCase {
+ " }\n" + " }\n"
+ " }\n" + " }\n"
+ " },\n" + " },\n"
+ " \"mappings\" : {\n" + " \"mappings\" : { },\n"
+ " \"type\" : {\n"
+ " \"key1\" : { }\n"
+ " }\n"
+ " },\n"
+ " \"aliases\" : { }\n" + " \"aliases\" : { }\n"
+ " }\n" + " }\n"
+ " },\n" + " },\n"

View File

@ -49,14 +49,12 @@ import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import static java.util.Collections.singletonMap;
import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.contains;
public class IndexTemplateMetadataTests extends OpenSearchTestCase { public class IndexTemplateMetadataTests extends OpenSearchTestCase {
public void testIndexTemplateMetadataXContentRoundTrip() throws Exception { public void testIndexTemplateMetadataXContentRoundTrip() throws Exception {
ToXContent.Params params = new ToXContent.MapParams(singletonMap("reduce_mappings", "true"));
String template = "{\"index_patterns\" : [ \".test-*\" ],\"order\" : 1000," String template = "{\"index_patterns\" : [ \".test-*\" ],\"order\" : 1000,"
+ "\"settings\" : {\"number_of_shards\" : 1,\"number_of_replicas\" : 0}," + "\"settings\" : {\"number_of_shards\" : 1,\"number_of_replicas\" : 0},"
@ -84,7 +82,7 @@ public class IndexTemplateMetadataTests extends OpenSearchTestCase {
final BytesReference templateBytesRoundTrip; final BytesReference templateBytesRoundTrip;
try (XContentBuilder builder = XContentBuilder.builder(JsonXContent.jsonXContent)) { try (XContentBuilder builder = XContentBuilder.builder(JsonXContent.jsonXContent)) {
builder.startObject(); builder.startObject();
IndexTemplateMetadata.Builder.toXContentWithTypes(indexTemplateMetadata, builder, params); IndexTemplateMetadata.Builder.toXContentWithTypes(indexTemplateMetadata, builder, ToXContent.EMPTY_PARAMS);
builder.endObject(); builder.endObject();
templateBytesRoundTrip = BytesReference.bytes(builder); templateBytesRoundTrip = BytesReference.bytes(builder);
} }

View File

@ -109,7 +109,6 @@ import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap; import static java.util.Collections.emptyMap;
import static java.util.Collections.singleton; import static java.util.Collections.singleton;
import static java.util.Collections.singletonList; import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import static org.opensearch.index.IndexSettings.INDEX_SOFT_DELETES_SETTING; import static org.opensearch.index.IndexSettings.INDEX_SOFT_DELETES_SETTING;
import static org.hamcrest.Matchers.endsWith; import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
@ -711,20 +710,18 @@ public class MetadataCreateIndexServiceTests extends OpenSearchTestCase {
templateBuilder.putAlias(AliasMetadata.builder("alias1")); templateBuilder.putAlias(AliasMetadata.builder("alias1"));
templateBuilder.putMapping("type", createMapping("mapping_from_template", "text")); templateBuilder.putMapping("type", createMapping("mapping_from_template", "text"));
}); });
request.mappings(singletonMap("type", createMapping("mapping_from_request", "text").string())); request.mappings(createMapping("mapping_from_request", "text").string());
Map<String, Map<String, Object>> parsedMappings = MetadataCreateIndexService.parseV1Mappings( Map<String, Object> parsedMappings = MetadataCreateIndexService.parseV1Mappings(
request.mappings(), request.mappings(),
Collections.singletonList(convertMappings(templateMetadata.getMappings())), Collections.singletonList(templateMetadata.getMappings()),
NamedXContentRegistry.EMPTY NamedXContentRegistry.EMPTY
); );
assertThat(parsedMappings, hasKey("type")); assertThat(parsedMappings, hasKey(MapperService.SINGLE_MAPPING_NAME));
Map<String, Object> mappingType = parsedMappings.get("type"); Map<String, Object> doc = (Map<String, Object>) parsedMappings.get(MapperService.SINGLE_MAPPING_NAME);
assertThat(mappingType, hasKey("type")); assertThat(doc, hasKey("properties"));
Map<String, Object> type = (Map<String, Object>) mappingType.get("type"); Map<String, Object> mappingsProperties = (Map<String, Object>) doc.get("properties");
assertThat(type, hasKey("properties"));
Map<String, Object> mappingsProperties = (Map<String, Object>) type.get("properties");
assertThat(mappingsProperties, hasKey("mapping_from_request")); assertThat(mappingsProperties, hasKey("mapping_from_request"));
assertThat(mappingsProperties, hasKey("mapping_from_template")); assertThat(mappingsProperties, hasKey("mapping_from_template"));
} }
@ -781,17 +778,17 @@ public class MetadataCreateIndexServiceTests extends OpenSearchTestCase {
IndexTemplateMetadata templateMetadata = addMatchingTemplate( IndexTemplateMetadata templateMetadata = addMatchingTemplate(
builder -> builder.putAlias(AliasMetadata.builder("alias").searchRouting("fromTemplate").build()) builder -> builder.putAlias(AliasMetadata.builder("alias").searchRouting("fromTemplate").build())
.putMapping("type", templateMapping) .putMapping("_doc", templateMapping)
.settings(Settings.builder().put("key1", "templateValue")) .settings(Settings.builder().put("key1", "templateValue"))
); );
request.mappings(singletonMap("type", reqMapping.string())); request.mappings(reqMapping.string());
request.aliases(singleton(new Alias("alias").searchRouting("fromRequest"))); request.aliases(Collections.singleton(new Alias("alias").searchRouting("fromRequest")));
request.settings(Settings.builder().put("key1", "requestValue").build()); request.settings(Settings.builder().put("key1", "requestValue").build());
Map<String, Map<String, Object>> parsedMappings = MetadataCreateIndexService.parseV1Mappings( Map<String, Object> parsedMappings = MetadataCreateIndexService.parseV1Mappings(
request.mappings(), request.mappings(),
Collections.singletonList(convertMappings(templateMetadata.mappings())), Collections.singletonList(templateMetadata.mappings()),
xContentRegistry() xContentRegistry()
); );
List<AliasMetadata> resolvedAliases = resolveAndValidateAliases( List<AliasMetadata> resolvedAliases = resolveAndValidateAliases(
@ -816,12 +813,10 @@ public class MetadataCreateIndexServiceTests extends OpenSearchTestCase {
assertThat(resolvedAliases.get(0).getSearchRouting(), equalTo("fromRequest")); assertThat(resolvedAliases.get(0).getSearchRouting(), equalTo("fromRequest"));
assertThat(aggregatedIndexSettings.get("key1"), equalTo("requestValue")); assertThat(aggregatedIndexSettings.get("key1"), equalTo("requestValue"));
assertThat(parsedMappings, hasKey("type")); assertThat(parsedMappings, hasKey("_doc"));
Map<String, Object> mappingType = parsedMappings.get("type"); Map<String, Object> doc = (Map<String, Object>) parsedMappings.get("_doc");
assertThat(mappingType, hasKey("type")); assertThat(doc, hasKey("properties"));
Map<String, Object> type = (Map<String, Object>) mappingType.get("type"); Map<String, Object> mappingsProperties = (Map<String, Object>) doc.get("properties");
assertThat(type, hasKey("properties"));
Map<String, Object> mappingsProperties = (Map<String, Object>) type.get("properties");
assertThat(mappingsProperties, hasKey("test")); assertThat(mappingsProperties, hasKey("test"));
assertThat((Map<String, Object>) mappingsProperties.get("test"), hasValue("keyword")); assertThat((Map<String, Object>) mappingsProperties.get("test"), hasValue("keyword"));
} }
@ -1046,9 +1041,9 @@ public class MetadataCreateIndexServiceTests extends OpenSearchTestCase {
} }
}); });
Map<String, Map<String, Object>> mappings = parseV1Mappings( Map<String, Object> mappings = parseV1Mappings(
singletonMap(MapperService.SINGLE_MAPPING_NAME, "{\"_doc\":{}}"), "{\"" + MapperService.SINGLE_MAPPING_NAME + "\":{}}",
Collections.singletonList(convertMappings(templateMetadata.mappings())), Collections.singletonList(templateMetadata.mappings()),
xContentRegistry() xContentRegistry()
); );
assertThat(mappings, Matchers.hasKey(MapperService.SINGLE_MAPPING_NAME)); assertThat(mappings, Matchers.hasKey(MapperService.SINGLE_MAPPING_NAME));
@ -1062,12 +1057,8 @@ public class MetadataCreateIndexServiceTests extends OpenSearchTestCase {
ExceptionsHelper.reThrowIfNotNull(e); ExceptionsHelper.reThrowIfNotNull(e);
} }
}); });
Map<String, Map<String, Object>> mappings = parseV1Mappings( Map<String, Object> mappings = parseV1Mappings("", Collections.singletonList(templateMetadata.mappings()), xContentRegistry());
emptyMap(), assertThat(mappings, Matchers.hasKey(MapperService.SINGLE_MAPPING_NAME));
Collections.singletonList(convertMappings(templateMetadata.mappings())),
xContentRegistry()
);
assertThat(mappings, Matchers.hasKey("type"));
} }
public void testParseMappingsWithTypelessTemplate() throws Exception { public void testParseMappingsWithTypelessTemplate() throws Exception {
@ -1078,11 +1069,7 @@ public class MetadataCreateIndexServiceTests extends OpenSearchTestCase {
ExceptionsHelper.reThrowIfNotNull(e); ExceptionsHelper.reThrowIfNotNull(e);
} }
}); });
Map<String, Map<String, Object>> mappings = parseV1Mappings( Map<String, Object> mappings = parseV1Mappings("", Collections.singletonList(templateMetadata.mappings()), xContentRegistry());
emptyMap(),
Collections.singletonList(convertMappings(templateMetadata.mappings())),
xContentRegistry()
);
assertThat(mappings, Matchers.hasKey(MapperService.SINGLE_MAPPING_NAME)); assertThat(mappings, Matchers.hasKey(MapperService.SINGLE_MAPPING_NAME));
} }
@ -1253,7 +1240,7 @@ public class MetadataCreateIndexServiceTests extends OpenSearchTestCase {
final String mapping = Strings.toString( final String mapping = Strings.toString(
XContentFactory.jsonBuilder() XContentFactory.jsonBuilder()
.startObject() .startObject()
.startObject("type") .startObject(MapperService.SINGLE_MAPPING_NAME)
.startObject("properties") .startObject("properties")
.startObject(fieldName) .startObject(fieldName)
.field("type", fieldType) .field("type", fieldType)

View File

@ -970,7 +970,6 @@ public class MetadataIndexTemplateServiceTests extends OpenSearchSingleNodeTestC
} }
} }
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/pull/57393")
public void testResolveConflictingMappings() throws Exception { public void testResolveConflictingMappings() throws Exception {
final MetadataIndexTemplateService service = getMetadataIndexTemplateService(); final MetadataIndexTemplateService service = getMetadataIndexTemplateService();
ClusterState state = ClusterState.EMPTY_STATE; ClusterState state = ClusterState.EMPTY_STATE;
@ -2066,6 +2065,27 @@ public class MetadataIndexTemplateServiceTests extends OpenSearchSingleNodeTestC
service.addIndexTemplateV2(stateWithDSAndTemplate, false, "logs", nonDSTemplate); service.addIndexTemplateV2(stateWithDSAndTemplate, false, "logs", nonDSTemplate);
} }
public void testLegacyNoopUpdate() {
ClusterState state = ClusterState.EMPTY_STATE;
PutRequest pr = new PutRequest("api", "id");
pr.patterns(Arrays.asList("foo", "bar"));
if (randomBoolean()) {
pr.settings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 3).build());
}
if (randomBoolean()) {
pr.mappings(Collections.emptyMap());
}
if (randomBoolean()) {
pr.aliases(Collections.singleton(new Alias("alias")));
}
pr.order(randomIntBetween(0, 10));
state = MetadataIndexTemplateService.innerPutTemplate(state, pr, new IndexTemplateMetadata.Builder("id"));
assertNotNull(state.metadata().templates().get("id"));
assertThat(MetadataIndexTemplateService.innerPutTemplate(state, pr, new IndexTemplateMetadata.Builder("id")), equalTo(state));
}
private static List<Throwable> putTemplate(NamedXContentRegistry xContentRegistry, PutRequest request) { private static List<Throwable> putTemplate(NamedXContentRegistry xContentRegistry, PutRequest request) {
MetadataCreateIndexService createIndexService = new MetadataCreateIndexService( MetadataCreateIndexService createIndexService = new MetadataCreateIndexService(
Settings.EMPTY, Settings.EMPTY,

View File

@ -39,6 +39,7 @@ import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.compress.CompressedXContent; import org.opensearch.common.compress.CompressedXContent;
import org.opensearch.index.Index; import org.opensearch.index.Index;
import org.opensearch.index.IndexService; import org.opensearch.index.IndexService;
import org.opensearch.index.mapper.MapperService;
import org.opensearch.plugins.Plugin; import org.opensearch.plugins.Plugin;
import org.opensearch.test.OpenSearchSingleNodeTestCase; import org.opensearch.test.OpenSearchSingleNodeTestCase;
import org.opensearch.test.InternalSettingsPlugin; import org.opensearch.test.InternalSettingsPlugin;
@ -57,8 +58,11 @@ public class MetadataMappingServiceTests extends OpenSearchSingleNodeTestCase {
} }
public void testMappingClusterStateUpdateDoesntChangeExistingIndices() throws Exception { public void testMappingClusterStateUpdateDoesntChangeExistingIndices() throws Exception {
final IndexService indexService = createIndex("test", client().admin().indices().prepareCreate("test").addMapping("type")); final IndexService indexService = createIndex(
final CompressedXContent currentMapping = indexService.mapperService().documentMapper("type").mappingSource(); "test",
client().admin().indices().prepareCreate("test").addMapping(MapperService.SINGLE_MAPPING_NAME)
);
final CompressedXContent currentMapping = indexService.mapperService().documentMapper().mappingSource();
final MetadataMappingService mappingService = getInstanceFromNode(MetadataMappingService.class); final MetadataMappingService mappingService = getInstanceFromNode(MetadataMappingService.class);
final ClusterService clusterService = getInstanceFromNode(ClusterService.class); final ClusterService clusterService = getInstanceFromNode(ClusterService.class);
@ -74,11 +78,11 @@ public class MetadataMappingServiceTests extends OpenSearchSingleNodeTestCase {
assertTrue(result.executionResults.values().iterator().next().isSuccess()); assertTrue(result.executionResults.values().iterator().next().isSuccess());
// the task really was a mapping update // the task really was a mapping update
assertThat( assertThat(
indexService.mapperService().documentMapper("type").mappingSource(), indexService.mapperService().documentMapper().mappingSource(),
not(equalTo(result.resultingState.metadata().index("test").getMappings().get("type").source())) not(equalTo(result.resultingState.metadata().index("test").getMappings().get(MapperService.SINGLE_MAPPING_NAME).source()))
); );
// since we never committed the cluster state update, the in-memory state is unchanged // since we never committed the cluster state update, the in-memory state is unchanged
assertThat(indexService.mapperService().documentMapper("type").mappingSource(), equalTo(currentMapping)); assertThat(indexService.mapperService().documentMapper().mappingSource(), equalTo(currentMapping));
} }
public void testClusterStateIsNotChangedWithIdenticalMappings() throws Exception { public void testClusterStateIsNotChangedWithIdenticalMappings() throws Exception {

View File

@ -264,11 +264,9 @@ public class ToAndFromJsonMetadataTests extends OpenSearchTestCase {
+ Version.CURRENT.id + Version.CURRENT.id
+ "\"\n" + "\"\n"
+ " },\n" + " },\n"
+ " \"mappings\" : [\n" + " \"mappings\" : {\n"
+ " {\n" + " \"key1\" : { }\n"
+ " \"key1\" : { }\n" + " },\n"
+ " }\n"
+ " ],\n"
+ " \"aliases\" : { }\n" + " \"aliases\" : { }\n"
+ " }\n" + " }\n"
+ " },\n" + " },\n"
@ -434,11 +432,7 @@ public class ToAndFromJsonMetadataTests extends OpenSearchTestCase {
+ " }\n" + " }\n"
+ " }\n" + " }\n"
+ " },\n" + " },\n"
+ " \"mappings\" : {\n" + " \"mappings\" : { },\n"
+ " \"type\" : {\n"
+ " \"key1\" : { }\n"
+ " }\n"
+ " },\n"
+ " \"aliases\" : { }\n" + " \"aliases\" : { }\n"
+ " }\n" + " }\n"
+ " },\n" + " },\n"
@ -500,9 +494,7 @@ public class ToAndFromJsonMetadataTests extends OpenSearchTestCase {
+ "\"\n" + "\"\n"
+ " },\n" + " },\n"
+ " \"mappings\" : {\n" + " \"mappings\" : {\n"
+ " \"type\" : {\n" + " \"key1\" : { }\n"
+ " \"key1\" : { }\n"
+ " }\n"
+ " },\n" + " },\n"
+ " \"aliases\" : { }\n" + " \"aliases\" : { }\n"
+ " }\n" + " }\n"
@ -610,11 +602,7 @@ public class ToAndFromJsonMetadataTests extends OpenSearchTestCase {
+ " }\n" + " }\n"
+ " }\n" + " }\n"
+ " },\n" + " },\n"
+ " \"mappings\" : {\n" + " \"mappings\" : { },\n"
+ " \"type\" : {\n"
+ " \"key1\" : { }\n"
+ " }\n"
+ " },\n"
+ " \"aliases\" : { }\n" + " \"aliases\" : { }\n"
+ " }\n" + " }\n"
+ " },\n" + " },\n"

View File

@ -57,7 +57,7 @@ public class AllFieldMapperTests extends OpenSearchSingleNodeTestCase {
); );
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc").endObject().endObject()); String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc").endObject().endObject());
indexService.mapperService().merge("_doc", new CompressedXContent(mapping), MergeReason.MAPPING_UPDATE); indexService.mapperService().merge("_doc", new CompressedXContent(mapping), MergeReason.MAPPING_UPDATE);
assertEquals(mapping, indexService.mapperService().documentMapper("_doc").mapping().toString()); assertEquals(mapping, indexService.mapperService().documentMapper().mapping().toString());
} }
} }

View File

@ -363,7 +363,7 @@ public class DocumentParserTests extends MapperServiceTestCase {
ParseContext context = new ParseContext.InternalParseContext( ParseContext context = new ParseContext.InternalParseContext(
settings, settings,
mapperService.documentMapperParser(), mapperService.documentMapperParser(),
mapperService.documentMapper("type"), mapperService.documentMapper(),
null, null,
null null
); );

View File

@ -93,7 +93,7 @@ public class FieldFilterMapperPluginTests extends OpenSearchSingleNodeTestCase {
public void testGetFieldMappings() { public void testGetFieldMappings() {
GetFieldMappingsResponse getFieldMappingsResponse = client().admin().indices().prepareGetFieldMappings().setFields("*").get(); GetFieldMappingsResponse getFieldMappingsResponse = client().admin().indices().prepareGetFieldMappings().setFields("*").get();
Map<String, Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetadata>>> mappings = getFieldMappingsResponse.mappings(); Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetadata>> mappings = getFieldMappingsResponse.mappings();
assertEquals(2, mappings.size()); assertEquals(2, mappings.size());
assertFieldMappings(mappings.get("index1"), ALL_FLAT_FIELDS); assertFieldMappings(mappings.get("index1"), ALL_FLAT_FIELDS);
assertFieldMappings(mappings.get("filtered"), FILTERED_FLAT_FIELDS); assertFieldMappings(mappings.get("filtered"), FILTERED_FLAT_FIELDS);
@ -107,6 +107,14 @@ public class FieldFilterMapperPluginTests extends OpenSearchSingleNodeTestCase {
assertFieldMappings(response.mappings().get("test"), FILTERED_FLAT_FIELDS); assertFieldMappings(response.mappings().get("test"), FILTERED_FLAT_FIELDS);
} }
public void testGetNonExistentFieldMapping() {
GetFieldMappingsResponse response = client().admin().indices().prepareGetFieldMappings("index1").setFields("non-existent").get();
Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetadata>> mappings = response.mappings();
assertEquals(1, mappings.size());
Map<String, GetFieldMappingsResponse.FieldMappingMetadata> fieldmapping = mappings.get("index1");
assertEquals(0, fieldmapping.size());
}
public void testFieldCapabilities() { public void testFieldCapabilities() {
List<String> allFields = new ArrayList<>(ALL_FLAT_FIELDS); List<String> allFields = new ArrayList<>(ALL_FLAT_FIELDS);
allFields.addAll(ALL_OBJECT_FIELDS); allFields.addAll(ALL_OBJECT_FIELDS);
@ -142,11 +150,10 @@ public class FieldFilterMapperPluginTests extends OpenSearchSingleNodeTestCase {
} }
private static void assertFieldMappings( private static void assertFieldMappings(
Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetadata>> mappings, Map<String, GetFieldMappingsResponse.FieldMappingMetadata> actual,
Collection<String> expectedFields Collection<String> expectedFields
) { ) {
assertEquals(1, mappings.size()); Map<String, GetFieldMappingsResponse.FieldMappingMetadata> fields = new HashMap<>(actual);
Map<String, GetFieldMappingsResponse.FieldMappingMetadata> fields = new HashMap<>(mappings.get("_doc"));
Set<String> builtInMetadataFields = IndicesModule.getBuiltInMetadataFields(); Set<String> builtInMetadataFields = IndicesModule.getBuiltInMetadataFields();
for (String field : builtInMetadataFields) { for (String field : builtInMetadataFields) {
GetFieldMappingsResponse.FieldMappingMetadata fieldMappingMetadata = fields.remove(field); GetFieldMappingsResponse.FieldMappingMetadata fieldMappingMetadata = fields.remove(field);

View File

@ -78,32 +78,6 @@ public class MapperServiceTests extends OpenSearchSingleNodeTestCase {
return Arrays.asList(InternalSettingsPlugin.class, ReloadableFilterPlugin.class); return Arrays.asList(InternalSettingsPlugin.class, ReloadableFilterPlugin.class);
} }
public void testTypeNameStartsWithIllegalDot() {
String index = "test-index";
String type = ".test-type";
String field = "field";
IllegalArgumentException e = expectThrows(
IllegalArgumentException.class,
() -> { client().admin().indices().prepareCreate(index).addMapping(type, field, "type=text").execute().actionGet(); }
);
assertTrue(e.getMessage(), e.getMessage().contains("mapping type name [.test-type] must not start with a '.'"));
}
public void testTypeNameTooLong() {
String index = "text-index";
String field = "field";
String type = new String(new char[256]).replace("\0", "a");
MapperException e = expectThrows(
MapperException.class,
() -> { client().admin().indices().prepareCreate(index).addMapping(type, field, "type=text").execute().actionGet(); }
);
assertTrue(
e.getMessage(),
e.getMessage().contains("mapping type name [" + type + "] is too long; limit is length 255 but was [256]")
);
}
public void testTypeValidation() { public void testTypeValidation() {
InvalidTypeNameException e = expectThrows(InvalidTypeNameException.class, () -> MapperService.validateTypeName("_type")); InvalidTypeNameException e = expectThrows(InvalidTypeNameException.class, () -> MapperService.validateTypeName("_type"));
assertEquals("mapping type name [_type] can't start with '_' unless it is called [_doc]", e.getMessage()); assertEquals("mapping type name [_type] can't start with '_' unless it is called [_doc]", e.getMessage());

View File

@ -63,16 +63,13 @@ public class UpdateMappingTests extends OpenSearchSingleNodeTestCase {
// test store, ... all the parameters that are not to be changed just like in other fields // test store, ... all the parameters that are not to be changed just like in other fields
XContentBuilder mapping = XContentFactory.jsonBuilder() XContentBuilder mapping = XContentFactory.jsonBuilder()
.startObject() .startObject()
.startObject("type")
.startObject(fieldName) .startObject(fieldName)
.field("enabled", true) .field("enabled", true)
.field("store", false) .field("store", false)
.endObject() .endObject()
.endObject()
.endObject(); .endObject();
XContentBuilder mappingUpdate = XContentFactory.jsonBuilder() XContentBuilder mappingUpdate = XContentFactory.jsonBuilder()
.startObject() .startObject()
.startObject("type")
.startObject(fieldName) .startObject(fieldName)
.field("enabled", true) .field("enabled", true)
.field("store", true) .field("store", true)
@ -82,65 +79,73 @@ public class UpdateMappingTests extends OpenSearchSingleNodeTestCase {
.field("type", "text") .field("type", "text")
.endObject() .endObject()
.endObject() .endObject()
.endObject()
.endObject(); .endObject();
testConflictWhileMergingAndMappingUnchanged(mapping, mappingUpdate); testConflictWhileMergingAndMappingUnchanged(mapping, mappingUpdate);
} }
protected void testConflictWhileMergingAndMappingUnchanged(XContentBuilder mapping, XContentBuilder mappingUpdate) throws IOException { protected void testConflictWhileMergingAndMappingUnchanged(XContentBuilder mapping, XContentBuilder mappingUpdate) throws IOException {
IndexService indexService = createIndex("test", Settings.builder().build(), "type", mapping); IndexService indexService = createIndex("test", Settings.builder().build(), MapperService.SINGLE_MAPPING_NAME, mapping);
CompressedXContent mappingBeforeUpdate = indexService.mapperService().documentMapper("type").mappingSource(); CompressedXContent mappingBeforeUpdate = indexService.mapperService().documentMapper().mappingSource();
// simulate like in MetadataMappingService#putMapping // simulate like in MetadataMappingService#putMapping
try { try {
indexService.mapperService() indexService.mapperService()
.merge("type", new CompressedXContent(BytesReference.bytes(mappingUpdate)), MapperService.MergeReason.MAPPING_UPDATE); .merge(
MapperService.SINGLE_MAPPING_NAME,
new CompressedXContent(BytesReference.bytes(mappingUpdate)),
MapperService.MergeReason.MAPPING_UPDATE
);
fail(); fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // expected
} }
// make sure simulate flag actually worked - no mappings applied // make sure simulate flag actually worked - no mappings applied
CompressedXContent mappingAfterUpdate = indexService.mapperService().documentMapper("type").mappingSource(); CompressedXContent mappingAfterUpdate = indexService.mapperService().documentMapper().mappingSource();
assertThat(mappingAfterUpdate, equalTo(mappingBeforeUpdate)); assertThat(mappingAfterUpdate, equalTo(mappingBeforeUpdate));
} }
public void testConflictSameType() throws Exception { public void testConflictSameType() throws Exception {
XContentBuilder mapping = XContentFactory.jsonBuilder() XContentBuilder mapping = XContentFactory.jsonBuilder()
.startObject() .startObject()
.startObject("type")
.startObject("properties") .startObject("properties")
.startObject("foo") .startObject("foo")
.field("type", "long") .field("type", "long")
.endObject() .endObject()
.endObject() .endObject()
.endObject()
.endObject(); .endObject();
MapperService mapperService = createIndex("test", Settings.builder().build(), "type", mapping).mapperService(); MapperService mapperService = createIndex("test", Settings.builder().build(), MapperService.SINGLE_MAPPING_NAME, mapping)
.mapperService();
XContentBuilder update = XContentFactory.jsonBuilder() XContentBuilder update = XContentFactory.jsonBuilder()
.startObject() .startObject()
.startObject("type")
.startObject("properties") .startObject("properties")
.startObject("foo") .startObject("foo")
.field("type", "double") .field("type", "double")
.endObject() .endObject()
.endObject() .endObject()
.endObject()
.endObject(); .endObject();
IllegalArgumentException e = expectThrows( IllegalArgumentException e = expectThrows(
IllegalArgumentException.class, IllegalArgumentException.class,
() -> mapperService.merge("type", new CompressedXContent(Strings.toString(update)), MapperService.MergeReason.MAPPING_UPDATE) () -> mapperService.merge(
MapperService.SINGLE_MAPPING_NAME,
new CompressedXContent(Strings.toString(update)),
MapperService.MergeReason.MAPPING_UPDATE
)
); );
assertThat(e.getMessage(), containsString("mapper [foo] cannot be changed from type [long] to [double]")); assertThat(e.getMessage(), containsString("mapper [foo] cannot be changed from type [long] to [double]"));
e = expectThrows( e = expectThrows(
IllegalArgumentException.class, IllegalArgumentException.class,
() -> mapperService.merge("type", new CompressedXContent(Strings.toString(update)), MapperService.MergeReason.MAPPING_UPDATE) () -> mapperService.merge(
MapperService.SINGLE_MAPPING_NAME,
new CompressedXContent(Strings.toString(update)),
MapperService.MergeReason.MAPPING_UPDATE
)
); );
assertThat(e.getMessage(), containsString("mapper [foo] cannot be changed from type [long] to [double]")); assertThat(e.getMessage(), containsString("mapper [foo] cannot be changed from type [long] to [double]"));
assertThat( assertThat(
((FieldMapper) mapperService.documentMapper("type").mapping().root().getMapper("foo")).fieldType().typeName(), ((FieldMapper) mapperService.documentMapper().mapping().root().getMapper("foo")).fieldType().typeName(),
equalTo("long") equalTo("long")
); );
} }
@ -148,35 +153,36 @@ public class UpdateMappingTests extends OpenSearchSingleNodeTestCase {
public void testConflictNewType() throws Exception { public void testConflictNewType() throws Exception {
XContentBuilder mapping = XContentFactory.jsonBuilder() XContentBuilder mapping = XContentFactory.jsonBuilder()
.startObject() .startObject()
.startObject("type")
.startObject("properties") .startObject("properties")
.startObject("foo") .startObject("foo")
.field("type", "long") .field("type", "long")
.endObject() .endObject()
.endObject() .endObject()
.endObject()
.endObject(); .endObject();
MapperService mapperService = createIndex("test", Settings.builder().build(), "type", mapping).mapperService(); MapperService mapperService = createIndex("test", Settings.builder().build(), MapperService.SINGLE_MAPPING_NAME, mapping)
.mapperService();
XContentBuilder update = XContentFactory.jsonBuilder() XContentBuilder update = XContentFactory.jsonBuilder()
.startObject() .startObject()
.startObject("type")
.startObject("properties") .startObject("properties")
.startObject("foo") .startObject("foo")
.field("type", "double") .field("type", "double")
.endObject() .endObject()
.endObject() .endObject()
.endObject()
.endObject(); .endObject();
IllegalArgumentException e = expectThrows( IllegalArgumentException e = expectThrows(
IllegalArgumentException.class, IllegalArgumentException.class,
() -> mapperService.merge("type", new CompressedXContent(Strings.toString(update)), MapperService.MergeReason.MAPPING_UPDATE) () -> mapperService.merge(
MapperService.SINGLE_MAPPING_NAME,
new CompressedXContent(Strings.toString(update)),
MapperService.MergeReason.MAPPING_UPDATE
)
); );
assertThat(e.getMessage(), containsString("mapper [foo] cannot be changed from type [long] to [double]")); assertThat(e.getMessage(), containsString("mapper [foo] cannot be changed from type [long] to [double]"));
assertThat( assertThat(
((FieldMapper) mapperService.documentMapper("type").mapping().root().getMapper("foo")).fieldType().typeName(), ((FieldMapper) mapperService.documentMapper().mapping().root().getMapper("foo")).fieldType().typeName(),
equalTo("long") equalTo("long")
); );
} }
@ -184,25 +190,31 @@ public class UpdateMappingTests extends OpenSearchSingleNodeTestCase {
public void testReuseMetaField() throws IOException { public void testReuseMetaField() throws IOException {
XContentBuilder mapping = XContentFactory.jsonBuilder() XContentBuilder mapping = XContentFactory.jsonBuilder()
.startObject() .startObject()
.startObject("type")
.startObject("properties") .startObject("properties")
.startObject("_id") .startObject("_id")
.field("type", "text") .field("type", "text")
.endObject() .endObject()
.endObject() .endObject()
.endObject()
.endObject(); .endObject();
MapperService mapperService = createIndex("test", Settings.builder().build()).mapperService(); MapperService mapperService = createIndex("test", Settings.builder().build()).mapperService();
MapperParsingException e = expectThrows( MapperParsingException e = expectThrows(
MapperParsingException.class, MapperParsingException.class,
() -> mapperService.merge("type", new CompressedXContent(Strings.toString(mapping)), MapperService.MergeReason.MAPPING_UPDATE) () -> mapperService.merge(
MapperService.SINGLE_MAPPING_NAME,
new CompressedXContent(Strings.toString(mapping)),
MapperService.MergeReason.MAPPING_UPDATE
)
); );
assertThat(e.getMessage(), containsString("Field [_id] is defined more than once")); assertThat(e.getMessage(), containsString("Field [_id] is defined more than once"));
MapperParsingException e2 = expectThrows( MapperParsingException e2 = expectThrows(
MapperParsingException.class, MapperParsingException.class,
() -> mapperService.merge("type", new CompressedXContent(Strings.toString(mapping)), MapperService.MergeReason.MAPPING_UPDATE) () -> mapperService.merge(
MapperService.SINGLE_MAPPING_NAME,
new CompressedXContent(Strings.toString(mapping)),
MapperService.MergeReason.MAPPING_UPDATE
)
); );
assertThat(e2.getMessage(), containsString("Field [_id] is defined more than once")); assertThat(e2.getMessage(), containsString("Field [_id] is defined more than once"));
} }
@ -211,47 +223,43 @@ public class UpdateMappingTests extends OpenSearchSingleNodeTestCase {
String mapping1 = Strings.toString( String mapping1 = Strings.toString(
XContentFactory.jsonBuilder() XContentFactory.jsonBuilder()
.startObject() .startObject()
.startObject("type")
.startObject("properties") .startObject("properties")
.startObject("foo") .startObject("foo")
.field("type", "object") .field("type", "object")
.endObject() .endObject()
.endObject() .endObject()
.endObject() .endObject()
.endObject()
); );
String mapping2 = Strings.toString( String mapping2 = Strings.toString(
XContentFactory.jsonBuilder() XContentFactory.jsonBuilder()
.startObject() .startObject()
.startObject("type")
.startObject("properties") .startObject("properties")
.startObject("foo") .startObject("foo")
.field("type", "long") .field("type", "long")
.endObject() .endObject()
.endObject() .endObject()
.endObject() .endObject()
.endObject()
); );
MapperService mapperService1 = createIndex("test1").mapperService(); MapperService mapperService1 = createIndex("test1").mapperService();
mapperService1.merge("type", new CompressedXContent(mapping1), MergeReason.MAPPING_UPDATE); mapperService1.merge(MapperService.SINGLE_MAPPING_NAME, new CompressedXContent(mapping1), MergeReason.MAPPING_UPDATE);
IllegalArgumentException e = expectThrows( IllegalArgumentException e = expectThrows(
IllegalArgumentException.class, IllegalArgumentException.class,
() -> mapperService1.merge("type", new CompressedXContent(mapping2), MergeReason.MAPPING_UPDATE) () -> mapperService1.merge(MapperService.SINGLE_MAPPING_NAME, new CompressedXContent(mapping2), MergeReason.MAPPING_UPDATE)
); );
assertThat(e.getMessage(), equalTo("can't merge a non object mapping [foo] with an object mapping")); assertThat(e.getMessage(), equalTo("can't merge a non object mapping [foo] with an object mapping"));
MapperService mapperService2 = createIndex("test2").mapperService(); MapperService mapperService2 = createIndex("test2").mapperService();
mapperService2.merge("type", new CompressedXContent(mapping2), MergeReason.MAPPING_UPDATE); mapperService2.merge(MapperService.SINGLE_MAPPING_NAME, new CompressedXContent(mapping2), MergeReason.MAPPING_UPDATE);
e = expectThrows( e = expectThrows(
IllegalArgumentException.class, IllegalArgumentException.class,
() -> mapperService2.merge("type", new CompressedXContent(mapping1), MergeReason.MAPPING_UPDATE) () -> mapperService2.merge(MapperService.SINGLE_MAPPING_NAME, new CompressedXContent(mapping1), MergeReason.MAPPING_UPDATE)
); );
assertThat(e.getMessage(), equalTo("can't merge a non object mapping [foo] with an object mapping")); assertThat(e.getMessage(), equalTo("can't merge a non object mapping [foo] with an object mapping"));
} }
public void testMappingVersion() { public void testMappingVersion() {
createIndex("test", client().admin().indices().prepareCreate("test").addMapping("type")); createIndex("test", client().admin().indices().prepareCreate("test").addMapping(MapperService.SINGLE_MAPPING_NAME));
final ClusterService clusterService = getInstanceFromNode(ClusterService.class); final ClusterService clusterService = getInstanceFromNode(ClusterService.class);
{ {
final long previousVersion = clusterService.state().metadata().index("test").getMappingVersion(); final long previousVersion = clusterService.state().metadata().index("test").getMappingVersion();

View File

@ -50,7 +50,7 @@ public class TypeQueryBuilderTests extends AbstractQueryTestCase<TypeQueryBuilde
@Override @Override
protected void doAssertLuceneQuery(TypeQueryBuilder queryBuilder, Query query, QueryShardContext context) throws IOException { protected void doAssertLuceneQuery(TypeQueryBuilder queryBuilder, Query query, QueryShardContext context) throws IOException {
if (createShardContext().getMapperService().documentMapper(queryBuilder.type()) == null) { if (createShardContext().getMapperService().documentMapper() == null) {
assertEquals(new MatchNoDocsQuery(), query); assertEquals(new MatchNoDocsQuery(), query);
} else { } else {
assertThat(query, equalTo(Queries.newNonNestedFilter(context.indexVersionCreated()))); assertThat(query, equalTo(Queries.newNonNestedFilter(context.indexVersionCreated())));

View File

@ -79,7 +79,6 @@ import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.io.stream.StreamInput; import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.lease.Releasable; import org.opensearch.common.lease.Releasable;
import org.opensearch.common.lease.Releasables; import org.opensearch.common.lease.Releasables;
import org.opensearch.common.lucene.uid.Versions;
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.unit.TimeValue; import org.opensearch.common.unit.TimeValue;
@ -92,12 +91,10 @@ import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentType; import org.opensearch.common.xcontent.XContentType;
import org.opensearch.env.NodeEnvironment; import org.opensearch.env.NodeEnvironment;
import org.opensearch.index.IndexSettings; import org.opensearch.index.IndexSettings;
import org.opensearch.index.VersionType;
import org.opensearch.index.codec.CodecService; import org.opensearch.index.codec.CodecService;
import org.opensearch.index.engine.CommitStats; import org.opensearch.index.engine.CommitStats;
import org.opensearch.index.engine.DocIdSeqNoAndSource; import org.opensearch.index.engine.DocIdSeqNoAndSource;
import org.opensearch.index.engine.Engine; import org.opensearch.index.engine.Engine;
import org.opensearch.index.engine.Engine.DeleteResult;
import org.opensearch.index.engine.EngineConfig; import org.opensearch.index.engine.EngineConfig;
import org.opensearch.index.engine.EngineConfigFactory; import org.opensearch.index.engine.EngineConfigFactory;
import org.opensearch.index.engine.EngineTestCase; import org.opensearch.index.engine.EngineTestCase;
@ -3358,11 +3355,7 @@ public class IndexShardTests extends IndexShardTestCase {
// Do some updates and deletes, then recheck the correlation again. // Do some updates and deletes, then recheck the correlation again.
for (int i = 0; i < numDoc / 2; i++) { for (int i = 0; i < numDoc / 2; i++) {
if (randomBoolean()) { indexDoc(indexShard, "_doc", Integer.toString(i), "{\"foo\": \"bar\"}");
deleteDoc(indexShard, "doc", Integer.toString(i));
} else {
indexDoc(indexShard, "_doc", Integer.toString(i), "{\"foo\": \"bar\"}");
}
} }
if (randomBoolean()) { if (randomBoolean()) {
indexShard.flush(new FlushRequest()); indexShard.flush(new FlushRequest());
@ -3939,7 +3932,10 @@ public class IndexShardTests extends IndexShardTestCase {
public void testSupplyTombstoneDoc() throws Exception { public void testSupplyTombstoneDoc() throws Exception {
IndexShard shard = newStartedShard(); IndexShard shard = newStartedShard();
String id = randomRealisticUnicodeOfLengthBetween(1, 10); String id = randomRealisticUnicodeOfLengthBetween(1, 10);
ParsedDocument deleteTombstone = shard.getEngine().config().getTombstoneDocSupplier().newDeleteTombstoneDoc("doc", id); ParsedDocument deleteTombstone = shard.getEngine()
.config()
.getTombstoneDocSupplier()
.newDeleteTombstoneDoc(MapperService.SINGLE_MAPPING_NAME, id);
assertThat(deleteTombstone.docs(), hasSize(1)); assertThat(deleteTombstone.docs(), hasSize(1));
ParseContext.Document deleteDoc = deleteTombstone.docs().get(0); ParseContext.Document deleteDoc = deleteTombstone.docs().get(0);
assertThat( assertThat(
@ -4294,38 +4290,6 @@ public class IndexShardTests extends IndexShardTestCase {
return Settings.builder().put(super.threadPoolSettings()).put("thread_pool.estimated_time_interval", "5ms").build(); return Settings.builder().put(super.threadPoolSettings()).put("thread_pool.estimated_time_interval", "5ms").build();
} }
public void testTypelessDelete() throws IOException {
Settings settings = Settings.builder()
.put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1)
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)
.build();
IndexMetadata metadata = IndexMetadata.builder("index")
.putMapping("some_type", "{ \"properties\": {}}")
.settings(settings)
.primaryTerm(0, 1)
.build();
IndexShard shard = newShard(new ShardId(metadata.getIndex(), 0), true, "n1", metadata, null);
recoverShardFromStore(shard);
Engine.IndexResult indexResult = indexDoc(shard, "some_type", "id", "{}");
assertTrue(indexResult.isCreated());
DeleteResult deleteResult = shard.applyDeleteOperationOnPrimary(
Versions.MATCH_ANY,
"some_other_type",
"id",
VersionType.INTERNAL,
UNASSIGNED_SEQ_NO,
1
);
assertFalse(deleteResult.isFound());
deleteResult = shard.applyDeleteOperationOnPrimary(Versions.MATCH_ANY, "_doc", "id", VersionType.INTERNAL, UNASSIGNED_SEQ_NO, 1);
assertTrue(deleteResult.isFound());
closeShards(shard);
}
public void testTypelessGet() throws IOException { public void testTypelessGet() throws IOException {
Settings settings = Settings.builder() Settings settings = Settings.builder()
.put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)

View File

@ -64,7 +64,6 @@ public class GeoContextMappingTests extends OpenSearchSingleNodeTestCase {
public void testIndexingWithNoContexts() throws Exception { public void testIndexingWithNoContexts() throws Exception {
XContentBuilder mapping = jsonBuilder().startObject() XContentBuilder mapping = jsonBuilder().startObject()
.startObject("type1")
.startObject("properties") .startObject("properties")
.startObject("completion") .startObject("completion")
.field("type", "completion") .field("type", "completion")
@ -76,16 +75,15 @@ public class GeoContextMappingTests extends OpenSearchSingleNodeTestCase {
.endArray() .endArray()
.endObject() .endObject()
.endObject() .endObject()
.endObject()
.endObject(); .endObject();
MapperService mapperService = createIndex("test", Settings.EMPTY, "type1", mapping).mapperService(); MapperService mapperService = createIndex("test", Settings.EMPTY, MapperService.SINGLE_MAPPING_NAME, mapping).mapperService();
MappedFieldType completionFieldType = mapperService.fieldType("completion"); MappedFieldType completionFieldType = mapperService.fieldType("completion");
ParsedDocument parsedDocument = mapperService.documentMapper() ParsedDocument parsedDocument = mapperService.documentMapper()
.parse( .parse(
new SourceToParse( new SourceToParse(
"test", "test",
"type1", MapperService.SINGLE_MAPPING_NAME,
"1", "1",
BytesReference.bytes( BytesReference.bytes(
jsonBuilder().startObject() jsonBuilder().startObject()
@ -114,7 +112,6 @@ public class GeoContextMappingTests extends OpenSearchSingleNodeTestCase {
public void testIndexingWithSimpleContexts() throws Exception { public void testIndexingWithSimpleContexts() throws Exception {
XContentBuilder mapping = jsonBuilder().startObject() XContentBuilder mapping = jsonBuilder().startObject()
.startObject("type1")
.startObject("properties") .startObject("properties")
.startObject("completion") .startObject("completion")
.field("type", "completion") .field("type", "completion")
@ -126,16 +123,15 @@ public class GeoContextMappingTests extends OpenSearchSingleNodeTestCase {
.endArray() .endArray()
.endObject() .endObject()
.endObject() .endObject()
.endObject()
.endObject(); .endObject();
MapperService mapperService = createIndex("test", Settings.EMPTY, "type1", mapping).mapperService(); MapperService mapperService = createIndex("test", Settings.EMPTY, MapperService.SINGLE_MAPPING_NAME, mapping).mapperService();
MappedFieldType completionFieldType = mapperService.fieldType("completion"); MappedFieldType completionFieldType = mapperService.fieldType("completion");
ParsedDocument parsedDocument = mapperService.documentMapper() ParsedDocument parsedDocument = mapperService.documentMapper()
.parse( .parse(
new SourceToParse( new SourceToParse(
"test", "test",
"type1", MapperService.SINGLE_MAPPING_NAME,
"1", "1",
BytesReference.bytes( BytesReference.bytes(
jsonBuilder().startObject() jsonBuilder().startObject()
@ -162,7 +158,6 @@ public class GeoContextMappingTests extends OpenSearchSingleNodeTestCase {
public void testIndexingWithContextList() throws Exception { public void testIndexingWithContextList() throws Exception {
XContentBuilder mapping = jsonBuilder().startObject() XContentBuilder mapping = jsonBuilder().startObject()
.startObject("type1")
.startObject("properties") .startObject("properties")
.startObject("completion") .startObject("completion")
.field("type", "completion") .field("type", "completion")
@ -174,16 +169,15 @@ public class GeoContextMappingTests extends OpenSearchSingleNodeTestCase {
.endArray() .endArray()
.endObject() .endObject()
.endObject() .endObject()
.endObject()
.endObject(); .endObject();
MapperService mapperService = createIndex("test", Settings.EMPTY, "type1", mapping).mapperService(); MapperService mapperService = createIndex("test", Settings.EMPTY, MapperService.SINGLE_MAPPING_NAME, mapping).mapperService();
MappedFieldType completionFieldType = mapperService.fieldType("completion"); MappedFieldType completionFieldType = mapperService.fieldType("completion");
ParsedDocument parsedDocument = mapperService.documentMapper() ParsedDocument parsedDocument = mapperService.documentMapper()
.parse( .parse(
new SourceToParse( new SourceToParse(
"test", "test",
"type1", MapperService.SINGLE_MAPPING_NAME,
"1", "1",
BytesReference.bytes( BytesReference.bytes(
jsonBuilder().startObject() jsonBuilder().startObject()
@ -214,7 +208,6 @@ public class GeoContextMappingTests extends OpenSearchSingleNodeTestCase {
public void testIndexingWithMultipleContexts() throws Exception { public void testIndexingWithMultipleContexts() throws Exception {
XContentBuilder mapping = jsonBuilder().startObject() XContentBuilder mapping = jsonBuilder().startObject()
.startObject("type1")
.startObject("properties") .startObject("properties")
.startObject("completion") .startObject("completion")
.field("type", "completion") .field("type", "completion")
@ -230,10 +223,9 @@ public class GeoContextMappingTests extends OpenSearchSingleNodeTestCase {
.endArray() .endArray()
.endObject() .endObject()
.endObject() .endObject()
.endObject()
.endObject(); .endObject();
MapperService mapperService = createIndex("test", Settings.EMPTY, "type1", mapping).mapperService(); MapperService mapperService = createIndex("test", Settings.EMPTY, MapperService.SINGLE_MAPPING_NAME, mapping).mapperService();
MappedFieldType completionFieldType = mapperService.fieldType("completion"); MappedFieldType completionFieldType = mapperService.fieldType("completion");
XContentBuilder builder = jsonBuilder().startObject() XContentBuilder builder = jsonBuilder().startObject()
.startArray("completion") .startArray("completion")
@ -248,7 +240,7 @@ public class GeoContextMappingTests extends OpenSearchSingleNodeTestCase {
.endArray() .endArray()
.endObject(); .endObject();
ParsedDocument parsedDocument = mapperService.documentMapper() ParsedDocument parsedDocument = mapperService.documentMapper()
.parse(new SourceToParse("test", "type1", "1", BytesReference.bytes(builder), XContentType.JSON)); .parse(new SourceToParse("test", MapperService.SINGLE_MAPPING_NAME, "1", BytesReference.bytes(builder), XContentType.JSON));
IndexableField[] fields = parsedDocument.rootDoc().getFields(completionFieldType.name()); IndexableField[] fields = parsedDocument.rootDoc().getFields(completionFieldType.name());
assertContextSuggestFields(fields, 3); assertContextSuggestFields(fields, 3);
} }