Skip to content

Commit 1fdae75

Browse files
committed
Mappings: Make index level mapping apis use MappedFieldType
The MapperService is the "index wide view" of mappings. Methods on it are used at query time to lookup how to query a field. This change reduces the exposed api so that any information returned is limited to that api exposed by MappedFieldType. In the future, MappedFieldType will be guaranteed to be the same across all document types for a given field. Note CompletionFieldType needed some more settings moved to it. Other than that, this change is almost purely cosmetic.
1 parent 9787266 commit 1fdae75

File tree

104 files changed

+766
-694
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+766
-694
lines changed

core/src/main/java/org/apache/lucene/queries/ExtendedCommonTermsQuery.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,12 @@
1919

2020
package org.apache.lucene.queries;
2121

22-
import org.apache.lucene.index.IndexReader;
2322
import org.apache.lucene.index.Term;
2423
import org.apache.lucene.index.TermContext;
25-
import org.apache.lucene.search.*;
2624
import org.apache.lucene.search.BooleanClause.Occur;
25+
import org.apache.lucene.search.Query;
2726
import org.elasticsearch.common.lucene.search.Queries;
28-
import org.elasticsearch.index.mapper.FieldMapper;
29-
30-
import java.io.IOException;
27+
import org.elasticsearch.index.mapper.MappedFieldType;
3128

3229
/**
3330
* Extended version of {@link CommonTermsQuery} that allows to pass in a
@@ -36,11 +33,11 @@
3633
*/
3734
public class ExtendedCommonTermsQuery extends CommonTermsQuery {
3835

39-
private final FieldMapper mapper;
36+
private final MappedFieldType fieldType;
4037

41-
public ExtendedCommonTermsQuery(Occur highFreqOccur, Occur lowFreqOccur, float maxTermFrequency, boolean disableCoord, FieldMapper mapper) {
38+
public ExtendedCommonTermsQuery(Occur highFreqOccur, Occur lowFreqOccur, float maxTermFrequency, boolean disableCoord, MappedFieldType fieldType) {
4239
super(highFreqOccur, lowFreqOccur, maxTermFrequency, disableCoord);
43-
this.mapper = mapper;
40+
this.fieldType = fieldType;
4441
}
4542

4643
private String lowFreqMinNumShouldMatchSpec;
@@ -81,10 +78,10 @@ public String getLowFreqMinimumNumberShouldMatchSpec() {
8178

8279
@Override
8380
protected Query newTermQuery(Term term, TermContext context) {
84-
if (mapper == null) {
81+
if (fieldType == null) {
8582
return super.newTermQuery(term, context);
8683
}
87-
final Query query = mapper.queryStringTermQuery(term);
84+
final Query query = fieldType.queryStringTermQuery(term);
8885
if (query == null) {
8986
return super.newTermQuery(term, context);
9087
} else {

core/src/main/java/org/apache/lucene/queryparser/classic/MapperQueryParser.java

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import com.google.common.base.Objects;
2323
import com.google.common.collect.ImmutableMap;
24-
2524
import org.apache.lucene.analysis.Analyzer;
2625
import org.apache.lucene.analysis.TokenStream;
2726
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
@@ -37,7 +36,7 @@
3736
import org.apache.lucene.util.automaton.RegExp;
3837
import org.elasticsearch.common.lucene.search.Queries;
3938
import org.elasticsearch.common.unit.Fuzziness;
40-
import org.elasticsearch.index.mapper.FieldMapper;
39+
import org.elasticsearch.index.mapper.MappedFieldType;
4140
import org.elasticsearch.index.mapper.MapperService;
4241
import org.elasticsearch.index.mapper.core.DateFieldMapper;
4342
import org.elasticsearch.index.query.QueryParseContext;
@@ -77,7 +76,7 @@ public class MapperQueryParser extends QueryParser {
7776
private boolean forcedAnalyzer;
7877
private boolean forcedQuoteAnalyzer;
7978

80-
private FieldMapper currentMapper;
79+
private MappedFieldType currentFieldType;
8180

8281
private boolean analyzeWildcard;
8382

@@ -148,8 +147,8 @@ Query handleBareFuzzy(String qfield, Token fuzzySlop, String termImage) throws P
148147

149148
@Override
150149
protected Query newTermQuery(Term term) {
151-
if (currentMapper != null) {
152-
Query termQuery = currentMapper.queryStringTermQuery(term);
150+
if (currentFieldType != null) {
151+
Query termQuery = currentFieldType.queryStringTermQuery(term);
153152
if (termQuery != null) {
154153
return termQuery;
155154
}
@@ -224,33 +223,33 @@ private Query getFieldQuerySingle(String field, String queryText, boolean quoted
224223
return getRangeQuerySingle(field, null, queryText.substring(1), true, false);
225224
}
226225
}
227-
currentMapper = null;
226+
currentFieldType = null;
228227
Analyzer oldAnalyzer = getAnalyzer();
229228
try {
230229
if (quoted) {
231230
setAnalyzer(quoteAnalyzer);
232231
if (quoteFieldSuffix != null) {
233-
currentMapper = parseContext.fieldMapper(field + quoteFieldSuffix);
232+
currentFieldType = parseContext.fieldMapper(field + quoteFieldSuffix);
234233
}
235234
}
236-
if (currentMapper == null) {
237-
currentMapper = parseContext.fieldMapper(field);
235+
if (currentFieldType == null) {
236+
currentFieldType = parseContext.fieldMapper(field);
238237
}
239-
if (currentMapper != null) {
238+
if (currentFieldType != null) {
240239
if (quoted) {
241240
if (!forcedQuoteAnalyzer) {
242-
setAnalyzer(parseContext.getSearchQuoteAnalyzer(currentMapper));
241+
setAnalyzer(parseContext.getSearchQuoteAnalyzer(currentFieldType));
243242
}
244243
} else {
245244
if (!forcedAnalyzer) {
246-
setAnalyzer(parseContext.getSearchAnalyzer(currentMapper));
245+
setAnalyzer(parseContext.getSearchAnalyzer(currentFieldType));
247246
}
248247
}
249-
if (currentMapper != null) {
248+
if (currentFieldType != null) {
250249
Query query = null;
251-
if (currentMapper.useTermQueryWithQueryString()) {
250+
if (currentFieldType.useTermQueryWithQueryString()) {
252251
try {
253-
query = currentMapper.termQuery(queryText, parseContext);
252+
query = currentFieldType.termQuery(queryText, parseContext);
254253
} catch (RuntimeException e) {
255254
if (settings.lenient()) {
256255
return null;
@@ -260,7 +259,7 @@ private Query getFieldQuerySingle(String field, String queryText, boolean quoted
260259
}
261260
}
262261
if (query == null) {
263-
query = super.getFieldQuery(currentMapper.fieldType().names().indexName(), queryText, quoted);
262+
query = super.getFieldQuery(currentFieldType.names().indexName(), queryText, quoted);
264263
}
265264
return query;
266265
}
@@ -361,20 +360,20 @@ protected Query getRangeQuery(String field, String part1, String part2, boolean
361360
}
362361

363362
private Query getRangeQuerySingle(String field, String part1, String part2, boolean startInclusive, boolean endInclusive) {
364-
currentMapper = parseContext.fieldMapper(field);
365-
if (currentMapper != null) {
366-
if (lowercaseExpandedTerms && !currentMapper.isNumeric()) {
363+
currentFieldType = parseContext.fieldMapper(field);
364+
if (currentFieldType != null) {
365+
if (lowercaseExpandedTerms && !currentFieldType.isNumeric()) {
367366
part1 = part1 == null ? null : part1.toLowerCase(locale);
368367
part2 = part2 == null ? null : part2.toLowerCase(locale);
369368
}
370369

371370
try {
372371
Query rangeQuery;
373-
if (currentMapper instanceof DateFieldMapper && settings.timeZone() != null) {
374-
DateFieldMapper dateFieldMapper = (DateFieldMapper) this.currentMapper;
375-
rangeQuery = dateFieldMapper.fieldType().rangeQuery(part1, part2, startInclusive, endInclusive, settings.timeZone(), null, parseContext);
372+
if (currentFieldType instanceof DateFieldMapper.DateFieldType && settings.timeZone() != null) {
373+
DateFieldMapper.DateFieldType dateFieldType = (DateFieldMapper.DateFieldType) this.currentFieldType;
374+
rangeQuery = dateFieldType.rangeQuery(part1, part2, startInclusive, endInclusive, settings.timeZone(), null, parseContext);
376375
} else {
377-
rangeQuery = currentMapper.rangeQuery(part1, part2, startInclusive, endInclusive, parseContext);
376+
rangeQuery = currentFieldType.rangeQuery(part1, part2, startInclusive, endInclusive, parseContext);
378377
}
379378
return rangeQuery;
380379
} catch (RuntimeException e) {
@@ -426,11 +425,11 @@ protected Query getFuzzyQuery(String field, String termStr, String minSimilarity
426425
}
427426

428427
private Query getFuzzyQuerySingle(String field, String termStr, String minSimilarity) throws ParseException {
429-
currentMapper = parseContext.fieldMapper(field);
430-
if (currentMapper!= null) {
428+
currentFieldType = parseContext.fieldMapper(field);
429+
if (currentFieldType != null) {
431430
try {
432431
//LUCENE 4 UPGRADE I disabled transpositions here by default - maybe this needs to be changed
433-
return currentMapper.fuzzyQuery(termStr, Fuzziness.build(minSimilarity), fuzzyPrefixLength, settings.fuzzyMaxExpansions(), false);
432+
return currentFieldType.fuzzyQuery(termStr, Fuzziness.build(minSimilarity), fuzzyPrefixLength, settings.fuzzyMaxExpansions(), false);
434433
} catch (RuntimeException e) {
435434
if (settings.lenient()) {
436435
return null;
@@ -495,20 +494,20 @@ protected Query getPrefixQuery(String field, String termStr) throws ParseExcepti
495494
}
496495

497496
private Query getPrefixQuerySingle(String field, String termStr) throws ParseException {
498-
currentMapper = null;
497+
currentFieldType = null;
499498
Analyzer oldAnalyzer = getAnalyzer();
500499
try {
501-
currentMapper = parseContext.fieldMapper(field);
502-
if (currentMapper != null) {
500+
currentFieldType = parseContext.fieldMapper(field);
501+
if (currentFieldType != null) {
503502
if (!forcedAnalyzer) {
504-
setAnalyzer(parseContext.getSearchAnalyzer(currentMapper));
503+
setAnalyzer(parseContext.getSearchAnalyzer(currentFieldType));
505504
}
506505
Query query = null;
507-
if (currentMapper.useTermQueryWithQueryString()) {
508-
query = currentMapper.prefixQuery(termStr, multiTermRewriteMethod, parseContext);
506+
if (currentFieldType.useTermQueryWithQueryString()) {
507+
query = currentFieldType.prefixQuery(termStr, multiTermRewriteMethod, parseContext);
509508
}
510509
if (query == null) {
511-
query = getPossiblyAnalyzedPrefixQuery(currentMapper.fieldType().names().indexName(), termStr);
510+
query = getPossiblyAnalyzedPrefixQuery(currentFieldType.names().indexName(), termStr);
512511
}
513512
return query;
514513
}
@@ -636,15 +635,15 @@ protected Query getWildcardQuery(String field, String termStr) throws ParseExcep
636635

637636
private Query getWildcardQuerySingle(String field, String termStr) throws ParseException {
638637
String indexedNameField = field;
639-
currentMapper = null;
638+
currentFieldType = null;
640639
Analyzer oldAnalyzer = getAnalyzer();
641640
try {
642-
currentMapper = parseContext.fieldMapper(field);
643-
if (currentMapper != null) {
641+
currentFieldType = parseContext.fieldMapper(field);
642+
if (currentFieldType != null) {
644643
if (!forcedAnalyzer) {
645-
setAnalyzer(parseContext.getSearchAnalyzer(currentMapper));
644+
setAnalyzer(parseContext.getSearchAnalyzer(currentFieldType));
646645
}
647-
indexedNameField = currentMapper.fieldType().names().indexName();
646+
indexedNameField = currentFieldType.names().indexName();
648647
return getPossiblyAnalyzedWildcardQuery(indexedNameField, termStr);
649648
}
650649
return getPossiblyAnalyzedWildcardQuery(indexedNameField, termStr);
@@ -768,17 +767,17 @@ protected Query getRegexpQuery(String field, String termStr) throws ParseExcepti
768767
}
769768

770769
private Query getRegexpQuerySingle(String field, String termStr) throws ParseException {
771-
currentMapper = null;
770+
currentFieldType = null;
772771
Analyzer oldAnalyzer = getAnalyzer();
773772
try {
774-
currentMapper = parseContext.fieldMapper(field);
775-
if (currentMapper != null) {
773+
currentFieldType = parseContext.fieldMapper(field);
774+
if (currentFieldType != null) {
776775
if (!forcedAnalyzer) {
777-
setAnalyzer(parseContext.getSearchAnalyzer(currentMapper));
776+
setAnalyzer(parseContext.getSearchAnalyzer(currentFieldType));
778777
}
779778
Query query = null;
780-
if (currentMapper.useTermQueryWithQueryString()) {
781-
query = currentMapper.regexpQuery(termStr, RegExp.ALL, maxDeterminizedStates, multiTermRewriteMethod, parseContext);
779+
if (currentFieldType.useTermQueryWithQueryString()) {
780+
query = currentFieldType.regexpQuery(termStr, RegExp.ALL, maxDeterminizedStates, multiTermRewriteMethod, parseContext);
782781
}
783782
if (query == null) {
784783
query = super.getRegexpQuery(field, termStr);

core/src/main/java/org/elasticsearch/action/admin/indices/analyze/TransportAnalyzeAction.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.elasticsearch.common.settings.Settings;
4040
import org.elasticsearch.index.analysis.*;
4141
import org.elasticsearch.index.mapper.FieldMapper;
42+
import org.elasticsearch.index.mapper.MappedFieldType;
4243
import org.elasticsearch.index.mapper.internal.AllFieldMapper;
4344
import org.elasticsearch.index.IndexService;
4445
import org.elasticsearch.index.shard.ShardId;
@@ -108,13 +109,13 @@ protected AnalyzeResponse shardOperation(AnalyzeRequest request, ShardId shardId
108109
if (indexService == null) {
109110
throw new IllegalArgumentException("No index provided, and trying to analyzer based on a specific field which requires the index parameter");
110111
}
111-
FieldMapper fieldMapper = indexService.mapperService().smartNameFieldMapper(request.field());
112-
if (fieldMapper != null) {
113-
if (fieldMapper.isNumeric()) {
112+
MappedFieldType fieldType = indexService.mapperService().smartNameFieldType(request.field());
113+
if (fieldType != null) {
114+
if (fieldType.isNumeric()) {
114115
throw new IllegalArgumentException("Can't process field [" + request.field() + "], Analysis requests are not supported on numeric fields");
115116
}
116-
analyzer = fieldMapper.fieldType().indexAnalyzer();
117-
field = fieldMapper.fieldType().names().indexName();
117+
analyzer = fieldType.indexAnalyzer();
118+
field = fieldType.names().indexName();
118119

119120
}
120121
}

core/src/main/java/org/elasticsearch/action/fieldstats/TransportFieldStatsTransportAction.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.elasticsearch.index.IndexService;
4040
import org.elasticsearch.index.engine.Engine;
4141
import org.elasticsearch.index.mapper.FieldMapper;
42+
import org.elasticsearch.index.mapper.MappedFieldType;
4243
import org.elasticsearch.index.mapper.MapperService;
4344
import org.elasticsearch.index.shard.IndexShard;
4445
import org.elasticsearch.index.shard.ShardId;
@@ -135,12 +136,12 @@ protected FieldStatsShardResponse shardOperation(FieldStatsShardRequest request)
135136
shard.readAllowed();
136137
try (Engine.Searcher searcher = shard.acquireSearcher("fieldstats")) {
137138
for (String field : request.getFields()) {
138-
FieldMapper fieldMapper = mapperService.fullName(field);
139-
if (fieldMapper != null) {
139+
MappedFieldType fieldType = mapperService.fullName(field);
140+
if (fieldType != null) {
140141
IndexReader reader = searcher.reader();
141142
Terms terms = MultiFields.getTerms(reader, field);
142143
if (terms != null) {
143-
fieldStats.put(field, fieldMapper.stats(terms, reader.maxDoc()));
144+
fieldStats.put(field, fieldType.stats(terms, reader.maxDoc()));
144145
}
145146
} else {
146147
throw new IllegalArgumentException("field [" + field + "] doesn't exist");

core/src/main/java/org/elasticsearch/index/codec/PerFieldMappingPostingFormatCodec.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.elasticsearch.common.logging.ESLogger;
2727
import org.elasticsearch.common.lucene.Lucene;
2828
import org.elasticsearch.index.mapper.FieldMapper;
29+
import org.elasticsearch.index.mapper.MappedFieldType;
2930
import org.elasticsearch.index.mapper.MapperService;
3031
import org.elasticsearch.index.mapper.core.CompletionFieldMapper;
3132

@@ -54,14 +55,14 @@ public PerFieldMappingPostingFormatCodec(Lucene50StoredFieldsFormat.Mode compres
5455

5556
@Override
5657
public PostingsFormat getPostingsFormatForField(String field) {
57-
final FieldMapper indexName = mapperService.indexName(field);
58+
final MappedFieldType indexName = mapperService.indexName(field);
5859
if (indexName == null) {
5960
logger.warn("no index mapper found for field: [{}] returning default postings format", field);
60-
} else if (indexName instanceof CompletionFieldMapper) {
61+
} else if (indexName instanceof CompletionFieldMapper.CompletionFieldType) {
6162
// CompletionFieldMapper needs a special postings format
62-
final CompletionFieldMapper mapper = (CompletionFieldMapper) indexName;
63+
final CompletionFieldMapper.CompletionFieldType fieldType = (CompletionFieldMapper.CompletionFieldType) indexName;
6364
final PostingsFormat defaultFormat = super.getPostingsFormatForField(field);
64-
return mapper.postingsFormat(defaultFormat);
65+
return fieldType.postingsFormat(defaultFormat);
6566
}
6667
return super.getPostingsFormatForField(field);
6768
}

core/src/main/java/org/elasticsearch/index/fielddata/IndexFieldData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ protected final Object missingObject(Object missingValue, boolean reversed) {
229229

230230
interface Builder {
231231

232-
IndexFieldData<?> build(Index index, @IndexSettings Settings indexSettings, FieldMapper mapper, IndexFieldDataCache cache,
232+
IndexFieldData<?> build(Index index, @IndexSettings Settings indexSettings, MappedFieldType fieldType, IndexFieldDataCache cache,
233233
CircuitBreakerService breakerService, MapperService mapperService);
234234
}
235235

core/src/main/java/org/elasticsearch/index/fielddata/IndexFieldDataService.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,13 @@ public void onMappingUpdate() {
228228
}
229229

230230
@SuppressWarnings("unchecked")
231-
public <IFD extends IndexFieldData<?>> IFD getForField(FieldMapper mapper) {
232-
final Names fieldNames = mapper.fieldType().names();
233-
final FieldDataType type = mapper.fieldType().fieldDataType();
231+
public <IFD extends IndexFieldData<?>> IFD getForField(MappedFieldType fieldType) {
232+
final Names fieldNames = fieldType.names();
233+
final FieldDataType type = fieldType.fieldDataType();
234234
if (type == null) {
235235
throw new IllegalArgumentException("found no fielddata type for field [" + fieldNames.fullName() + "]");
236236
}
237-
final boolean docValues = mapper.fieldType().hasDocValues();
237+
final boolean docValues = fieldType.hasDocValues();
238238
final String key = fieldNames.indexName();
239239
IndexFieldData<?> fieldData = loadedFieldData.get(key);
240240
if (fieldData == null) {
@@ -279,7 +279,7 @@ public <IFD extends IndexFieldData<?>> IFD getForField(FieldMapper mapper) {
279279
fieldDataCaches.put(fieldNames.indexName(), cache);
280280
}
281281

282-
fieldData = builder.build(index, indexSettings, mapper, cache, circuitBreakerService, indexService.mapperService());
282+
fieldData = builder.build(index, indexSettings, fieldType, cache, circuitBreakerService, indexService.mapperService());
283283
loadedFieldData.put(fieldNames.indexName(), fieldData);
284284
}
285285
} finally {

0 commit comments

Comments
 (0)