Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for component template APIs #411

Merged
merged 18 commits into from
Nov 13, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

### Added
- Added support for point-in-time search and associated APIs ([#405](https://github.com/opensearch-project/opensearch-net/pull/405))
- Added support for the component template APIs ([#411](https://github.com/opensearch-project/opensearch-net/pull/411))

### Removed
- Removed the `Features` API which is not supported by OpenSearch from the low-level client ([#331](https://github.com/opensearch-project/opensearch-net/pull/331))
Expand Down
34 changes: 15 additions & 19 deletions guides/index-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,19 @@ Composable index templates are a new type of index template that allow you to de

```csharp
// Create a component template
var createResponse = client.LowLevel.Cluster.PutComponentTemplate<VoidResponse>("books_mappings", PostData.Serializable(new
{
template = new
{
mappings = new
{
properties = new
{
title = new { type = "text" },
author = new { type = "text" },
published_on = new { type = "date" },
pages = new { type = "integer" }
}
}
}
}));
client.Cluster.PutComponentTemplate("books_mappings", ct => ct
.Template(t => t
.Map(m => m
.Properties(p => p
.Text(tp => tp
.Name("title"))
.Text(tp => tp
.Name("author"))
.Date(d => d
.Name("published_on"))
.Number(n => n
.Name("pages")
.Type(NumberType.Integer))))));

// Create an index template for "books"
var createBooksTemplateResponse = client.LowLevel.Indices.PutTemplateV2ForAll<VoidResponse>("books", PostData.Serializable(new
Expand Down Expand Up @@ -178,7 +175,6 @@ When we create an index named `books-fiction-horror`, OpenSearch will apply the
client.Indices.Create("books-fiction-horror");
var getResponse = client.Indices.Get("books-fiction-horror");
Console.WriteLine(getResponse.Indices["books-fiction-horror"].Settings.NumberOfShards); // 1 Console.WriteLine(getResponse.Indices["books-fiction-horror"].Mappings.Properties["pages"].Type); // integer
Console.WriteLine($"Create response: {componentTemplateCreateResponse}"); // Create response: {"acknowledged":true}
```

### Get an Index Template
Expand All @@ -204,5 +200,5 @@ Let's delete all resources created in this guide:
```csharp
client.Indices.Delete("books-");
client.LowLevel.Indices.DeleteTemplateV2ForAll("books-fiction");
client.LowLevel.Cluster.DeleteComponentTemplate("books_mappings");
```
client.Cluster.DeleteComponentTemplate("books_mappings");
```
5 changes: 2 additions & 3 deletions src/ApiGenerator/Configuration/CodeConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,13 @@ public static class CodeConfiguration
new("{delete,get}_all_pits"),

new("cluster.allocation_explain"),
new("cluster.delete_component_template"),
new("cluster.delete_voting_config_exclusions"),
new("cluster.exists_component_template"),
new("cluster.get_component_template"),
new("cluster.get_settings"),
new("cluster.health"),
new("cluster.pending_tasks"),

new("cluster.*_component_template"),

new("dangling_indices.*"),
new("ingest.*"),
new("nodes.*"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using OpenSearch.Net.Utf8Json;

namespace OpenSearch.Client;

[ReadAs(typeof(ComponentTemplate))]
public interface IComponentTemplate
{
[DataMember(Name = "template")]
ITemplate Template { get; set; }

[DataMember(Name = "version")]
long? Version { get; set; }

[DataMember(Name = "_meta")]
[JsonFormatter(typeof(VerbatimDictionaryInterfaceKeysFormatter<string, object>))]
IDictionary<string, object> Meta { get; set; }
}

public class ComponentTemplate : IComponentTemplate
{
public ITemplate Template { get; set; }
public long? Version { get; set; }
public IDictionary<string, object> Meta { get; set; }
}

[ReadAs(typeof(Template))]
public interface ITemplate
{
[DataMember(Name = "aliases")]
IAliases Aliases { get; set; }

[DataMember(Name = "mappings")]
ITypeMapping Mappings { get; set; }

[DataMember(Name = "settings")]
IIndexSettings Settings { get; set; }
}

public class Template : ITemplate
{
public IAliases Aliases { get; set; }
public ITypeMapping Mappings { get; set; }
public IIndexSettings Settings { get; set; }
}

public class TemplateDescriptor : DescriptorBase<TemplateDescriptor, ITemplate>, ITemplate
{
IAliases ITemplate.Aliases { get; set; }
ITypeMapping ITemplate.Mappings { get; set; }
IIndexSettings ITemplate.Settings { get; set; }

public TemplateDescriptor Aliases(Func<AliasesDescriptor, IPromise<IAliases>> aliasDescriptor) =>
Assign(aliasDescriptor, (a, v) => a.Aliases = v?.Invoke(new AliasesDescriptor())?.Value);

public TemplateDescriptor Map<T>(Func<TypeMappingDescriptor<T>, ITypeMapping> selector) where T : class =>
Assign(selector, (a, v) => a.Mappings = v?.Invoke(new TypeMappingDescriptor<T>()));

public TemplateDescriptor Map(Func<TypeMappingDescriptor<object>, ITypeMapping> selector) =>
Assign(selector, (a, v) => a.Mappings = v?.Invoke(new TypeMappingDescriptor<object>()));

public TemplateDescriptor Settings(Func<IndexSettingsDescriptor, IPromise<IIndexSettings>> settingsSelector) =>
Assign(settingsSelector, (a, v) => a.Settings = v?.Invoke(new IndexSettingsDescriptor())?.Value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

namespace OpenSearch.Client;

[MapsApi("cluster.exists_component_template")]
public partial interface IComponentTemplateExistsRequest { }

public partial class ComponentTemplateExistsRequest { }

public partial class ComponentTemplateExistsDescriptor { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

namespace OpenSearch.Client;

[MapsApi("cluster.delete_component_template")]
public partial interface IDeleteComponentTemplateRequest { }

public partial class DeleteComponentTemplateRequest { }

public partial class DeleteComponentTemplateDescriptor { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

using System.Runtime.Serialization;

namespace OpenSearch.Client;

[DataContract]
public class DeleteComponentTemplateResponse : AcknowledgedResponseBase { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

namespace OpenSearch.Client;

[MapsApi("cluster.get_component_template")]
public partial interface IGetComponentTemplateRequest { }

public partial class GetComponentTemplateRequest { }

public partial class GetComponentTemplateDescriptor { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

using System.Collections.Generic;
using System.Runtime.Serialization;

namespace OpenSearch.Client;

[DataContract]
public class GetComponentTemplateResponse : ResponseBase
{
[DataMember(Name = "component_templates")]
public IReadOnlyCollection<NamedComponentTemplate> ComponentTemplates { get; internal set; }
}

[DataContract]
public class NamedComponentTemplate
{
[DataMember(Name = "name")]
public string Name { get; internal set; }

[DataMember(Name = "component_template")]
public ComponentTemplate ComponentTemplate { get; internal set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

using System;
using System.Collections.Generic;

namespace OpenSearch.Client;

[MapsApi("cluster.put_component_template")]
public partial interface IPutComponentTemplateRequest : IComponentTemplate { }

public partial class PutComponentTemplateRequest
{
public ITemplate Template { get; set; }
public long? Version { get; set; }
public IDictionary<string, object> Meta { get; set; }
}

public partial class PutComponentTemplateDescriptor
{
ITemplate IComponentTemplate.Template { get; set; }
long? IComponentTemplate.Version { get; set; }
IDictionary<string, object> IComponentTemplate.Meta { get; set; }

public PutComponentTemplateDescriptor Version(long? version) => Assign(version, (a, v) => a.Version = v);

public PutComponentTemplateDescriptor Meta(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> metaSelector) =>
Assign(metaSelector(new FluentDictionary<string, object>()), (a, v) => a.Meta = v);

public PutComponentTemplateDescriptor Meta(Dictionary<string, object> metaDictionary) => Assign(metaDictionary, (a, v) => a.Meta = v);

public PutComponentTemplateDescriptor Template(Func<TemplateDescriptor, ITemplate> selector) =>
Assign(selector, (a, v) => a.Template = v?.Invoke(new TemplateDescriptor()));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

using System.Runtime.Serialization;

namespace OpenSearch.Client;

[DataContract]
public class PutComponentTemplateResponse : AcknowledgedResponseBase { }
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@

using System.Runtime.Serialization;

namespace OpenSearch.Client
namespace OpenSearch.Client;

[DataContract]
public class ExistsResponse : ResponseBase
{
[DataContract]
public class ExistsResponse : ResponseBase
{
public bool Exists => ApiCall != null && ApiCall.Success && ApiCall.HttpStatusCode == 200;
}
public bool Exists => ApiCall is { Success: true, HttpStatusCode: 200 };
}
12 changes: 12 additions & 0 deletions src/OpenSearch.Client/_Generated/ApiUrlsLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,18 @@ internal static partial class ApiUrlsLookups
internal static readonly ApiUrls ClusterAllocationExplain =
new(new[] { "_cluster/allocation/explain" });

internal static readonly ApiUrls ClusterDeleteComponentTemplate =
new(new[] { "_component_template/{name}" });

internal static readonly ApiUrls ClusterDeleteVotingConfigExclusions =
new(new[] { "_cluster/voting_config_exclusions" });

internal static readonly ApiUrls ClusterComponentTemplateExists =
new(new[] { "_component_template/{name}" });

internal static readonly ApiUrls ClusterGetComponentTemplate =
new(new[] { "_component_template", "_component_template/{name}" });

internal static readonly ApiUrls ClusterGetSettings = new(new[] { "_cluster/settings" });

internal static readonly ApiUrls ClusterHealth =
Expand All @@ -59,6 +68,9 @@ internal static partial class ApiUrlsLookups
internal static readonly ApiUrls ClusterPendingTasks =
new(new[] { "_cluster/pending_tasks" });

internal static readonly ApiUrls ClusterPutComponentTemplate =
new(new[] { "_component_template/{name}" });

internal static readonly ApiUrls NoNamespaceCreatePit =
new(new[] { "{index}/_search/point_in_time" });

Expand Down
Loading
Loading