Add factory methods for common value fetchers. (#63438)

This PR adds factory methods for the most common implementations:
* `SourceValueFetcher.identity` to pass through the source value untouched.
* `SourceValueFetcher.toString` to simply convert the source value to a string.
This commit is contained in:
Julie Tibshirani 2020-10-08 11:58:36 -07:00
parent a506705569
commit ae2fc4118d
13 changed files with 42 additions and 95 deletions

View File

@ -90,15 +90,7 @@ public class RankFeaturesFieldMapper extends ParametrizedFieldMapper {
@Override
public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) {
if (format != null) {
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats.");
}
return new SourceValueFetcher(name(), mapperService) {
@Override
protected Object parseSourceValue(Object value) {
return value;
}
};
return SourceValueFetcher.identity(name(), mapperService, format);
}
@Override

View File

@ -224,15 +224,7 @@ public final class ParentJoinFieldMapper extends FieldMapper {
@Override
public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) {
if (format != null) {
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats.");
}
return new SourceValueFetcher(name(), mapperService) {
@Override
protected Object parseSourceValue(Object value) {
return value;
}
};
return SourceValueFetcher.identity(name(), mapperService, format);
}
@Override

View File

@ -226,15 +226,7 @@ public class PercolatorFieldMapper extends ParametrizedFieldMapper {
@Override
public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) {
if (format != null) {
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats.");
}
return new SourceValueFetcher(name(), mapperService) {
@Override
protected Object parseSourceValue(Object value) {
return value;
}
};
return SourceValueFetcher.identity(name(), mapperService, format);
}
Query percolateQuery(String name, PercolateQuery.QueryStore queryStore, List<BytesReference> documents,

View File

@ -108,15 +108,7 @@ public class Murmur3FieldMapper extends ParametrizedFieldMapper {
@Override
public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) {
if (format != null) {
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats.");
}
return new SourceValueFetcher(name(), mapperService) {
@Override
protected String parseSourceValue(Object value) {
return value.toString();
}
};
return SourceValueFetcher.toString(name(), mapperService, format);
}
@Override

View File

@ -100,15 +100,7 @@ public class BinaryFieldMapper extends ParametrizedFieldMapper {
@Override
public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) {
if (format != null) {
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats.");
}
return new SourceValueFetcher(name(), mapperService) {
@Override
protected Object parseSourceValue(Object value) {
return value;
}
};
return SourceValueFetcher.identity(name(), mapperService, format);
}
@Override

View File

@ -87,4 +87,34 @@ public abstract class SourceValueFetcher implements ValueFetcher {
* {@link FieldMapper#parseCreateField} or {@link FieldMapper#parse}.
*/
protected abstract Object parseSourceValue(Object value);
/**
* Creates a {@link SourceValueFetcher} that passes through source values unmodified.
*/
public static SourceValueFetcher identity(String fieldName, MapperService mapperService, String format) {
if (format != null) {
throw new IllegalArgumentException("Field [" + fieldName + "] doesn't support formats.");
}
return new SourceValueFetcher(fieldName, mapperService) {
@Override
protected Object parseSourceValue(Object value) {
return value;
}
};
}
/**
* Creates a {@link SourceValueFetcher} that converts source values to strings.
*/
public static SourceValueFetcher toString(String fieldName, MapperService mapperService, String format) {
if (format != null) {
throw new IllegalArgumentException("Field [" + fieldName + "] doesn't support formats.");
}
return new SourceValueFetcher(fieldName, mapperService) {
@Override
protected Object parseSourceValue(Object value) {
return value.toString();
}
};
}
}

View File

@ -690,15 +690,7 @@ public class TextFieldMapper extends ParametrizedFieldMapper {
@Override
public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) {
if (format != null) {
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats.");
}
return new SourceValueFetcher(name(), mapperService) {
@Override
protected Object parseSourceValue(Object value) {
return value.toString();
}
};
return SourceValueFetcher.toString(name(), mapperService, format);
}
@Override

View File

@ -108,12 +108,7 @@ public class ExternalMapper extends ParametrizedFieldMapper {
@Override
public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) {
return new SourceValueFetcher(name(), mapperService) {
@Override
protected Object parseSourceValue(Object value) {
return value;
}
};
return SourceValueFetcher.identity(name(), mapperService, format);
}
}

View File

@ -78,12 +78,7 @@ public class FakeStringFieldMapper extends ParametrizedFieldMapper {
@Override
public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) {
return new SourceValueFetcher(name(), mapperService) {
@Override
protected String parseSourceValue(Object value) {
return value.toString();
}
};
return SourceValueFetcher.toString(name(), mapperService, format);
}
}

View File

@ -138,15 +138,7 @@ public class HistogramFieldMapper extends ParametrizedFieldMapper {
@Override
public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) {
if (format != null) {
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats.");
}
return new SourceValueFetcher(name(), mapperService) {
@Override
protected Object parseSourceValue(Object value) {
return value;
}
};
return SourceValueFetcher.identity(name(), mapperService, format);
}
@Override

View File

@ -474,15 +474,7 @@ public final class FlatObjectFieldMapper extends DynamicKeyFieldMapper {
@Override
public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) {
if (format != null) {
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats.");
}
return new SourceValueFetcher(name(), mapperService) {
@Override
protected Object parseSourceValue(Object value) {
return value;
}
};
return SourceValueFetcher.identity(name(), mapperService, format);
}
}

View File

@ -134,16 +134,7 @@ public class VersionStringFieldMapper extends ParametrizedFieldMapper {
@Override
public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) {
if (format != null) {
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats.");
}
return new SourceValueFetcher(name(), mapperService, null) {
@Override
protected String parseSourceValue(Object value) {
return value.toString();
}
};
return SourceValueFetcher.toString(name(), mapperService, format);
}
@Override

View File

@ -29,6 +29,6 @@ public class VersionStringFieldTypeTests extends FieldTypeTestCase {
assertEquals(Collections.singletonList("true"), fetchSourceValue(mapper, true));
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> fetchSourceValue(mapper, "value", "format"));
assertEquals("Field [field] of type [version] doesn't support formats.", e.getMessage());
assertEquals("Field [field] doesn't support formats.", e.getMessage());
}
}