예제 #1
0
        /// <summary>
        /// 搜索建议
        /// </summary>
        public static IDictionary <string, Suggest[]> TermSuggest <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 TermSuggesterDescriptor <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).Term("term_suggest", f => sd));

            response.ThrowIfException();

            return(response.Suggestions);
        }
예제 #2
0
        public static SuggestDictionary <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, IElasticSearchIndex
        {
            var sd = new TermSuggesterDescriptor <T>();

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

            new CompletionSuggesterDescriptor <T>();
            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);
        }