コード例 #1
0
        public List <SearchResult> Search(string keywords, int Top = 20, string HighLightAttr = null, RenderContext context = null)
        {
            if (context == null)
            {
                context = new RenderContext()
                {
                    WebSite = this.SiteDb.WebSite, Culture = this.SiteDb.WebSite.DefaultCulture
                };
            }

            if (string.IsNullOrEmpty(keywords))
            {
                return(new List <SearchResult>());
            }

            var ids = this.IndexData.FindAll(keywords);

            List <SearchResult> result = new List <SearchResult>();

            int icount = 0;

            foreach (var item in ids)
            {
                var meta = this.IndexData.GetMeta(item);
                if (meta != null)
                {
                    var searchresult = SearchResultConverter.ConvertTo(context, meta);
                    if (searchresult != null)
                    {
                        var find = result.Find(o => o.Id == searchresult.Id);
                        if (find != null)
                        {
                            find.Found.AddRange(searchresult.Found);
                        }
                        else
                        {
                            result.Add(searchresult);
                            icount += 1;
                        }
                    }

                    if (icount >= Top)
                    {
                        break;
                    }
                }
            }

            SetData(result, keywords, context.Culture, HighLightAttr);

            this.Log.Add(new SearchLog()
            {
                IP = context.Request.IP, Keywords = keywords, Time = DateTime.Now, DocFound = ids.Count, ResultCount = result.Count(), Skip = 0
            });

            return(result);
        }
コード例 #2
0
        public PagedResult SearchByFolders(List <Guid> FolderIds, string keywords, int pagesize = 20, int pagenumber = 1, string HighLightAttr = null, RenderContext context = null)
        {
            if (context == null)
            {
                context = new RenderContext()
                {
                    WebSite = this.SiteDb.WebSite, Culture = this.SiteDb.WebSite.DefaultCulture
                };
            }
            PagedResult pageresult = new PagedResult();

            List <string> words   = ToWordList(keywords);
            string        baseurl = this.SiteDb.WebSite.BaseUrl();

            if (pagesize <= 0)
            {
                pagesize = 20;
            }

            int skip = (pagenumber - 1) * pagesize;

            if (skip < 0)
            {
                skip = 0;
            }

            var ids = this.IndexData.FindAll(keywords);

            pageresult.PageNumber = pagenumber;
            pageresult.PageSize   = pagesize;
            pageresult.TotalCount = ids.Count();
            pageresult.TotalPages = Lib.Helper.CalculationHelper.GetPageCount(pageresult.TotalCount, pagesize);

            List <SearchResult> result = new List <SearchResult>();
            int totalcount             = skip + pagesize;
            int icount = 0;

            foreach (var item in ids)
            {
                var meta = this.IndexData.GetMeta(item);
                if (meta != null)
                {
                    var searchresult = SearchResultConverter.ConvertTo(context, meta);

                    if (searchresult != null)
                    {
                        // if already set,
                        var resultfind = result.Find(o => o.Id == searchresult.Id);
                        if (resultfind != null)
                        {
                            continue;
                        }

                        // try to find textcontent.....
                        var contentresults = searchresult.Found.FindAll(o => o.ObjectType.ToLower() == "textcontent");
                        if (contentresults == null || !contentresults.Any())
                        {
                            continue;
                        }

                        foreach (var contentresult in contentresults)
                        {
                            var content = this.SiteDb.TextContent.Get(contentresult.ObjectId);

                            if (content != null && FolderIds.Contains(content.FolderId))
                            {
                                var contentview = Sites.Helper.ContentHelper.ToView(content, context.Culture, null);
                                searchresult.Title = this.HighLight(Helper.ContentHelper.GetTitle(this.SiteDb, contentview), words, HighLightAttr);

                                var fulltext = string.Join(" ", contentview.Values.Values);

                                searchresult.Summary = this.GetSummary(fulltext, words, HighLightAttr, 250);

                                if (!searchresult.Url.ToLower().StartsWith("http://") && !searchresult.Url.ToLower().StartsWith("https://"))
                                {
                                    searchresult.Url = Lib.Helper.UrlHelper.Combine(baseurl, searchresult.Url);
                                }
                                result.Add(searchresult);
                            }
                        }
                    }
                }

                if (icount >= totalcount)
                {
                    break;
                }
            }

            var resultset = result.Skip(skip).Take(pagesize).ToList();

            this.Log.Add(new SearchLog()
            {
                IP = context.Request.IP, Keywords = keywords, Time = DateTime.Now, DocFound = ids.Count, ResultCount = resultset.Count(), Skip = skip
            });

            pageresult.DataList = resultset.ToList <object>();
            return(pageresult);
        }
コード例 #3
0
        public PagedResult SearchWithPaging(string keywords, int pagesize = 20, int pagenumber = 1, string HighLightAttr = null, RenderContext context = null)
        {
            if (context == null)
            {
                context = new RenderContext()
                {
                    WebSite = this.SiteDb.WebSite, Culture = this.SiteDb.WebSite.DefaultCulture
                };
            }
            PagedResult pageresult = new PagedResult();

            if (pagesize <= 0)
            {
                pagesize = 20;
            }

            int skip = (pagenumber - 1) * pagesize;

            if (skip < 0)
            {
                skip = 0;
            }

            var ids = this.IndexData.FindAll(keywords);

            pageresult.PageNumber = pagenumber;
            pageresult.PageSize   = pagesize;
            pageresult.TotalCount = ids.Count();
            pageresult.TotalPages = Lib.Helper.CalculationHelper.GetPageCount(pageresult.TotalCount, pagesize);

            List <SearchResult> result = new List <SearchResult>();

            int totalcount = skip + pagesize;
            int icount     = 0;

            foreach (var item in ids)
            {
                var meta = this.IndexData.GetMeta(item);
                if (meta != null)
                {
                    var searchresult = SearchResultConverter.ConvertTo(context, meta);
                    if (searchresult != null)
                    {
                        var find = result.Find(o => o.Id == searchresult.Id);
                        if (find != null)
                        {
                            find.Found.AddRange(searchresult.Found);
                        }
                        else
                        {
                            result.Add(searchresult);
                            icount += 1;
                        }
                    }
                }

                if (icount >= totalcount)
                {
                    break;
                }
            }

            var resultset = result.Skip(skip).Take(pagesize).ToList();

            // set summary.
            SetData(resultset, keywords, context.Culture, HighLightAttr);


            this.Log.Add(new SearchLog()
            {
                IP = context.Request.IP, Keywords = keywords, Time = DateTime.Now, DocFound = ids.Count, ResultCount = resultset.Count(), Skip = skip
            });

            pageresult.DataList = resultset.ToList <object>();
            return(pageresult);
        }