Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ public interface IVariableWidthHistogramAggregation : IBucketAggregation

[DataMember(Name = "initial_buffer")]
int? InitialBuffer { get; set; }

[DataMember(Name = "shard_size")]
int? ShardSize { get; set; }

[DataMember(Name = "script")]
IScript Script { get; set; }
}

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

public IScript Script { get; set; }

internal override void WrapInContainer(AggregationContainer c) => c.VariableWidthHistogram = this;
}

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

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

/// <inheritdoc cref="IVariableWidthHistogramAggregation.ShardSize" />
public VariableWidthHistogramAggregationDescriptor<T> ShardSize(int? shardSize) => Assign(shardSize, (a, v) => a.ShardSize = v);

/// <inheritdoc cref="IVariableWidthHistogramAggregation.Script" />
public VariableWidthHistogramAggregationDescriptor<T> Script(string script) => Assign((InlineScript)script, (a, v) => a.Script = v);

/// <inheritdoc cref="IVariableWidthHistogramAggregation.Script" />
public VariableWidthHistogramAggregationDescriptor<T> Script(Func<ScriptDescriptor, IScript> scriptSelector) =>
Assign(scriptSelector, (a, v) => a.Script = v?.Invoke(new ScriptDescriptor()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,74 @@ protected override void ExpectResponse(ISearchResponse<Project> response)
counts.Meta["foo"].Should().Be("bar");
}
}

// hide
[SkipVersion("<7.11.0", "Variable width aggregation added in 7.11.0")]
public class VariableWidthHistogramWithScriptUsageTests : AggregationUsageTestBase<ReadOnlyCluster>
{
public VariableWidthHistogramWithScriptUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { }

private const string Script = "Math.min(_value, -1)"; // use inline script to force the number of commits to be -1 for all documents

protected override object AggregationJson => new
{
commits = new
{
meta = new
{
foo = "bar"
},
variable_width_histogram = new
{
field = "numberOfCommits",
buckets = 2,
initial_buffer = 2,
shard_size = 100,
script = new {
source = Script
}
}
}
};

protected override Func<AggregationContainerDescriptor<Project>, IAggregationContainer> FluentAggs => a => a
.VariableWidthHistogram("commits", v => v
.Field(f => f.NumberOfCommits)
.Buckets(2)
.InitialBuffer(2)
.ShardSize(100)
.Script(Script)
.Meta(m => m
.Add("foo", "bar")
));

protected override AggregationDictionary InitializerAggs =>
new VariableWidthHistogramAggregation("commits")
{
Field = Field<Project>(f => f.NumberOfCommits),
Buckets = 2,
InitialBuffer = 2,
ShardSize = 100,
Script = new InlineScript(Script),
Meta = new Dictionary<string, object>
{
{ "foo", "bar" }
}
};

protected override void ExpectResponse(ISearchResponse<Project> response)
{
response.ShouldBeValid();
var counts = response.Aggregations.VariableWidthHistogram("commits");
counts.Should().NotBeNull();
counts.Buckets.Should().HaveCount(1);
var firstBucket = counts.Buckets.First();
firstBucket.Key.Should().Be(-1);
firstBucket.Minimum.Should().Be(-1);
firstBucket.Maximum.Should().Be(-1);
firstBucket.DocCount.Should().BeGreaterOrEqualTo(1);
counts.Meta.Should().NotBeNull().And.HaveCount(1);
counts.Meta["foo"].Should().Be("bar");
}
}
}