Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google CustomSearchControl showing inconsistent pagination links

When using CustomSearchControl the number of result pages vary depending on which page you are viewing. For example, go to this site and search for: car

On the first result page, it will show links to 3 pages, but when you click on 2 or 3, it ends at page 2.

Why does this happen, and is there a workaround for this?

If it possible to get the actual number of results, and build my own pagination?

like image 602
userBG Avatar asked Nov 14 '22 13:11

userBG


1 Answers

The reason is that google only estimates how many results are available. As you load each page it refines the estimate, and so the number of pages changes.

You can check the estimatedResultCount property of the customSearchControl.searcher.cursor object, but unfortunately it won't help you because that's just the estimate the CSE uses to generate the pagination itself. (As each page is loaded, it will change to the new estimate.) There's also a resultCount property, but I believe it's for backward compatibility, as it is always equal to estimatedResultCount.

Unfortunately, the CSE is also not smart enough to remember its estimate. So you can start with an estimate of 127, go to the last page and have it be 94, then go to the second-to-last page and have it be 120, even though it should know at that point that it can't be more than 94. So I guess if you really wanted to you could make your own pagination that at least keeps track of the minimal value. It appears that's generally (always?) on the last page, so if it needed to be as good as possible, you could programmatically load the last page, take the estimate to build your pagination, then switch back to the first page for the user.

If you decide to go that route, you can build your own pagination using the customSearchControl.searcher.cursor.gotoPage(page) function. (https://developers.google.com/web-search/docs/reference#_class_GSearch)

like image 76
Nathan Stretch Avatar answered Nov 16 '22 02:11

Nathan Stretch