Sitecore Paging SOLR Responses

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:

 

Advertisements

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in c#, Sitecore 7, Sitecore 8, Sitecore 9 and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.