if (script == null) { @SuppressWarnings("unchecked") ValuesSourceConfig<VS> config = new ValuesSourceConfig<>(ValuesSourceType.ANY); config.format(resolveFormat(null, valueType, timeZone)); return config; ValuesSourceConfig<VS> config = new ValuesSourceConfig<VS>(valuesSourceType); config.missing(missing); config.timezone(timeZone); config.format(resolveFormat(format, valueType, timeZone)); config.script(createScript(script, context)); config.scriptValueType(valueType); return config; if (fieldType == null) { ValuesSourceType valuesSourceType = valueType != null ? valueType.getValuesSourceType() : ValuesSourceType.ANY; ValuesSourceConfig<VS> config = new ValuesSourceConfig<>(valuesSourceType); config.missing(missing); config.timezone(timeZone); config.format(resolveFormat(format, valueType, timeZone)); config.unmapped(true); if (valueType != null) { config.scriptValueType(valueType); if (valueType == null) { if (indexFieldData instanceof IndexNumericFieldData) { config = new ValuesSourceConfig<>(ValuesSourceType.NUMERIC); } else if (indexFieldData instanceof IndexGeoPointFieldData) {
@Override protected CompositeValuesSourceConfig innerBuild(SearchContext context, ValuesSourceConfig<?> config) throws IOException { ValuesSource orig = config.toValuesSource(context.getQueryShardContext()); if (orig == null) { orig = ValuesSource.Numeric.EMPTY; } if (orig instanceof ValuesSource.Numeric) { ValuesSource.Numeric numeric = (ValuesSource.Numeric) orig; final HistogramValuesSource vs = new HistogramValuesSource(numeric, interval); final MappedFieldType fieldType = config.fieldContext() != null ? config.fieldContext().fieldType() : null; return new CompositeValuesSourceConfig(name, fieldType, vs, config.format(), order(), missingBucket(), missing()); } else { throw new IllegalArgumentException("invalid source, expected numeric, got " + orig.getClass().getSimpleName()); } } }
if (!valid()) { throw new IllegalStateException( "value source config is invalid; must have either a field context or a script or marked as unwrapped"); if (unmapped()) { if (missing() == null) { } else if (valueSourceType() == ValuesSourceType.NUMERIC) { vs = (VS) ValuesSource.Numeric.EMPTY; } else if (valueSourceType() == ValuesSourceType.GEOPOINT) { vs = (VS) ValuesSource.GeoPoint.EMPTY; } else if (valueSourceType() == ValuesSourceType.ANY || valueSourceType() == ValuesSourceType.BYTES) { vs = (VS) ValuesSource.Bytes.WithOrdinals.EMPTY; } else { throw new IllegalArgumentException("Can't deal with unmapped ValuesSource type " + valueSourceType()); vs = originalValuesSource(); if (missing() == null) { return vs; final BytesRef missing = format.parseBytesRef(missing().toString()); if (vs instanceof ValuesSource.Bytes.WithOrdinals) { return (VS) MissingValues.replaceMissing((ValuesSource.Bytes.WithOrdinals) vs, missing); Number missing = format.parseDouble(missing().toString(), false, context::nowInMillis); return (VS) MissingValues.replaceMissing((ValuesSource.Numeric) vs, missing); } else if (vs instanceof ValuesSource.GeoPoint) {
/** * Return the original values source, before we apply `missing`. */ private VS originalValuesSource() throws IOException { if (fieldContext() == null) { if (valueSourceType() == ValuesSourceType.NUMERIC) { return (VS) numericScript(); } if (valueSourceType() == ValuesSourceType.BYTES) { return (VS) bytesScript(); } throw new AggregationExecutionException("value source of type [" + valueSourceType().name() + "] is not supported by scripts"); } if (valueSourceType() == ValuesSourceType.NUMERIC) { return (VS) numericField(); } if (valueSourceType() == ValuesSourceType.GEOPOINT) { return (VS) geoPointField(); } // falling back to bytes values return (VS) bytesField(); }
private ValuesSource.Numeric numericField() throws IOException { if (!(fieldContext().indexFieldData() instanceof IndexNumericFieldData)) { throw new IllegalArgumentException("Expected numeric type on field [" + fieldContext().field() + "], but got [" + fieldContext().fieldType().typeName() + "]"); } ValuesSource.Numeric dataSource = new ValuesSource.Numeric.FieldData((IndexNumericFieldData)fieldContext().indexFieldData()); if (script() != null) { dataSource = new ValuesSource.Numeric.WithScript(dataSource, script()); } return dataSource; }
public SignificantTermsAggregatorFactory(String name, ValuesSourceConfig<ValuesSource> config, IncludeExclude includeExclude, String executionHint, QueryBuilder filterBuilder, TermsAggregator.BucketCountThresholds bucketCountThresholds, SignificanceHeuristic significanceHeuristic, SearchContext context, AggregatorFactory<?> parent, AggregatorFactories.Builder subFactoriesBuilder, Map<String, Object> metaData) throws IOException { super(name, config, context, parent, subFactoriesBuilder, metaData); if (!config.unmapped()) { this.fieldType = config.fieldContext().fieldType(); this.indexedFieldName = fieldType.name(); } this.includeExclude = includeExclude; this.executionHint = executionHint; this.filter = filterBuilder == null ? null : filterBuilder.toFilter(context.getQueryShardContext()); IndexSearcher searcher = context.searcher(); this.supersetNumDocs = filter == null // Important - need to use the doc count that includes deleted docs // or we have this issue: https://github.com/elastic/elasticsearch/issues/7951 ? searcher.getIndexReader().maxDoc() : searcher.count(filter); this.bucketCountThresholds = bucketCountThresholds; this.significanceHeuristic = significanceHeuristic; }
ValuesSourceConfig<VS> config = new ValuesSourceConfig<>(ValuesSourceType.ANY); return config.format(resolveFormat(null, valueType)); ValuesSourceConfig<VS> config = new ValuesSourceConfig<>(valuesSourceType); config.missing(missingMap.get(field)); return config.format(resolveFormat(format, valueType)); if (fieldType == null) { ValuesSourceType valuesSourceType = valueType != null ? valueType.getValuesSourceType() : this.valuesSourceType; ValuesSourceConfig<VS> config = new ValuesSourceConfig<>(valuesSourceType); config.missing(missingMap.get(field)); config.format(resolveFormat(format, valueType)); return config.unmapped(true); if (valuesSourceType == ValuesSourceType.ANY) { if (indexFieldData instanceof IndexNumericFieldData) { config = new ValuesSourceConfig<>(ValuesSourceType.NUMERIC); } else if (indexFieldData instanceof IndexGeoPointFieldData) { config = new ValuesSourceConfig<>(ValuesSourceType.GEOPOINT); } else { config = new ValuesSourceConfig<>(ValuesSourceType.BYTES); config = new ValuesSourceConfig<>(valuesSourceType); config.fieldContext(new FieldContext(field, indexFieldData, fieldType)); config.missing(missingMap.get(field)); return config.format(fieldType.docValueFormat(format, null));
@Override public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException { ValuesSourceConfig<ValuesSource.Bytes> config = new ValuesSourceConfig<>(ValuesSource.Bytes.class); config.unmapped(true); return new TopKAggregator.Factory(aggregationName, config, size, capacity); config.fieldContext(new FieldContext(field, context.fieldData().getForField(mapper), mapper)); return new TopKAggregator.Factory(aggregationName, config, size, capacity);
MaxAggregator(String name, ValuesSourceConfig<ValuesSource.Numeric> config, ValuesSource.Numeric valuesSource, SearchContext context, Aggregator parent, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) throws IOException { super(name, context, parent, pipelineAggregators, metaData); this.valuesSource = valuesSource; if (valuesSource != null) { maxes = context.bigArrays().newDoubleArray(1, false); maxes.fill(0, maxes.size(), Double.NEGATIVE_INFINITY); } this.formatter = config.format(); this.pointConverter = getPointReaderOrNull(context, parent, config); if (pointConverter != null) { pointField = config.fieldContext().field(); } else { pointField = null; } }
@Override protected Aggregator doCreateInternal(ValuesSource.Numeric valuesSource, Aggregator parent, boolean collectsFromSingleBucket, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) throws IOException { return new ExtendedStatsAggregator(name, valuesSource, config.format(), context, parent, sigma, pipelineAggregators, metaData); } }
@Override protected ValuesSourceConfig<WithOrdinals> resolveConfig(SearchContext context) { ValuesSourceConfig<WithOrdinals> config = new ValuesSourceConfig<>(ValuesSourceType.BYTES); joinFieldResolveConfig(context, config); return config; }
private ValuesSource.GeoPoint geoPointField() throws IOException { if (!(fieldContext().indexFieldData() instanceof IndexGeoPointFieldData)) { throw new IllegalArgumentException("Expected geo_point type on field [" + fieldContext().field() + "], but got [" + fieldContext().fieldType().typeName() + "]"); } return new ValuesSource.GeoPoint.Fielddata((IndexGeoPointFieldData) fieldContext().indexFieldData()); } }
public NumericMultiValuesSource(Map<String, ValuesSourceConfig<ValuesSource.Numeric>> valuesSourceConfigs, QueryShardContext context) throws IOException { values = new HashMap<>(valuesSourceConfigs.size()); for (Map.Entry<String, ValuesSourceConfig<ValuesSource.Numeric>> entry : valuesSourceConfigs.entrySet()) { values.put(entry.getKey(), entry.getValue().toValuesSource(context)); } }
public final CompositeValuesSourceConfig build(SearchContext context) throws IOException { ValuesSourceConfig<?> config = ValuesSourceConfig.resolve(context.getQueryShardContext(), valueType, field, script, missing, null, format); if (config.unmapped() && field != null && missing == null && missingBucket == false) { // this source cannot produce any values so we refuse to build // since composite buckets are not created on null values by default. throw new QueryShardException(context.getQueryShardContext(), "failed to find field [" + field + "] and [missing_bucket] is not set"); } if (missingBucket && missing != null) { throw new QueryShardException(context.getQueryShardContext(), "cannot use [missing] option in conjunction with [missing_bucket]"); } return innerBuild(context, config); } }
@Override public void doValidate() { if (config == null || !config.valid()) { resolveValuesSourceConfigFromAncestors(name, parent, config.valueSourceType()); } }
private void resolveValuesSourceConfigFromAncestors(String aggName, AggregatorFactory parent, Class<VS> requiredValuesSourceType) { ValuesSourceConfig config; while (parent != null) { if (parent instanceof ValuesSourceAggregatorFactory) { config = ((ValuesSourceAggregatorFactory) parent).config; if (config != null && config.valid()) { if (requiredValuesSourceType == null || requiredValuesSourceType.isAssignableFrom(config.valueSourceType)) { ValueFormat format = config.format; this.config = config; // if the user explicitly defined a format pattern, we'll do our best to keep it even when we inherit the // value source form one of the ancestor aggregations if (this.config.formatPattern != null && format != null && format instanceof ValueFormat.Patternable) { this.config.format = ((ValueFormat.Patternable) format).create(this.config.formatPattern); } return; } } } parent = parent.parent(); } throw new AggregationExecutionException("could not find the appropriate value context to perform aggregation [" + aggName + "]"); } }
private void setFieldInfo(SearchContext context) { if (!config.unmapped()) { this.indexedFieldName = config.fieldContext().field(); fieldType = context.smartNameFieldType(indexedFieldName); } }
/** * Return the original values source, before we apply `missing`. */ private VS originalValuesSource() throws IOException { if (fieldContext() == null) { if (valueSourceType() == ValuesSourceType.NUMERIC) { return (VS) numericScript(); } if (valueSourceType() == ValuesSourceType.BYTES) { return (VS) bytesScript(); } throw new AggregationExecutionException("value source of type [" + valueSourceType().name() + "] is not supported by scripts"); } if (valueSourceType() == ValuesSourceType.NUMERIC) { return (VS) numericField(); } if (valueSourceType() == ValuesSourceType.GEOPOINT) { return (VS) geoPointField(); } // falling back to bytes values return (VS) bytesField(); }
ValuesSourceConfig<VS> config = new ValuesSourceConfig<>(ValuesSourceType.ANY); return config.format(resolveFormat(null, valueType)); ValuesSourceConfig<VS> config = new ValuesSourceConfig<>(valuesSourceType); config.missing(missingMap.get(field)); return config.format(resolveFormat(format, valueType)); if (fieldType == null) { ValuesSourceType valuesSourceType = valueType != null ? valueType.getValuesSourceType() : this.valuesSourceType; ValuesSourceConfig<VS> config = new ValuesSourceConfig<>(valuesSourceType); config.missing(missingMap.get(field)); config.format(resolveFormat(format, valueType)); return config.unmapped(true); if (valuesSourceType == ValuesSourceType.ANY) { if (indexFieldData instanceof IndexNumericFieldData) { config = new ValuesSourceConfig<>(ValuesSourceType.NUMERIC); } else if (indexFieldData instanceof IndexGeoPointFieldData) { config = new ValuesSourceConfig<>(ValuesSourceType.GEOPOINT); } else { config = new ValuesSourceConfig<>(ValuesSourceType.BYTES); config = new ValuesSourceConfig<>(valuesSourceType); config.fieldContext(new FieldContext(field, indexFieldData, fieldType)); config.missing(missingMap.get(field)); return config.format(fieldType.docValueFormat(format, null));
ValuesSourceConfig<ValuesSource.Bytes.WithOrdinals.ParentChild> config = new ValuesSourceConfig<>(ValuesSource.Bytes.WithOrdinals.ParentChild.class); DocumentMapper childDocMapper = context.mapperService().documentMapper(childType); childFilter = childDocMapper.typeFilter(); ParentChildIndexFieldData parentChildIndexFieldData = context.fieldData().getForField(parentFieldMapper.fieldType()); config.fieldContext(new FieldContext(parentFieldMapper.fieldType().names().indexName(), parentChildIndexFieldData, parentFieldMapper.fieldType())); } else { config.unmapped(true); config.unmapped(true);