public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { var suggestRequest = value as ISuggestRequest; if (suggestRequest == null) return; var dict = new Dictionary<string, object>(); if (!suggestRequest.GlobalText.IsNullOrEmpty()) { writer.WritePropertyName("text"); writer.WriteValue(suggestRequest.GlobalText); } if (suggestRequest.Suggest != null) { foreach (var kv in suggestRequest.Suggest) { var item = kv.Value; var bucket = new SuggestBucket() { Text = item.Text }; var completion = item as ICompletionSuggester; if (completion != null) bucket.Completion = completion; var phrase = item as IPhraseSuggester; if (phrase != null) bucket.Phrase = phrase; var term = item as ITermSuggester; if (term != null) bucket.Term = term; writer.WritePropertyName(kv.Key); serializer.Serialize(writer, bucket); dict.Add(kv.Key, bucket); } } }
private SuggestContainerDescriptor <T> AssignToBucket <TSuggester>(string name, TSuggester suggester, Action <SuggestBucket, TSuggester> assign) where TSuggester : ISuggester { var bucket = new SuggestBucket(); assign(bucket, suggester); return(Assign(name, bucket)); }
private SuggestDescriptor <T> AssignToBucket <TSuggester>(string name, TSuggester suggester, Action <SuggestBucket, TSuggester> assign) where TSuggester : ISuggester { var bucket = new SuggestBucket { Text = suggester.Text }; assign(bucket, suggester); return(Assign(a => a.Suggest.Add(name, bucket))); }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { var suggestRequest = value as ISuggestRequest; if (suggestRequest == null) { return; } var dict = new Dictionary <string, object>(); if (!suggestRequest.GlobalText.IsNullOrEmpty()) { writer.WritePropertyName("text"); writer.WriteValue(suggestRequest.GlobalText); } if (suggestRequest.Suggest != null) { foreach (var kv in suggestRequest.Suggest) { var item = kv.Value; var bucket = new SuggestBucket() { Text = item.Text }; var completion = item as ICompletionSuggester; if (completion != null) { bucket.Completion = completion; } var phrase = item as IPhraseSuggester; if (phrase != null) { bucket.Phrase = phrase; } var term = item as ITermSuggester; if (term != null) { bucket.Term = term; } writer.WritePropertyName(kv.Key); serializer.Serialize(writer, bucket); dict.Add(kv.Key, bucket); } } }
/// <summary> /// The term suggester suggests terms based on edit distance. The provided suggest text is analyzed before terms are suggested. /// 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. /// </summary> public SuggestDescriptor <T> Term(string name, Func <TermSuggestDescriptor <T>, TermSuggestDescriptor <T> > suggest) { name.ThrowIfNullOrEmpty("name"); suggest.ThrowIfNull("suggest"); var desc = new TermSuggestDescriptor <T>(); var item = suggest(desc); ITermSuggester i = item; var bucket = new SuggestBucket { Text = i._Text, Term = item }; this._Suggest.Add(name, bucket); return(this); }
/// <summary> /// The completion suggester is a so-called prefix suggester. /// It does not do spell correction like the term or phrase suggesters but allows basic auto-complete functionality. /// </summary> public SearchDescriptor <T> SuggestCompletion(string name, Func <CompletionSuggesterDescriptor <T>, CompletionSuggesterDescriptor <T> > suggest) => Assign(a => { name.ThrowIfNullOrEmpty(nameof(name)); suggest.ThrowIfNull(nameof(suggest)); if (a.Suggest == null) { a.Suggest = new Dictionary <string, ISuggestBucket>(); } var desc = new CompletionSuggesterDescriptor <T>(); var item = suggest(desc); ICompletionSuggester i = item; var bucket = new SuggestBucket { Text = i.Text, Completion = item }; a.Suggest.Add(name, bucket); });
//TODO this is ugly public static object GetCustomJson(ISuggestRequest suggestRequest) { if (suggestRequest == null || (suggestRequest.GlobalText.IsNullOrEmpty() && suggestRequest.Suggest == null)) { return(null); } var dict = new Dictionary <string, object>(); if (!suggestRequest.GlobalText.IsNullOrEmpty()) { dict.Add("text", suggestRequest.GlobalText); } if (suggestRequest.Suggest != null) { foreach (var kv in suggestRequest.Suggest) { var item = kv.Value; var bucket = new SuggestBucket() { Text = item.Text }; var completion = item as ICompletionSuggester; if (completion != null) { bucket.Completion = completion; } var phrase = item as IPhraseSuggester; if (phrase != null) { bucket.Phrase = phrase; } var term = item as ITermSuggester; if (term != null) { bucket.Term = term; } dict.Add(kv.Key, bucket); } } return(dict); }
///// <summary> ///// To avoid repetition of the suggest text, it is possible to define a global text. ///// </summary> //public SearchDescriptor<T> SuggestGlobalText(string globalSuggestText) //{ // if (Self.Suggest == null) // Self.Suggest = new Dictionary<string, ISuggester>(); // Self.Suggest.Add("text", globalSuggestText); // return this; //} /// <summary> /// The term suggester suggests terms based on edit distance. The provided suggest text is analyzed before terms are suggested. /// 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. /// </summary> public SearchDescriptor <T> SuggestTerm(string name, Func <TermSuggestDescriptor <T>, TermSuggestDescriptor <T> > suggest) { name.ThrowIfNullOrEmpty("name"); suggest.ThrowIfNull("suggest"); if (Self.Suggest == null) { Self.Suggest = new Dictionary <string, ISuggestBucket>(); } var desc = new TermSuggestDescriptor <T>(); var item = suggest(desc); ITermSuggester i = item; var bucket = new SuggestBucket { Text = i.Text, Term = item }; Self.Suggest.Add(name, bucket); return(this); }
/// <summary> /// The completion suggester is a so-called prefix suggester. /// It does not do spell correction like the term or phrase suggesters but allows basic auto-complete functionality. /// </summary> public SuggestDescriptor <T> Completion(string name, Func <CompletionSuggestDescriptor <T>, CompletionSuggestDescriptor <T> > suggest) { name.ThrowIfNullOrEmpty("name"); suggest.ThrowIfNull("suggest"); if (this._Suggest == null) { this._Suggest = new Dictionary <string, object>(); } var desc = new CompletionSuggestDescriptor <T>(); var item = suggest(desc); ICompletionSuggester i = item; var bucket = new SuggestBucket { Text = i._Text, Completion = item }; this._Suggest.Add(name, bucket); return(this); }