- .NET Clients: other versions:
- Introduction
- Installation
- 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 Set Query Usage
- Terms List Query Usage
- Terms Lookup Query Usage
- Terms Query Usage
- Wildcard Query Usage
- Compound queries
- Joining queries
- Geo queries
- Specialized queries
- Span queries
- NEST specific queries
- Aggregations
- Metric Aggregations
- Average Aggregation Usage
- Boxplot Aggregation Usage
- Cardinality Aggregation Usage
- Extended Stats Aggregation Usage
- Geo Bounds Aggregation Usage
- Geo Centroid Aggregation Usage
- Geo Line Aggregation Usage
- Max Aggregation Usage
- Median Absolute Deviation Aggregation Usage
- Min Aggregation Usage
- Percentile Ranks Aggregation Usage
- Percentiles Aggregation Usage
- Rate Aggregation Usage
- Scripted Metric Aggregation Usage
- Stats Aggregation Usage
- String Stats Aggregation Usage
- Sum Aggregation Usage
- T Test Aggregation Usage
- Top Hits Aggregation Usage
- Top Metrics 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
- Diversified Sampler Aggregation Usage
- Filter Aggregation Usage
- Filters Aggregation Usage
- Geo Distance Aggregation Usage
- Geo Hash Grid Aggregation Usage
- Geo Tile Grid Aggregation Usage
- Global Aggregation Usage
- Histogram Aggregation Usage
- Ip Range Aggregation Usage
- Missing Aggregation Usage
- Multi Terms Aggregation Usage
- Nested Aggregation Usage
- Parent Aggregation Usage
- Range Aggregation Usage
- Rare Terms Aggregation Usage
- Reverse Nested Aggregation Usage
- Sampler Aggregation Usage
- Significant Terms Aggregation Usage
- Significant Text Aggregation Usage
- Terms Aggregation Usage
- Variable Width Histogram Usage
- Pipeline Aggregations
- Average Bucket Aggregation Usage
- Bucket Script Aggregation Usage
- Bucket Selector Aggregation Usage
- Bucket Sort Aggregation Usage
- Cumulative Cardinality 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
- Moving Percentiles Aggregation Usage
- Normalize Aggregation Usage
- Percentiles Bucket Aggregation Usage
- Serial Differencing Aggregation Usage
- Stats Bucket Aggregation Usage
- Sum Bucket Aggregation Usage
- Matrix Aggregations
- Metric Aggregations
IMPORTANT: No additional bug fixes or documentation updates
will be released for this version. 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": "state.commits = []" }, "map_script": { "source": "if (doc['state'].value == \"Stable\") { state.commits.add(doc['numberOfCommits'].value) }" }, "combine_script": { "source": "def sum = 0.0; for (c in state.commits) { sum += c } return sum" }, "reduce_script": { "source": "def sum = 0.0; for (a in states) { 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; } // ReSharper disable once UnusedAutoPropertyAccessor.Local 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)) .CombineScript(ss => ss.Source(_first.Combine).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 }, CombineScript = new InlineScript(_first.Combine) { 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": "state.map = [:]", "lang": "painless" }, "combine_script": { "source": "return state.map;", "lang": "painless" }, "map_script": { "source": "if (state.map.containsKey(doc['state'].value)) state.map[doc['state'].value] += 1; else state.map[doc['state'].value] = 1;", "lang": "painless" }, "reduce_script": { "source": "def reduce = [:]; for (map in states){ for (entry in 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": "state.commits = []", "lang": "painless" }, "map_script": { "source": "if (doc['state'].value == \"Stable\") { state.commits.add(doc['numberOfCommits'].value) }", "lang": "painless" }, "combine_script": { "source": "def sum = 0.0; for (c in state.commits) { sum += c } return sum", "lang": "painless" }, "reduce_script": { "source": "def sum = 0.0; for (a in states) { 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.