Skip to content

Commit

Permalink
Regenerate with latest spec fixes for 8.7 (#7684) (#7693)
Browse files Browse the repository at this point in the history
Co-authored-by: Steve Gordon <sgordon@hotmail.co.uk>
  • Loading branch information
github-actions[bot] and stevejgordon committed May 5, 2023
1 parent ce38ec1 commit f5af64d
Show file tree
Hide file tree
Showing 10 changed files with 259 additions and 42 deletions.
53 changes: 53 additions & 0 deletions src/Elastic.Clients.Elasticsearch/Serialization/Stringified.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,59 @@ public static long ReadStringifiedLong(ref Utf8JsonReader reader)
}

return reader.GetInt64();
}
}

internal sealed class StringifiedIntegerConverter : JsonConverter<int?>
{
public override int? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => ReadStringifiedInteger(ref reader);

public override void Write(Utf8JsonWriter writer, int? value, JsonSerializerOptions options) => writer.WriteNumberValue(value.Value);

public static int ReadStringifiedInteger(ref Utf8JsonReader reader)
{
if (reader.TokenType == JsonTokenType.PropertyName)
reader.Read();

if (reader.TokenType == JsonTokenType.String)
{
var intString = reader.GetString();

if (!int.TryParse(intString, out var intValue))
{
throw new JsonException("Unable to parse string value to integer.");
}

return intValue;
}

return reader.GetInt32();
}
}

internal sealed class StringifiedBoolConverter : JsonConverter<bool?>
{
public override bool? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => ReadStringifiedBool(ref reader);

public override void Write(Utf8JsonWriter writer, bool? value, JsonSerializerOptions options) => writer.WriteBooleanValue(value.Value);

public static bool ReadStringifiedBool(ref Utf8JsonReader reader)
{
if (reader.TokenType == JsonTokenType.PropertyName)
reader.Read();

if (reader.TokenType == JsonTokenType.String)
{
var boolString = reader.GetString();

if (!bool.TryParse(boolString, out var boolValue))
{
throw new JsonException("Unable to parse string value to bool.");
}

return boolValue;
}

return reader.GetBoolean();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public override SubmitAsyncSearchRequest Read(ref Utf8JsonReader reader, Type ty

if (property == "knn")
{
variant.Knn = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.KnnQuery?>(ref reader, options);
variant.Knn = JsonSerializer.Deserialize<ICollection<Elastic.Clients.Elasticsearch.KnnQuery>?>(ref reader, options);
continue;
}

Expand Down Expand Up @@ -817,8 +817,8 @@ public SubmitAsyncSearchRequest(Elastic.Clients.Elasticsearch.Indices? indices)
/// <summary>
/// <para>Defines the approximate kNN search to run.</para>
/// </summary>
[JsonInclude, JsonPropertyName("knn")]
public Elastic.Clients.Elasticsearch.KnnQuery? Knn { get; set; }
[JsonInclude, JsonPropertyName("knn"), SingleOrManyCollectionConverter(typeof(Elastic.Clients.Elasticsearch.KnnQuery))]
public ICollection<Elastic.Clients.Elasticsearch.KnnQuery>? Knn { get; set; }

/// <summary>
/// <para>Minimum _score for matching documents. Documents with a lower _score are<br/>not included in the search results.</para>
Expand Down Expand Up @@ -998,9 +998,10 @@ public SubmitAsyncSearchRequestDescriptor<TDocument> Indices(Elastic.Clients.Ela
private Elastic.Clients.Elasticsearch.Core.Search.Highlight? HighlightValue { get; set; }
private Core.Search.HighlightDescriptor<TDocument> HighlightDescriptor { get; set; }
private Action<Core.Search.HighlightDescriptor<TDocument>> HighlightDescriptorAction { get; set; }
private Elastic.Clients.Elasticsearch.KnnQuery? KnnValue { get; set; }
private ICollection<Elastic.Clients.Elasticsearch.KnnQuery>? KnnValue { get; set; }
private KnnQueryDescriptor<TDocument> KnnDescriptor { get; set; }
private Action<KnnQueryDescriptor<TDocument>> KnnDescriptorAction { get; set; }
private Action<KnnQueryDescriptor<TDocument>>[] KnnDescriptorActions { get; set; }
private Elastic.Clients.Elasticsearch.QueryDsl.Query? PostFilterValue { get; set; }
private QueryDsl.QueryDescriptor<TDocument> PostFilterDescriptor { get; set; }
private Action<QueryDsl.QueryDescriptor<TDocument>> PostFilterDescriptorAction { get; set; }
Expand Down Expand Up @@ -1197,10 +1198,11 @@ public SubmitAsyncSearchRequestDescriptor<TDocument> Highlight(Action<Core.Searc
/// <summary>
/// <para>Defines the approximate kNN search to run.</para>
/// </summary>
public SubmitAsyncSearchRequestDescriptor<TDocument> Knn(Elastic.Clients.Elasticsearch.KnnQuery? knn)
public SubmitAsyncSearchRequestDescriptor<TDocument> Knn(ICollection<Elastic.Clients.Elasticsearch.KnnQuery>? knn)
{
KnnDescriptor = null;
KnnDescriptorAction = null;
KnnDescriptorActions = null;
KnnValue = knn;
return Self;
}
Expand All @@ -1209,6 +1211,7 @@ public SubmitAsyncSearchRequestDescriptor<TDocument> Knn(KnnQueryDescriptor<TDoc
{
KnnValue = null;
KnnDescriptorAction = null;
KnnDescriptorActions = null;
KnnDescriptor = descriptor;
return Self;
}
Expand All @@ -1217,10 +1220,20 @@ public SubmitAsyncSearchRequestDescriptor<TDocument> Knn(Action<KnnQueryDescript
{
KnnValue = null;
KnnDescriptor = null;
KnnDescriptorActions = null;
KnnDescriptorAction = configure;
return Self;
}

public SubmitAsyncSearchRequestDescriptor<TDocument> Knn(params Action<KnnQueryDescriptor<TDocument>>[] configure)
{
KnnValue = null;
KnnDescriptor = null;
KnnDescriptorAction = null;
KnnDescriptorActions = configure;
return Self;
}

public SubmitAsyncSearchRequestDescriptor<TDocument> PostFilter(Elastic.Clients.Elasticsearch.QueryDsl.Query? postFilter)
{
PostFilterDescriptor = null;
Expand Down Expand Up @@ -1707,10 +1720,23 @@ protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions o
writer.WritePropertyName("knn");
JsonSerializer.Serialize(writer, new KnnQueryDescriptor<TDocument>(KnnDescriptorAction), options);
}
else if (KnnDescriptorActions is not null)
{
writer.WritePropertyName("knn");
if (KnnDescriptorActions.Length > 1)
writer.WriteStartArray();
foreach (var action in KnnDescriptorActions)
{
JsonSerializer.Serialize(writer, new KnnQueryDescriptor<TDocument>(action), options);
}

if (KnnDescriptorActions.Length > 1)
writer.WriteEndArray();
}
else if (KnnValue is not null)
{
writer.WritePropertyName("knn");
JsonSerializer.Serialize(writer, KnnValue, options);
SingleOrManySerializationHelper.Serialize<Elastic.Clients.Elasticsearch.KnnQuery>(KnnValue, writer, options);
}

if (PostFilterDescriptor is not null)
Expand Down Expand Up @@ -2042,9 +2068,10 @@ public SubmitAsyncSearchRequestDescriptor Indices(Elastic.Clients.Elasticsearch.
private Elastic.Clients.Elasticsearch.Core.Search.Highlight? HighlightValue { get; set; }
private Core.Search.HighlightDescriptor HighlightDescriptor { get; set; }
private Action<Core.Search.HighlightDescriptor> HighlightDescriptorAction { get; set; }
private Elastic.Clients.Elasticsearch.KnnQuery? KnnValue { get; set; }
private ICollection<Elastic.Clients.Elasticsearch.KnnQuery>? KnnValue { get; set; }
private KnnQueryDescriptor KnnDescriptor { get; set; }
private Action<KnnQueryDescriptor> KnnDescriptorAction { get; set; }
private Action<KnnQueryDescriptor>[] KnnDescriptorActions { get; set; }
private Elastic.Clients.Elasticsearch.QueryDsl.Query? PostFilterValue { get; set; }
private QueryDsl.QueryDescriptor PostFilterDescriptor { get; set; }
private Action<QueryDsl.QueryDescriptor> PostFilterDescriptorAction { get; set; }
Expand Down Expand Up @@ -2241,10 +2268,11 @@ public SubmitAsyncSearchRequestDescriptor Highlight(Action<Core.Search.Highlight
/// <summary>
/// <para>Defines the approximate kNN search to run.</para>
/// </summary>
public SubmitAsyncSearchRequestDescriptor Knn(Elastic.Clients.Elasticsearch.KnnQuery? knn)
public SubmitAsyncSearchRequestDescriptor Knn(ICollection<Elastic.Clients.Elasticsearch.KnnQuery>? knn)
{
KnnDescriptor = null;
KnnDescriptorAction = null;
KnnDescriptorActions = null;
KnnValue = knn;
return Self;
}
Expand All @@ -2253,6 +2281,7 @@ public SubmitAsyncSearchRequestDescriptor Knn(KnnQueryDescriptor descriptor)
{
KnnValue = null;
KnnDescriptorAction = null;
KnnDescriptorActions = null;
KnnDescriptor = descriptor;
return Self;
}
Expand All @@ -2261,10 +2290,20 @@ public SubmitAsyncSearchRequestDescriptor Knn(Action<KnnQueryDescriptor> configu
{
KnnValue = null;
KnnDescriptor = null;
KnnDescriptorActions = null;
KnnDescriptorAction = configure;
return Self;
}

public SubmitAsyncSearchRequestDescriptor Knn(params Action<KnnQueryDescriptor>[] configure)
{
KnnValue = null;
KnnDescriptor = null;
KnnDescriptorAction = null;
KnnDescriptorActions = configure;
return Self;
}

public SubmitAsyncSearchRequestDescriptor PostFilter(Elastic.Clients.Elasticsearch.QueryDsl.Query? postFilter)
{
PostFilterDescriptor = null;
Expand Down Expand Up @@ -2751,10 +2790,23 @@ protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions o
writer.WritePropertyName("knn");
JsonSerializer.Serialize(writer, new KnnQueryDescriptor(KnnDescriptorAction), options);
}
else if (KnnDescriptorActions is not null)
{
writer.WritePropertyName("knn");
if (KnnDescriptorActions.Length > 1)
writer.WriteStartArray();
foreach (var action in KnnDescriptorActions)
{
JsonSerializer.Serialize(writer, new KnnQueryDescriptor(action), options);
}

if (KnnDescriptorActions.Length > 1)
writer.WriteEndArray();
}
else if (KnnValue is not null)
{
writer.WritePropertyName("knn");
JsonSerializer.Serialize(writer, KnnValue, options);
SingleOrManySerializationHelper.Serialize<Elastic.Clients.Elasticsearch.KnnQuery>(KnnValue, writer, options);
}

if (PostFilterDescriptor is not null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public sealed partial class LimitTokenCountTokenFilter : ITokenFilter
[JsonInclude, JsonPropertyName("consume_all_tokens")]
public bool? ConsumeAllTokens { get; set; }
[JsonInclude, JsonPropertyName("max_token_count")]
[JsonConverter(typeof(StringifiedIntegerConverter))]
public int? MaxTokenCount { get; set; }

[JsonInclude, JsonPropertyName("type")]
Expand Down Expand Up @@ -80,10 +81,10 @@ protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions o
writer.WriteBooleanValue(ConsumeAllTokensValue.Value);
}

if (MaxTokenCountValue.HasValue)
if (MaxTokenCountValue is not null)
{
writer.WritePropertyName("max_token_count");
writer.WriteNumberValue(MaxTokenCountValue.Value);
JsonSerializer.Serialize(writer, MaxTokenCountValue, options);
}

writer.WritePropertyName("type");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,64 @@

namespace Elastic.Clients.Elasticsearch.Analysis;

internal sealed partial class StemmerTokenFilterConverter : JsonConverter<StemmerTokenFilter>
{
public override StemmerTokenFilter Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.StartObject)
throw new JsonException("Unexpected JSON detected.");
var variant = new StemmerTokenFilter();
while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
{
if (reader.TokenType == JsonTokenType.PropertyName)
{
var property = reader.GetString();
if (property == "language" || property == "name")
{
variant.Language = JsonSerializer.Deserialize<string?>(ref reader, options);
continue;
}

if (property == "version")
{
variant.Version = JsonSerializer.Deserialize<string?>(ref reader, options);
continue;
}
}
}

return variant;
}

public override void Write(Utf8JsonWriter writer, StemmerTokenFilter value, JsonSerializerOptions options)
{
writer.WriteStartObject();
if (!string.IsNullOrEmpty(value.Language))
{
writer.WritePropertyName("language");
writer.WriteStringValue(value.Language);
}

writer.WritePropertyName("type");
writer.WriteStringValue("stemmer");
if (value.Version is not null)
{
writer.WritePropertyName("version");
JsonSerializer.Serialize(writer, value.Version, options);
}

writer.WriteEndObject();
}
}

[JsonConverter(typeof(StemmerTokenFilterConverter))]
public sealed partial class StemmerTokenFilter : ITokenFilter
{
[JsonInclude, JsonPropertyName("language")]
public string Language { get; set; }
public string? Language { get; set; }

[JsonInclude, JsonPropertyName("type")]
public string Type => "stemmer";

[JsonInclude, JsonPropertyName("version")]
public string? Version { get; set; }
}

Expand All @@ -47,10 +96,10 @@ public StemmerTokenFilterDescriptor() : base()
{
}

private string LanguageValue { get; set; }
private string? LanguageValue { get; set; }
private string? VersionValue { get; set; }

public StemmerTokenFilterDescriptor Language(string language)
public StemmerTokenFilterDescriptor Language(string? language)
{
LanguageValue = language;
return Self;
Expand All @@ -65,8 +114,12 @@ public StemmerTokenFilterDescriptor Version(string? version)
protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
{
writer.WriteStartObject();
writer.WritePropertyName("language");
writer.WriteStringValue(LanguageValue);
if (!string.IsNullOrEmpty(LanguageValue))
{
writer.WritePropertyName("language");
writer.WriteStringValue(LanguageValue);
}

writer.WritePropertyName("type");
writer.WriteStringValue("stemmer");
if (VersionValue is not null)
Expand Down

0 comments on commit f5af64d

Please sign in to comment.