Skip to content

Replace some usages of QueryShardContext#getMapperService (#63239) #63485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1654,7 +1654,7 @@ public Get setIfPrimaryTerm(long primaryTerm) {
this.ifPrimaryTerm = primaryTerm;
return this;
}

public long getIfPrimaryTerm() {
return ifPrimaryTerm;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ private Translog.Operation readDocAsOp(int docIndex) throws IOException {
SourceFieldMapper.NAME;
final FieldsVisitor fields = new FieldsVisitor(true, sourceField);
leaf.reader().document(segmentDocID, fields);
fields.postProcess(mapperService);
fields.postProcess(mapperService::fieldType, mapperService.documentMapper());

final Translog.Operation op;
final boolean isTombstone = parallelArray.isTombStone[docIndex];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.StoredFieldVisitor;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.Nullable;
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;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.RoutingFieldMapper;
import org.elasticsearch.index.mapper.SourceFieldMapper;
import org.elasticsearch.index.mapper.Uid;
Expand All @@ -39,6 +39,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

import static java.util.Collections.emptyMap;
import static java.util.Collections.unmodifiableSet;
Expand Down Expand Up @@ -88,13 +89,12 @@ public Status needsField(FieldInfo fieldInfo) {
: Status.NO;
}

public void postProcess(MapperService mapperService) {
final DocumentMapper mapper = mapperService.documentMapper();
public final void postProcess(Function<String, MappedFieldType> fieldTypeLookup, @Nullable DocumentMapper mapper) {
if (mapper != null) {
type = mapper.type();
}
for (Map.Entry<String, List<Object>> entry : fields().entrySet()) {
MappedFieldType fieldType = mapperService.fieldType(entry.getKey());
MappedFieldType fieldType = fieldTypeLookup.apply(entry.getKey());
if (fieldType == null) {
throw new IllegalStateException("Field [" + entry.getKey()
+ "] exists in the index but not in mappings");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ private GetResult innerGetLoadFromStoredFields(String type, String id, String[]

// put stored fields into result objects
if (!fieldVisitor.fields().isEmpty()) {
fieldVisitor.postProcess(mapperService);
fieldVisitor.postProcess(mapperService::fieldType, mapperService.documentMapper());
documentFields = new HashMap<>();
metadataFields = new HashMap<>();
for (Map.Entry<String, List<Object>> entry : fieldVisitor.fields().entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ private static Query newLegacyExistsQuery(QueryShardContext context, String fiel
}

private static Query newFieldExistsQuery(QueryShardContext context, String field) {
MappedFieldType fieldType = context.getMapperService().fieldType(field);
MappedFieldType fieldType = context.fieldMapper(field);
if (fieldType == null) {
// The field does not exist as a leaf but could be an object so
// check for an object mapper
Expand All @@ -211,7 +211,7 @@ private static Query newObjectFieldExistsQuery(QueryShardContext context, String
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
Collection<String> fields = context.simpleMatchToIndexNames(objField + ".*");
for (String field : fields) {
Query existsQuery = context.getMapperService().fieldType(field).existsQuery(context);
Query existsQuery = context.fieldMapper(field).existsQuery(context);
booleanQuery.add(existsQuery, Occur.SHOULD);
}
return new ConstantScoreQuery(booleanQuery.build());
Expand All @@ -223,7 +223,7 @@ private static Query newObjectFieldExistsQuery(QueryShardContext context, String
*/
private static Collection<String> getMappedField(QueryShardContext context, String fieldPattern) {
final FieldNamesFieldMapper.FieldNamesFieldType fieldNamesFieldType = (FieldNamesFieldMapper.FieldNamesFieldType) context
.getMapperService().fieldType(FieldNamesFieldMapper.NAME);
.fieldMapper(FieldNamesFieldMapper.NAME);

if (fieldNamesFieldType == null) {
// can only happen when no types exist, so no docs exist either
Expand All @@ -241,7 +241,7 @@ private static Collection<String> getMappedField(QueryShardContext context, Stri

if (fields.size() == 1) {
String field = fields.iterator().next();
MappedFieldType fieldType = context.getMapperService().fieldType(field);
MappedFieldType fieldType = context.fieldMapper(field);
if (fieldType == null) {
// The field does not exist as a leaf but could be an object so
// check for an object mapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,11 @@ protected void setupInnerHitsContext(QueryShardContext queryShardContext,
innerHitsContext.storedFieldsContext(innerHitBuilder.getStoredFieldsContext());
}
if (innerHitBuilder.getDocValueFields() != null) {
FetchDocValuesContext docValuesContext = FetchDocValuesContext.create(
queryShardContext.getMapperService(), innerHitBuilder.getDocValueFields());
FetchDocValuesContext docValuesContext = FetchDocValuesContext.create(queryShardContext::simpleMatchToIndexNames,
queryShardContext.getIndexSettings().getMaxDocvalueFields(), innerHitBuilder.getDocValueFields());
innerHitsContext.docValuesContext(docValuesContext);
}
if (innerHitBuilder.getFetchFields() != null) {
String indexName = queryShardContext.index().getName();
FetchFieldsContext fieldsContext = new FetchFieldsContext(innerHitBuilder.getFetchFields());
innerHitsContext.fetchFieldsContext(fieldsContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ protected Query doToQuery(QueryShardContext context) throws IOException {

// ToParentBlockJoinQuery requires that the inner query only matches documents
// in its child space
if (new NestedHelper(context.getMapperService()).mightMatchNonNestedDocs(innerQuery, path)) {
NestedHelper nestedHelper = new NestedHelper(context.getMapperService()::getObjectMapper, context.getMapperService()::fieldType);
if (nestedHelper.mightMatchNonNestedDocs(innerQuery, path)) {
innerQuery = Queries.filtered(innerQuery, nestedObjectMapper.nestedTypeFilter());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public Analyzer getSearchAnalyzer(MappedFieldType fieldType) {
if (fieldType.getTextSearchInfo().getSearchAnalyzer() != null) {
return fieldType.getTextSearchInfo().getSearchAnalyzer();
}
return getMapperService().searchAnalyzer();
return mapperService.searchAnalyzer();
}

/**
Expand All @@ -277,7 +277,7 @@ public Analyzer getSearchQuoteAnalyzer(MappedFieldType fieldType) {
if (fieldType.getTextSearchInfo().getSearchQuoteAnalyzer() != null) {
return fieldType.getTextSearchInfo().getSearchQuoteAnalyzer();
}
return getMapperService().searchQuoteAnalyzer();
return mapperService.searchQuoteAnalyzer();
}

public ValuesSourceRegistry getValuesSourceRegistry() {
Expand Down Expand Up @@ -324,7 +324,7 @@ public Collection<String> queryTypes() {
public SearchLookup lookup() {
if (this.lookup == null) {
this.lookup = new SearchLookup(
getMapperService(),
mapperService,
(fieldType, searchLookup) -> indexFieldDataService.apply(fieldType, fullyQualifiedIndex.getName(), searchLookup),
types
);
Expand All @@ -341,7 +341,7 @@ public SearchLookup newFetchLookup() {
* Real customization coming soon, I promise!
*/
return new SearchLookup(
getMapperService(),
mapperService,
(fieldType, searchLookup) -> indexFieldDataService.apply(fieldType, fullyQualifiedIndex.getName(), searchLookup),
types
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
* if the {@link FieldNamesFieldMapper} is enabled.
*/
final FieldNamesFieldMapper.FieldNamesFieldType fieldNamesFieldType =
(FieldNamesFieldMapper.FieldNamesFieldType) context.getMapperService().fieldType(FieldNamesFieldMapper.NAME);
(FieldNamesFieldMapper.FieldNamesFieldType) context.fieldMapper(FieldNamesFieldMapper.NAME);
if (fieldNamesFieldType == null) {
return new MatchNoDocsQuery("No mappings yet");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ protected int doHashCode() {

@Override
protected ScoreFunction doToFunction(QueryShardContext context) {
MappedFieldType fieldType = context.getMapperService().fieldType(field);
MappedFieldType fieldType = context.fieldMapper(field);
IndexNumericFieldData fieldData = null;
if (fieldType == null) {
if(missing == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ protected ScoreFunction doToFunction(QueryShardContext context) {
} else {
final MappedFieldType fieldType;
if (field != null) {
fieldType = context.getMapperService().fieldType(field);
fieldType = context.fieldMapper(field);
} else {
deprecationLogger.deprecate("seed_requires_field",
"As of version 7.0 Elasticsearch will require that a [field] parameter is provided when a [seed] is set");
fieldType = context.getMapperService().fieldType(IdFieldMapper.NAME);
fieldType = context.fieldMapper(IdFieldMapper.NAME);
}
if (fieldType == null) {
if (context.getMapperService().documentMapper() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.apache.lucene.index.PrefixCodedTerms;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.ConstantScoreQuery;
Expand All @@ -31,18 +32,21 @@
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermInSetQuery;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.BooleanClause.Occur;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.ObjectMapper;

import java.util.function.Function;

/** Utility class to filter parent and children clauses when building nested
* queries. */
public final class NestedHelper {

private final MapperService mapperService;
private final Function<String, ObjectMapper> objectMapperLookup;
private final Function<String, MappedFieldType> fieldTypeLookup;

public NestedHelper(MapperService mapperService) {
this.mapperService = mapperService;
public NestedHelper(Function<String, ObjectMapper> objectMapperLookup, Function<String, MappedFieldType> fieldTypeLookup) {
this.objectMapperLookup = objectMapperLookup;
this.fieldTypeLookup = fieldTypeLookup;
}

/** Returns true if the given query might match nested documents. */
Expand Down Expand Up @@ -103,12 +107,12 @@ boolean mightMatchNestedDocs(String field) {
// we might add a nested filter when it is nor required.
return true;
}
if (mapperService.fieldType(field) == null) {
if (fieldTypeLookup.apply(field) == null) {
// field does not exist
return false;
}
for (String parent = parentObject(field); parent != null; parent = parentObject(parent)) {
ObjectMapper mapper = mapperService.getObjectMapper(parent);
ObjectMapper mapper = objectMapperLookup.apply(parent);
if (mapper != null && mapper.nested().isNested()) {
return true;
}
Expand Down Expand Up @@ -172,11 +176,11 @@ boolean mightMatchNonNestedDocs(String field, String nestedPath) {
// we might add a nested filter when it is nor required.
return true;
}
if (mapperService.fieldType(field) == null) {
if (fieldTypeLookup.apply(field) == null) {
return false;
}
for (String parent = parentObject(field); parent != null; parent = parentObject(parent)) {
ObjectMapper mapper = mapperService.getObjectMapper(parent);
ObjectMapper mapper = objectMapperLookup.apply(parent);
if (mapper!= null && mapper.nested().isNested()) {
if (mapper.fullPath().equals(nestedPath)) {
// If the mapper does not include in its parent or in the root object then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ static Map<String, Float> resolveMappingField(QueryShardContext context, String
fieldName = fieldName + fieldSuffix;
}

MappedFieldType fieldType = context.getMapperService().fieldType(fieldName);
MappedFieldType fieldType = context.fieldMapper(fieldName);
if (fieldType == null) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ private Query getPossiblyAnalyzedPrefixQuery(String field, String termStr, Mappe

private Query existsQuery(String fieldName) {
final FieldNamesFieldMapper.FieldNamesFieldType fieldNamesFieldType =
(FieldNamesFieldMapper.FieldNamesFieldType) context.getMapperService().fieldType(FieldNamesFieldMapper.NAME);
(FieldNamesFieldMapper.FieldNamesFieldType) context.fieldMapper(FieldNamesFieldMapper.NAME);
if (fieldNamesFieldType == null) {
return new MatchNoDocsQuery("No mappings yet");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,10 @@ public Query buildFilteredQuery(Query query) {
filters.add(typeFilter);
}

NestedHelper nestedHelper = new NestedHelper(mapperService()::getObjectMapper, mapperService()::fieldType);
if (mapperService().hasNested()
&& new NestedHelper(mapperService()).mightMatchNestedDocs(query)
&& (aliasFilter == null || new NestedHelper(mapperService()).mightMatchNestedDocs(aliasFilter))) {
&& nestedHelper.mightMatchNestedDocs(query)
&& (aliasFilter == null || nestedHelper.mightMatchNestedDocs(aliasFilter))) {
filters.add(Queries.newNonNestedFilter(mapperService().getIndexSettings().getIndexVersionCreated()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,8 @@ private void parseSource(DefaultSearchContext context, SearchSourceBuilder sourc
context.fetchSourceContext(source.fetchSource());
}
if (source.docValueFields() != null) {
FetchDocValuesContext docValuesContext = FetchDocValuesContext.create(context.mapperService(), source.docValueFields());
FetchDocValuesContext docValuesContext = FetchDocValuesContext.create(context.mapperService()::simpleMatchToFullName,
context.mapperService().getIndexSettings().getMaxDocvalueFields(), source.docValueFields());
context.docValuesContext(docValuesContext);
}
if (source.fetchFields() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ public BucketCardinality bucketCardinality() {
protected TopHitsAggregatorFactory doBuild(QueryShardContext queryShardContext, AggregatorFactory parent, Builder subfactoriesBuilder)
throws IOException {
long innerResultWindow = from() + size();
int maxInnerResultWindow = queryShardContext.getMapperService().getIndexSettings().getMaxInnerResultWindow();
int maxInnerResultWindow = queryShardContext.getIndexSettings().getMaxInnerResultWindow();
if (innerResultWindow > maxInnerResultWindow) {
throw new IllegalArgumentException(
"Top hits result window is too large, the top hits aggregator [" + name + "]'s from + size must be less " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ public Aggregator createInternal(SearchContext searchContext,
subSearchContext.storedFieldsContext(storedFieldsContext);
}
if (docValueFields != null) {
FetchDocValuesContext docValuesContext = FetchDocValuesContext.create(searchContext.mapperService(), docValueFields);
FetchDocValuesContext docValuesContext = FetchDocValuesContext.create(searchContext.mapperService()::simpleMatchToFullName,
searchContext.mapperService().getIndexSettings().getMaxDocvalueFields(), docValueFields);
subSearchContext.docValuesContext(docValuesContext);
}
if (fetchFields != null) {
Expand Down
Loading