Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Episerver FIND Search page

Vote:
 

Hi,

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;

            if (!string.IsNullOrEmpty(pg))
            {
                pageNumber = Convert.ToInt32(pg);
            }

            var model = new BlogAndEventsSearchResultsModel(currentPage);
            var searchResult = SearchClient.Instance.Search();

            if (!string.IsNullOrEmpty(q))
            {
                searchResult = searchResult.For(q)
                    .Filter(x => x.VisibleInMenu.Match(true));

                TrackQueryResult qs = SearchClient.Instance.Statistics().TrackQuery(q);
            }

            if (!string.IsNullOrEmpty(type))
            {
                searchResult = searchResult.Filter(x => x.PageTypeName.MatchCaseInsensitive(type + "page"));
            }

            if (!string.IsNullOrEmpty(region))
            {
                searchResult = searchResult.Filter(x => x.RegionTags.MatchCaseInsensitive(region));
            }

            var results = searchResult
              .ExcludeDeleted()
              .CurrentlyPublished()
              .PublishedInCurrentLanguage()
              .Skip((pageNumber - 1) * pageSize)
              .Take(pageSize)

              .GetContentResult();

            // 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;

            return View(model);

The bottom lines relating to data are for me to display what is in Results per pagination page number.

#164412
Edited, Oct 24, 2016 17:37
Vote:
 

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?

#165545
Edited, Oct 25, 2016 7:10
Vote:
 

It's funny - TotalMatching is completely different to the actual number of records

#168816
Edited, Oct 25, 2016 11:57
Vote:
 

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.

#168818
Oct 25, 2016 13:05
Vote:
 

Aaaahhh!!! that was it - I cant believe I have missed that - all it needed was a re-index.

Thanks for all your help,

Jon

#168824
Oct 25, 2016 14:50
Vote:
 

Awesome!

#170679
Oct 26, 2016 8:34
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.