Skip to content

Commit

Permalink
Remove type from documentMapper method
Browse files Browse the repository at this point in the history
Signed-off-by: Nicholas Walter Knize <[email protected]>
  • Loading branch information
nknize committed Mar 11, 2022
1 parent 860e24e commit 253189c
Show file tree
Hide file tree
Showing 28 changed files with 250 additions and 342 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
);
}
}
docMapper = mapperService.documentMapper(type);
docMapper = mapperService.documentMapper();
for (BytesReference document : documents) {
docs.add(docMapper.parse(new SourceToParse(context.index().getName(), type, "_temp_id", document, documentXContentType)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public void init() throws Exception {
.endObject()
);
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();

queries = new ArrayList<>();
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ protected Collection<Class<? extends Plugin>> getPlugins() {

public void testSizeEnabled() throws Exception {
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());
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 points = false;
Expand All @@ -80,27 +80,27 @@ public void testSizeEnabled() throws Exception {

public void testSizeDisabled() throws Exception {
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());
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());
}

public void testSizeNotSet() throws Exception {
IndexService service = createIndex("test", Settings.EMPTY, "type");
DocumentMapper docMapper = service.mapperService().documentMapper("type");
IndexService service = createIndex("test", Settings.EMPTY, MapperService.SINGLE_MAPPING_NAME);
DocumentMapper docMapper = service.mapperService().documentMapper();

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());
}

public void testThatDisablingWorksWhenMerging() throws Exception {
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));

String disabledMapping = Strings.toString(
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
indices.create:
index: test_index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ public void testDelayedMappingPropagationOnReplica() throws Exception {
final IndexService indexService = indicesService.indexServiceSafe(index);
assertNotNull(indexService);
final MapperService mapperService = indexService.mapperService();
DocumentMapper mapper = mapperService.documentMapper(MapperService.SINGLE_MAPPING_NAME);
DocumentMapper mapper = mapperService.documentMapper();
assertNotNull(mapper);
assertNotNull(mapper.mappers().getMapper("field"));
});
Expand All @@ -389,7 +389,7 @@ public void testDelayedMappingPropagationOnReplica() throws Exception {
final IndexService indexService = indicesService.indexServiceSafe(index);
assertNotNull(indexService);
final MapperService mapperService = indexService.mapperService();
DocumentMapper mapper = mapperService.documentMapper(MapperService.SINGLE_MAPPING_NAME);
DocumentMapper mapper = mapperService.documentMapper();
assertNotNull(mapper);
assertNotNull(mapper.mappers().getMapper("field2"));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.opensearch.common.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.index.mapper.MapperService;
import org.opensearch.plugins.Plugin;
import org.opensearch.test.OpenSearchIntegTestCase;
import org.opensearch.test.InternalSettingsPlugin;
Expand Down Expand Up @@ -78,7 +77,7 @@ public void testGetMappingsWhereThereAreNone() {
assertThat(response.mappings().size(), equalTo(1));
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 {
Expand Down Expand Up @@ -113,40 +112,26 @@ public void testGetFieldMappings() throws Exception {
GetFieldMappingsResponse response = client().admin()
.indices()
.prepareGetFieldMappings("indexa")
.setTypes(MapperService.SINGLE_MAPPING_NAME)
.setFields("field1", "obj.subfield")
.get();
assertThat(response.fieldMappings("indexa", MapperService.SINGLE_MAPPING_NAME, "field1").fullName(), equalTo("field1"));
assertThat(response.fieldMappings("indexa", MapperService.SINGLE_MAPPING_NAME, "field1").sourceAsMap(), hasKey("field1"));
assertThat(response.fieldMappings("indexa", MapperService.SINGLE_MAPPING_NAME, "obj.subfield").fullName(), equalTo("obj.subfield"));
assertThat(response.fieldMappings("indexa", MapperService.SINGLE_MAPPING_NAME, "obj.subfield").sourceAsMap(), hasKey("subfield"));
assertThat(response.fieldMappings("indexb", "typeB", "field1"), nullValue());
assertThat(response.fieldMappings("indexa", "field1").fullName(), equalTo("field1"));
assertThat(response.fieldMappings("indexa", "field1").sourceAsMap(), hasKey("field1"));
assertThat(response.fieldMappings("indexa", "obj.subfield").fullName(), equalTo("obj.subfield"));
assertThat(response.fieldMappings("indexa", "obj.subfield").sourceAsMap(), hasKey("subfield"));

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

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

@SuppressWarnings("unchecked")
Expand All @@ -162,30 +147,16 @@ public void testSimpleGetFieldMappingsWithDefaults() throws Exception {
.includeDefaults(true)
.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(
(Map<String, Object>) response.fieldMappings("test", MapperService.SINGLE_MAPPING_NAME, "num").sourceAsMap().get("num"),
hasEntry("index", Boolean.TRUE)
);
assertThat(
(Map<String, Object>) response.fieldMappings("test", MapperService.SINGLE_MAPPING_NAME, "num").sourceAsMap().get("num"),
hasEntry("type", "long")
);
assertThat(
(Map<String, Object>) response.fieldMappings("test", MapperService.SINGLE_MAPPING_NAME, "field1").sourceAsMap().get("field1"),
(Map<String, Object>) response.fieldMappings("test", "field1").sourceAsMap().get("field1"),
hasEntry("index", Boolean.TRUE)
);
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(
(Map<String, Object>) response.fieldMappings("test", MapperService.SINGLE_MAPPING_NAME, "field1").sourceAsMap().get("field1"),
hasEntry("type", "text")
);
assertThat(
(Map<String, Object>) response.fieldMappings("test", MapperService.SINGLE_MAPPING_NAME, "field2").sourceAsMap().get("field2"),
hasEntry("type", "text")
);
assertThat(
(Map<String, Object>) response.fieldMappings("test", MapperService.SINGLE_MAPPING_NAME, "obj.subfield")
.sourceAsMap()
.get("subfield"),
(Map<String, Object>) response.fieldMappings("test", "obj.subfield").sourceAsMap().get("subfield"),
hasEntry("type", "keyword")
);
}
Expand All @@ -196,12 +167,12 @@ public void testGetFieldMappingsWithFieldAlias() throws Exception {

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

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

FieldMappingMetadata field1Mapping = response.fieldMappings("test", MapperService.SINGLE_MAPPING_NAME, "field1");
FieldMappingMetadata field1Mapping = response.fieldMappings("test", "field1");
assertThat(field1Mapping.fullName(), equalTo("field1"));
assertThat(field1Mapping.sourceAsMap(), hasKey("field1"));
}
Expand All @@ -214,7 +185,6 @@ public void testSimpleGetFieldMappingsWithPretty() throws Exception {
GetFieldMappingsResponse response = client().admin()
.indices()
.prepareGetFieldMappings("index")
.setTypes("type")
.setFields("field1", "obj.subfield")
.get();
XContentBuilder responseBuilder = XContentFactory.jsonBuilder().prettyPrint();
Expand All @@ -227,7 +197,7 @@ public void testSimpleGetFieldMappingsWithPretty() throws Exception {

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();
response.toXContent(responseBuilder, new ToXContent.MapParams(params));
responseStrings = Strings.toString(responseBuilder);
Expand All @@ -247,10 +217,9 @@ public void testGetFieldMappingsWithBlocks() throws Exception {
GetFieldMappingsResponse response = client().admin()
.indices()
.prepareGetFieldMappings("test")
.setTypes("_doc")
.setFields("field1", "obj.subfield")
.get();
assertThat(response.fieldMappings("test", "_doc", "field1").fullName(), equalTo("field1"));
assertThat(response.fieldMappings("test", "field1").fullName(), equalTo("field1"));
} finally {
disableIndexBlock("test", block);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,37 +32,39 @@

package org.opensearch.action.admin.indices.mapping.get;

import org.opensearch.Version;
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.action.OriginalIndices;
import org.opensearch.action.support.IndicesOptions;
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.StreamOutput;

import java.io.IOException;

public class GetFieldMappingsIndexRequest extends SingleShardRequest<GetFieldMappingsIndexRequest> {

private final boolean probablySingleFieldRequest;
private final boolean includeDefaults;
private final String[] fields;
private final String[] types;

private OriginalIndices originalIndices;
private final OriginalIndices originalIndices;

GetFieldMappingsIndexRequest(StreamInput in) throws IOException {
super(in);
types = in.readStringArray();
if (in.getVersion().before(Version.V_2_0_0)) {
in.readStringArray(); // removed types array
}
fields = in.readStringArray();
includeDefaults = in.readBoolean();
probablySingleFieldRequest = in.readBoolean();
if (in.getVersion().before(Version.V_2_0_0)) {
in.readBoolean(); // removed probablySingleField boolean
}
originalIndices = OriginalIndices.readOriginalIndices(in);
}

GetFieldMappingsIndexRequest(GetFieldMappingsRequest other, String index, boolean probablySingleFieldRequest) {
this.probablySingleFieldRequest = probablySingleFieldRequest;
GetFieldMappingsIndexRequest(GetFieldMappingsRequest other, String index) {
this.includeDefaults = other.includeDefaults();
this.types = other.types();
this.fields = other.fields();
assert index != null;
this.index(index);
Expand All @@ -74,18 +76,10 @@ public ActionRequestValidationException validate() {
return null;
}

public String[] types() {
return types;
}

public String[] fields() {
return fields;
}

public boolean probablySingleFieldRequest() {
return probablySingleFieldRequest;
}

public boolean includeDefaults() {
return includeDefaults;
}
Expand All @@ -103,10 +97,14 @@ public IndicesOptions indicesOptions() {
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeStringArray(types);
if (out.getVersion().before(Version.V_2_0_0)) {
out.writeStringArray(Strings.EMPTY_ARRAY);
}
out.writeStringArray(fields);
out.writeBoolean(includeDefaults);
out.writeBoolean(probablySingleFieldRequest);
if (out.getVersion().before(Version.V_2_0_0)) {
out.writeBoolean(false);
}
OriginalIndices.writeOriginalIndices(originalIndices, out);
}

Expand Down
Loading

0 comments on commit 253189c

Please sign in to comment.