/// <summary> /// Performs a search in the WeBlog search index, with a sort /// </summary> /// <typeparam name="T">The type of the items to be returned from the search</typeparam> /// <param name="query">The query to execute</param> /// <param name="maximumResults">The maximum number of results</param> /// <param name="sortField">The index field to sort on</param> /// <returns>An array of search results, or an empty array if there was an issue</returns> public T[] Execute <T>(QueryBase query, int maximumResults, Action <List <T>, Item> func, string sortField, bool reverseSort) { if (query is CombinedQuery) { // Add on database (query as CombinedQuery).Add(new FieldQuery(Sitecore.Search.BuiltinFields.Database, Sitecore.Context.Database.Name), QueryOccurance.Must); // Add language var langCode = DatabaseCrawler.TransformLanguageCode(Sitecore.Context.Language.Name); (query as CombinedQuery).Add(new FieldQuery(Constants.Index.Fields.Language, langCode), QueryOccurance.Must); } // I have to use Action<T> cause the compiler can't work out how to use implicit operators when T is one of the items classes (generated by CIG) var items = new List <T>(); if (maximumResults > 0) { var index = GetSearchIndex(); if (index != null && index.GetDocumentCount() > 0) { using (var searchContext = new SortableIndexSearchContext(index)) { SearchHits hits; if (!string.IsNullOrEmpty(sortField)) { #if FEATURE_CONTENT_SEARCH var sort = new Lucene.Net.Search.Sort(new SortField(sortField, SortField.STRING, reverseSort)); #else var sort = new Lucene.Net.Search.Sort(sortField, reverseSort); #endif hits = searchContext.Search(query, sort); } else { hits = searchContext.Search(query); } if (hits != null) { foreach (var result in hits.FetchResults(0, maximumResults)) { var item = SearchManager.GetObject(result); if (item != null) { func(items, (Item)item); } } } } } else { Log.Warn("WeBlog index was not found or didn't contain any documents", this); } } return(items.ToArray()); }
public virtual KeyValuePair <int, List <SitecoreItem> > RunQuery(Query query, int pageSize, int pageNumber, string sortField, string sortDirection) { var items = new List <SitecoreItem>(); int hitCount; if (Config.EnableBucketDebug || Constants.EnableTemporaryBucketDebug) { Log.Info("Bucket Debug Query: " + query, this); } if (Config.SOLREnabled == "true") { GetValue(query, items); return(new KeyValuePair <int, List <SitecoreItem> >(items.Count, items)); } else { if (Index is Index) { (Index as Index).Analyzer = new StandardAnalyzer(Consts.StopWords); } using (var context = new SortableIndexSearchContext(Index)) { BooleanQuery.SetMaxClauseCount(Config.LuceneMaxClauseCount); var sortingDir = sortDirection == "asc" ? false : true; var searchHits = string.IsNullOrWhiteSpace(sortField) ? context.Search(query) : context.Search(query, new Sort(sortField, sortingDir)); if (searchHits.IsNull()) { return(new KeyValuePair <int, List <SitecoreItem> >()); } hitCount = searchHits.Length; if (pageSize == 0) { pageSize = searchHits.Length; } if (pageNumber == 1) { pageNumber = 1; } var resultCollection = new SearchResultCollection(); resultCollection = searchHits.FetchResults((pageNumber - 1) * pageSize, pageSize); SearchHelper.GetItemsFromSearchResult(resultCollection, items); } return(new KeyValuePair <int, List <SitecoreItem> >(hitCount, items)); } }