When querying Sitecore items from the SOLR index, you do not need to get all items in one go. In fact, the default SOLR implementation will stop at 5000 returned items, so for large queries, you need to use the .Page() method:
private IEnumerable<MyItem> GetPage(int page, int pageSize) { using (IProviderSearchContext searchContext = ContentSearchManager.GetIndex("sitecore_web_index").CreateSearchContext()) { return searchContext.GetQueryable<MyItem>() .Page(page, pageSize) .ToArray(); } }
If you are using filters or predicates, remember that the .Page() method is the last to call. If you filter your query after the .Page() is called, you would at best have a filtered result based on the paged result, at worst have an error at execution time:
// This is wrong. The predicate filters are applied after the // paging have occurred. At best, you will first retrieve some MyItem's // (lets's say 10), then filter on those. The result will be less than // the expected 10 results. searchContext.GetQueryable<MyItem>() .Page(page, pageSize) .Where(Predicates.IsDerived<MyItem>(Constants.Templates.Advert)) .OrderBy(myItem => myItem.SortOrder) .ToArray(); // This is correct. First the filtered list of MyItems are retrieved, // then we grab a portion of those searchContext.GetQueryable<MyItem>() .Where(Predicates.IsDerived<MyItem>(Constants.Templates.Advert)) .OrderBy(myItem => myItem.SortOrder) .Page(page, pageSize) .ToArray();
If you are new into Sitecore and SOLR, I would recommend that you read the article Sitecore ContentSearch – Get items from SOLR or Lucene – A base class implementation. Here you will get the basics on how to:
- Map a C# class to a Sitecore Template using the IndexFieldAttribute’s
In the example above, the <MyItem> is a C# model class where properties have been mapped to Sitecore Template Field Names - How to create a nifty base class for all of your search model classes.
- How to use Predicates to do filtered search queries.
MORE TO READ:
- Sitecore ContentSearch – Get items from SOLR or Lucene – A base class implementation by briancaos
- Predicate Builder by all things sitecore
- Walkthrough: Setting up Solr by Sitecore
Advertisements