示例#1
0
        private void DoSearch()
        {
            if (Page.IsPostBack)
            {
                return;
            }

            if (Request.QueryString.Get("q") == null)
            {
                return;
            }

            query = Request.QueryString.Get("q");

            if (this.query.Length == 0)
            {
                return;
            }

            //txtSearchInput.Text = Server.HtmlEncode(query).Replace(""", "\"") ;
            txtSearchInput.Text = SecurityHelper.SanitizeHtml(query);
            if (WebConfigSettings.EscapingSpecialCharactersInKeyword)
            {
                query = query.Replace("-", "\\-");
            }

            queryErrorOccurred = false;

            CanhCam.SearchIndex.IndexItemCollection searchResults = CanhCam.SearchIndex.IndexHelper.Search(
                siteSettings.SiteId,
                isSiteEditor,
                GetUserRoles(),
                News.FeatureGuid,
                CultureInfo.CurrentUICulture.Name,
                DateTime.MinValue,
                DateTime.MaxValue,
                query,
                WebConfigSettings.EnableSearchResultsHighlighting,
                WebConfigSettings.SearchResultsFragmentSize,
                pageNumber,
                pageSize,
                WebConfigSettings.SearchMaxClauseCount,
                out totalHits,
                out queryErrorOccurred);

            XmlDocument doc = new XmlDocument();

            doc.LoadXml("<NewsList></NewsList>");
            XmlElement root = doc.DocumentElement;

            XmlHelper.AddNode(doc, root, "SiteRoot", SiteRoot);
            XmlHelper.AddNode(doc, root, "TotalNews", totalHits.ToString());
            XmlHelper.AddNode(doc, root, "Keyword", Server.HtmlEncode(query));
            XmlHelper.AddNode(doc, root, "ProductText", ResourceHelper.GetResourceString("ProductResources", "ProductFeatureName"));
            XmlHelper.AddNode(doc, root, "NewsText", ResourceHelper.GetResourceString("NewsResources", "NewsFeatureName"));

            if (searchResults.Count == 0)
            {
                if (InitIndexIfNeeded())
                {
                    return;
                }

                if (txtSearchInput.Text.Length > 0)
                {
                    string noResults = Resources.NewsResources.SearchResultsNotFound;
                    if (queryErrorOccurred)
                    {
                        noResults = ResourceHelper.GetResourceString("Resource", "SearchQueryInvalid");
                    }

                    XmlHelper.AddNode(doc, root, "NoResults", noResults);
                }
            }
            else
            {
                float  duration     = searchResults.ExecutionTime * 0.0000001F;
                string searchSumary = string.Format(Resources.NewsResources.SearchResultsFormat, totalHits.ToString(CultureInfo.InvariantCulture), Server.HtmlEncode(query), duration.ToString());

                XmlHelper.AddNode(doc, root, "SearchSumary", searchSumary);
                XmlHelper.AddNode(doc, root, "Duration", duration.ToString());

                totalPages = 1;

                if (pageSize > 0)
                {
                    totalPages = totalHits / pageSize;
                }

                if (totalHits <= pageSize)
                {
                    totalPages = 1;
                }
                else
                {
                    int remainder;
                    Math.DivRem(totalHits, pageSize, out remainder);
                    if (remainder > 0)
                    {
                        totalPages += 1;
                    }
                }

                string searchUrl = SiteRoot
                                   + "/News/SearchResults.aspx?q=" + Server.UrlEncode(query)
                                   + "&amp;p={0}";

                pgrTop.PageURLFormat = searchUrl;
                pgrTop.ShowFirstLast = true;
                pgrTop.CurrentIndex  = pageNumber;
                pgrTop.PageSize      = pageSize;
                pgrTop.PageCount     = totalPages;
                pgrTop.Visible       = (totalPages > 1);

                pgrBottom.PageURLFormat = searchUrl;
                pgrBottom.ShowFirstLast = true;
                pgrBottom.CurrentIndex  = pageNumber;
                pgrBottom.PageSize      = pageSize;
                pgrBottom.PageCount     = totalPages;
                pgrBottom.Visible       = (totalPages > 1);

                string newsGuids = string.Empty;
                string sepa      = string.Empty;
                foreach (IndexItem item in searchResults)
                {
                    if (!newsGuids.ContainsCaseInsensitive(item.ItemGuid.ToString()))
                    {
                        newsGuids += sepa + item.ItemGuid.ToString();
                        sepa       = ";";
                    }
                }

                if (newsGuids.Length > 0)
                {
                    List <News> lstNews = News.GetByGuids(siteSettings.SiteId, newsGuids, 1, WorkingCulture.LanguageId);
                    foreach (News news in lstNews)
                    {
                        XmlElement newsXml = doc.CreateElement("News");
                        root.AppendChild(newsXml);

                        NewsHelper.BuildNewsDataXml(doc, newsXml, news, timeZone, timeOffset, string.Empty);

                        if (WebConfigSettings.EnableSearchResultsHighlighting)
                        {
                            foreach (IndexItem item in searchResults)
                            {
                                if (item.ItemGuid == news.NewsGuid)
                                {
                                    XmlHelper.AddNode(doc, newsXml, "HighlightContent", item.Intro);

                                    break;
                                }
                            }
                        }
                    }
                }
            }

            XmlHelper.XMLTransform(xmlTransformer, SiteUtils.GetXsltBasePath("news", "NewsSearchResults.xslt"), doc);
        }
示例#2
0
        public void ProcessRequest(HttpContext context)
        {
            Encoding encoding = new UTF8Encoding();

            context.Response.ContentEncoding = encoding;

            string query = WebUtils.ParseStringFromQueryString("q", string.Empty);

            if (query.Length == 0)
            {
                return;
            }

            //https://weblearn.ox.ac.uk/portal/help/TOCDisplay/content.hlp?docId=howdoiperformanadvancedsearch
            if (WebConfigSettings.EscapingSpecialCharactersInKeyword) //Escaping Special Characters: + - && || ! ( ) { } [ ] ^ " ~ * ? : \
            {
                query = query.Replace("-", "\\-")
                        .Replace("+", "\\+")
                        .Replace("&&", "\\&&")
                        .Replace("||", "\\||")
                        .Replace("!", "\\!")
                        .Replace("(", "\\(")
                        .Replace(")", "\\)")
                        .Replace("{", "\\{")
                        .Replace("}", "\\}")
                        .Replace("[", "\\[")
                        .Replace("]", "\\]")
                        .Replace("^", "\\^")
                        .Replace("\"", "\\\"")
                        .Replace("~", "\\~")
                        .Replace("*", "\\*")
                        .Replace("?", "\\?")
                        .Replace(":", "\\:")
                        .Replace("\\", "\\\\")
                ;
            }

            query = query.Trim().Replace(" ", " + ") + "*";

            SiteSettings siteSettings       = CacheHelper.GetCurrentSiteSettings();
            int          pageSize           = 20;
            int          totalHits          = 1;
            bool         queryErrorOccurred = false;
            bool         isSiteEditor       = WebUser.IsAdminOrContentAdmin || (SiteUtils.UserIsSiteEditor());
            string       result             = string.Empty;

            CanhCam.SearchIndex.IndexItemCollection searchResults = CanhCam.SearchIndex.IndexHelper.Search(
                siteSettings.SiteId,
                isSiteEditor,
                GetUserRoles(context, siteSettings.SiteId),
                Product.FeatureGuid,
                WorkingCulture.DefaultName,
                DateTime.MinValue,
                DateTime.MaxValue,
                query,
                false,
                WebConfigSettings.SearchResultsFragmentSize,
                1,
                pageSize,
                WebConfigSettings.SearchMaxClauseCount,
                out totalHits,
                out queryErrorOccurred);

            if (searchResults.Count > 0)
            {
                string productGuids = string.Empty;
                string sepa         = string.Empty;
                foreach (IndexItem item in searchResults)
                {
                    if (!productGuids.ContainsCaseInsensitive(item.ItemGuid.ToString()))
                    {
                        productGuids += sepa + item.ItemGuid.ToString();
                        sepa          = ";";
                    }
                }

                if (productGuids.Length > 0)
                {
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml("<ProductList></ProductList>");
                    XmlElement root = doc.DocumentElement;

                    var timeOffset = SiteUtils.GetUserTimeOffset();
                    var timeZone   = SiteUtils.GetUserTimeZone();

                    List <Product> lstProducts = Product.GetByGuids(siteSettings.SiteId, productGuids, 1, WorkingCulture.LanguageId);
                    foreach (Product product in lstProducts)
                    {
                        XmlElement productXml = doc.CreateElement("Product");
                        root.AppendChild(productXml);

                        ProductHelper.BuildProductDataXml(doc, productXml, product, timeZone, timeOffset, string.Empty);
                    }

                    result = XmlHelper.TransformXML(SiteUtils.GetXsltBasePath("product", "ProductSuggestSearch.xslt"), doc);
                }
            }

            if (result.Length > 0)
            {
                context.Response.Write(result);
            }
            else
            {
                context.Response.Write(" ");
            }

            context.Response.End();
        }
示例#3
0
        private void DoSearch()
        {
            if (Page.IsPostBack)
            {
                return;
            }

            if (Request.QueryString.Get("q") == null)
            {
                return;
            }

            query = Request.QueryString.Get("q");

            if (this.query.Length == 0)
            {
                return;
            }

            //txtSearchInput.Text = Server.HtmlEncode(query).Replace("&quot;", "\"") ;
            txtSearchInput.Text = SecurityHelper.SanitizeHtml(query);

            //https://weblearn.ox.ac.uk/portal/help/TOCDisplay/content.hlp?docId=howdoiperformanadvancedsearch
            string queryToSearch = query;

            if (WebConfigSettings.EscapingSpecialCharactersInKeyword) //Escaping Special Characters: + - && || ! ( ) { } [ ] ^ " ~ * ? : \
            {
                queryToSearch = queryToSearch.Replace("-", "\\-")
                                .Replace("+", "\\+")
                                .Replace("&&", "\\&&")
                                .Replace("||", "\\||")
                                .Replace("!", "\\!")
                                .Replace("(", "\\(")
                                .Replace(")", "\\)")
                                .Replace("{", "\\{")
                                .Replace("}", "\\}")
                                .Replace("[", "\\[")
                                .Replace("]", "\\]")
                                .Replace("^", "\\^")
                                .Replace("\"", "\\\"")
                                .Replace("~", "\\~")
                                .Replace("*", "\\*")
                                .Replace("?", "\\?")
                                .Replace(":", "\\:")
                                .Replace("\\", "\\\\")
                ;
            }

            if (ConfigHelper.GetBoolProperty("Product:AppendWildcardToEndKeyword", false) && query.Length > 1)
            {
                queryToSearch = queryToSearch + "*";
            }

            queryErrorOccurred = false;

            CanhCam.SearchIndex.IndexItemCollection searchResults = CanhCam.SearchIndex.IndexHelper.Search(
                siteSettings.SiteId,
                isSiteEditor,
                GetUserRoles(),
                Product.FeatureGuid,
                CultureInfo.CurrentUICulture.Name,
                DateTime.MinValue,
                DateTime.MaxValue,
                queryToSearch,
                WebConfigSettings.EnableSearchResultsHighlighting,
                WebConfigSettings.SearchResultsFragmentSize,
                pageNumber,
                pageSize,
                WebConfigSettings.SearchMaxClauseCount,
                out totalHits,
                out queryErrorOccurred);

            XmlDocument doc = new XmlDocument();

            doc.LoadXml("<ProductList></ProductList>");
            XmlElement root = doc.DocumentElement;

            XmlHelper.AddNode(doc, root, "SiteRoot", SiteRoot);
            XmlHelper.AddNode(doc, root, "TotalProducts", totalHits.ToString());
            XmlHelper.AddNode(doc, root, "Keyword", Server.HtmlEncode(query));
            XmlHelper.AddNode(doc, root, "ProductText", ResourceHelper.GetResourceString("ProductResources", "ProductFeatureName"));
            XmlHelper.AddNode(doc, root, "NewsText", ResourceHelper.GetResourceString("ProductResources", "NewsFeatureName"));

            if (searchResults.Count == 0)
            {
                if (InitIndexIfNeeded())
                {
                    return;
                }

                if (txtSearchInput.Text.Length > 0)
                {
                    string noResults = Resources.ProductResources.SearchResultsNotFound;
                    if (queryErrorOccurred)
                    {
                        noResults = ResourceHelper.GetResourceString("Resource", "SearchQueryInvalid");
                    }

                    XmlHelper.AddNode(doc, root, "NoResults", noResults);
                }
            }
            else
            {
                float  duration     = searchResults.ExecutionTime * 0.0000001F;
                string searchSumary = string.Format(Resources.ProductResources.SearchResultsFormat, totalHits.ToString(CultureInfo.InvariantCulture), Server.HtmlEncode(query), duration.ToString());

                XmlHelper.AddNode(doc, root, "SearchSumary", searchSumary);
                XmlHelper.AddNode(doc, root, "Duration", duration.ToString());

                totalPages = 1;

                if (pageSize > 0)
                {
                    totalPages = totalHits / pageSize;
                }

                if (totalHits <= pageSize)
                {
                    totalPages = 1;
                }
                else
                {
                    int remainder;
                    Math.DivRem(totalHits, pageSize, out remainder);
                    if (remainder > 0)
                    {
                        totalPages += 1;
                    }
                }

                string searchUrl = SiteRoot
                                   + "/Product/SearchResults.aspx?q=" + Server.UrlEncode(query)
                                   + "&amp;p={0}";

                pgrTop.PageURLFormat = searchUrl;
                pgrTop.ShowFirstLast = true;
                pgrTop.CurrentIndex  = pageNumber;
                pgrTop.PageSize      = pageSize;
                pgrTop.PageCount     = totalPages;
                pgrTop.Visible       = (totalPages > 1);

                pgrBottom.PageURLFormat = searchUrl;
                pgrBottom.ShowFirstLast = true;
                pgrBottom.CurrentIndex  = pageNumber;
                pgrBottom.PageSize      = pageSize;
                pgrBottom.PageCount     = totalPages;
                pgrBottom.Visible       = (totalPages > 1);

                string productGuids = string.Empty;
                string sepa         = string.Empty;
                foreach (IndexItem item in searchResults)
                {
                    if (!productGuids.ContainsCaseInsensitive(item.ItemGuid.ToString()))
                    {
                        productGuids += sepa + item.ItemGuid.ToString();
                        sepa          = ";";
                    }
                }

                if (productGuids.Length > 0)
                {
                    List <Product> lstProducts = Product.GetByGuids(siteSettings.SiteId, productGuids, 1, WorkingCulture.LanguageId);
                    foreach (Product product in lstProducts)
                    {
                        XmlElement productXml = doc.CreateElement("Product");
                        root.AppendChild(productXml);

                        ProductHelper.BuildProductDataXml(doc, productXml, product, timeZone, timeOffset, string.Empty);

                        if (WebConfigSettings.EnableSearchResultsHighlighting)
                        {
                            foreach (IndexItem item in searchResults)
                            {
                                if (item.ItemGuid == product.ProductGuid)
                                {
                                    XmlHelper.AddNode(doc, productXml, "HighlightContent", item.Intro);

                                    break;
                                }
                            }
                        }
                    }
                }
            }

            XmlHelper.XMLTransform(xmlTransformer, SiteUtils.GetXsltBasePath("product", "ProductSearchResults.xslt"), doc);
        }