|
1 | 1 | using System;
|
2 | 2 | using System.Collections.Generic;
|
3 | 3 | using Newtonsoft.Json;
|
| 4 | +using System.Linq; |
4 | 5 |
|
5 | 6 | namespace Nest
|
6 | 7 | {
|
7 | 8 | [JsonObject(MemberSerialization = MemberSerialization.OptIn)]
|
8 | 9 | [JsonConverter(typeof(ReadAsTypeJsonConverter<CompletionSuggester>))]
|
9 | 10 | public interface ICompletionSuggester : ISuggester
|
10 | 11 | {
|
11 |
| - [JsonProperty(PropertyName = "fuzzy")] |
| 12 | + [JsonIgnore] |
| 13 | + string Prefix { get; set; } |
| 14 | + |
| 15 | + [JsonIgnore] |
| 16 | + string Regex { get; set; } |
| 17 | + |
| 18 | + [JsonProperty("payload")] |
| 19 | + Fields Payload { get; set; } |
| 20 | + |
| 21 | + [JsonProperty("fuzzy")] |
12 | 22 | IFuzzySuggester Fuzzy { get; set; }
|
13 | 23 |
|
14 |
| - [JsonProperty("context")] |
15 |
| - IDictionary<string, object> Context { get; set; } |
| 24 | + [JsonProperty("contexts")] |
| 25 | + IDictionary<string, IList<ISuggestContextQuery>> Contexts { get; set; } |
16 | 26 | }
|
17 | 27 |
|
18 | 28 | public class CompletionSuggester : SuggesterBase, ICompletionSuggester
|
19 | 29 | {
|
20 | 30 | public IFuzzySuggester Fuzzy { get; set; }
|
21 |
| - public IDictionary<string, object> Context { get; set; } |
| 31 | + |
| 32 | + public IDictionary<string, IList<ISuggestContextQuery>> Contexts { get; set; } |
| 33 | + |
| 34 | + public string Prefix { get; set; } |
| 35 | + |
| 36 | + public string Regex { get; set; } |
| 37 | + |
| 38 | + public Fields Payload { get; set; } |
22 | 39 | }
|
23 | 40 |
|
24 |
| - public class CompletionSuggesterDescriptor<T> : SuggestDescriptorBase<CompletionSuggesterDescriptor<T>, ICompletionSuggester, T>, ICompletionSuggester |
| 41 | + public class CompletionSuggesterDescriptor<T> : SuggestDescriptorBase<CompletionSuggesterDescriptor<T>, ICompletionSuggester, T>, ICompletionSuggester |
25 | 42 | where T : class
|
26 | 43 | {
|
27 | 44 | IFuzzySuggester ICompletionSuggester.Fuzzy { get; set; }
|
28 | 45 |
|
29 |
| - IDictionary<string, object> ICompletionSuggester.Context { get; set; } |
| 46 | + IDictionary<string, IList<ISuggestContextQuery>> ICompletionSuggester.Contexts { get; set; } |
| 47 | + |
| 48 | + string ICompletionSuggester.Prefix { get; set; } |
| 49 | + |
| 50 | + string ICompletionSuggester.Regex { get; set; } |
| 51 | + |
| 52 | + Fields ICompletionSuggester.Payload { get; set; } |
| 53 | + |
| 54 | + public CompletionSuggesterDescriptor<T> Prefix(string prefix) => Assign(a => a.Prefix = prefix); |
| 55 | + |
| 56 | + public CompletionSuggesterDescriptor<T> Regex(string regex) => Assign(a => a.Regex = regex); |
30 | 57 |
|
31 | 58 | public CompletionSuggesterDescriptor<T> Fuzzy(Func<FuzzySuggestDescriptor<T>, IFuzzySuggester> selector = null) =>
|
32 | 59 | Assign(a => a.Fuzzy = selector.InvokeOrDefault(new FuzzySuggestDescriptor<T>()));
|
33 | 60 |
|
34 |
| - public CompletionSuggesterDescriptor<T> Context(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> selector) => |
35 |
| - Assign(a => a.Context = selector?.Invoke(new FluentDictionary<string, object>())); |
36 |
| - |
| 61 | + public CompletionSuggesterDescriptor<T> Contexts(Func<SuggestContextQueriesDescriptor<T>, IPromise<IDictionary<string, IList<ISuggestContextQuery>>>> contexts) => |
| 62 | + Assign(a => a.Contexts = contexts?.Invoke(new SuggestContextQueriesDescriptor<T>()).Value); |
| 63 | + |
| 64 | + public CompletionSuggesterDescriptor<T> Payload(Func<FieldsDescriptor<T>, IPromise<Fields>> payload) => |
| 65 | + Assign(a => a.Payload = payload?.Invoke(new FieldsDescriptor<T>())?.Value); |
| 66 | + |
| 67 | + public CompletionSuggesterDescriptor<T> Payload(Fields payload) => Assign(a => a.Payload = payload); |
| 68 | + } |
| 69 | + |
| 70 | + public class SuggestContextQueriesDescriptor<T> |
| 71 | + : DescriptorPromiseBase<SuggestContextQueriesDescriptor<T>, IDictionary<string, IList<ISuggestContextQuery>>> |
| 72 | + { |
| 73 | + public SuggestContextQueriesDescriptor() : base(new Dictionary<string, IList<ISuggestContextQuery>>()) { } |
| 74 | + |
| 75 | + public SuggestContextQueriesDescriptor<T> Category(string name, params Func<CategorySuggestContextQueryDescriptor<T>, ISuggestContextQuery>[] categoryDescriptors) => |
| 76 | + AddContextQueries(name, categoryDescriptors?.Select(d => d?.Invoke(new CategorySuggestContextQueryDescriptor<T>())).ToList()); |
| 77 | + |
| 78 | + public SuggestContextQueriesDescriptor<T> GeoLocation(string name, params Func<GeoSuggestContextQueryDescriptor<T>, ISuggestContextQuery>[] geoDescriptors) => |
| 79 | + AddContextQueries(name, geoDescriptors?.Select(d => d?.Invoke(new GeoSuggestContextQueryDescriptor<T>())).ToList()); |
| 80 | + |
| 81 | + private SuggestContextQueriesDescriptor<T> AddContextQueries(string name, List<ISuggestContextQuery> contextQueries) => |
| 82 | + contextQueries == null ? this : this.Assign(a => a.Add(name, contextQueries)); |
37 | 83 | }
|
38 | 84 | }
|
0 commit comments