set ValuesSourceConfig fields as private

This commit is contained in:
Nicholas Knize 2016-05-31 12:28:10 -05:00
parent 90b8f5d0d8
commit 54575e55ca
5 changed files with 83 additions and 65 deletions

View File

@ -61,29 +61,30 @@ public class AggregationContext {
assert config.valid() : "value source config is invalid - must have either a field context or a script or marked as unmapped"; assert config.valid() : "value source config is invalid - must have either a field context or a script or marked as unmapped";
final VS vs; final VS vs;
if (config.unmapped) { if (config.unmapped()) {
if (config.missing == null) { if (config.missing() == null) {
// otherwise we will have values because of the missing value // otherwise we will have values because of the missing value
vs = null; vs = null;
} else if (config.valueSourceType == ValuesSourceType.NUMERIC) { } else if (config.valueSourceType() == ValuesSourceType.NUMERIC) {
vs = (VS) ValuesSource.Numeric.EMPTY; vs = (VS) ValuesSource.Numeric.EMPTY;
} else if (config.valueSourceType == ValuesSourceType.GEOPOINT) { } else if (config.valueSourceType() == ValuesSourceType.GEOPOINT) {
vs = (VS) ValuesSource.GeoPoint.EMPTY; vs = (VS) ValuesSource.GeoPoint.EMPTY;
} else if (config.valueSourceType == ValuesSourceType.ANY || config.valueSourceType == ValuesSourceType.BYTES) { } else if (config.valueSourceType() == ValuesSourceType.ANY || config.valueSourceType() == ValuesSourceType.BYTES) {
vs = (VS) ValuesSource.Bytes.EMPTY; vs = (VS) ValuesSource.Bytes.EMPTY;
} else { } else {
throw new SearchParseException(searchContext, "Can't deal with unmapped ValuesSource type " + config.valueSourceType, null); throw new SearchParseException(searchContext, "Can't deal with unmapped ValuesSource type "
+ config.valueSourceType(), null);
} }
} else { } else {
vs = originalValuesSource(config); vs = originalValuesSource(config);
} }
if (config.missing == null) { if (config.missing() == null) {
return vs; return vs;
} }
if (vs instanceof ValuesSource.Bytes) { if (vs instanceof ValuesSource.Bytes) {
final BytesRef missing = new BytesRef(config.missing.toString()); final BytesRef missing = new BytesRef(config.missing().toString());
if (vs instanceof ValuesSource.Bytes.WithOrdinals) { if (vs instanceof ValuesSource.Bytes.WithOrdinals) {
return (VS) MissingValues.replaceMissing((ValuesSource.Bytes.WithOrdinals) vs, missing); return (VS) MissingValues.replaceMissing((ValuesSource.Bytes.WithOrdinals) vs, missing);
} else { } else {
@ -91,20 +92,20 @@ public class AggregationContext {
} }
} else if (vs instanceof ValuesSource.Numeric) { } else if (vs instanceof ValuesSource.Numeric) {
Number missing = null; Number missing = null;
if (config.missing instanceof Number) { if (config.missing() instanceof Number) {
missing = (Number) config.missing; missing = (Number) config.missing();
} else { } else {
if (config.fieldContext != null && config.fieldContext.fieldType() != null) { if (config.fieldContext() != null && config.fieldContext().fieldType() != null) {
missing = config.fieldContext.fieldType().docValueFormat(null, DateTimeZone.UTC) missing = config.fieldContext().fieldType().docValueFormat(null, DateTimeZone.UTC)
.parseDouble(config.missing.toString(), false, context.nowCallable()); .parseDouble(config.missing().toString(), false, context.nowCallable());
} else { } else {
missing = Double.parseDouble(config.missing.toString()); missing = Double.parseDouble(config.missing().toString());
} }
} }
return (VS) MissingValues.replaceMissing((ValuesSource.Numeric) vs, missing); return (VS) MissingValues.replaceMissing((ValuesSource.Numeric) vs, missing);
} else if (vs instanceof ValuesSource.GeoPoint) { } else if (vs instanceof ValuesSource.GeoPoint) {
// TODO: also support the structured formats of geo points // TODO: also support the structured formats of geo points
final GeoPoint missing = GeoUtils.parseGeoPoint(config.missing.toString(), new GeoPoint()); final GeoPoint missing = GeoUtils.parseGeoPoint(config.missing().toString(), new GeoPoint());
return (VS) MissingValues.replaceMissing((ValuesSource.GeoPoint) vs, missing); return (VS) MissingValues.replaceMissing((ValuesSource.GeoPoint) vs, missing);
} else { } else {
// Should not happen // Should not happen
@ -116,21 +117,21 @@ public class AggregationContext {
* Return the original values source, before we apply `missing`. * Return the original values source, before we apply `missing`.
*/ */
private <VS extends ValuesSource> VS originalValuesSource(ValuesSourceConfig<VS> config) throws IOException { private <VS extends ValuesSource> VS originalValuesSource(ValuesSourceConfig<VS> config) throws IOException {
if (config.fieldContext == null) { if (config.fieldContext() == null) {
if (config.valueSourceType == ValuesSourceType.NUMERIC) { if (config.valueSourceType() == ValuesSourceType.NUMERIC) {
return (VS) numericScript(config); return (VS) numericScript(config);
} }
if (config.valueSourceType == ValuesSourceType.BYTES) { if (config.valueSourceType() == ValuesSourceType.BYTES) {
return (VS) bytesScript(config); return (VS) bytesScript(config);
} }
throw new AggregationExecutionException("value source of type [" + config.valueSourceType.name() throw new AggregationExecutionException("value source of type [" + config.valueSourceType().name()
+ "] is not supported by scripts"); + "] is not supported by scripts");
} }
if (config.valueSourceType == ValuesSourceType.NUMERIC) { if (config.valueSourceType() == ValuesSourceType.NUMERIC) {
return (VS) numericField(config); return (VS) numericField(config);
} }
if (config.valueSourceType == ValuesSourceType.GEOPOINT) { if (config.valueSourceType() == ValuesSourceType.GEOPOINT) {
return (VS) geoPointField(config); return (VS) geoPointField(config);
} }
// falling back to bytes values // falling back to bytes values
@ -138,25 +139,25 @@ public class AggregationContext {
} }
private ValuesSource.Numeric numericScript(ValuesSourceConfig<?> config) throws IOException { private ValuesSource.Numeric numericScript(ValuesSourceConfig<?> config) throws IOException {
return new ValuesSource.Numeric.Script(config.script, config.scriptValueType); return new ValuesSource.Numeric.Script(config.script(), config.scriptValueType());
} }
private ValuesSource.Numeric numericField(ValuesSourceConfig<?> config) throws IOException { private ValuesSource.Numeric numericField(ValuesSourceConfig<?> config) throws IOException {
if (!(config.fieldContext.indexFieldData() instanceof IndexNumericFieldData)) { if (!(config.fieldContext().indexFieldData() instanceof IndexNumericFieldData)) {
throw new IllegalArgumentException("Expected numeric type on field [" + config.fieldContext.field() + throw new IllegalArgumentException("Expected numeric type on field [" + config.fieldContext().field() +
"], but got [" + config.fieldContext.fieldType().typeName() + "]"); "], but got [" + config.fieldContext().fieldType().typeName() + "]");
} }
ValuesSource.Numeric dataSource = new ValuesSource.Numeric.FieldData((IndexNumericFieldData) config.fieldContext.indexFieldData()); ValuesSource.Numeric dataSource = new ValuesSource.Numeric.FieldData((IndexNumericFieldData)config.fieldContext().indexFieldData());
if (config.script != null) { if (config.script() != null) {
dataSource = new ValuesSource.Numeric.WithScript(dataSource, config.script); dataSource = new ValuesSource.Numeric.WithScript(dataSource, config.script());
} }
return dataSource; return dataSource;
} }
private ValuesSource bytesField(ValuesSourceConfig<?> config) throws IOException { private ValuesSource bytesField(ValuesSourceConfig<?> config) throws IOException {
final IndexFieldData<?> indexFieldData = config.fieldContext.indexFieldData(); final IndexFieldData<?> indexFieldData = config.fieldContext().indexFieldData();
ValuesSource dataSource; ValuesSource dataSource;
if (indexFieldData instanceof ParentChildIndexFieldData) { if (indexFieldData instanceof ParentChildIndexFieldData) {
dataSource = new ValuesSource.Bytes.WithOrdinals.ParentChild((ParentChildIndexFieldData) indexFieldData); dataSource = new ValuesSource.Bytes.WithOrdinals.ParentChild((ParentChildIndexFieldData) indexFieldData);
@ -165,24 +166,24 @@ public class AggregationContext {
} else { } else {
dataSource = new ValuesSource.Bytes.FieldData(indexFieldData); dataSource = new ValuesSource.Bytes.FieldData(indexFieldData);
} }
if (config.script != null) { if (config.script() != null) {
dataSource = new ValuesSource.WithScript(dataSource, config.script); dataSource = new ValuesSource.WithScript(dataSource, config.script());
} }
return dataSource; return dataSource;
} }
private ValuesSource.Bytes bytesScript(ValuesSourceConfig<?> config) throws IOException { private ValuesSource.Bytes bytesScript(ValuesSourceConfig<?> config) throws IOException {
return new ValuesSource.Bytes.Script(config.script); return new ValuesSource.Bytes.Script(config.script());
} }
private ValuesSource.GeoPoint geoPointField(ValuesSourceConfig<?> config) throws IOException { private ValuesSource.GeoPoint geoPointField(ValuesSourceConfig<?> config) throws IOException {
if (!(config.fieldContext.indexFieldData() instanceof IndexGeoPointFieldData)) { if (!(config.fieldContext().indexFieldData() instanceof IndexGeoPointFieldData)) {
throw new IllegalArgumentException("Expected geo_point type on field [" + config.fieldContext.field() + throw new IllegalArgumentException("Expected geo_point type on field [" + config.fieldContext().field() +
"], but got [" + config.fieldContext.fieldType().typeName() + "]"); "], but got [" + config.fieldContext().fieldType().typeName() + "]");
} }
return new ValuesSource.GeoPoint.Fielddata((IndexGeoPointFieldData) config.fieldContext.indexFieldData()); return new ValuesSource.GeoPoint.Fielddata((IndexGeoPointFieldData) config.fieldContext().indexFieldData());
} }
} }

View File

@ -317,7 +317,7 @@ public abstract class ValuesSourceAggregationBuilder<VS extends ValuesSource, AB
if (script == null) { if (script == null) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
ValuesSourceConfig<VS> config = new ValuesSourceConfig(ValuesSourceType.ANY); ValuesSourceConfig<VS> config = new ValuesSourceConfig(ValuesSourceType.ANY);
config.format = resolveFormat(null, valueType); config.format(resolveFormat(null, valueType));
return config; return config;
} }
ValuesSourceType valuesSourceType = valueType != null ? valueType.getValuesSourceType() : this.valuesSourceType; ValuesSourceType valuesSourceType = valueType != null ? valueType.getValuesSourceType() : this.valuesSourceType;
@ -329,11 +329,11 @@ public abstract class ValuesSourceAggregationBuilder<VS extends ValuesSource, AB
valuesSourceType = ValuesSourceType.BYTES; valuesSourceType = ValuesSourceType.BYTES;
} }
ValuesSourceConfig<VS> config = new ValuesSourceConfig<VS>(valuesSourceType); ValuesSourceConfig<VS> config = new ValuesSourceConfig<VS>(valuesSourceType);
config.missing = missing; config.missing(missing);
config.timeZone = timeZone; config.timezone(timeZone);
config.format = resolveFormat(format, valueType); config.format(resolveFormat(format, valueType));
config.script = createScript(script, context.searchContext()); config.script(createScript(script, context.searchContext()));
config.scriptValueType = valueType; config.scriptValueType(valueType);
return config; return config;
} }
@ -341,13 +341,13 @@ public abstract class ValuesSourceAggregationBuilder<VS extends ValuesSource, AB
if (fieldType == null) { if (fieldType == null) {
ValuesSourceType valuesSourceType = valueType != null ? valueType.getValuesSourceType() : this.valuesSourceType; ValuesSourceType valuesSourceType = valueType != null ? valueType.getValuesSourceType() : this.valuesSourceType;
ValuesSourceConfig<VS> config = new ValuesSourceConfig<>(valuesSourceType); ValuesSourceConfig<VS> config = new ValuesSourceConfig<>(valuesSourceType);
config.missing = missing; config.missing(missing);
config.timeZone = timeZone; config.timezone(timeZone);
config.format = resolveFormat(format, valueType); config.format(resolveFormat(format, valueType));
config.unmapped = true; config.unmapped(true);
if (valueType != null) { if (valueType != null) {
// todo do we really need this for unmapped? // todo do we really need this for unmapped?
config.scriptValueType = valueType; config.scriptValueType(valueType);
} }
return config; return config;
} }
@ -367,11 +367,11 @@ public abstract class ValuesSourceAggregationBuilder<VS extends ValuesSource, AB
config = new ValuesSourceConfig(valuesSourceType); config = new ValuesSourceConfig(valuesSourceType);
} }
config.fieldContext = new FieldContext(field, indexFieldData, fieldType); config.fieldContext(new FieldContext(field, indexFieldData, fieldType));
config.missing = missing; config.missing(missing);
config.timeZone = timeZone; config.timezone(timeZone);
config.script = createScript(script, context.searchContext()); config.script(createScript(script, context.searchContext()));
config.format = fieldType.docValueFormat(format, timeZone); config.format(fieldType.docValueFormat(format, timeZone));
return config; return config;
} }

View File

@ -42,7 +42,7 @@ public abstract class ValuesSourceAggregatorFactory<VS extends ValuesSource, AF
} }
public DateTimeZone timeZone() { public DateTimeZone timeZone() {
return config.timeZone; return config.timezone();
} }
@Override @Override

View File

@ -27,15 +27,15 @@ import org.joda.time.DateTimeZone;
*/ */
public class ValuesSourceConfig<VS extends ValuesSource> { public class ValuesSourceConfig<VS extends ValuesSource> {
final ValuesSourceType valueSourceType; private final ValuesSourceType valueSourceType;
FieldContext fieldContext; private FieldContext fieldContext;
SearchScript script; private SearchScript script;
ValueType scriptValueType; private ValueType scriptValueType;
boolean unmapped = false; private boolean unmapped = false;
String formatPattern; private String formatPattern;
DocValueFormat format = DocValueFormat.RAW; private DocValueFormat format = DocValueFormat.RAW;
Object missing; private Object missing;
DateTimeZone timeZone; private DateTimeZone timeZone;
public ValuesSourceConfig(ValuesSourceType valueSourceType) { public ValuesSourceConfig(ValuesSourceType valueSourceType) {
this.valueSourceType = valueSourceType; this.valueSourceType = valueSourceType;
@ -71,6 +71,15 @@ public class ValuesSourceConfig<VS extends ValuesSource> {
return this; return this;
} }
public ValuesSourceConfig<VS> scriptValueType(ValueType scriptValueType) {
this.scriptValueType = scriptValueType;
return this;
}
public ValueType scriptValueType() {
return this.scriptValueType;
}
public ValuesSourceConfig<VS> unmapped(boolean unmapped) { public ValuesSourceConfig<VS> unmapped(boolean unmapped) {
this.unmapped = unmapped; this.unmapped = unmapped;
return this; return this;
@ -86,11 +95,19 @@ public class ValuesSourceConfig<VS extends ValuesSource> {
return this; return this;
} }
public Object missing() {
return this.missing;
}
public ValuesSourceConfig<VS> timezone(final DateTimeZone timeZone) { public ValuesSourceConfig<VS> timezone(final DateTimeZone timeZone) {
this.timeZone= timeZone; this.timeZone= timeZone;
return this; return this;
} }
public DateTimeZone timezone() {
return this.timeZone;
}
public DocValueFormat format() { public DocValueFormat format() {
return format; return format;
} }

View File

@ -30,7 +30,7 @@ import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.script.Script; import org.elasticsearch.script.Script;
import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.aggregations.AggregationInitializationException; import org.elasticsearch.search.aggregations.AggregationInitializationException;
import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.search.aggregations.AggregatorFactories;
import org.elasticsearch.search.aggregations.AggregatorFactories.Builder; import org.elasticsearch.search.aggregations.AggregatorFactories.Builder;
import org.elasticsearch.search.aggregations.AggregatorFactory; import org.elasticsearch.search.aggregations.AggregatorFactory;
@ -48,7 +48,7 @@ import java.util.Objects;
* *
*/ */
public abstract class MultiValuesSourceAggregationBuilder<VS extends ValuesSource, AB extends MultiValuesSourceAggregationBuilder<VS, AB>> public abstract class MultiValuesSourceAggregationBuilder<VS extends ValuesSource, AB extends MultiValuesSourceAggregationBuilder<VS, AB>>
extends AggregationBuilder<AB> { extends AbstractAggregationBuilder<AB> {
public static final ParseField MULTIVALUE_MODE_FIELD = new ParseField("mode"); public static final ParseField MULTIVALUE_MODE_FIELD = new ParseField("mode");