public static LastEditedItem[] GetLastEditedData(IDashboardSite site, int userId, int max) { // List for constructing the raw query List <string> query = new List <string>(); // Get a reference to the internal searcher BaseSearchProvider externalSearcher = ExamineManager.Instance.SearchProviderCollection["InternalSearcher"]; // Limit the search to pages under the specified site query.Add("sky_path:" + site.Id); // Limit the search to pages last edited by the specified user if (userId >= 0) { query.Add("writerID:" + userId); } // Initialize the criteria for the Examine search ISearchCriteria criteria = externalSearcher.CreateSearchCriteria().RawQuery(String.Join(" AND ", query)); // Make the actual search in Examine ISearchResults results = externalSearcher.Search(criteria); // Order the results (and limit the amount of results) IEnumerable <SearchResult> sorted = results.OrderByDescending(x => x.Fields["updateDate"]).Take(max); return(( from result in sorted select new LastEditedItem { Id = result.Id, CreateDate = ParseExamineDate(result.Fields["createDate"]), UpdateDate = ParseExamineDate(result.Fields["updateDate"]), IsPublished = UmbracoContext.Current.ContentCache.GetById(result.Id) != null, Name = result.Fields["nodeName"], Path = result.Fields["path"] } ).ToArray()); }
/// <summary> /// Executes the search. /// </summary> /// <returns>The <see cref="SearchResponse"/> containing the search results.</returns> public SearchResponse Execute() { SearchResponse searchResponse = new SearchResponse(); BaseSearchProvider searchProvider = ExamineManager.Instance.SearchProviderCollection[SearchConstants.SearcherName]; IBooleanOperation searchCriteria = searchProvider.CreateSearchCriteria().OrderBy(string.Empty); if (!string.IsNullOrWhiteSpace(this.Query)) { searchCriteria = searchProvider .CreateSearchCriteria() .GroupedOr(SearchConstants.MergedDataField.AsEnumerableOfOne(), this.Query.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries).Select(w => w.Trim().MultipleCharacterWildcard()) .ToArray()); } if (this.Categories.Any()) { searchCriteria.And().Field(SearchConstants.CategoryField, string.Join(" ", this.Categories)); } if (searchCriteria != null) { ISearchResults searchResults = null; try { searchResults = searchProvider.Search(searchCriteria.Compile()); } catch (NullReferenceException) { // If the query object can't be compiled then an exception within Examine is raised } if (searchResults != null) { Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29); Formatter formatter = new SimpleHTMLFormatter("<strong>", "</strong>"); foreach (SearchResult searchResult in searchResults.OrderByDescending(x => x.Score)) { // Check to see if the result is culture specific. // This is a bit hacky but there is no way with property wrappers like Vorto to separate the results into // different indexes so we have to fall back to regular expressions. string fieldResult = searchResult.Fields[SearchConstants.MergedDataField]; RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Multiline; string opts = $"({string.Join("|", this.Query.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries))})"; // First check to see if there is any matches for any installed languages and remove any // That are not in our culture collection. // ReSharper disable once LoopCanBeConvertedToQuery foreach (Language language in this.languages) { if (!this.Cultures.Contains(language.CultureInfo)) { fieldResult = Regex.Replace( fieldResult, string.Format(SearchConstants.CultureRegexTemplate, language.IsoCode, opts), string.Empty, options); } } // Now clean up the languages we do have a result for. MatchCollection matches = AllCultureRegex.Matches(fieldResult); foreach (Match match in matches) { if (match.Success) { string replacement = match.Groups["replacement"].Value; fieldResult = Regex.Replace( fieldResult, Regex.Escape(match.Value), replacement + " ", options); } } // Now check to see if we have any match left over. If not, break out. if (!new Regex(string.Format(SearchConstants.QueryRegexTemplate, opts), options).Match(fieldResult).Success) { continue; } this.AddSearchMatch(analyzer, formatter, searchResults, searchResponse, searchResult, fieldResult); } searchResponse.TotalCount = searchResponse.SearchMatches.Count; } } return(searchResponse); }