Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Bug in Search Result Paging #535
Thank you for reporting an issue, suggesting an enhancement, or asking a question. We appreciate your feedback - to help the team understand your
[ ] Enhancement
[X ] Bug
[ ] Question
Please specify what version of the library you are using: [ 2.0.7 ]
Expected / Desired Behavior / Question
Using pnp.sp.search with a search query, start row, and row limit to get a SearchResults object. When I use getPage(pageNumber) on the object, I should get the proper items within the search results.
Upon using getPage(1), which should return the second page, the last item from the first page is repeated as the first item on the second page. Using getPage(0), which should return the first page throws an error with the SharePoint postQuery post as the StartRow sent to SharePoint is -1. I believe the following line of code in the getPage method (in search.ts):
Steps to Reproduce
Create a search with pnp.sp.search using a start row of 0 and row limit of 2 to get the SearchResults object. Use getPage(1) on the SearchResults object, to see 2nd item repeated, then use getPage(0) to get the postQuery error via SharePoint.
The getPage method is one based. Meaning it starts at page 1. This is different than the startRow (zero based). I didn't say clearly what I was thinking above and used "row" where I meant page. Pages are 1 based, meaning you start at page 1 and move up. Page 0 in the way it is implemented is incorrect.
So start row 0 should == getPage(1) - and it does in my testing.
I don't see the duplication of search results in my testing.
OK. I changed my code to reflect your comments about getPage being one based and I am still not seeing what I expect.
My search query is sorted by a date field, so I would expect items to be sorted when I get values from multiple pages. When I run my query with no RowLimit to retrieve all items (I have 6 items), I get them back in the order I expect like the following:
So, if I then run the same query with a RowLimit of 2 and StartRow of 0, I get (as expected):
Then, I ask for page 2 (by calling getPage(2)), the PostQuery to SharePoint sends RowLimit: 2 and StartRow: 3 (which should actually be 2, since the rows are 0 based). This gives me:
If I then ask for page 1 (by calling getPage(1)), the PostQuery to SharePoint sends RowLimit: 2 and Start Row: 1 (which should actually be 0, since the rows are 0 based).
The problem appears to be in the following line of code of the getPage method in search.ts (as I mentioned in my initial post):
When I call getPage(1) with a RowLimit of 2, this value ends up as 1 -> 2 * (1 - 1) + 1 instead of 0. When I call getPage(2) with a RowLimit of 2, this value ends up at 2 -> 2 * (2 - 1) + 1 instead of 2. This code seems to be expecting the StartRow to be 1 based rather than 0 based (as you stated it is above).
Since the pages are 1 based, I feel this code should be: