Skip to main content
Removed sitecore-search tag because it's for a different SaaS product
Link
Anna Bastron
  • 2.1k
  • 1
  • 8
  • 26
Source Link
sukesh
  • 3.3k
  • 3
  • 29
  • 78

Solr - How to fetch data by page

There are no results when I apply pagination to the query. But when I comment that code, I get results.

Search log without pagination:

INFO Solr Query - ?q=: AND val:__boost&start=0&rows=1000000&fl=*,score&fq=((_templatename:("Product Details Page") AND _language:("en")) AND is_active_b:("True"))&fq=_indexname:(stratum_products_master_index)&wt=xml

Search log with pagination:

INFO Solr Query - ?q=: AND val:__boost&start=3&rows=3&fl=*,score&fq=((_templatename:("Product Details Page") AND _language:("en")) AND is_active_b:("True"))&fq=_indexname:(stratum_products_master_index)&wt=xml

I observe that for some reason it is start=3 even when I'm passing pageNumber=1 & pageSize=3.
When I comment this line, it works query = query.Page(pageNumber, pageSize);

Where am I going wrong.

private IQueryable<ProductSearchResultItem> GetSearchQuery(string searchTerm, string tagId, int pageNumber, int pageSize)
        {
            var predicate = GetSearchPredicate(searchTerm, tagId);
            IQueryable<ProductSearchResultItem> query = searchService.GetSearchQuery<ProductSearchResultItem>(Constants.SearchIndexes.Products, predicate);

            /// Apply pagination 
            query = query.Page(pageNumber, pageSize);
            return query;
        }

private Expression<Func<ProductSearchResultItem, bool>> GetSearchPredicate(string searchTerm, string tagId, string languageRegionalIsoCode = "en")
        {
            Item productDetailsTemplateItem = SitecoreUtility.GetItem(Templates.ProductDetailsPage.ID);
            string productDetailsTemplateName = productDetailsTemplateItem != null ? productDetailsTemplateItem.Name : string.Empty;

            /// Initialize queries with True for AND queries and False for OR queries
            var andPredicate = PredicateBuilder.True<ProductSearchResultItem>();

            if (!string.IsNullOrWhiteSpace(productDetailsTemplateName))
            {
                /// get only product pages of the specified language
                andPredicate = andPredicate.And(x => x.TemplateName.Equals(productDetailsTemplateName, StringComparison.InvariantCultureIgnoreCase));
                    
                ///get items of specified language
                andPredicate = andPredicate.And(x => x.Language.Equals(languageRegionalIsoCode, StringComparison.InvariantCultureIgnoreCase));

                /// get only active items
                andPredicate = andPredicate.And(x => x.IsActive);

                ///filter by searchTerm
                if (!string.IsNullOrWhiteSpace(searchTerm))
                {
                    andPredicate = andPredicate.And(x => x.Title.Like(searchTerm, 0.75f));
                }

                ///filter by tag
                if (!string.IsNullOrWhiteSpace(tagId))
                {
                    andPredicate = andPredicate.And(x => x.Tags.Contains(new Guid(tagId)));
                }
            }            

            return andPredicate;
        }