|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Globalization; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using Elasticsearch.Net; |
| 7 | +using Newtonsoft.Json; |
| 8 | +using System.Linq.Expressions; |
| 9 | +using Nest.Resolvers; |
| 10 | +using Nest.Domain; |
| 11 | + |
| 12 | +namespace Nest |
| 13 | +{ |
| 14 | + [DescriptorFor("Suggest")] |
| 15 | + public partial class SuggestDescriptor<T> : |
| 16 | + IndicesOptionalPathDescriptor<SuggestDescriptor<T>, SuggestRequestParameters> |
| 17 | + , IPathInfo<SuggestRequestParameters> |
| 18 | + where T : class |
| 19 | + { |
| 20 | + internal IDictionary<string, object> _Suggest = new Dictionary<string, object>(); |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// To avoid repetition of the suggest text, it is possible to define a global text. |
| 24 | + /// </summary> |
| 25 | + public SuggestDescriptor<T> GlobalText(string globalSuggestText) |
| 26 | + { |
| 27 | + this._Suggest.Add("text", globalSuggestText); |
| 28 | + return this; |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// The term suggester suggests terms based on edit distance. The provided suggest text is analyzed before terms are suggested. |
| 33 | + /// The suggested terms are provided per analyzed suggest text token. The term suggester doesn’t take the query into account that is part of request. |
| 34 | + /// </summary> |
| 35 | + public SuggestDescriptor<T> Term(string name, Func<TermSuggestDescriptor<T>, TermSuggestDescriptor<T>> suggest) |
| 36 | + { |
| 37 | + name.ThrowIfNullOrEmpty("name"); |
| 38 | + suggest.ThrowIfNull("suggest"); |
| 39 | + var desc = new TermSuggestDescriptor<T>(); |
| 40 | + var item = suggest(desc); |
| 41 | + var bucket = new SuggestDescriptorBucket<T> { _Text = item._Text, TermSuggest = item }; |
| 42 | + this._Suggest.Add(name, bucket); |
| 43 | + return this; |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// The phrase suggester adds additional logic on top of the term suggester to select entire corrected phrases |
| 48 | + /// instead of individual tokens weighted based on ngram-langugage models. |
| 49 | + /// </summary> |
| 50 | + public SuggestDescriptor<T> Phrase(string name, Func<PhraseSuggestDescriptor<T>, PhraseSuggestDescriptor<T>> suggest) |
| 51 | + { |
| 52 | + name.ThrowIfNullOrEmpty("name"); |
| 53 | + suggest.ThrowIfNull("suggest"); |
| 54 | + if (this._Suggest == null) |
| 55 | + this._Suggest = new Dictionary<string, object>(); |
| 56 | + |
| 57 | + var desc = new PhraseSuggestDescriptor<T>(); |
| 58 | + var item = suggest(desc); |
| 59 | + var bucket = new SuggestDescriptorBucket<T> { _Text = item._Text, PhraseSuggest = item }; |
| 60 | + this._Suggest.Add(name, bucket); |
| 61 | + return this; |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// The completion suggester is a so-called prefix suggester. |
| 66 | + /// It does not do spell correction like the term or phrase suggesters but allows basic auto-complete functionality. |
| 67 | + /// </summary> |
| 68 | + public SuggestDescriptor<T> Completion(string name, Func<CompletionSuggestDescriptor<T>, CompletionSuggestDescriptor<T>> suggest) |
| 69 | + { |
| 70 | + name.ThrowIfNullOrEmpty("name"); |
| 71 | + suggest.ThrowIfNull("suggest"); |
| 72 | + if (this._Suggest == null) |
| 73 | + this._Suggest = new Dictionary<string, object>(); |
| 74 | + |
| 75 | + var desc = new CompletionSuggestDescriptor<T>(); |
| 76 | + var item = suggest(desc); |
| 77 | + var bucket = new SuggestDescriptorBucket<T> { _Text = item._Text, CompletionSuggest = item }; |
| 78 | + this._Suggest.Add(name, bucket); |
| 79 | + return this; |
| 80 | + } |
| 81 | + |
| 82 | + ElasticsearchPathInfo<SuggestRequestParameters> IPathInfo<SuggestRequestParameters>.ToPathInfo(IConnectionSettingsValues settings) |
| 83 | + { |
| 84 | + var pathInfo = base.ToPathInfo<SuggestRequestParameters>(settings, this._QueryString); |
| 85 | + pathInfo.HttpMethod = PathInfoHttpMethod.POST; |
| 86 | + |
| 87 | + return pathInfo; |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments