/// <summary> /// Triggered by Sitecore to initialize the index rebuilding strategy. /// </summary> /// <param name="index">Index associated with the index rebuilding strategy.</param> public void Initialize(SC.ContentSearch.ISearchIndex index) { Assert.ArgumentNotNull(index, "index"); if (this._ruleDatabase == null && !string.IsNullOrEmpty(index.Name)) { string[] parts = index.Name.Split('_'); if (parts.Length > 1) { this._ruleDatabase = SC.Configuration.Factory.GetDatabase(parts[1]); } } SC.Diagnostics.Assert.IsNotNull(this._ruleDatabase, "_ruleDatabase"); if (string.IsNullOrEmpty(this._indexedDatabaseName)) { this._indexedDatabaseName = this._ruleDatabase.Name; } SC.ContentSearch.Diagnostics.CrawlingLog.Log.Info(string.Format( "[Index={0}] Initializing {1} with interval '{2}'.", index.Name, this.GetType().Name, this._alarmClock.Interval)); this._index = index; this._alarmClock.Ring += (sender, args) => this.Handle(args); }
/// <summary> /// Initializes a new instance of the <see cref="IndexRebuildingRuleContext"/> class. /// Constructor requires ISearchIndex. /// </summary> /// <param name="index"> /// The search index to investigate. /// </param> /// <param name="indexedDatabaseName"> /// The name of the Sitecore database /// associated with that search index. /// </param> public IndexRebuildingRuleContext( SC.ContentSearch.ISearchIndex index, string indexedDatabaseName = null) { this.IndexName = index.Name; this.IndexedDatabaseName = indexedDatabaseName; this.LastUpdated = index.Summary.LastUpdated.ToLocalTime(); SC.Diagnostics.Log.Info(this + " : LastUpdated : " + this.LastUpdated, this); }
public override ActionResult Index() { var model = this.GetModel() as HandlebarQueryContainerModel; var items = new List <Sitecore.Data.Items.Item>(); var container = model.Item; string rootFolderId = container.Fields["Search Root"].Value; var rootFolder = Sitecore.Context.Database.GetItem(rootFolderId); string searchQuery = container.Fields["Free Text Query"].Value; var baseTemplateFld = (MultilistField)container.Fields["Base Template"]; var baseTemplates = baseTemplateFld.GetItems(); var fieldFiltersFld = (NameValueListField)container.Fields["Field Filters"]; var fieldFilters = fieldFiltersFld.NameValues; var queryParam = container.Fields["Query Parameter"].Value; queryParam = string.IsNullOrEmpty(queryParam) ? "q" : queryParam; var additionalSearch = Request.QueryString[queryParam]; var indexNameOverride = container.Fields["Index Name Override"].Value; var orderByField = container.Fields["Order By Field"].Value; bool orderByAscending = container.Fields["Order By Direction"].Value.ToLower() == "ascending"; Sitecore.ContentSearch.ISearchIndex index = null; if (!string.IsNullOrEmpty(indexNameOverride)) { //Overriden Index Name index = Sitecore.ContentSearch.ContentSearchManager.GetIndex(indexNameOverride); } else { //Use context of root Item to get index if (rootFolder != null) { var tempItem = (Sitecore.ContentSearch.SitecoreIndexableItem)rootFolder; index = Sitecore.ContentSearch.ContentSearchManager.GetIndex(tempItem); } } if (index != null) { using (var context = index.CreateSearchContext()) { var rootPath = rootFolder.Paths.Path; var predicate = Sitecore.ContentSearch.Linq.Utilities.PredicateBuilder.True <Sitecore.ContentSearch.SearchTypes.SearchResultItem>(); //ensure item falls under root item predicate = Sitecore.ContentSearch.Linq.Utilities.PredicateBuilder.And(predicate, item => item.Path.StartsWith(rootPath)); if (!string.IsNullOrEmpty(searchQuery)) { predicate = Sitecore.ContentSearch.Linq.Utilities.PredicateBuilder.And(predicate, item => item.Content.Contains(searchQuery)); } if (baseTemplates.Length > 0) { var innerPredicate = Sitecore.ContentSearch.Linq.Utilities.PredicateBuilder.False <SearchResultItem>(); foreach (var template in baseTemplates) { innerPredicate = Sitecore.ContentSearch.Linq.Utilities.PredicateBuilder.Or(innerPredicate, item => item.TemplateId == template.ID); } predicate = Sitecore.ContentSearch.Linq.Utilities.PredicateBuilder.And(predicate, innerPredicate); } foreach (string key in fieldFilters.Keys) { string filterValue = HttpUtility.UrlDecode(fieldFilters[key]); if (filterValue.StartsWith("!")) { filterValue = filterValue.Replace("!", ""); predicate = Sitecore.ContentSearch.Linq.Utilities.PredicateBuilder.And(predicate, item => item[key] != filterValue); } else { if (filterValue.StartsWith("$")) { var pageItem = Sitecore.Context.Item; var pageIDFormat1 = pageItem.ID.Guid.ToString(); var pageIDFormat2 = pageIDFormat1.Replace("-", ""); if (filterValue == "$ID") { predicate = Sitecore.ContentSearch.Linq.Utilities.PredicateBuilder.And(predicate, item => item[key] == pageIDFormat1 || item[key] == pageIDFormat2); } else { filterValue = filterValue.Replace("$", ""); if (pageItem.Fields[filterValue] != null && !string.IsNullOrEmpty(pageItem.Fields[filterValue].Value)) { filterValue = pageItem.Fields[filterValue].Value; predicate = Sitecore.ContentSearch.Linq.Utilities.PredicateBuilder.And(predicate, item => item[key] == filterValue); } } } else { if (filterValue.Length > 30 && filterValue.Length < 40) { Guid possibleGuid = Guid.Empty; if (Guid.TryParse(filterValue, out possibleGuid)) { //Sometimes Guid's get stored in Lucene index with and without dashes. var possibleFormat1 = possibleGuid.ToString(); var possibleFormat2 = possibleFormat1.Replace("-", ""); predicate = Sitecore.ContentSearch.Linq.Utilities.PredicateBuilder.And(predicate, item => item[key] == possibleFormat1 || item[key] == possibleFormat2); } else { predicate = Sitecore.ContentSearch.Linq.Utilities.PredicateBuilder.And(predicate, item => item[key] == filterValue); } } else { predicate = Sitecore.ContentSearch.Linq.Utilities.PredicateBuilder.And(predicate, item => item[key] == filterValue); } } } } if (!string.IsNullOrEmpty(additionalSearch)) { predicate = Sitecore.ContentSearch.Linq.Utilities.PredicateBuilder.And(predicate, item => item.Content.Contains(additionalSearch)); } var query = context.GetQueryable <Sitecore.ContentSearch.SearchTypes.SearchResultItem>().Where(predicate); if (!string.IsNullOrEmpty(orderByField)) { if (orderByAscending) { query = query.OrderBy(item => item[orderByField]); } else { query = query.OrderByDescending(item => item[orderByField]); } } var results = query.GetResults(); foreach (var result in results.Hits) { var item = result.Document.GetItem(); items.Add(item); } } } HandlebarManager.SetupContainer(items); return(View(model)); }