Skip to content

Commit

Permalink
Remove MapperService#types(). (#29617)
Browse files Browse the repository at this point in the history
This isn't be necessary with a single type per index.
  • Loading branch information
jpountz authored May 2, 2018
1 parent 231a63f commit 368ddc4
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -581,11 +581,7 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
final List<ParsedDocument> docs = new ArrayList<>();
final DocumentMapper docMapper;
final MapperService mapperService = context.getMapperService();
Collection<String> types = mapperService.types();
if (types.size() != 1) {
throw new IllegalStateException("Only a single type should exist, but [" + types.size() + " types exists");
}
String type = types.iterator().next();
String type = mapperService.documentMapper().type();
if (documentType != null) {
DEPRECATION_LOGGER.deprecated("[document_type] parameter has been deprecated because types have been deprecated");
if (documentType.equals(type) == false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,11 @@
import org.elasticsearch.transport.TransportService;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static java.util.Collections.singletonMap;

Expand Down Expand Up @@ -96,15 +94,16 @@ protected GetFieldMappingsResponse shardOperation(final GetFieldMappingsIndexReq
Predicate<String> metadataFieldPredicate = indicesService::isMetaDataField;
Predicate<String> fieldPredicate = metadataFieldPredicate.or(indicesService.getFieldFilter().apply(shardId.getIndexName()));

DocumentMapper mapper = indexService.mapperService().documentMapper();
Collection<String> typeIntersection;
if (request.types().length == 0) {
typeIntersection = indexService.mapperService().types();

typeIntersection = mapper == null
? Collections.emptySet()
: Collections.singleton(mapper.type());
} else {
typeIntersection = indexService.mapperService().types()
.stream()
.filter(type -> Regex.simpleMatch(request.types(), type))
.collect(Collectors.toCollection(ArrayList::new));
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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.IdFieldMapper;
import org.elasticsearch.index.mapper.IgnoredFieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType;
Expand All @@ -34,7 +35,6 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -84,10 +84,9 @@ public Status needsField(FieldInfo fieldInfo) throws IOException {
}

public void postProcess(MapperService mapperService) {
final Collection<String> types = mapperService.types();
assert types.size() <= 1 : types;
if (types.isEmpty() == false) {
type = types.iterator().next();
final DocumentMapper mapper = mapperService.documentMapper();
if (mapper != null) {
type = mapper.type();
}
for (Map.Entry<String, List<Object>> entry : fields().entrySet()) {
MappedFieldType fieldType = mapperService.fullName(entry.getKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -149,23 +147,18 @@ private FetchSourceContext normalizeFetchSourceContent(@Nullable FetchSourceCont
private GetResult innerGet(String type, String id, String[] gFields, boolean realtime, long version, VersionType versionType,
FetchSourceContext fetchSourceContext, boolean readFromTranslog) {
fetchSourceContext = normalizeFetchSourceContent(fetchSourceContext, gFields);
final Collection<String> types;
if (type == null || type.equals("_all")) {
types = mapperService.types();
} else {
types = Collections.singleton(type);
DocumentMapper mapper = mapperService.documentMapper();
type = mapper == null ? null : mapper.type();
}

Engine.GetResult get = null;
for (String typeX : types) {
Term uidTerm = mapperService.createUidTerm(typeX, id);
if (type != null) {
Term uidTerm = mapperService.createUidTerm(type, id);
if (uidTerm != null) {
get = indexShard.get(new Engine.Get(realtime, readFromTranslog, typeX, id, uidTerm)
get = indexShard.get(new Engine.Get(realtime, readFromTranslog, type, id, uidTerm)
.version(version).versionType(versionType));
if (get.exists()) {
type = typeX;
break;
} else {
if (get.exists() == false) {
get.release();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -673,15 +673,6 @@ public DocumentMapper parse(String mappingType, CompressedXContent mappingSource
return documentParser.parse(mappingType, mappingSource, applyDefault ? defaultMappingSource : null);
}

/**
* Get the set of types.
* @deprecated Indices may have one type at most, use {@link #documentMapper()} instead.
*/
@Deprecated
public Set<String> types() {
return mapper == null ? Collections.emptySet() : Collections.singleton(mapper.type());
}

/**
* Return the document mapper, or {@code null} if no mapping has been put yet.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,7 @@ public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName) {
return new DocValuesIndexFieldData.Builder();
} else {
// means the index has a single type and the type field is implicit
Function<MapperService, String> typeFunction = mapperService -> {
Collection<String> types = mapperService.types();
if (types.size() > 1) {
throw new AssertionError();
}
// If we reach here, there is necessarily one type since we were able to find a `_type` field
String type = types.iterator().next();
return type;
};
Function<MapperService, String> typeFunction = mapperService -> mapperService.documentMapper().type();
return new ConstantIndexFieldData.Builder(typeFunction);
}
}
Expand All @@ -144,12 +136,11 @@ public Query termQuery(Object value, QueryShardContext context) {

@Override
public Query termsQuery(List<?> values, QueryShardContext context) {
Collection<String> indexTypes = context.getMapperService().types();
if (indexTypes.isEmpty()) {
DocumentMapper mapper = context.getMapperService().documentMapper();
if (mapper == null) {
return new MatchNoDocsQuery("No types");
}
assert indexTypes.size() == 1;
BytesRef indexType = indexedValueForSearch(indexTypes.iterator().next());
BytesRef indexType = indexedValueForSearch(mapper.type());
if (values.stream()
.map(this::indexedValueForSearch)
.anyMatch(indexType::equals)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.IdFieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.Uid;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -163,19 +163,17 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
if (this.ids.isEmpty()) {
return Queries.newMatchNoDocsQuery("Missing ids in \"" + this.getName() + "\" query.");
} else {
final DocumentMapper mapper = context.getMapperService().documentMapper();
Collection<String> typesForQuery;
if (types.length == 0) {
typesForQuery = context.queryTypes();
} else if (types.length == 1 && MetaData.ALL.equals(types[0])) {
typesForQuery = context.getMapperService().types();
typesForQuery = Collections.singleton(mapper.type());
} else {
typesForQuery = new HashSet<>();
Collections.addAll(typesForQuery, types);
typesForQuery = new HashSet<>(Arrays.asList(types));
}

final Collection<String> mappingTypes = context.getMapperService().types();
assert mappingTypes.size() == 1;
if (typesForQuery.contains(mappingTypes.iterator().next())) {
if (typesForQuery.contains(mapper.type())) {
return idField.termsQuery(new ArrayList<>(ids), context);
} else {
return new MatchNoDocsQuery("Type mismatch");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -262,11 +263,9 @@ MappedFieldType failIfFieldMappingNotFound(String name, MappedFieldType fieldMap
*/
public Collection<String> queryTypes() {
String[] types = getTypes();
if (types == null || types.length == 0) {
return getMapperService().types();
}
if (types.length == 1 && types[0].equals("_all")) {
return getMapperService().types();
if (types == null || types.length == 0 || (types.length == 1 && types[0].equals("_all"))) {
DocumentMapper mapper = getMapperService().documentMapper();
return mapper == null ? Collections.emptyList() : Collections.singleton(mapper.type());
}
return Arrays.asList(types);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ protected ScoreFunction doToFunction(QueryShardContext context) {
fieldType = context.getMapperService().fullName(IdFieldMapper.NAME);
}
if (fieldType == null) {
if (context.getMapperService().types().isEmpty()) {
if (context.getMapperService().documentMapper() == null) {
// no mappings: the index is empty anyway
return new RandomScoreFunction(hash(context.nowInMillis()), salt, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
import org.mockito.Mockito;

import java.io.IOException;
import java.util.Collections;
import java.util.Set;

public class TypeFieldTypeTests extends FieldTypeTestCase {
@Override
Expand All @@ -60,26 +58,27 @@ public void testTermsQuery() throws Exception {
Mockito.when(context.indexVersionCreated()).thenReturn(indexVersionCreated);

MapperService mapperService = Mockito.mock(MapperService.class);
Set<String> types = Collections.emptySet();
Mockito.when(mapperService.types()).thenReturn(types);
Mockito.when(mapperService.documentMapper()).thenReturn(null);
Mockito.when(context.getMapperService()).thenReturn(mapperService);

TypeFieldMapper.TypeFieldType ft = new TypeFieldMapper.TypeFieldType();
ft.setName(TypeFieldMapper.NAME);
Query query = ft.termQuery("my_type", context);
assertEquals(new MatchNoDocsQuery(), query);

types = Collections.singleton("my_type");
Mockito.when(mapperService.types()).thenReturn(types);
DocumentMapper mapper = Mockito.mock(DocumentMapper.class);
Mockito.when(mapper.type()).thenReturn("my_type");
Mockito.when(mapperService.documentMapper()).thenReturn(mapper);
query = ft.termQuery("my_type", context);
assertEquals(new MatchAllDocsQuery(), query);

Mockito.when(mapperService.hasNested()).thenReturn(true);
query = ft.termQuery("my_type", context);
assertEquals(Queries.newNonNestedFilter(context.indexVersionCreated()), query);

types = Collections.singleton("other_type");
Mockito.when(mapperService.types()).thenReturn(types);
mapper = Mockito.mock(DocumentMapper.class);
Mockito.when(mapper.type()).thenReturn("other_type");
Mockito.when(mapperService.documentMapper()).thenReturn(mapper);
query = ft.termQuery("my_type", context);
assertEquals(new MatchNoDocsQuery(), query);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected void doAssertLuceneQuery(IdsQueryBuilder queryBuilder, Query query, Se
|| context.getQueryShardContext().fieldMapper(IdFieldMapper.NAME) == null
// there are types, but disjoint from the query
|| (allTypes == false &&
Arrays.asList(queryBuilder.types()).indexOf(context.mapperService().types().iterator().next()) == -1)) {
Arrays.asList(queryBuilder.types()).indexOf(context.mapperService().documentMapper().type()) == -1)) {
assertThat(query, instanceOf(MatchNoDocsQuery.class));
} else {
assertThat(query, instanceOf(TermInSetQuery.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.elasticsearch.index.fielddata.IndexFieldDataCache;
import org.elasticsearch.index.fielddata.IndexFieldDataService;
import org.elasticsearch.index.mapper.ContentPath;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.Mapper.BuilderContext;
import org.elasticsearch.index.mapper.MapperService;
Expand All @@ -60,7 +61,6 @@
import org.elasticsearch.indices.breaker.NoneCircuitBreakerService;
import org.elasticsearch.indices.fielddata.cache.IndicesFieldDataCache;
import org.elasticsearch.mock.orig.Mockito;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
import org.elasticsearch.search.fetch.FetchPhase;
import org.elasticsearch.search.fetch.subphase.DocValueFieldsFetchSubPhase;
import org.elasticsearch.search.fetch.subphase.FetchSourceSubPhase;
Expand Down Expand Up @@ -129,7 +129,9 @@ protected AggregatorFactory<?> createAggregatorFactory(Query query,
MapperService mapperService = mapperServiceMock();
when(mapperService.getIndexSettings()).thenReturn(indexSettings);
when(mapperService.hasNested()).thenReturn(false);
when(mapperService.types()).thenReturn(Collections.singleton(TYPE_NAME));
DocumentMapper mapper = mock(DocumentMapper.class);
when(mapper.type()).thenReturn(TYPE_NAME);
when(mapperService.documentMapper()).thenReturn(mapper);
when(searchContext.mapperService()).thenReturn(mapperService);
IndexFieldDataService ifds = new IndexFieldDataService(indexSettings,
new IndicesFieldDataCache(Settings.EMPTY, new IndexFieldDataCache.Listener() {
Expand Down

0 comments on commit 368ddc4

Please sign in to comment.