Esempio n. 1
0
        public static async Task <List <string> > SimpleCompletionSuggest <T>(this IElasticClient client,
                                                                              string index,
                                                                              string keyword, string analyzer = null, int size = 20)
            where T : CompletionSuggestIndexBase
        {
            var data = new List <string>();

            if (!ValidateHelper.IsPlumpString(keyword))
            {
                return(data);
            }

            var sd = new CompletionSuggesterDescriptor <T>();

            sd = sd.Field(f => f.CompletionSearchTitle).Text(keyword);
            if (ValidateHelper.IsPlumpString(analyzer))
            {
                sd = sd.Analyzer(analyzer);
            }
            //允许错4个单词
            sd = sd.Fuzzy(f => f.Fuzziness(Fuzziness.EditDistance(4)));
            sd = sd.Size(size);

            var s_name = "p";

            var response = await client.SuggestAsync <T>(x => x.Index(index).Completion(s_name, f => sd));

            response.ThrowIfException();

            var list = response.Suggestions?[s_name]?.FirstOrDefault()?.Options?.ToList();

            if (!ValidateHelper.IsPlumpList(list))
            {
                return(data);
            }
            var sggs = list.OrderByDescending(x => x.Score).Select(x => x.Text);

            data.AddRange(sggs);

            return(data.Where(x => ValidateHelper.IsPlumpString(x)).Distinct().ToList());
        }
Esempio n. 2
0
        /// <summary>
        /// 搜索建议
        /// </summary>
        public static IDictionary <string, Suggest[]> CompletionSuggest <T>(this IElasticClient client,
                                                                            string index,
                                                                            Expression <Func <T, object> > targetField, string text, string analyzer = null,
                                                                            string highlight_pre = "<em>", string hightlight_post = "</em>", int size = 20)
            where T : class, IElasticSearchIndex
        {
            var sd = new CompletionSuggesterDescriptor <T>();

            sd = sd.Field(targetField).Text(text);
            if (ValidateHelper.IsPlumpString(analyzer))
            {
                sd = sd.Analyzer(analyzer);
            }
            sd = sd.Size(size);

            var response = client.Suggest <T>(x => x.Index(index).Completion("completion_suggest", f => sd));

            response.ThrowIfException();

            return(response.Suggestions);
        }