Skip to content

Rename QueryShardContext#fieldMapper to getFieldType #63399

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
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 @@ -27,10 +27,10 @@
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.RankFeatureFieldMapper.RankFeatureFieldType;
import org.elasticsearch.index.mapper.RankFeatureMetaFieldMapper;
import org.elasticsearch.index.mapper.RankFeaturesFieldMapper.RankFeaturesFieldType;
import org.elasticsearch.index.mapper.MappedFieldType;

import java.io.IOException;
import java.util.Arrays;
Expand Down Expand Up @@ -337,7 +337,7 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
final MappedFieldType ft = context.fieldMapper(field);
final MappedFieldType ft = context.getFieldType(field);

if (ft instanceof RankFeatureFieldType) {
final RankFeatureFieldType fft = (RankFeatureFieldType) ft;
Expand All @@ -346,7 +346,7 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
final int lastDotIndex = field.lastIndexOf('.');
if (lastDotIndex != -1) {
final String parentField = field.substring(0, lastDotIndex);
final MappedFieldType parentFt = context.fieldMapper(parentField);
final MappedFieldType parentFt = context.getFieldType(parentField);
if (parentFt instanceof RankFeaturesFieldType) {
return scoreFunction.toQuery(parentField, field.substring(lastDotIndex + 1), true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
throw new IllegalStateException("no document to percolate");
}

MappedFieldType fieldType = context.fieldMapper(field);
MappedFieldType fieldType = context.getFieldType(field);
if (fieldType == null) {
throw new QueryShardException(context, "field [" + field + "] does not exist");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void testStoringQueryBuilders() throws IOException {
when(queryShardContext.getXContentRegistry()).thenReturn(xContentRegistry());
when(queryShardContext.getForField(fieldMapper.fieldType()))
.thenReturn(new BytesBinaryIndexFieldData(fieldMapper.name(), CoreValuesSourceType.BYTES));
when(queryShardContext.fieldMapper(Mockito.anyString())).thenAnswer(invocation -> {
when(queryShardContext.getFieldType(Mockito.anyString())).thenAnswer(invocation -> {
final String fieldName = (String) invocation.getArguments()[0];
return new KeywordFieldMapper.KeywordFieldType(fieldName);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.fielddata.IndexFieldData;
import org.elasticsearch.index.fielddata.LeafFieldData;
import org.elasticsearch.index.fielddata.LeafNumericFieldData;
import org.elasticsearch.index.fielddata.IndexFieldData;
import org.elasticsearch.index.fielddata.SortedNumericDoubleValues;
import org.elasticsearch.index.query.QueryRewriteContext;
import org.elasticsearch.index.query.QueryShardContext;
Expand Down Expand Up @@ -109,7 +109,7 @@ public static ExampleRescoreBuilder fromXContent(XContentParser parser) {
@Override
public RescoreContext innerBuildContext(int windowSize, QueryShardContext context) throws IOException {
IndexFieldData<?> factorField =
this.factorField == null ? null : context.getForField(context.fieldMapper(this.factorField));
this.factorField == null ? null : context.getForField(context.getFieldType(this.factorField));
return new ExampleRescoreContext(windowSize, factor, factorField);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ protected Query doToQuery(QueryShardContext context) {
if (shape == null || supplier != null) {
throw new UnsupportedOperationException("query must be rewritten first");
}
final MappedFieldType fieldType = context.fieldMapper(fieldName);
final MappedFieldType fieldType = context.getFieldType(fieldName);
if (fieldType == null) {
if (ignoreUnmapped) {
return new MatchNoDocsQuery();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public String getWriteableName() {

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
MappedFieldType fieldType = context.fieldMapper(field);
MappedFieldType fieldType = context.getFieldType(field);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find some of these usages suspicious: do they really mean to check if the returned field type is null after the unmapped fields handling (unmapped fields may get mapped as text), or would they rather need to call isFieldMapped ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure these should be calling isFieldMapped. We should maybe think about moving the unmapped field handling out of QueryShardContext and have it at the callsites that really need it? It's a bit too magic otherwise.

if (fieldType == null) {
return Queries.newMatchNoDocsQuery("Can't run [" + NAME + "] query on unmapped fields!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public static Query newFilter(QueryShardContext context, String fieldPattern, bo

private static Query newFieldExistsQuery(QueryShardContext context, String field) {
if (context.isFieldMapped(field)) {
Query filter = context.fieldMapper(field).existsQuery(context);
Query filter = context.getFieldType(field).existsQuery(context);
return new ConstantScoreQuery(filter);
} else {
// The field does not exist as a leaf but could be an object so
Expand All @@ -181,7 +181,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.fieldMapper(field).existsQuery(context);
Query existsQuery = context.getFieldType(field).existsQuery(context);
booleanQuery.add(existsQuery, Occur.SHOULD);
}
return new ConstantScoreQuery(booleanQuery.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public static FieldMaskingSpanQueryBuilder fromXContent(XContentParser parser) t
@Override
protected SpanQuery doToQuery(QueryShardContext context) throws IOException {
String fieldInQuery = fieldName;
MappedFieldType fieldType = context.fieldMapper(fieldName);
MappedFieldType fieldType = context.getFieldType(fieldName);
if (fieldType != null) {
fieldInQuery = fieldType.name();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically everywhere that does this null check should be calling isMapped instead.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public String getWriteableName() {
protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException {
QueryShardContext context = queryRewriteContext.convertToShardContext();
if (context != null) {
MappedFieldType fieldType = context.fieldMapper(fieldName);
MappedFieldType fieldType = context.getFieldType(fieldName);
if (fieldType == null) {
return new MatchNoneQueryBuilder();
}
Expand All @@ -334,7 +334,7 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
MappedFieldType fieldType = context.fieldMapper(fieldName);
MappedFieldType fieldType = context.getFieldType(fieldName);
if (fieldType == null) {
throw new IllegalStateException("Rewrite first");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ QueryValidationException checkLatLon() {

@Override
public Query doToQuery(QueryShardContext context) {
MappedFieldType fieldType = context.fieldMapper(fieldName);
MappedFieldType fieldType = context.getFieldType(fieldName);
if (fieldType == null) {
if (ignoreUnmapped) {
return new MatchNoDocsQuery();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public boolean ignoreUnmapped() {

@Override
protected Query doToQuery(QueryShardContext shardContext) throws IOException {
MappedFieldType fieldType = shardContext.fieldMapper(fieldName);
MappedFieldType fieldType = shardContext.getFieldType(fieldName);
if (fieldType == null) {
if (ignoreUnmapped) {
return new MatchNoDocsQuery();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public boolean ignoreUnmapped() {

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
MappedFieldType fieldType = context.fieldMapper(fieldName);
MappedFieldType fieldType = context.getFieldType(fieldName);
if (fieldType == null) {
if (ignoreUnmapped) {
return new MatchNoDocsQuery();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws
return new MatchNoneQueryBuilder();
}
QueryShardContext context = queryRewriteContext.convertToShardContext();
if (context != null && context.fieldMapper(IdFieldMapper.NAME) == null) {
if (context != null && context.getFieldType(IdFieldMapper.NAME) == null) {
// no mappings yet
return new MatchNoneQueryBuilder();
}
Expand All @@ -149,7 +149,7 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
MappedFieldType idField = context.fieldMapper(IdFieldMapper.NAME);
MappedFieldType idField = context.getFieldType(IdFieldMapper.NAME);
if (idField == null || ids.isEmpty()) {
throw new IllegalStateException("Rewrite first");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

package org.elasticsearch.index.query;

import org.apache.lucene.queries.intervals.IntervalQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.queries.intervals.IntervalQuery;
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
Expand Down Expand Up @@ -132,15 +132,15 @@ public static IntervalQueryBuilder fromXContent(XContentParser parser) throws IO

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
MappedFieldType fieldType = context.fieldMapper(field);
MappedFieldType fieldType = context.getFieldType(field);
if (fieldType == null) {
// Be lenient with unmapped fields so that cross-index search will work nicely
return new MatchNoDocsQuery();
}
Set<String> maskedFields = new HashSet<>();
sourceProvider.extractFields(maskedFields);
for (String maskedField : maskedFields) {
MappedFieldType ft = context.fieldMapper(maskedField);
MappedFieldType ft = context.getFieldType(maskedField);
if (ft == null) {
// Be lenient with unmapped fields so that cross-index search will work nicely
return new MatchNoDocsQuery();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public IntervalsSource getSource(QueryShardContext context, MappedFieldType fiel
}
IntervalsSource source;
if (useField != null) {
fieldType = context.fieldMapper(useField);
fieldType = context.getFieldType(useField);
assert fieldType != null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These assertions look dodgy (I think I put them in in the first place, so I'm allowed to say that). It should be throwing an error if passed a field that doesn't exist or doesn't support text searches.

source = Intervals.fixField(useField, fieldType.intervals(query, maxGaps, ordered, analyzer, false));
}
Expand Down Expand Up @@ -530,7 +530,7 @@ public IntervalsSource getSource(QueryShardContext context, MappedFieldType fiel
}
IntervalsSource source;
if (useField != null) {
fieldType = context.fieldMapper(useField);
fieldType = context.getFieldType(useField);
assert fieldType != null;
source = Intervals.fixField(useField, fieldType.intervals(prefix, 0, false, analyzer, true));
}
Expand Down Expand Up @@ -645,7 +645,7 @@ public IntervalsSource getSource(QueryShardContext context, MappedFieldType fiel
}
IntervalsSource source;
if (useField != null) {
fieldType = context.fieldMapper(useField);
fieldType = context.getFieldType(useField);
assert fieldType != null;
checkPositions(fieldType);
if (this.analyzer == null) {
Expand Down Expand Up @@ -782,7 +782,7 @@ public IntervalsSource getSource(QueryShardContext context, MappedFieldType fiel
}
IntervalsSource source;
if (useField != null) {
fieldType = context.fieldMapper(useField);
fieldType = context.getFieldType(useField);
assert fieldType != null;
checkPositions(fieldType);
if (this.analyzer == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
}
} else {
for (String field : fields) {
MappedFieldType fieldType = context.fieldMapper(field);
MappedFieldType fieldType = context.getFieldType(field);
if (fieldType != null && SUPPORTED_FIELD_TYPES.contains(fieldType.getClass()) == false) {
if (failOnUnsupportedField) {
throw new IllegalArgumentException("more_like_this only supports text/keyword fields: [" + field + "]");
Expand Down Expand Up @@ -1106,7 +1106,7 @@ private static void checkRoutingMissingException(MultiTermVectorsItemResponse re
}

private static void handleExclude(BooleanQuery.Builder boolQuery, Item[] likeItems, QueryShardContext context) {
MappedFieldType idField = context.fieldMapper(IdFieldMapper.NAME);
MappedFieldType idField = context.getFieldType(IdFieldMapper.NAME);
if (idField == null) {
// no mappings, nothing to exclude
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.ConstantFieldType;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.query.support.QueryParsers;

import java.io.IOException;
Expand All @@ -51,11 +51,11 @@ public class PrefixQueryBuilder extends AbstractQueryBuilder<PrefixQueryBuilder>
private final String fieldName;

private final String value;

public static final boolean DEFAULT_CASE_INSENSITIVITY = false;
private static final ParseField CASE_INSENSITIVE_FIELD = new ParseField("case_insensitive");
private boolean caseInsensitive = DEFAULT_CASE_INSENSITIVITY;


private String rewrite;

Expand Down Expand Up @@ -86,7 +86,7 @@ public PrefixQueryBuilder(StreamInput in) throws IOException {
rewrite = in.readOptionalString();
if (in.getVersion().onOrAfter(Version.V_7_10_0)) {
caseInsensitive = in.readBoolean();
}
}
}

@Override
Expand All @@ -96,7 +96,7 @@ protected void doWriteTo(StreamOutput out) throws IOException {
out.writeOptionalString(rewrite);
if (out.getVersion().onOrAfter(Version.V_7_10_0)) {
out.writeBoolean(caseInsensitive);
}
}
}

@Override
Expand All @@ -107,18 +107,18 @@ public String fieldName() {
public String value() {
return this.value;
}

public PrefixQueryBuilder caseInsensitive(boolean caseInsensitive) {
if (caseInsensitive == false) {
throw new IllegalArgumentException("The case insensitive setting cannot be set to false.");
}
this.caseInsensitive = caseInsensitive;
return this;
}
}

public boolean caseInsensitive() {
return this.caseInsensitive;
}
}

public PrefixQueryBuilder rewrite(String rewrite) {
this.rewrite = rewrite;
Expand Down Expand Up @@ -152,7 +152,7 @@ public static PrefixQueryBuilder fromXContent(XContentParser parser) throws IOEx

String queryName = null;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
boolean caseInsensitive = DEFAULT_CASE_INSENSITIVITY;
boolean caseInsensitive = DEFAULT_CASE_INSENSITIVITY;
String currentFieldName = null;
XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
Expand Down Expand Up @@ -197,7 +197,7 @@ public static PrefixQueryBuilder fromXContent(XContentParser parser) throws IOEx
.boost(boost)
.queryName(queryName);
if (caseInsensitive) {
result.caseInsensitive(caseInsensitive);
result.caseInsensitive(caseInsensitive);
}
return result;
}
Expand All @@ -206,12 +206,12 @@ public static PrefixQueryBuilder fromXContent(XContentParser parser) throws IOEx
public String getWriteableName() {
return NAME;
}

@Override
protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException {
QueryShardContext context = queryRewriteContext.convertToShardContext();
if (context != null) {
MappedFieldType fieldType = context.fieldMapper(this.fieldName);
MappedFieldType fieldType = context.getFieldType(this.fieldName);
if (fieldType == null) {
return new MatchNoneQueryBuilder();
} else if (fieldType instanceof ConstantFieldType) {
Expand All @@ -230,13 +230,13 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws
}

return super.doRewrite(queryRewriteContext);
}
}

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
MultiTermQuery.RewriteMethod method = QueryParsers.parseRewriteMethod(rewrite, null, LoggingDeprecationHandler.INSTANCE);

MappedFieldType fieldType = context.fieldMapper(fieldName);
MappedFieldType fieldType = context.getFieldType(fieldName);
if (fieldType == null) {
throw new IllegalStateException("Rewrite first");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public Set<String> simpleMatchToIndexNames(String pattern) {
* @see QueryShardContext#setAllowUnmappedFields(boolean)
* @see QueryShardContext#setMapUnmappedFieldAsString(boolean)
*/
public MappedFieldType fieldMapper(String name) {
public MappedFieldType getFieldType(String name) {
return failIfFieldMappingNotFound(name, mapperService.fieldType(name));
}

Expand Down
Loading