Skip to content

Commit 6888a2c

Browse files
authored
Add script property to variable width histogram (#6648)
* Add script property to VariableWidthHistogram * Add test for VariableWidthHistogram script usage * Change from script to IScript to match the IMultiTermsAggregation
1 parent 96f36d0 commit 6888a2c

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

src/Nest/Aggregations/Bucket/VariableWidthHistogram/VariableWidthHistogramAggregation.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ public interface IVariableWidthHistogramAggregation : IBucketAggregation
2424

2525
[DataMember(Name = "initial_buffer")]
2626
int? InitialBuffer { get; set; }
27-
27+
2828
[DataMember(Name = "shard_size")]
2929
int? ShardSize { get; set; }
30+
31+
[DataMember(Name = "script")]
32+
IScript Script { get; set; }
3033
}
3134

3235
public class VariableWidthHistogramAggregation : BucketAggregationBase, IVariableWidthHistogramAggregation
@@ -42,6 +45,8 @@ public VariableWidthHistogramAggregation(string name) : base(name) { }
4245
/// <inheritdoc />
4346
public int? ShardSize { get; set; }
4447

48+
public IScript Script { get; set; }
49+
4550
internal override void WrapInContainer(AggregationContainer c) => c.VariableWidthHistogram = this;
4651
}
4752

@@ -53,6 +58,7 @@ public class VariableWidthHistogramAggregationDescriptor<T>
5358
int? IVariableWidthHistogramAggregation.Buckets { get; set; }
5459
int? IVariableWidthHistogramAggregation.InitialBuffer { get; set; }
5560
int? IVariableWidthHistogramAggregation.ShardSize { get; set; }
61+
IScript IVariableWidthHistogramAggregation.Script { get; set; }
5662

5763
/// <inheritdoc cref="IVariableWidthHistogramAggregation.Field" />
5864
public VariableWidthHistogramAggregationDescriptor<T> Field(Field field) => Assign(field, (a, v) => a.Field = v);
@@ -68,5 +74,12 @@ public class VariableWidthHistogramAggregationDescriptor<T>
6874

6975
/// <inheritdoc cref="IVariableWidthHistogramAggregation.ShardSize" />
7076
public VariableWidthHistogramAggregationDescriptor<T> ShardSize(int? shardSize) => Assign(shardSize, (a, v) => a.ShardSize = v);
77+
78+
/// <inheritdoc cref="IVariableWidthHistogramAggregation.Script" />
79+
public VariableWidthHistogramAggregationDescriptor<T> Script(string script) => Assign((InlineScript)script, (a, v) => a.Script = v);
80+
81+
/// <inheritdoc cref="IVariableWidthHistogramAggregation.Script" />
82+
public VariableWidthHistogramAggregationDescriptor<T> Script(Func<ScriptDescriptor, IScript> scriptSelector) =>
83+
Assign(scriptSelector, (a, v) => a.Script = v?.Invoke(new ScriptDescriptor()));
7184
}
7285
}

tests/Tests/Aggregations/Bucket/VariableWidthHistogram/VariableWidthHistogramUsageTests.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,74 @@ protected override void ExpectResponse(ISearchResponse<Project> response)
8383
counts.Meta["foo"].Should().Be("bar");
8484
}
8585
}
86+
87+
// hide
88+
[SkipVersion("<7.11.0", "Variable width aggregation added in 7.11.0")]
89+
public class VariableWidthHistogramWithScriptUsageTests : AggregationUsageTestBase<ReadOnlyCluster>
90+
{
91+
public VariableWidthHistogramWithScriptUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { }
92+
93+
private const string Script = "Math.min(_value, -1)"; // use inline script to force the number of commits to be -1 for all documents
94+
95+
protected override object AggregationJson => new
96+
{
97+
commits = new
98+
{
99+
meta = new
100+
{
101+
foo = "bar"
102+
},
103+
variable_width_histogram = new
104+
{
105+
field = "numberOfCommits",
106+
buckets = 2,
107+
initial_buffer = 2,
108+
shard_size = 100,
109+
script = new {
110+
source = Script
111+
}
112+
}
113+
}
114+
};
115+
116+
protected override Func<AggregationContainerDescriptor<Project>, IAggregationContainer> FluentAggs => a => a
117+
.VariableWidthHistogram("commits", v => v
118+
.Field(f => f.NumberOfCommits)
119+
.Buckets(2)
120+
.InitialBuffer(2)
121+
.ShardSize(100)
122+
.Script(Script)
123+
.Meta(m => m
124+
.Add("foo", "bar")
125+
));
126+
127+
protected override AggregationDictionary InitializerAggs =>
128+
new VariableWidthHistogramAggregation("commits")
129+
{
130+
Field = Field<Project>(f => f.NumberOfCommits),
131+
Buckets = 2,
132+
InitialBuffer = 2,
133+
ShardSize = 100,
134+
Script = new InlineScript(Script),
135+
Meta = new Dictionary<string, object>
136+
{
137+
{ "foo", "bar" }
138+
}
139+
};
140+
141+
protected override void ExpectResponse(ISearchResponse<Project> response)
142+
{
143+
response.ShouldBeValid();
144+
var counts = response.Aggregations.VariableWidthHistogram("commits");
145+
counts.Should().NotBeNull();
146+
counts.Buckets.Should().HaveCount(1);
147+
var firstBucket = counts.Buckets.First();
148+
firstBucket.Key.Should().Be(-1);
149+
firstBucket.Minimum.Should().Be(-1);
150+
firstBucket.Maximum.Should().Be(-1);
151+
firstBucket.DocCount.Should().BeGreaterOrEqualTo(1);
152+
counts.Meta.Should().NotBeNull().And.HaveCount(1);
153+
counts.Meta["foo"].Should().Be("bar");
154+
}
155+
}
86156
}

0 commit comments

Comments
 (0)