예제 #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());
        }
예제 #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);
        }
예제 #3
0
파일: ESTest.cs 프로젝트: lulzzz/WCloud
        /// <summary>
        /// 搜索建议
        /// https://elasticsearch.cn/article/142
        /// https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/suggest-usage.html
        /// https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search-suggesters.html
        /// https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search-suggesters-completion.html
        /// </summary>
        static ISuggestDictionary <T> SuggestSample <T>(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, IESIndex
        {
            var sd = new TermSuggesterDescriptor <T>();

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

            var csd = new CompletionSuggesterDescriptor <T>();
            var psd = new PhraseSuggesterDescriptor <T>();

            var response = client.Search <T>(s => s.Suggest(ss => ss
                                                            .Term("my-term-suggest", t => t
                                                                  .MaxEdits(1)
                                                                  .MaxInspections(2)
                                                                  .MaxTermFrequency(3)
                                                                  .MinDocFrequency(4)
                                                                  .MinWordLength(5)
                                                                  .PrefixLength(6)
                                                                  .SuggestMode(SuggestMode.Always)
                                                                  .Analyzer("standard")
                                                                  .Field("")
                                                                  .ShardSize(7)
                                                                  .Size(8)
                                                                  .Text(text)
                                                                  )
                                                            .Completion("my-completion-suggest", c => c
                                                                        .Contexts(ctxs => ctxs
                                                                                  .Context("color",
                                                                                           ctx => ctx.Context(text)
                                                                                           )
                                                                                  )
                                                                        .Fuzzy(f => f
                                                                               .Fuzziness(Fuzziness.Auto)
                                                                               .MinLength(1)
                                                                               .PrefixLength(2)
                                                                               .Transpositions()
                                                                               .UnicodeAware(false)
                                                                               )
                                                                        .Analyzer("simple")
                                                                        .Field("")
                                                                        .Size(8)
                                                                        .Prefix(text)
                                                                        )
                                                            .Phrase("my-phrase-suggest", ph => ph
                                                                    .Collate(c => c
                                                                             .Query(q => q
                                                                                    .Source("{ \"match\": { \"{{field_name}}\": \"{{suggestion}}\" }}")
                                                                                    )
                                                                             .Params(p => p.Add("field_name", "title"))
                                                                             .Prune()
                                                                             )
                                                                    .Confidence(10.1)
                                                                    .DirectGenerator(d => d
                                                                                     .Field("")
                                                                                     )
                                                                    .GramSize(1)
                                                                    .Field("")
                                                                    .Text(text)
                                                                    .RealWordErrorLikelihood(0.5)
                                                                    )
                                                            ));

            response.ThrowIfException();

            return(response.Suggest);
        }