Skip to content

Commit

Permalink
Add Transform settings (#4729)
Browse files Browse the repository at this point in the history
Relates: #4718, elastic/elasticsearch#54862

Add transform settings to Transform APIs
  • Loading branch information
russcam authored Jun 10, 2020
1 parent e9d54e0 commit 3c70367
Show file tree
Hide file tree
Showing 6 changed files with 264 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public partial interface IPreviewTransformRequest
/// <inheritdoc cref="ITransformSyncContainer"/>
[DataMember(Name = "sync")]
public ITransformSyncContainer Sync { get; set; }

/// <inheritdoc cref="IPutTransformRequest.Settings"/>
[DataMember(Name = "settings")]
public ITransformSettings Settings { get; set; }
}

public partial class PreviewTransformRequest
Expand All @@ -50,6 +54,10 @@ public partial class PreviewTransformRequest

/// <inheritdoc cref="IPreviewTransformRequest.Sync"/>
public ITransformSyncContainer Sync { get; set; }

/// <inheritdoc cref="IPutTransformRequest.Settings"/>
[DataMember(Name = "settings")]
public ITransformSettings Settings { get; set; }
}

public partial class PreviewTransformDescriptor<TDocument> : IPreviewTransformRequest where TDocument : class
Expand All @@ -60,6 +68,7 @@ public partial class PreviewTransformDescriptor<TDocument> : IPreviewTransformRe
Time IPreviewTransformRequest.Frequency { get; set; }
ITransformPivot IPreviewTransformRequest.Pivot { get; set; }
ITransformSyncContainer IPreviewTransformRequest.Sync { get; set; }
ITransformSettings IPreviewTransformRequest.Settings { get; set; }

/// <inheritdoc cref="IPreviewTransformRequest.Description"/>
public PreviewTransformDescriptor<TDocument> Description(string description) =>
Expand All @@ -83,5 +92,9 @@ public PreviewTransformDescriptor<TDocument> Pivot(Func<TransformPivotDescriptor
/// <inheritdoc cref="IPreviewTransformRequest.Sync"/>
public PreviewTransformDescriptor<TDocument> Sync(Func<TransformSyncContainerDescriptor<TDocument>, ITransformSyncContainer> selector) =>
Assign(selector.InvokeOrDefault(new TransformSyncContainerDescriptor<TDocument>()), (a, v) => a.Sync = v);

/// <inheritdoc cref="IPreviewTransformRequest.Settings"/>
public PreviewTransformDescriptor<TDocument> Settings(Func<TransformSettingsDescriptor, ITransformSettings> selector) =>
Assign(selector?.Invoke(new TransformSettingsDescriptor()), (a, v) => a.Settings = v);
}
}
12 changes: 12 additions & 0 deletions src/Nest/XPack/Transform/PutTransform/PutTransformRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public partial interface IPutTransformRequest
/// <inheritdoc cref="ITransformSyncContainer"/>
[DataMember(Name = "sync")]
public ITransformSyncContainer Sync { get; set; }

/// <inheritdoc cref="ITransformSettings"/>
[DataMember(Name = "settings")]
public ITransformSettings Settings { get; set; }
}

/// <inheritdoc cref="IPutTransformRequest"/>
Expand All @@ -51,6 +55,9 @@ public partial class PutTransformRequest

/// <inheritdoc cref="IPutTransformRequest.Sync"/>
public ITransformSyncContainer Sync { get; set; }

/// <inheritdoc cref="IPutTransformRequest.Settings"/>
public ITransformSettings Settings { get; set; }
}

public partial class PutTransformDescriptor<TDocument> : IPutTransformRequest where TDocument : class
Expand All @@ -61,6 +68,7 @@ public partial class PutTransformDescriptor<TDocument> : IPutTransformRequest wh
Time IPutTransformRequest.Frequency { get; set; }
ITransformPivot IPutTransformRequest.Pivot { get; set; }
ITransformSyncContainer IPutTransformRequest.Sync { get; set; }
ITransformSettings IPutTransformRequest.Settings { get; set; }

/// <inheritdoc cref="IPutTransformRequest.Description"/>
public PutTransformDescriptor<TDocument> Description(string description) =>
Expand All @@ -84,5 +92,9 @@ public PutTransformDescriptor<TDocument> Pivot(Func<TransformPivotDescriptor<TDo
/// <inheritdoc cref="IPutTransformRequest.Sync"/>
public PutTransformDescriptor<TDocument> Sync(Func<TransformSyncContainerDescriptor<TDocument>, ITransformSyncContainer> selector) =>
Assign(selector?.Invoke(new TransformSyncContainerDescriptor<TDocument>()), (a, v) => a.Sync = v);

/// <inheritdoc cref="IPutTransformRequest.Settings"/>
public PutTransformDescriptor<TDocument> Settings(Func<TransformSettingsDescriptor, ITransformSettings> selector) =>
Assign(selector?.Invoke(new TransformSettingsDescriptor()), (a, v) => a.Settings = v);
}
}
4 changes: 4 additions & 0 deletions src/Nest/XPack/Transform/Transform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,9 @@ public class Transform
/// <inheritdoc cref="ITransformSyncContainer"/>
[DataMember(Name = "sync")]
public ITransformSyncContainer Sync { get; set; }

/// <inheritdoc cref="ITransformSettings"/>
[DataMember(Name = "settings")]
public ITransformSettings Settings { get; set; }
}
}
49 changes: 49 additions & 0 deletions src/Nest/XPack/Transform/TransformSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using System.Runtime.Serialization;
using Elasticsearch.Net.Utf8Json;

namespace Nest
{
/// <summary>
/// Transform settings
/// <para />
/// Valid in Elasticsearch 7.8.0+
/// </summary>
[InterfaceDataContract]
[ReadAs(typeof(TransformSettings))]
public interface ITransformSettings
{
[DataMember(Name = "docs_per_second")]
public float? DocsPerSecond { get; set; }

[DataMember(Name = "max_page_search_size")]
public int? MaxPageSearchSize { get; set; }
}

/// <inheritdoc />
public class TransformSettings : ITransformSettings
{
/// <inheritdoc />
public float? DocsPerSecond { get; set; }

/// <inheritdoc />
public int? MaxPageSearchSize { get; set; }
}

public class TransformSettingsDescriptor : DescriptorBase<TransformSettingsDescriptor, ITransformSettings>, ITransformSettings
{
float? ITransformSettings.DocsPerSecond { get; set; }
int? ITransformSettings.MaxPageSearchSize { get; set; }

/// <inheritdoc cref="ITransformSettings.DocsPerSecond"/>
public TransformSettingsDescriptor DocsPerSecond(float? docsPerSecond) =>
Assign(docsPerSecond, (a, v) => a.DocsPerSecond = v);

/// <inheritdoc cref="ITransformSettings.MaxPageSearchSize"/>
public TransformSettingsDescriptor MaxPageSearchSize(int? maxPageSearchSize) =>
Assign(maxPageSearchSize, (a, v) => a.MaxPageSearchSize = v);
}
}
22 changes: 17 additions & 5 deletions src/Nest/XPack/Transform/UpdateTransform/UpdateTransformRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,31 @@ public partial interface IUpdateTransformRequest
/// <inheritdoc cref="ITransformSyncContainer"/>
[DataMember(Name = "sync")]
public ITransformSyncContainer Sync { get; set; }

/// <inheritdoc cref="ITransformSettings"/>
[DataMember(Name = "settings")]
public ITransformSettings Settings { get; set; }
}

public partial class UpdateTransformRequest
{
/// <inheritdoc cref="IPutTransformRequest.Description"/>
/// <inheritdoc cref="IUpdateTransformRequest.Description"/>
public string Description { get; set; }

/// <inheritdoc cref="IPutTransformRequest.Source"/>
/// <inheritdoc cref="IUpdateTransformRequest.Source"/>
public ITransformSource Source { get; set; }

/// <inheritdoc cref="IPutTransformRequest.Destination"/>
/// <inheritdoc cref="IUpdateTransformRequest.Destination"/>
public ITransformDestination Destination { get; set; }

/// <inheritdoc cref="IPutTransformRequest.Frequency"/>
/// <inheritdoc cref="IUpdateTransformRequest.Frequency"/>
public Time Frequency { get; set; }

/// <inheritdoc cref="IPutTransformRequest.Sync"/>
/// <inheritdoc cref="IUpdateTransformRequest.Sync"/>
public ITransformSyncContainer Sync { get; set; }

/// <inheritdoc cref="IUpdateTransformRequest.Settings"/>
public ITransformSettings Settings { get; set; }
}

public partial class UpdateTransformDescriptor<TDocument> : IUpdateTransformRequest where TDocument : class
Expand All @@ -52,6 +59,7 @@ public partial class UpdateTransformDescriptor<TDocument> : IUpdateTransformRequ
ITransformDestination IUpdateTransformRequest.Destination { get; set; }
Time IUpdateTransformRequest.Frequency { get; set; }
ITransformSyncContainer IUpdateTransformRequest.Sync { get; set; }
ITransformSettings IUpdateTransformRequest.Settings { get; set; }

/// <inheritdoc cref="IUpdateTransformRequest.Description"/>
public UpdateTransformDescriptor<TDocument> Description(string description) =>
Expand All @@ -71,5 +79,9 @@ public UpdateTransformDescriptor<TDocument> Destination(Func<TransformDestinatio
/// <inheritdoc cref="IUpdateTransformRequest.Sync"/>
public UpdateTransformDescriptor<TDocument> Sync(Func<TransformSyncContainerDescriptor<TDocument>, ITransformSyncContainer> selector) =>
Assign(selector?.Invoke(new TransformSyncContainerDescriptor<TDocument>()), (a, v) => a.Sync = v);

/// <inheritdoc cref="IUpdateTransformRequest.Settings"/>
public UpdateTransformDescriptor<TDocument> Settings(Func<TransformSettingsDescriptor, ITransformSettings> selector) =>
Assign(selector?.Invoke(new TransformSettingsDescriptor()), (a, v) => a.Settings = v);
}
}
169 changes: 169 additions & 0 deletions tests/Tests/XPack/Transform/TransformApiWithSettingsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using System;
using System.Collections.Generic;
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
using Elasticsearch.Net;
using Nest;
using Tests.Core.ManagedElasticsearch.Clusters;
using Tests.Domain;
using Tests.Framework.EndpointTests;
using Tests.Framework.EndpointTests.TestState;
using static Nest.Infer;

namespace Tests.XPack.Transform
{
[SkipVersion("<7.8.0", "Settings introduced in 7.8.0")]
public class TransformApiWithSettingsTests : ApiIntegrationTestBase<WritableCluster, PreviewTransformResponse<ProjectTransform>, IPreviewTransformRequest, PreviewTransformDescriptor<Project>, PreviewTransformRequest>
{
public TransformApiWithSettingsTests(WritableCluster cluster, EndpointUsage usage) : base(cluster, usage) { }

protected override LazyResponses ClientUsage() => Calls(
(client, f) => client.Transform.Preview<Project, ProjectTransform>(f),
(client, f) => client.Transform.PreviewAsync<Project, ProjectTransform>(f),
(client, r) => client.Transform.Preview<ProjectTransform>(r),
(client, r) => client.Transform.PreviewAsync<ProjectTransform>(r)
);

protected override HttpMethod HttpMethod => HttpMethod.POST;
protected override string UrlPath => $"_transform/_preview";
protected override bool ExpectIsValid => true;
protected override int ExpectStatusCode => 200;
protected override bool SupportsDeserialization => false;
protected override object ExpectJson => new
{
description = CallIsolatedValue,
frequency = "1s",
source = new { index = new[] { "project" }, query = new { match_all = new { } } },
dest = new { index = $"transform-{CallIsolatedValue}" },
pivot = new
{
aggregations = new
{
averageCommits = new
{
avg = new
{
field = "numberOfCommits"
}
},
sumIntoMaster = new
{
scripted_metric = new
{
combine_script = new
{
source = "long sum = 0; for (s in state.masterCommits) { sum += s } return sum"
},
init_script = new
{
source = "state.masterCommits = []"
},
map_script = new
{
source = "state.masterCommits.add(doc['branches.keyword'].contains('master')? 1 : 0)"
},
reduce_script = new
{
source = "long sum = 0; for (s in states) { sum += s } return sum"
}
}
}
},
group_by = new
{
weekStartedOn = new
{
date_histogram = new
{
calendar_interval = "week",
field = "startedOn"
}
}
}
},
sync = new
{
time = new
{
field = "lastActivity"
}
},
settings = new
{
docs_per_second = 200.0,
max_page_search_size = 200
}
};

protected override PreviewTransformRequest Initializer => new PreviewTransformRequest
{
Description = CallIsolatedValue,
Frequency = "1s",
Source = new TransformSource { Index = Index<Project>(), Query = new MatchAllQuery() },
Destination = new TransformDestination { Index = $"transform-{CallIsolatedValue}" },
Pivot = new TransformPivot
{
Aggregations =
new AverageAggregation("averageCommits", Field<Project>(f => f.NumberOfCommits)) &&
new ScriptedMetricAggregation("sumIntoMaster")
{
InitScript = new InlineScript("state.masterCommits = []"),
MapScript = new InlineScript("state.masterCommits.add(doc['branches.keyword'].contains('master')? 1 : 0)"),
CombineScript = new InlineScript("long sum = 0; for (s in state.masterCommits) { sum += s } return sum"),
ReduceScript = new InlineScript("long sum = 0; for (s in states) { sum += s } return sum")
},
GroupBy = new Dictionary<string, ISingleGroupSource>
{
{
"weekStartedOn",
new DateHistogramGroupSource() { Field = Field<Project>(f => f.StartedOn), CalendarInterval = DateInterval.Week }
}
}
},
Sync = new TransformSyncContainer(new TransformTimeSync { Field = Field<Project>(f => f.LastActivity) }),
Settings = new TransformSettings { MaxPageSearchSize = 200, DocsPerSecond = 200 }
};

protected override Func<PreviewTransformDescriptor<Project>, IPreviewTransformRequest> Fluent => f => f
.Description(CallIsolatedValue)
.Frequency(new Time(1, TimeUnit.Second))
.Source(s => s
.Index<Project>()
.Query(q => q.MatchAll())
)
.Destination(de => de
.Index($"transform-{CallIsolatedValue}")
)
.Pivot(p => p
.Aggregations(a => a
.Average("averageCommits", avg => avg
.Field(f => f.NumberOfCommits)
)
.ScriptedMetric("sumIntoMaster", sm => sm
.InitScript("state.masterCommits = []")
.MapScript("state.masterCommits.add(doc['branches.keyword'].contains('master')? 1 : 0)")
.CombineScript("long sum = 0; for (s in state.masterCommits) { sum += s } return sum")
.ReduceScript("long sum = 0; for (s in states) { sum += s } return sum")
)
)
.GroupBy(g => g
.DateHistogram("weekStartedOn", dh => dh
.Field(f => f.StartedOn)
.CalendarInterval(DateInterval.Week)
)
)
)
.Sync(sy => sy
.Time(t => t
.Field(f => f.LastActivity)
)
)
.Settings(s => s
.MaxPageSearchSize(200)
.DocsPerSecond(200)
);
}
}

0 comments on commit 3c70367

Please sign in to comment.