Remove MapperService#types(). (#29617)

This isn't be necessary with a single type per index.
This commit is contained in:
Adrien Grand 2018-05-02 11:35:12 +02:00 committed by GitHub
parent 231a63fdf8
commit 368ddc408f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 44 additions and 77 deletions

View File

@ -581,11 +581,7 @@ public class PercolateQueryBuilder extends AbstractQueryBuilder<PercolateQueryBu
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) {

View File

@ -47,13 +47,11 @@ import org.elasticsearch.threadpool.ThreadPool;
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;
@ -96,15 +94,16 @@ public class TransportGetFieldMappingsIndexAction extends TransportSingleShardAc
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());
}

View File

@ -23,6 +23,7 @@ import org.apache.lucene.index.StoredFieldVisitor;
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;
@ -34,7 +35,6 @@ import org.elasticsearch.index.mapper.Uid;
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;
@ -84,10 +84,9 @@ public class FieldsVisitor extends StoredFieldVisitor {
}
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());

View File

@ -48,8 +48,6 @@ import org.elasticsearch.index.shard.IndexShard;
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;
@ -149,23 +147,18 @@ public final class ShardGetService extends AbstractIndexShardComponent {
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();
}
}

View File

@ -673,15 +673,6 @@ public class MapperService extends AbstractIndexComponent implements Closeable {
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.
*/

View File

@ -114,15 +114,7 @@ public class TypeFieldMapper extends MetadataFieldMapper {
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);
}
}
@ -144,12 +136,11 @@ public class TypeFieldMapper extends MetadataFieldMapper {
@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)) {

View File

@ -31,9 +31,9 @@ import org.elasticsearch.common.lucene.search.Queries;
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;
@ -163,19 +163,17 @@ public class IdsQueryBuilder extends AbstractQueryBuilder<IdsQueryBuilder> {
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");

View File

@ -56,6 +56,7 @@ import org.elasticsearch.transport.RemoteClusterAware;
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;
@ -262,11 +263,9 @@ public class QueryShardContext extends QueryRewriteContext {
*/
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);
}

View File

@ -172,7 +172,7 @@ public class RandomScoreFunctionBuilder extends ScoreFunctionBuilder<RandomScore
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);
}

View File

@ -37,8 +37,6 @@ import org.elasticsearch.test.VersionUtils;
import org.mockito.Mockito;
import java.io.IOException;
import java.util.Collections;
import java.util.Set;
public class TypeFieldTypeTests extends FieldTypeTestCase {
@Override
@ -60,8 +58,7 @@ public class TypeFieldTypeTests extends FieldTypeTestCase {
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();
@ -69,8 +66,9 @@ public class TypeFieldTypeTests extends FieldTypeTestCase {
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);
@ -78,8 +76,9 @@ public class TypeFieldTypeTests extends FieldTypeTestCase {
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);
}

View File

@ -84,7 +84,7 @@ public class IdsQueryBuilderTests extends AbstractQueryTestCase<IdsQueryBuilder>
|| 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));

View File

@ -48,6 +48,7 @@ import org.elasticsearch.index.engine.Engine;
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;
@ -60,7 +61,6 @@ import org.elasticsearch.indices.breaker.CircuitBreakerService;
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;
@ -129,7 +129,9 @@ public abstract class AggregatorTestCase extends ESTestCase {
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() {