-
Notifications
You must be signed in to change notification settings - Fork 25k
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
Add WeightedAvg metric aggregation #31037
Merged
Merged
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
21ace7a
Add WeightedAvg aggregation
polyfractal 7a34911
Merge remote-tracking branch 'origin/master' into weighted_avg
polyfractal 2d89f4c
Use Kahan Summation for weights, use new multivalue mode, assert
polyfractal 35e3148
Merge remote-tracking branch 'origin/master' into weighted_avg
polyfractal f252493
More tests
polyfractal fe9dd45
Add Builder for MVSourceFieldConfig, tests
polyfractal b245071
Add Documentation
polyfractal b7acb87
checkstyle
polyfractal 7883d39
Review cleanup
polyfractal 7257c1b
Merge remote-tracking branch 'origin/master' into weighted_avg
polyfractal b501da4
Merge remote-tracking branch 'origin/master' into weighted_avg
polyfractal 3baf06f
Remove unecessary stream ctor, extend LeafOnly
polyfractal 3959bb4
Add clarification comment
polyfractal afbfee7
Remove MultiValueMode options and MultiValuesSourceConfig
polyfractal 177ff7e
Add example to docs for multi-valued fields, fix comment typo
polyfractal 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -22,8 +22,9 @@ | |
import org.elasticsearch.common.lease.Releasables; | ||
import org.elasticsearch.common.util.BigArrays; | ||
import org.elasticsearch.common.util.DoubleArray; | ||
import org.elasticsearch.index.fielddata.NumericDoubleValues; | ||
import org.elasticsearch.index.fielddata.SortedNumericDoubleValues; | ||
import org.elasticsearch.search.DocValueFormat; | ||
import org.elasticsearch.search.aggregations.AggregationExecutionException; | ||
import org.elasticsearch.search.aggregations.Aggregator; | ||
import org.elasticsearch.search.aggregations.InternalAggregation; | ||
import org.elasticsearch.search.aggregations.LeafBucketCollector; | ||
|
@@ -77,8 +78,8 @@ public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, | |
return LeafBucketCollector.NO_OP_COLLECTOR; | ||
} | ||
final BigArrays bigArrays = context.bigArrays(); | ||
final NumericDoubleValues docValues = valuesSources.getField(VALUE_FIELD.getPreferredName(), ctx); | ||
final NumericDoubleValues docWeights = valuesSources.getField(WEIGHT_FIELD.getPreferredName(), 1.0, ctx); | ||
final SortedNumericDoubleValues docValues = valuesSources.getField(VALUE_FIELD.getPreferredName(), ctx); | ||
final SortedNumericDoubleValues docWeights = valuesSources.getField(WEIGHT_FIELD.getPreferredName(), ctx); | ||
|
||
return new LeafBucketCollectorBase(sub, docValues) { | ||
@Override | ||
|
@@ -88,13 +89,23 @@ public void collect(int doc, long bucket) throws IOException { | |
sumCompensations = bigArrays.grow(sumCompensations, bucket + 1); | ||
weightCompensations = bigArrays.grow(weightCompensations, bucket + 1); | ||
|
||
if (docValues.advanceExact(doc)) { | ||
boolean advanced = docWeights.advanceExact(doc); | ||
assert advanced; | ||
final double weight = docWeights.doubleValue(); | ||
|
||
kahanSum(docValues.doubleValue() * weight, sums, sumCompensations, bucket); | ||
kahanSum(weight, weights, weightCompensations, bucket); | ||
if (docValues.advanceExact(doc) && docWeights.advanceExact(doc)) { | ||
if (docWeights.docValueCount() > 1) { | ||
throw new AggregationExecutionException("Encountered more than one weight for a " + | ||
"single document. Use a script to combine multiple weights-per-doc into a single value."); | ||
} | ||
// There should always be one weight if advanceExact lands us here, either | ||
// a real weight or a `missing` value | ||
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. nit: |
||
assert docWeights.docValueCount() == 1; | ||
final double weight = docWeights.nextValue(); | ||
|
||
final int numValues = docValues.docValueCount(); | ||
assert numValues > 0; | ||
|
||
for (int i = 0; i < numValues; i++) { | ||
kahanSum(docValues.nextValue() * weight, sums, sumCompensations, bucket); | ||
kahanSum(weight, weights, weightCompensations, bucket); | ||
} | ||
} | ||
} | ||
}; | ||
|
This file contains 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
This file contains 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
This file contains 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
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.
I wonder if its worth having an example of a single weight being applied to each value independantly to help solidify what we mean?