Google CustomSearchControl 显示不一致的分页链接
使用 CustomSearchControl 时,结果页面的数量会根据您正在查看的页面而有所不同。例如,转到此网站并搜索: car
在第一个结果页面上,它会显示 3 个页面的链接,但是当您单击 2 或 3 时,它会在第 2 页结束。
为什么会发生这种情况,有解决方法吗?
是否可以获得实际的结果数量,并构建我自己的分页?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
原因是谷歌只估计有多少结果可用。当您加载每个页面时,它会优化估计,因此页面数会发生变化。
您可以检查customSearchControl.searcher.cursor 对象的estimatedResultCount 属性,但不幸的是它不会帮助您,因为这只是CSE 用于生成分页本身的估计值。 (加载每个页面时,它将更改为新的估计值。)还有一个 resultCount 属性,但我相信它是为了向后兼容,因为它始终等于estimatedResultCount。
不幸的是,CSE 也不够聪明,无法记住其估计。因此,您可以从估计值 127 开始,转到最后一页,将其设置为 94,然后转到倒数第二页,将其设置为 120,即使此时它应该知道它不能超过 94。所以我想如果您真的愿意,您可以制作自己的分页,至少跟踪最小值。看起来通常(总是?)在最后一页,因此如果需要尽可能好,您可以以编程方式加载最后一页,进行估计来构建分页,然后切换回用户的第一页。
如果您决定走这条路,您可以使用 customSearchControl.searcher.cursor.gotoPage(page) 函数构建自己的分页。 (https://developers.google.com/web-search/docs/reference# _class_G搜索)
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)