Skip to content

Commit 8c73755

Browse files
authored
Remove QueryShardContext#getMapperService (elastic#63998)
We have been removing usages of getMapperService with the purpose of removing the method at some point. This is so we don't have to expose the entire MapperService to all of the users of QueryShardContext, but rather only specific methods so we can better control its usages, especially around resolving field types. This commit removes the last few usages and adds the missing methods to QueryShardContext. They all revolve around retrieving DocumentMapper from MapperService, yet we can expose specific methods without even exposing DocumentMapper, which would also cause issues.
1 parent 0bffbf0 commit 8c73755

File tree

5 files changed

+34
-28
lines changed

5 files changed

+34
-28
lines changed

modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@
6767
import org.elasticsearch.index.analysis.FieldNameAnalyzer;
6868
import org.elasticsearch.index.fielddata.IndexFieldData;
6969
import org.elasticsearch.index.fielddata.IndexFieldDataCache;
70-
import org.elasticsearch.index.mapper.DocumentMapper;
7170
import org.elasticsearch.index.mapper.MappedFieldType;
72-
import org.elasticsearch.index.mapper.MapperService;
7371
import org.elasticsearch.index.mapper.ParseContext;
7472
import org.elasticsearch.index.mapper.ParsedDocument;
7573
import org.elasticsearch.index.mapper.SourceToParse;
@@ -538,23 +536,21 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
538536
}
539537

540538
final List<ParsedDocument> docs = new ArrayList<>();
541-
final DocumentMapper docMapper;
542-
final MapperService mapperService = context.getMapperService();
543-
String type = mapperService.documentMapper().type();
539+
String type = context.getType();
544540
if (documentType != null) {
545541
deprecationLogger.deprecate("percolate_with_document_type", DOCUMENT_TYPE_DEPRECATION_MESSAGE);
546542
if (documentType.equals(type) == false) {
547543
throw new IllegalArgumentException("specified document_type [" + documentType +
548544
"] is not equal to the actual type [" + type + "]");
549545
}
550546
}
551-
docMapper = mapperService.documentMapper(type);
552547
for (BytesReference document : documents) {
553-
docs.add(docMapper.parse(new SourceToParse(context.index().getName(), type, "_temp_id", document, documentXContentType)));
548+
docs.add(context.parseDocument(type,
549+
new SourceToParse(context.index().getName(), type, "_temp_id", document, documentXContentType)));
554550
}
555551

556-
FieldNameAnalyzer fieldNameAnalyzer = (FieldNameAnalyzer)docMapper.mappers().indexAnalyzer();
557-
// Need to this custom impl because FieldNameAnalyzer is strict and the percolator sometimes isn't when
552+
FieldNameAnalyzer fieldNameAnalyzer = context.getFieldNameIndexAnalyzer();
553+
// We need this custom analyzer because FieldNameAnalyzer is strict and the percolator sometimes isn't when
558554
// 'index.percolator.map_unmapped_fields_as_string' is enabled:
559555
Analyzer analyzer = new DelegatingAnalyzerWrapper(Analyzer.PER_FIELD_REUSE_STRATEGY) {
560556
@Override
@@ -570,9 +566,9 @@ protected Analyzer getWrappedAnalyzer(String fieldName) {
570566
final IndexSearcher docSearcher;
571567
final boolean excludeNestedDocuments;
572568
if (docs.size() > 1 || docs.get(0).docs().size() > 1) {
573-
assert docs.size() != 1 || docMapper.hasNestedObjects();
569+
assert docs.size() != 1 || context.hasNested();
574570
docSearcher = createMultiDocumentSearcher(analyzer, docs);
575-
excludeNestedDocuments = docMapper.hasNestedObjects() && docs.stream()
571+
excludeNestedDocuments = context.hasNested() && docs.stream()
576572
.map(ParsedDocument::docs)
577573
.mapToInt(List::size)
578574
.anyMatch(size -> size > 1);

server/src/main/java/org/elasticsearch/index/mapper/MappingLookup.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public FieldTypeLookup fieldTypes() {
149149
* A smart analyzer used for indexing that takes into account specific analyzers configured
150150
* per {@link FieldMapper}.
151151
*/
152-
public Analyzer indexAnalyzer() {
152+
public FieldNameAnalyzer indexAnalyzer() {
153153
return this.indexAnalyzer;
154154
}
155155

server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.elasticsearch.index.Index;
4343
import org.elasticsearch.index.IndexSettings;
4444
import org.elasticsearch.index.IndexSortConfig;
45+
import org.elasticsearch.index.analysis.FieldNameAnalyzer;
4546
import org.elasticsearch.index.analysis.IndexAnalyzers;
4647
import org.elasticsearch.index.cache.bitset.BitsetFilterCache;
4748
import org.elasticsearch.index.fielddata.IndexFieldData;
@@ -50,8 +51,11 @@
5051
import org.elasticsearch.index.mapper.FieldMapper;
5152
import org.elasticsearch.index.mapper.MappedFieldType;
5253
import org.elasticsearch.index.mapper.Mapper;
54+
import org.elasticsearch.index.mapper.MapperParsingException;
5355
import org.elasticsearch.index.mapper.MapperService;
5456
import org.elasticsearch.index.mapper.ObjectMapper;
57+
import org.elasticsearch.index.mapper.ParsedDocument;
58+
import org.elasticsearch.index.mapper.SourceToParse;
5559
import org.elasticsearch.index.mapper.TextFieldMapper;
5660
import org.elasticsearch.index.query.support.NestedScope;
5761
import org.elasticsearch.index.similarity.SimilarityService;
@@ -233,6 +237,24 @@ public Map<String, Query> copyNamedQueries() {
233237
return unmodifiableMap(new HashMap<>(namedQueries));
234238
}
235239

240+
public ParsedDocument parseDocument(String type, SourceToParse source) throws MapperParsingException {
241+
DocumentMapper documentMapper = mapperService.documentMapper(type);
242+
return documentMapper == null ? null : mapperService.documentMapper().parse(source);
243+
}
244+
245+
public FieldNameAnalyzer getFieldNameIndexAnalyzer() {
246+
DocumentMapper documentMapper = mapperService.documentMapper();
247+
return documentMapper == null ? null : documentMapper.mappers().indexAnalyzer();
248+
}
249+
250+
public boolean hasNested() {
251+
return mapperService.hasNested();
252+
}
253+
254+
public boolean hasMappings() {
255+
return mapperService.documentMapper() != null;
256+
}
257+
236258
/**
237259
* Returns all the fields that match a given pattern. If prefixed with a
238260
* type then the fields will be returned with a type prefix.
@@ -510,13 +532,6 @@ public IndexSettings getIndexSettings() {
510532
return indexSettings;
511533
}
512534

513-
/**
514-
* Return the MapperService.
515-
*/
516-
public MapperService getMapperService() {
517-
return mapperService;
518-
}
519-
520535
public String getType() {
521536
return mapperService.documentMapper() == null ? null : mapperService.documentMapper().type();
522537
}

server/src/main/java/org/elasticsearch/index/query/functionscore/RandomScoreFunctionBuilder.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,19 +169,14 @@ protected ScoreFunction doToFunction(QueryShardContext context) {
169169
fieldName = field;
170170
}
171171
if (context.isFieldMapped(fieldName) == false) {
172-
if (context.getMapperService().documentMapper() == null) {
172+
if (context.hasMappings() == false) {
173173
// no mappings: the index is empty anyway
174174
return new RandomScoreFunction(hash(context.nowInMillis()), salt, null);
175175
}
176176
throw new IllegalArgumentException("Field [" + field + "] is not mapped on [" + context.index() +
177-
"] and cannot be used as a source of random numbers.");
178-
}
179-
int seed;
180-
if (this.seed == null) {
181-
seed = hash(context.nowInMillis());
182-
} else {
183-
seed = this.seed;
177+
"] and cannot be used as a source of random numbers.");
184178
}
179+
int seed = this.seed == null ? hash(context.nowInMillis()) : this.seed;
185180
return new RandomScoreFunction(seed, salt, context.getForField(context.getFieldType(fieldName)));
186181
}
187182
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/DocumentPermissions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ private static void buildRoleQuery(User user, ScriptService scriptService, Shard
122122
failIfQueryUsesClient(queryBuilder, queryShardContext);
123123
Query roleQuery = queryShardContext.toQuery(queryBuilder).query();
124124
filter.add(roleQuery, SHOULD);
125-
if (queryShardContext.getMapperService().hasNested()) {
125+
if (queryShardContext.hasNested()) {
126126
NestedHelper nestedHelper = new NestedHelper(queryShardContext::getObjectMapper, queryShardContext::isFieldMapped);
127127
if (nestedHelper.mightMatchNestedDocs(roleQuery)) {
128128
roleQuery = new BooleanQuery.Builder().add(roleQuery, FILTER)

0 commit comments

Comments
 (0)