-
Notifications
You must be signed in to change notification settings - Fork 25.3k
[ML-Dataframe] Feature/fib multi aggs and sources #34525
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
hendrikmuhs
merged 5 commits into
elastic:feature/fib
from
hendrikmuhs:feature/fib-multi-aggs
Oct 19, 2018
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4f61ebb
support multiple sources and aggregations
63c9247
extract result parsing into a separate class
d0284f3
simplify test by removing unnecessary aggregations
50669ca
address review comments
f331658
add some documentation/notes on inner design
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
.../main/java/org/elasticsearch/xpack/ml/featureindexbuilder/job/AggregationResultUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.ml.featureindexbuilder.job; | ||
|
||
import org.apache.log4j.Logger; | ||
import org.elasticsearch.search.aggregations.Aggregation; | ||
import org.elasticsearch.search.aggregations.AggregationBuilder; | ||
import org.elasticsearch.search.aggregations.bucket.composite.CompositeAggregation; | ||
import org.elasticsearch.search.aggregations.bucket.composite.CompositeValuesSourceBuilder; | ||
import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation; | ||
import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation.SingleValue; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
final class AggregationResultUtils { | ||
private static final Logger logger = Logger.getLogger(AggregationResultUtils.class.getName()); | ||
|
||
/** | ||
* Extracts aggregation results from a composite aggregation and puts it into a map. | ||
* | ||
* @param agg The aggregation result | ||
* @param sources The original sources used for querying | ||
* @param aggregationBuilders the aggregation used for querying | ||
* @return a map containing the results of the aggregation in a consumable way | ||
*/ | ||
public static Stream<Map<String, Object>> extractCompositeAggregationResults(CompositeAggregation agg, | ||
List<CompositeValuesSourceBuilder<?>> sources, Collection<AggregationBuilder> aggregationBuilders) { | ||
return agg.getBuckets().stream().map(bucket -> { | ||
Map<String, Object> document = new HashMap<>(); | ||
for (CompositeValuesSourceBuilder<?> source : sources) { | ||
String destinationFieldName = source.name(); | ||
document.put(destinationFieldName, bucket.getKey().get(destinationFieldName)); | ||
} | ||
for (AggregationBuilder aggregationBuilder : aggregationBuilders) { | ||
String aggName = aggregationBuilder.getName(); | ||
|
||
// TODO: support other aggregation types | ||
Aggregation aggResult = bucket.getAggregations().get(aggName); | ||
|
||
if (aggResult instanceof NumericMetricsAggregation.SingleValue) { | ||
NumericMetricsAggregation.SingleValue aggResultSingleValue = (SingleValue) aggResult; | ||
document.put(aggName, aggResultSingleValue.value()); | ||
} else { | ||
// Execution should never reach this point! | ||
// Creating jobs with unsupported aggregations shall not be possible | ||
logger.error("Dataframe Internal Error: unsupported aggregation ["+ aggResult.getName() +"], ignoring"); | ||
assert false; | ||
} | ||
} | ||
return document; | ||
}); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,22 +17,23 @@ | |
import org.elasticsearch.search.aggregations.bucket.composite.CompositeAggregation; | ||
import org.elasticsearch.search.aggregations.bucket.composite.CompositeAggregationBuilder; | ||
import org.elasticsearch.search.aggregations.bucket.composite.CompositeValuesSourceBuilder; | ||
import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation; | ||
import org.elasticsearch.search.builder.SearchSourceBuilder; | ||
import org.elasticsearch.xpack.core.indexing.AsyncTwoPhaseIndexer; | ||
import org.elasticsearch.xpack.core.indexing.IndexerState; | ||
import org.elasticsearch.xpack.core.indexing.IterationResult; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import static org.elasticsearch.xpack.core.ml.job.persistence.ElasticsearchMappings.DOC_TYPE; | ||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; | ||
import static org.elasticsearch.xpack.core.ml.job.persistence.ElasticsearchMappings.DOC_TYPE; | ||
|
||
public abstract class FeatureIndexBuilderIndexer extends AsyncTwoPhaseIndexer<Map<String, Object>, FeatureIndexBuilderJobStats> { | ||
|
||
|
@@ -58,36 +59,37 @@ protected void onStartJob(long now) { | |
@Override | ||
protected IterationResult<Map<String, Object>> doProcess(SearchResponse searchResponse) { | ||
final CompositeAggregation agg = searchResponse.getAggregations().get("feature"); | ||
return new IterationResult<>(processBuckets(agg), agg.afterKey(), agg.getBuckets().isEmpty()); | ||
return new IterationResult<>(processBucketsToIndexRequests(agg).collect(Collectors.toList()), agg.afterKey(), | ||
agg.getBuckets().isEmpty()); | ||
} | ||
|
||
/* | ||
* Mocked demo case | ||
* Parses the result and creates a stream of indexable documents | ||
* | ||
* TODO: replace with proper implementation | ||
* Implementation decisions: | ||
* | ||
* Extraction uses generic maps as intermediate exchange format in order to hook in ingest pipelines/processors | ||
* in later versions, see {@link IngestDocument). | ||
*/ | ||
private List<IndexRequest> processBuckets(CompositeAggregation agg) { | ||
// for now only 1 source supported | ||
String destinationFieldName = job.getConfig().getSourceConfig().getSources().get(0).name(); | ||
String aggName = job.getConfig().getAggregationConfig().getAggregatorFactories().iterator().next().getName(); | ||
private Stream<IndexRequest> processBucketsToIndexRequests(CompositeAggregation agg) { | ||
String indexName = job.getConfig().getDestinationIndex(); | ||
List<CompositeValuesSourceBuilder<?>> sources = job.getConfig().getSourceConfig().getSources(); | ||
Collection<AggregationBuilder> aggregationBuilders = job.getConfig().getAggregationConfig().getAggregatorFactories(); | ||
|
||
return agg.getBuckets().stream().map(b -> { | ||
NumericMetricsAggregation.SingleValue aggResult = b.getAggregations().get(aggName); | ||
return AggregationResultUtils.extractCompositeAggregationResults(agg, sources, aggregationBuilders).map(document -> { | ||
XContentBuilder builder; | ||
try { | ||
builder = jsonBuilder(); | ||
builder.startObject(); | ||
builder.field(destinationFieldName, b.getKey().get(destinationFieldName)); | ||
builder.field(aggName, aggResult.value()); | ||
builder.map(document); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned above, I think whatever object we return should know how to take a builder and add itself appropriately. |
||
builder.endObject(); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
|
||
String indexName = job.getConfig().getDestinationIndex(); | ||
IndexRequest request = new IndexRequest(indexName, DOC_TYPE).source(builder); | ||
return request; | ||
}).collect(Collectors.toList()); | ||
}); | ||
} | ||
|
||
@Override | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like we are needlessly creating a leaky/incomplete abstraction. We should return a stream of objects that knows how to transform themselves into an
XContentObject
instead of creating aHashMap
and then requiring the caller to know how to do the transformation (even thought the caller is us). This will afford us a nice separation of duties and an OK abstraction, though definitely not perfect as aggregations are so flexible.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you elaborate on what is leaky/incomplete?
Using a map is intentional, will add some reasoning as documentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to me that all of this building could be self contained. As it stands, we have multiple functions passing around not well defined objects and just happen to know the inner workings of it. Maybe that is just how aggregations are, but seems to me a very easy way to introduce latent bugs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To answer the questions:
We have no schema for the output, it could be anything, having that said, this is an internal helper class, not exposed to the outside world. Before it goes out
XContent::map
ensures it is proper.Regarding using maps as intermediate format: I checked for a more high-level construct and it turns out, there isn't one. The concept of a document in ingest is
Map<String, Object>
, so I choose the same here. Eventually we want to support running post-pivot ingest transformations. So this allows us to easily hook in ingest. (LBNL the map makes testing easier, but that isn't the primary reason.)