Exemple #1
0
    protected void Button45_Click(object sender, EventArgs e)
    {
        ArticleQuery query = new ArticleQuery();

        query.Title = "化工";
        SearchResultDataSet <Article> articles = HHOnline.SearchBarrel.NewsSearchManager.Search(query);
    }
Exemple #2
0
    protected void Button43_Click(object sender, EventArgs e)
    {
        ProductQuery query = new ProductQuery();

        query.ProductNameFilter = "黑 sd";
        SearchResultDataSet <Product> products = HHOnline.SearchBarrel.ProductSearchManager.Search(query);
    }
Exemple #3
0
    private void BindSearchResult()
    {
        string query = Request.QueryString["w"];

        if (!string.IsNullOrEmpty(query))
        {
            query = HttpUtility.UrlDecode(query);
            TextBox txt = sArticle.FindControl("txtSearch") as TextBox;
            if (txt != null)
            {
                txt.Text = query;
            }

            ArticleQuery pq = new ArticleQuery();
            pq.Title    = query;
            pq.PageSize = Int32.MaxValue;

            SearchResultDataSet <Article> articles = NewsSearchManager.Search(pq);
            List <Article> ps = articles.Records;

            if (ps == null || ps.Count == 0)
            {
                ltSearchDuration.Text = "搜索用时:" + articles.SearchDuration.ToString() + "秒";
                msgBox.ShowMsg("没有搜索到相应的资讯信息!", System.Drawing.Color.Gray);
            }
            else
            {
                msgBox.HideMsg();
                ltSearchDuration.Text   = "搜索用时:" + articles.SearchDuration.ToString() + "ms";
                cpArticle.DataSource    = ps;
                cpArticle.BindToControl = dlArticle;
                dlArticle.DataSource    = cpArticle.DataSourcePaged;
                dlArticle.DataBind();
            }
        }
    }
        public string RenderHTML()
        {
            if (_Cache.ContainsKey(_productID))
            {
                return(_Cache[_productID]);
            }
            ProductQuery query = new ProductQuery();
            Product      pT    = Products.GetProduct(_productID);

            query.ProductNameFilter = pT.ProductName;
            query.PageSize          = Int32.MaxValue;
            query.ProductOrderBy    = ProductOrderBy.DisplayOrder;
            query.SortOrder         = SortOrder.Descending;
            query.PageSize          = int.MaxValue;
            query.HasPublished      = true;
            SearchResultDataSet <Product> ps = ProductSearchManager.Search(query);

            if (ps == null || ps.Records == null || ps.Records.Count == 0 || (ps.Records.Count == 1 && ps.Records[0].ProductID == _productID))
            {
                return("<div class=\"nopiInfo\"><span>没有相关产品信息!</span></div>");
            }
            string         nav      = GlobalSettings.RelativeWebRoot + "pages/view.aspx?product-product";
            List <Product> psList   = null;
            Product        p        = null;
            StringBuilder  sb       = new StringBuilder();
            int            curCount = ps.Records.Count;

            psList = ps.Records.GetRange(0, Math.Min(curCount, _Max));
            sb.AppendLine("<table cellpadding=\"0\" cellspacing=\"0\" class=\"" + _CssClass + "\">");
            string pId    = string.Empty;
            int    length = psList.Count;
            string pn     = string.Empty;

            for (int i = 0; i < psList.Count; i++)
            {
                p = psList[i];
                if (p.ProductID == _productID)
                {
                    continue;
                }
                if (i % _Columns == 0)
                {
                    sb.AppendLine("<tr>");
                }
                pId = GlobalSettings.Encrypt(p.ProductID.ToString());
                sb.AppendLine("<td>");
                sb.AppendLine("<div class=\"piThumbnail\">" +
                              "<a href=\"" + nav + "&ID=" + pId + "\" target=\"_blank\">" +
                              "<div style=\"background-image:url(" + p.GetDefaultImageUrl(100, 100) + ")\" title=\"" + HtmlHelper.RemoveHtml(p.ProductName) + "\" >" +
                              "</div>" +
                              "</a>" +
                              "</div>");
                pn = HtmlHelper.RemoveHtml(p.ProductName);
                sb.AppendLine("<div class=\"piProductName\" title='" + pn + "'>" + GlobalSettings.SubString(pn, 15) + "</div>");
                sb.AppendLine("<div class=\"piPrice\">" + GetPrice(p.ProductID) + "</div>");
                sb.AppendLine("</td>");

                if (i % _Columns == _Columns - 1)
                {
                    sb.AppendLine("</tr>");
                }
            }

            sb.AppendLine("</table>");
            if (!_Cache.ContainsKey(_productID))
            {
                lock (_lock)
                    if (!_Cache.ContainsKey(_productID))
                    {
                        if (_Cache.Count > maxCached)
                        {
                            _Cache.Clear();
                        }
                        _Cache.Add(_productID, sb.ToString());
                    }
            }
            return(sb.ToString());
        }
        public static SearchResultDataSet <Article> Search(ArticleQuery query)
        {
            //触发事件
            GlobalEvents.UserSearch(query.Title);

            //索引文件不存在时,返回null
            if (!GlobalSettings.CheckFileExist(PhysicalIndexDirectory))
            {
                return(new SearchResultDataSet <Article>());
            }
            DateTime     startTime    = DateTime.Now;
            BooleanQuery currentQuery = new BooleanQuery();

            //CategoryID
            if (query.CategoryID.HasValue && query.CategoryID.Value != 0)
            {
                Term  categoryIDTearm = new Term(NewsIndexField.CategoryID, query.CategoryID.ToString());
                Query categoryIDQuery = new TermQuery(categoryIDTearm);
                currentQuery.Add(categoryIDQuery, BooleanClause.Occur.MUST);
            }

            //KeyWord
            if (!string.IsNullOrEmpty(query.Title))
            {
                query.Title = SearchHelper.LuceneKeywordsScrubber(query.Title);
                if (!string.IsNullOrEmpty(query.Title))
                {
                    string[] searchFieldsForKeyword = new string[4];
                    searchFieldsForKeyword[0] = NewsIndexField.Title;
                    searchFieldsForKeyword[1] = NewsIndexField.SubTitle;
                    searchFieldsForKeyword[2] = NewsIndexField.Abstract;
                    searchFieldsForKeyword[3] = NewsIndexField.Keywords;

                    MultiFieldQueryParser articleWordQueryParser = new MultiFieldQueryParser(searchFieldsForKeyword, SearchHelper.GetChineseAnalyzer());
                    articleWordQueryParser.SetLowercaseExpandedTerms(true);
                    articleWordQueryParser.SetDefaultOperator(QueryParser.OR_OPERATOR);

                    string keyWordsSplit    = SearchHelper.SplitKeywordsBySpace(query.Title);
                    Query  articleWordQuery = articleWordQueryParser.Parse(keyWordsSplit);
                    currentQuery.Add(articleWordQuery, BooleanClause.Occur.MUST);
                }
            }

            //Search
            IndexSearcher searcher = new IndexSearcher(PhysicalIndexDirectory);
            Hits          hits     = searcher.Search(currentQuery);
            SearchResultDataSet <Article> articles = new SearchResultDataSet <Article>();
            int pageLowerBound = query.PageIndex * query.PageSize;
            int pageUpperBound = pageLowerBound + query.PageSize;

            if (pageUpperBound > hits.Length())
            {
                pageUpperBound = hits.Length();
            }

            //HighLight
            PanGu.HighLight.Highlighter highlighter = null;
            if (!string.IsNullOrEmpty(query.Title))
            {
                highlighter = new PanGu.HighLight.Highlighter(new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"#c60a00\">", "</font>"), new PanGu.Segment());
                highlighter.FragmentSize = 100;
            }
            for (int i = pageLowerBound; i < pageUpperBound; i++)
            {
                Article item = ConvertDocumentToArticle(hits.Doc(i));
                if (!string.IsNullOrEmpty(query.Title))
                {
                    string bestBody = null;
                    if (!string.IsNullOrEmpty(item.Abstract) && item.Abstract.Length > MaxNumFragmentsRequired)
                    {
                        bestBody = highlighter.GetBestFragment(query.Title, item.Abstract);
                    }

                    if (!string.IsNullOrEmpty(bestBody))
                    {
                        item.Abstract = bestBody;
                    }
                    else
                    {
                        item.Abstract = HtmlHelper.TrimHtml(item.Abstract, 100);
                    }

                    string bestSubject = null;
                    if (!string.IsNullOrEmpty(item.Title) && item.Title.Length > MaxNumFragmentsRequired)
                    {
                        bestSubject = highlighter.GetBestFragment(query.Title, item.Title);
                    }

                    if (!string.IsNullOrEmpty(bestSubject))
                    {
                        item.Title = bestSubject;
                    }
                }
                articles.Records.Add(item);
            }
            searcher.Close();
            articles.TotalRecords = hits.Length();

            DateTime endTime = DateTime.Now;

            articles.SearchDuration = (endTime.Ticks - startTime.Ticks) / 1E7f;
            articles.PageIndex      = query.PageIndex;
            articles.PageSize       = query.PageSize;

            return(articles);
        }
    void BindData()
    {
        ProductQuery query = this.Query;

        lnkGrid.PostBackUrl = Request.RawUrl;
        lnkList.PostBackUrl = Request.RawUrl;
        lnkGrid.Attributes.Add("rel", "grid");
        lnkList.Attributes.Add("rel", "list");

        #region -Adapt Show-
        string s = Request.QueryString["s"];
        if (!string.IsNullOrEmpty(s))
        {
            switch (s)
            {
            case "grid":
                lnkGrid.CssClass   = "showByGrid showBy showByGridActive";
                lnkList.CssClass   = "showByList showBy";
                cpProduct.PageSize = 10;
                break;

            case "list":
                lnkList.CssClass   = "showByList showBy showByListActive";
                lnkGrid.CssClass   = "showByGrid showBy";
                cpProduct.PageSize = 50;
                break;
            }
        }
        else
        {
            cpProduct.PageSize = 10;
            s = "grid";
            lnkGrid.CssClass = "showByGrid showBy showByGridActive";
            lnkList.CssClass = "showByList showBy";
        }
        #endregion

        #region -BindData-
        string         sortBy    = Request.QueryString["sortby"];
        ProductOrderBy orderBy   = ProductOrderBy.DisplayOrder;
        SortOrder      sortOrder = SortOrder.Descending;
        if (!string.IsNullOrEmpty(sortBy))
        {
            try
            {
                ddlSortBy.Items.FindByValue(sortBy).Selected = true;
            }
            catch { ddlSortBy.SelectedIndex = 0; }
            GetData(out orderBy, out sortOrder, sortBy);
        }
        query.ProductOrderBy = orderBy;
        query.SortOrder      = sortOrder;
        query.PageSize       = int.MaxValue;
        query.HasPublished   = true;
        List <Product> prods = null;
        if (!IsSearch)
        {
            prods = Products.GetProducts(query).Records;
        }
        else
        {
            if (string.IsNullOrEmpty(query.ProductNameFilter))
            {
                prods = Products.GetProducts(query).Records;
            }
            else
            {
                SearchResultDataSet <Product> _pros = ProductSearchManager.Search(query);
                prods                 = _pros.Records;
                pnlSearch.Visible     = true;
                ltSearchDuration.Text = "搜索用时:" + _pros.SearchDuration.ToString() + "秒。";
            }
        }
        if (prods == null || prods.Count == 0)
        {
            msgBox.ShowMsg("没有符合条件的产品存在!", System.Drawing.Color.Gray);
            return;
        }
        msgBox.HideMsg();
        bool islogin = Context.User.Identity.IsAuthenticated;
        if (orderBy == ProductOrderBy.Price)
        {
            prods.Sort(new SortProductByPrice(sortOrder,
                                              (islogin ? Profile.AccountInfo.UserID : 0),
                                              islogin
                                              ));
        }

        if (s == "grid")
        {
            dlProduct2.Visible      = false;
            dlProduct.Visible       = true;
            cpProduct.DataSource    = prods;
            cpProduct.BindToControl = dlProduct;
            dlProduct.DataSource    = cpProduct.DataSourcePaged;
            dlProduct.DataBind();
        }
        else
        {
            dlProduct.Visible       = false;
            dlProduct2.Visible      = true;
            cpProduct.DataSource    = prods;
            cpProduct.BindToControl = dlProduct2;
            dlProduct2.DataSource   = cpProduct.DataSourcePaged;
            dlProduct2.DataBind();
        }
        #endregion
    }