|
| 1 | +// Licensed to Elasticsearch B.V under one or more agreements. |
| 2 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Linq; |
| 8 | +using System.Text.Json; |
| 9 | +using System.Text.Json.Serialization; |
| 10 | + |
| 11 | +using Elastic.Clients.Elasticsearch.Serialization; |
| 12 | + |
| 13 | +namespace Elastic.Clients.Elasticsearch.Core.Search; |
| 14 | + |
| 15 | +[GenericConverter(typeof(SuggestDictionaryConverter<>), unwrap:true)] |
| 16 | +public sealed partial class SuggestDictionary<TDocument> : |
| 17 | + IsAReadOnlyDictionary<string, IReadOnlyCollection<ISuggest>> |
| 18 | +{ |
| 19 | + internal SuggestDictionary(IReadOnlyDictionary<string, IReadOnlyCollection<ISuggest>> backingDictionary) : |
| 20 | + base(backingDictionary) |
| 21 | + { |
| 22 | + } |
| 23 | + |
| 24 | + public IReadOnlyCollection<TermSuggest>? GetTerm(string key) => TryGet<TermSuggest>(key); |
| 25 | + |
| 26 | + public IReadOnlyCollection<PhraseSuggest>? GetPhrase(string key) => TryGet<PhraseSuggest>(key); |
| 27 | + |
| 28 | + public IReadOnlyCollection<CompletionSuggest<TDocument>>? GetCompletion(string key) => TryGet<CompletionSuggest<TDocument>>(key); |
| 29 | + |
| 30 | + private IReadOnlyCollection<TSuggest>? TryGet<TSuggest>(string key) where TSuggest : class, ISuggest => |
| 31 | + BackingDictionary.TryGetValue(key, out var items) ? items.Cast<TSuggest>().ToArray() : null; |
| 32 | +} |
| 33 | + |
| 34 | +internal sealed class SuggestDictionaryConverter<TDocument> : |
| 35 | + JsonConverter<SuggestDictionary<TDocument>> |
| 36 | +{ |
| 37 | + public override SuggestDictionary<TDocument>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 38 | + { |
| 39 | + var dictionary = new Dictionary<string, IReadOnlyCollection<ISuggest>>(); |
| 40 | + |
| 41 | + if (reader.TokenType != JsonTokenType.StartObject) |
| 42 | + return new SuggestDictionary<TDocument>(dictionary); |
| 43 | + |
| 44 | + while (reader.Read()) |
| 45 | + { |
| 46 | + if (reader.TokenType == JsonTokenType.EndObject) |
| 47 | + break; |
| 48 | + |
| 49 | + // TODO: Future optimization, get raw bytes span and parse based on those |
| 50 | + var name = reader.GetString() ?? throw new JsonException("Key must not be 'null'."); |
| 51 | + |
| 52 | + reader.Read(); |
| 53 | + ReadVariant(ref reader, options, dictionary, name); |
| 54 | + } |
| 55 | + |
| 56 | + return new SuggestDictionary<TDocument>(dictionary); |
| 57 | + } |
| 58 | + |
| 59 | + public static void ReadVariant(ref Utf8JsonReader reader, JsonSerializerOptions options, Dictionary<string, IReadOnlyCollection<ISuggest>> dictionary, string name) |
| 60 | + { |
| 61 | + var nameParts = name.Split('#'); |
| 62 | + |
| 63 | + if (nameParts.Length != 2) |
| 64 | + throw new JsonException($"Unable to parse typed-key from suggestion name '{name}'"); |
| 65 | + |
| 66 | + var variantName = nameParts[0]; |
| 67 | + switch (variantName) |
| 68 | + { |
| 69 | + case "term": |
| 70 | + { |
| 71 | + var suggest = JsonSerializer.Deserialize<TermSuggest[]>(ref reader, options); |
| 72 | + dictionary.Add(nameParts[1], suggest); |
| 73 | + break; |
| 74 | + } |
| 75 | + |
| 76 | + case "phrase": |
| 77 | + { |
| 78 | + var suggest = JsonSerializer.Deserialize<PhraseSuggest[]>(ref reader, options); |
| 79 | + dictionary.Add(nameParts[1], suggest); |
| 80 | + break; |
| 81 | + } |
| 82 | + |
| 83 | + case "completion": |
| 84 | + { |
| 85 | + var suggest = JsonSerializer.Deserialize<CompletionSuggest<TDocument>[]>(ref reader, options); |
| 86 | + dictionary.Add(nameParts[1], suggest); |
| 87 | + break; |
| 88 | + } |
| 89 | + |
| 90 | + default: |
| 91 | + throw new Exception($"The suggest variant '{variantName}' in this response is currently not supported."); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public override void Write(Utf8JsonWriter writer, SuggestDictionary<TDocument> value, JsonSerializerOptions options) => throw new NotImplementedException(); |
| 96 | +} |
| 97 | + |
| 98 | +public interface ISuggest |
| 99 | +{ |
| 100 | +} |
| 101 | + |
| 102 | +public sealed partial class TermSuggest : |
| 103 | + ISuggest |
| 104 | +{ |
| 105 | + [JsonInclude, JsonPropertyName("length")] |
| 106 | + public int Length { get; init; } |
| 107 | + |
| 108 | + [JsonInclude, JsonPropertyName("offset")] |
| 109 | + public int Offset { get; init; } |
| 110 | + |
| 111 | + [JsonInclude, JsonPropertyName("options"), SingleOrManyCollectionConverter(typeof(TermSuggestOption))] |
| 112 | + public IReadOnlyCollection<TermSuggestOption> Options { get; init; } |
| 113 | + |
| 114 | + [JsonInclude, JsonPropertyName("text")] |
| 115 | + public string Text { get; init; } |
| 116 | +} |
| 117 | + |
| 118 | +public sealed partial class TermSuggestOption |
| 119 | +{ |
| 120 | + [JsonInclude, JsonPropertyName("collate_match")] |
| 121 | + public bool? CollateMatch { get; init; } |
| 122 | + |
| 123 | + [JsonInclude, JsonPropertyName("freq")] |
| 124 | + public long Freq { get; init; } |
| 125 | + |
| 126 | + [JsonInclude, JsonPropertyName("highlighted")] |
| 127 | + public string? Highlighted { get; init; } |
| 128 | + |
| 129 | + [JsonInclude, JsonPropertyName("score")] |
| 130 | + public double Score { get; init; } |
| 131 | + |
| 132 | + [JsonInclude, JsonPropertyName("text")] |
| 133 | + public string Text { get; init; } |
| 134 | +} |
| 135 | + |
| 136 | +public sealed partial class PhraseSuggest : |
| 137 | + ISuggest |
| 138 | +{ |
| 139 | + [JsonInclude, JsonPropertyName("length")] |
| 140 | + public int Length { get; init; } |
| 141 | + |
| 142 | + [JsonInclude, JsonPropertyName("offset")] |
| 143 | + public int Offset { get; init; } |
| 144 | + |
| 145 | + [JsonInclude, JsonPropertyName("options"), SingleOrManyCollectionConverter(typeof(PhraseSuggestOption))] |
| 146 | + public IReadOnlyCollection<PhraseSuggestOption> Options { get; init; } |
| 147 | + |
| 148 | + [JsonInclude, JsonPropertyName("text")] |
| 149 | + public string Text { get; init; } |
| 150 | +} |
| 151 | + |
| 152 | +public sealed partial class PhraseSuggestOption |
| 153 | +{ |
| 154 | + [JsonInclude, JsonPropertyName("collate_match")] |
| 155 | + public bool? CollateMatch { get; init; } |
| 156 | + |
| 157 | + [JsonInclude, JsonPropertyName("highlighted")] |
| 158 | + public string? Highlighted { get; init; } |
| 159 | + |
| 160 | + [JsonInclude, JsonPropertyName("score")] |
| 161 | + public double Score { get; init; } |
| 162 | + |
| 163 | + [JsonInclude, JsonPropertyName("text")] |
| 164 | + public string Text { get; init; } |
| 165 | +} |
| 166 | + |
| 167 | +public sealed partial class CompletionSuggest<TDocument> : |
| 168 | + ISuggest |
| 169 | +{ |
| 170 | + [JsonInclude, JsonPropertyName("length")] |
| 171 | + public int Length { get; init; } |
| 172 | + |
| 173 | + [JsonInclude, JsonPropertyName("offset")] |
| 174 | + public int Offset { get; init; } |
| 175 | + |
| 176 | + [JsonInclude, JsonPropertyName("options"), GenericConverter(typeof(SingleOrManyCollectionConverter<>), unwrap:true)] |
| 177 | + public IReadOnlyCollection<CompletionSuggestOption<TDocument>> Options { get; init; } |
| 178 | + |
| 179 | + [JsonInclude, JsonPropertyName("text")] |
| 180 | + public string Text { get; init; } |
| 181 | +} |
| 182 | + |
| 183 | +public sealed partial class CompletionSuggestOption<TDocument> |
| 184 | +{ |
| 185 | + [JsonInclude, JsonPropertyName("_id")] |
| 186 | + public string? Id { get; init; } |
| 187 | + |
| 188 | + [JsonInclude, JsonPropertyName("_index")] |
| 189 | + public string? Index { get; init; } |
| 190 | + |
| 191 | + [JsonInclude, JsonPropertyName("_routing")] |
| 192 | + public string? Routing { get; init; } |
| 193 | + |
| 194 | + [JsonInclude, JsonPropertyName("_score")] |
| 195 | + public double? Score0 { get; init; } |
| 196 | + |
| 197 | + [JsonInclude, JsonPropertyName("_source")] |
| 198 | + [SourceConverter] |
| 199 | + public TDocument? Source { get; init; } |
| 200 | + |
| 201 | + [JsonInclude, JsonPropertyName("collate_match")] |
| 202 | + public bool? CollateMatch { get; init; } |
| 203 | + |
| 204 | + [JsonInclude, JsonPropertyName("contexts")] |
| 205 | + public IReadOnlyDictionary<string, IReadOnlyCollection<Context>>? Contexts { get; init; } |
| 206 | + |
| 207 | + [JsonInclude, JsonPropertyName("fields")] |
| 208 | + public IReadOnlyDictionary<string, object>? Fields { get; init; } |
| 209 | + |
| 210 | + [JsonInclude, JsonPropertyName("score")] |
| 211 | + public double? Score { get; init; } |
| 212 | + |
| 213 | + [JsonInclude, JsonPropertyName("text")] |
| 214 | + public string Text { get; init; } |
| 215 | +} |
0 commit comments