i dont know why I can never get a search page to work correclty in Episerver.
I am trying to pull back certain page types from the FIND index using the following code but when i page through using pagination I get the following: PageSize = 10
Page 1 I get 10 results - Great!
Page 2 I get 5 results - weird
Page 3 I get 5 results
Page 4 i get 6 results.
My Results.TotalMatching says I have 38 results.
This is my code:
int pageSize = 10;
int pageNumber = 1;
pageNumber = Convert.ToInt32(pg);
var model = new BlogAndEventsSearchResultsModel(currentPage);
var searchResult = SearchClient.Instance.Search();
searchResult = searchResult.For(q)
.Filter(x => x.VisibleInMenu.Match(true));
TrackQueryResult qs = SearchClient.Instance.Statistics().TrackQuery(q);
searchResult = searchResult.Filter(x => x.PageTypeName.MatchCaseInsensitive(type + "page"));
searchResult = searchResult.Filter(x => x.RegionTags.MatchCaseInsensitive(region));
var results = searchResult
.Skip((pageNumber - 1) * pageSize)
// A view model would be preferred, but for now
// we'll just pass the search result to the view
// through the ViewBag
ViewBag.SearchResult = results;
string data = "";
foreach (var p in results)
data += "NAME: " + p.Name;
int totalPages = (int)Math.Ceiling((decimal)results.TotalMatching / (decimal)pageSize);
ViewBag.SearchPageCount = totalPages;
ViewBag.SearchResultCount = results.TotalMatching;
ViewBag.LanguageName = System.Threading.Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName;
ViewBag.PageSize = data;
The bottom lines relating to data are for me to display what is in Results per pagination page number.
It's early, but it looks correct to me. Have you tried debugging through the results to see if you actually get the incorrect number of items when iterating the results from find? Could there be a problem with your filter or paging inputs?
It's funny - TotalMatching is completely different to the actual number of records
Is there a mismatch in your find index and the DB content? Have you tried reindexing? "GetContentResult" will try to fetch the IContent objects from the database from the IDs fetched from the find index.
Aaaahhh!!! that was it - I cant believe I have missed that - all it needed was a re-index.
Thanks for all your help,