- .NET Clients: other versions:
- Introduction
- Breaking changes
- API Conventions
- Elasticsearch.Net - Low level client
- NEST - High level client
- Troubleshooting
- Search
- Query DSL
- Full text queries
- Term level queries
- Exists Query Usage
- Fuzzy Date Query Usage
- Fuzzy Numeric Query Usage
- Fuzzy Query Usage
- Ids Query Usage
- Prefix Query Usage
- Date Range Query Usage
- Long Range Query Usage
- Numeric Range Query Usage
- Term Range Query Usage
- Regexp Query Usage
- Term Query Usage
- Terms List Query Usage
- Terms Lookup Query Usage
- Terms Query Usage
- Terms Set Query Usage
- Type Query Usage
- Wildcard Query Usage
- Compound queries
- Joining queries
- Geo queries
- Geo Bounding Box Query Usage
- Geo Distance Query Usage
- Geo Polygon Query Usage
- Geo Shape Circle Query Usage
- Geo Shape Envelope Query Usage
- Geo Shape Geometry Collection Query Usage
- Geo Shape Indexed Shape Query Usage
- Geo Shape Line String Query Usage
- Geo Shape Multi Line String Query Usage
- Geo Shape Multi Point Query Usage
- Geo Shape Multi Polygon Query Usage
- Geo Shape Point Query Usage
- Geo Shape Polygon Query Usage
- Specialized queries
- Span queries
- NEST specific queries
- Aggregations
- Metric Aggregations
- Average Aggregation Usage
- Cardinality Aggregation Usage
- Extended Stats Aggregation Usage
- Geo Bounds Aggregation Usage
- Geo Centroid Aggregation Usage
- Max Aggregation Usage
- Median Absolute Deviation Aggregation Usage
- Min Aggregation Usage
- Percentile Ranks Aggregation Usage
- Percentiles Aggregation Usage
- Scripted Metric Aggregation Usage
- Stats Aggregation Usage
- Sum Aggregation Usage
- Top Hits Aggregation Usage
- Value Count Aggregation Usage
- Weighted Average Aggregation Usage
- Bucket Aggregations
- Adjacency Matrix Usage
- Auto Date Histogram Aggregation Usage
- Children Aggregation Usage
- Composite Aggregation Usage
- Date Histogram Aggregation Usage
- Date Range Aggregation Usage
- Filter Aggregation Usage
- Filters Aggregation Usage
- Geo Distance Aggregation Usage
- Geo Hash Grid Aggregation Usage
- Global Aggregation Usage
- Histogram Aggregation Usage
- Ip Range Aggregation Usage
- Missing Aggregation Usage
- Nested Aggregation Usage
- Parent Aggregation Usage
- Range Aggregation Usage
- Reverse Nested Aggregation Usage
- Sampler Aggregation Usage
- Significant Terms Aggregation Usage
- Significant Text Aggregation Usage
- Terms Aggregation Usage
- Pipeline Aggregations
- Average Bucket Aggregation Usage
- Bucket Script Aggregation Usage
- Bucket Selector Aggregation Usage
- Bucket Sort Aggregation Usage
- Cumulative Sum Aggregation Usage
- Derivative Aggregation Usage
- Extended Stats Bucket Aggregation Usage
- Max Bucket Aggregation Usage
- Min Bucket Aggregation Usage
- Moving Average Ewma Aggregation Usage
- Moving Average Holt Linear Aggregation Usage
- Moving Average Holt Winters Aggregation Usage
- Moving Average Linear Aggregation Usage
- Moving Average Simple Aggregation Usage
- Moving Function Aggregation Usage
- Percentiles Bucket Aggregation Usage
- Serial Differencing Aggregation Usage
- Stats Bucket Aggregation Usage
- Sum Bucket Aggregation Usage
- Matrix Aggregations
- Metric Aggregations
NOTE: You are looking at documentation for an older release. For the latest information, see the current release documentation.
Scripted Metric Aggregation Usage
edit
IMPORTANT: This documentation is no longer updated. Refer to Elastic's version policy and the latest documentation.
Scripted Metric Aggregation Usage
editFluent DSL example
edita => a .ScriptedMetric("sum_the_hard_way", sm => sm .InitScript(ss => ss.Source(Script.Init)) .MapScript(ss => ss.Source(Script.Map)) .CombineScript(ss => ss.Source(Script.Combine)) .ReduceScript(ss => ss.Source(Script.Reduce)) )
Object Initializer syntax example
editnew ScriptedMetricAggregation("sum_the_hard_way") { InitScript = new InlineScript(Script.Init), MapScript = new InlineScript(Script.Map), CombineScript = new InlineScript(Script.Combine), ReduceScript = new InlineScript(Script.Reduce) }
Example json output.
{ "sum_the_hard_way": { "scripted_metric": { "init_script": { "source": "params._agg.commits = []" }, "map_script": { "source": "if (doc['state'].value == \"Stable\") { params._agg.commits.add(doc['numberOfCommits'].value) }" }, "combine_script": { "source": "def sum = 0.0; for (c in params._agg.commits) { sum += c } return sum" }, "reduce_script": { "source": "def sum = 0.0; for (a in params._aggs) { sum += a } return sum" } } } }
Handling Responses
editresponse.ShouldBeValid(); var sumTheHardWay = response.Aggregations.ScriptedMetric("sum_the_hard_way"); sumTheHardWay.Should().NotBeNull(); sumTheHardWay.Value<int>().Should().BeGreaterThan(0);
private class Scripted { public string Combine { get; set; } public string Init { get; set; } public string Language { get; set; } public string Map { get; set; } public string Reduce { get; set; } }
Fluent DSL example
edita => a .ScriptedMetric("by_state_total", sm => sm .InitScript(ss => ss.Source(First.Init).Lang(First.Language)) .MapScript(ss => ss.Source(First.Map).Lang(First.Language)) .ReduceScript(ss => ss.Source(First.Reduce).Lang(First.Language)) ) .ScriptedMetric("total_commits", sm => sm .InitScript(ss => ss.Source(Second.Init).Lang(Second.Language)) .MapScript(ss => ss.Source(Second.Map).Lang(Second.Language)) .CombineScript(ss => ss.Source(Second.Combine).Lang(Second.Language)) .ReduceScript(ss => ss.Source(Second.Reduce).Lang(Second.Language)) )
Object Initializer syntax example
editnew ScriptedMetricAggregation("by_state_total") { InitScript = new InlineScript(First.Init) { Lang = First.Language }, MapScript = new InlineScript(First.Map) { Lang = First.Language }, ReduceScript = new InlineScript(First.Reduce) { Lang = First.Language } } && new ScriptedMetricAggregation("total_commits") { InitScript = new InlineScript(Second.Init) { Lang = Second.Language }, MapScript = new InlineScript(Second.Map) { Lang = Second.Language }, CombineScript = new InlineScript(Second.Combine) { Lang = Second.Language }, ReduceScript = new InlineScript(Second.Reduce) { Lang = Second.Language } }
Example json output.
{ "by_state_total": { "scripted_metric": { "init_script": { "source": "params._agg.map = [:]", "lang": "painless" }, "map_script": { "source": "if (params._agg.map.containsKey(doc['state'].value)) params._agg.map[doc['state'].value] += 1 else params._agg.map[doc['state'].value] = 1;", "lang": "painless" }, "reduce_script": { "source": "def reduce = [:]; for (agg in params._aggs) { for (entry in agg.map.entrySet()) { if (reduce.containsKey(entry.getKey())) reduce[entry.getKey()] += entry.getValue(); else reduce[entry.getKey()] = entry.getValue(); } } return reduce;", "lang": "painless" } } }, "total_commits": { "scripted_metric": { "init_script": { "source": "params._agg.commits = []", "lang": "painless" }, "map_script": { "source": "if (doc['state'].value == \"Stable\") { params._agg.commits.add(doc['numberOfCommits'].value) }", "lang": "painless" }, "combine_script": { "source": "def sum = 0.0; for (c in params._agg.commits) { sum += c } return sum", "lang": "painless" }, "reduce_script": { "source": "def sum = 0.0; for (a in params._aggs) { sum += a } return sum", "lang": "painless" } } } }
Handling Responses
editresponse.ShouldBeValid(); var byStateTotal = response.Aggregations.ScriptedMetric("by_state_total"); var totalCommits = response.Aggregations.ScriptedMetric("total_commits"); byStateTotal.Should().NotBeNull(); totalCommits.Should().NotBeNull(); byStateTotal.Value<IDictionary<string, int>>().Should().NotBeNull(); totalCommits.Value<int>().Should().BeGreaterThan(0);
On this page
Was this helpful?
Thank you for your feedback.