Example #1
0
 private HighlightDescriptor <T> Get_HL(HighlightDescriptor <T> hl, QueryDes qd) => hl
 .PreTags(qd.HLPreTag)
 .PostTags(qd.HLPostTag)
 .Fields(
     f => f.Field("oc_code"),
     f => f.Field("oc_number")
     );
        /// <summary>
        /// 私有方法,构造高亮查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pageParams"></param>
        /// <param name="searchDescriptor"></param>
        private void BuildHighLightQuery <T>(IPageParam pageParams, ref SearchDescriptor <T> searchDescriptor) where T : class
        {
            int keysLength = (pageParams.Highlight?.Keys?.Length).Value;

            Func <HighlightFieldDescriptor <T>, IHighlightField>[] filedDesciptor = new Func <HighlightFieldDescriptor <T>, IHighlightField> [keysLength];
            int keysIndex = 0;

            foreach (string key in pageParams.Highlight?.Keys)
            {
                filedDesciptor[keysIndex] = hf => hf.Field(key)//简介高亮
                                            .HighlightQuery(q => q
                                                            .Match(m => m
                                                                   .Field(key)
                                                                   .Query(pageParams.KeyWord)));
                keysIndex++;
            }
            //构造hightlight
            IHighlight highLight = new HighlightDescriptor <T>()
                                   .PreTags(pageParams.Highlight.PreTags)
                                   .PostTags(pageParams.Highlight.PostTags)
                                   .Fields(filedDesciptor);

            //设置高亮
            searchDescriptor = searchDescriptor.Highlight(s => highLight);
        }
Example #3
0
 private static HighlightDescriptor <Person> GetHLDescriptor(HighlightDescriptor <Person> hl, SearchParam p)
 {
     return(hl.PreTags("<b>")
            .PostTags("</b>")
            .Fields(f => f.Field("company.std"),
                    f => f.Field("company.max"),
                    f => f.Field(fld => fld.company)));
 }
Example #4
0
        /// <summary>
        /// Allow to highlight search results on one or more fields. The implementation uses the either lucene fast-vector-highlighter or highlighter.
        /// </summary>
        public SearchDescriptor <T> Highlight(Action <HighlightDescriptor <T> > highlightDescriptor)
        {
            highlightDescriptor.ThrowIfNull("highlightDescriptor");
            var d = new HighlightDescriptor <T>();

            highlightDescriptor(d);
            Self.Highlight = d;
            return(this);
        }
 private IHighlight highlightSelector(HighlightDescriptor <post> arg)
 {
     return(arg
            .PreTags(Highlight_Token)
            .PostTags(Highlight_Token)
            .Encoder(HighlighterEncoder.Default)
            .Fields(
                fs => fs.Field(p => p.body),
                fs => fs.Field(p => p.title)
                ));
 }
 protected override HighlightDescriptor <BookIndexItem> Highlight(
     HighlightDescriptor <BookIndexItem> highlight)
 => highlight
 .PreTags(PreHighlightTag)
 .PostTags(PostHighlightTag)
 .Fields(
     fs => fs.Field(f => f.Title),
     fs => fs.Field(f => f.Description),
     fs => fs.Field(f => f.Authors),
     fs => fs.Field(f => f.Tags),
     fs => fs.Field(f => f.DatePublication),
     fs => fs.Field(f => f.Issue),
     fs => fs.Field(f => f.Publisher),
     fs => fs.Field(f => f.Language));
Example #7
0
        public virtual async Task <ISearchResponse <T> > SearchAsync <T, TKey>(string indexName, SearchDescriptor <T> query, int skip, int size, string[] includeFields = null,
                                                                               string preTags   = "<strong style=\"color: red;\">", string productTags = "</strong>",
                                                                               bool disableHigh = false, params string[] highField) where T : ElasticEntity <TKey>
        {
            query.Index(indexName);
            var highdes = new HighlightDescriptor <T>();

            if (disableHigh)
            {
                preTags     = "";
                productTags = "";
            }
            highdes.PreTags(preTags).PostTags(productTags);

            var ishigh = highField != null && highField.Length > 0;

            var hfs = new List <Func <HighlightFieldDescriptor <T>, IHighlightField> >();

            //Pagination
            query.Skip(skip).Take(size);
            //Keyword highlighting
            if (ishigh)
            {
                foreach (var s in highField)
                {
                    hfs.Add(f => f.Field(s));
                }
            }

            highdes.Fields(hfs.ToArray());
            query.Highlight(h => highdes);
            if (includeFields != null)
            {
                query.Source(ss => ss.Includes(ff => ff.Fields(includeFields.ToArray())));
            }


            var data     = JsonConvert.SerializeObject(query);
            var response = await ElasticSearchClient.SearchAsync <T>(query);


            return(response);
        }
        /// <summary>
        /// 构造高亮查询
        /// </summary>
        private void BuildHighLightQuery <T>(IPageParam param, ref SearchDescriptor <T> searchRequest) where T : class
        {
            var keysLength      = param.Highlight?.Keys?.Length ?? 0;
            var fieldDescriptor = new Func <HighlightFieldDescriptor <T>, IHighlightField> [keysLength];
            var keysIndex       = 0;

            foreach (var key in param.Highlight?.Keys)
            {
                fieldDescriptor[keysIndex] = hf => hf.Field(key)
                                             .HighlightQuery(q => q.Match(m => m.Field(key).Query(param.Keyword)));
                keysIndex++;
            }

            IHighlight highlight = new HighlightDescriptor <T>()
                                   .PreTags(param.Highlight.PreTags)
                                   .PostTags(param.Highlight.PostTags)
                                   .Fields(fieldDescriptor);

            searchRequest = searchRequest.Highlight(s => highlight);
        }
        /// <summary>
        /// search
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="indexName"></param>
        /// <param name="query"></param>
        /// <param name="skip">skip num</param>
        /// <param name="size">return document size</param>
        /// <param name="includeFields">return fields</param>
        /// <param name="preTags">Highlight tags</param>
        /// <param name="postTags">Highlight tags</param>
        /// <param name="disableHigh"></param>
        /// <param name="highField">Highlight fields</param>
        /// <returns></returns>
        public virtual async Task <ISearchResponse <T> > SearchAsync <T, TKey>(string indexName, SearchDescriptor <T> query,
                                                                               int skip, int size, string[] includeFields = null,
                                                                               string preTags = "<strong style=\"color: red;\">", string postTags = "</strong>", bool disableHigh = false,
                                                                               params string[] highField) where T : class
        {
            query.Index(indexName);
            var highlight = new HighlightDescriptor <T>();

            if (disableHigh)
            {
                preTags  = "";
                postTags = "";
            }

            highlight.PreTags(preTags).PostTags(postTags);

            var isHigh = highField != null && highField.Length > 0;

            var hfs = new List <Func <HighlightFieldDescriptor <T>, IHighlightField> >();

            //分页
            query.Skip(skip).Take(size);
            //关键词高亮
            if (isHigh)
            {
                foreach (var s in highField)
                {
                    hfs.Add(f => f.Field(s));
                }
            }

            highlight.Fields(hfs.ToArray());
            query.Highlight(h => highlight);
            if (includeFields != null)
            {
                query.Source(ss => ss.Includes(ff => ff.Fields(includeFields.ToArray())));
            }
            var response = await _esClient.SearchAsync <T>(query);

            return(response);
        }
Example #10
0
        public static HighlightDescriptor <T> GetHighlight <T>(bool enable)
            where T : class
        {
            HighlightDescriptor <T> hh = new HighlightDescriptor <T>();

            if (enable)
            {
                hh = hh.Order(HighlighterOrder.Score)
                     .PreTags("<highl>")
                     .PostTags("</highl>")
                     .Fields(ff => ff
                             .Field("*")
                             .RequireFieldMatch(false)
                             .Type(HighlighterType.Unified)
                             .FragmentSize(100)
                             .NumberOfFragments(3)
                             .Fragmenter(HighlighterFragmenter.Span)
                             .BoundaryScanner(BoundaryScanner.Sentence)
                             .BoundaryScannerLocale("cs_CZ")
                             );
            }
            return(hh);
        }
 protected abstract HighlightDescriptor <TContentIndexItem> Highlight(
     HighlightDescriptor <TContentIndexItem> highlight);
Example #12
0
 private static HighlightDescriptor <T> HL_Create <T>(HighlightDescriptor <T> hl, IEnumerable <string> names) where T : class => hl
 .PreTags("<font color=\"red\">")
 .PostTags("</font>")
 .Fields(names.Select <string, Func <HighlightFieldDescriptor <T>, IHighlightField> >(n => f => f.Field(n)).ToArray());