/// <summary> /// During page load, we use lucene search API to get a list of publishers. This information comes from the cache for performance. /// That information is bound to the search dropdown. /// Also, rebinds the freetext search to the search box. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load(object sender, EventArgs e) { if (NWTD.Profile.CurrentUserLevel.Equals(NWTD.UserLevel.ANONYMOUS)) { this.pnlSearchByPublisher.Visible = false; return; } NWTD.Web.UI.ClientScript.AddRequiredScripts(this.Page); Page.ClientScript.RegisterClientScriptInclude(this.Page.GetType(), "Search_js", CommerceHelper.GetAbsolutePath("Structure/User/NWTDControls/Scripts/Search.js")); this.btnSubmitSearch.Text = this.ButtonText; //it would be weird to show the facet if it's already there //if (!CMSContext.Current.CurrentUrl.Contains("/catalog/searchresults.aspx")) { this.ddlPublisher.Visible = true; //The key used for retrieving cache data string cacheKey = Mediachase.Commerce.Catalog.CatalogCache.CreateCacheKey("search-publisher-names"); FacetGroup publisherfacet; // check cache first object cachedObject = Mediachase.Commerce.Catalog.CatalogCache.Get(cacheKey); if (cachedObject != null) { publisherfacet = (FacetGroup)cachedObject; } else { //So far, this seems like the best way to ennumerate the existing Publishers //Although, it does seem kind of convoluted. //Anyway, get the list using search and then cache it Mediachase.Search.Extensions.CatalogEntrySearchCriteria criteria = new Mediachase.Search.Extensions.CatalogEntrySearchCriteria(); if (!string.IsNullOrEmpty(global::NWTD.Catalog.UserStateAvailablityField)) { //Incorporate the state availablity flag criteria.Add( global::NWTD.Catalog.UserStateAvailablityField, new SimpleValue() { key = string.Empty, value = "y", locale = "en-us", Descriptions = new Descriptions() { defaultLocale = "en-us" } }); } //Create a search criteria object criteria.Add(SearchFilterHelper.Current.SearchConfig.SearchFilters.SingleOrDefault(filter => filter.field == "Publisher")); var manager = new SearchManager(Mediachase.Commerce.Core.AppContext.Current.ApplicationName); //conduct the search Mediachase.Search.SearchResults results = manager.Search(criteria); //get the results FacetGroup[] facets = results.FacetGroups; //get the publisher field publisherfacet = facets.SingleOrDefault(facet => facet.FieldName == "Publisher"); //cache the results for five minutes for faster future loads TimeSpan cacheTimeout = new TimeSpan(0, 5, 00); Mediachase.Commerce.Catalog.CatalogCache.Insert(cacheKey, publisherfacet, cacheTimeout); } //Bind the publisehrs to the dropdown this.ddlPublisher.DataSource = publisherfacet.Facets; this.ddlPublisher.DataBind(); //Add an empty item for searching all publishers this.ddlPublisher.Items.Insert(0, new ListItem("All Publishers")); //select the currently selected publisehr foreach (ListItem item in ddlPublisher.Items) { if (item.Value == this.SelectedPublisher) { item.Selected = true; } } //Re-do the keyword in the textbox as well this.tbKeyWord.Text = string.IsNullOrEmpty(this.KeyWords) ?this.DefaultSearchText : this.KeyWords; //this.ddlPublisher.SelectedValue = this.Publisher; }
/// <summary> /// Searches the specified criteria. /// </summary> /// <param name="criteria">The criteria.</param> /// <returns></returns> public SearchResults Search(ISearchCriteria criteria) { if (_SearchList == null || _SearchList.Count == 0) { foreach (IndexerDefinition indexer in SearchConfiguration.Instance.Indexers) { try { _SearchList.Add(indexer.Name, new IndexSearcher(GetApplicationPath(indexer))); } catch (FileNotFoundException ex) { string msg = String.Format("No search indexers found for \"{0}\" indexer.", indexer.Name); IndexNotFoundException ne = new IndexNotFoundException(msg, ex); Logger.Error(msg, ne); throw ne; } } } if (_SearchList.Count == 0) { string msg = String.Format("No Search Indexers defined in the configuration file."); Logger.Error(msg); throw new IndexNotFoundException(msg); } if (!_SearchList.ContainsKey(criteria.Scope)) { string msg = String.Format("Specified scope \"{0}\" not declared in the configuration file.", criteria.Scope); Logger.Error(msg); throw new IndexNotFoundException(msg); } IndexSearcher searcher = _SearchList[criteria.Scope]; if (_SearchList.Count == 0) { string msg = String.Format("No Search Indexers defined in the configuration file."); Logger.Error(msg); throw new IndexNotFoundException(msg); } Hits hits = null; try { if (criteria.Sort != null) { SearchSortField[] fields = criteria.Sort.GetSort(); List <SortField> sortFields = new List <SortField>(); foreach (SearchSortField field in fields) { sortFields.Add(new SortField(field.FieldName, field.IsDescending)); } hits = searcher.Search(criteria.Query, new Sort(sortFields.ToArray())); } else { hits = searcher.Search(criteria.Query); } } catch (SystemException ex) { string msg = String.Format("Search failed."); Logger.Error(msg, ex); throw; } SearchResults results = new SearchResults(searcher.Reader, hits, criteria); return(results); }