Google App Engine:如何计算给定偏移量的查询结果?
我正在对排序模型实现分页,就目前情况而言,我的查询正在获取太多数据。我显示页面链接的方式与 Google 类似:当前页面突出显示,并且周围有一个您可以导航到的页面“填充”。例如,如果有 20 页,填充为 5,并且您在第 10 页,则页面链接将如下所示:
5 6 7 8 9 [10] 11 12 13 14 15 ...
... 事情是,我需要计算当前页面之后的页面数,以便知道应该显示当前页面之后的页面链接数。为此,我将填充页面所需的项目数加上当前页面,再加一(以了解是否显示“...”),然后获取这些结果。这会导致大量的结果查询,最终我只需要其中的一小部分。
Google App Engine 的 api 提供了 count() 函数,返回查询提取的结果数。但是,它不允许我指定偏移量。
我该如何解决这个问题?
我正在考虑获取当前页面之后下一页上的第一个项目,然后在另一个查询上执行 count() ,该查询对该项目的值进行排序(如果有意义的话)。我走在正确的轨道上还是我完全错过了一些东西?我对应用程序引擎还比较陌生,所以放轻松!谢谢:)
更新:
谢谢你,彼得。
游标确实是合适的使用方法。对于任何想要完成同样任务的人来说,这是一个示例:
# For example, the following query has 27 results.
book_query = Book.all().filter("name_lowercase < ", "b" )
# Let's fetch 10 books starting at offset 0...
r = book_query.fetch(10, 0)
# This returns a cursor to the book after the last fetched result, index 10
c = book_query.cursor()
# Now let's count the number of results after our fetch, limit 100.
# To use cursors, the query must be exactly the same.
book_query2 = Book.all().filter("name_lowercase < ", "b" ).with_cursor(c)
book_query2.count(100) # Returns 17
I'm implementing pagination on sorted models and, as it stands, my queries are fetching way too much data. The way I display the links to the pages is similar to Google: the current page is highlighted and there is a surrounding "padding" of pages that you can navigate to. For instance, if there are 20 pages, the padding is 5, and you're on page 10, the page links would look like this:
... 5 6 7 8 9 [10] 11 12 13 14 15 ...
The thing is, I need to calculate the number of pages AFTER the current page in order to know how many page links past the current page should be shown. To do this, I sum the number of items I would need for the padding pages, plus the current page, plus one (to know whether to show the "..."), and then fetch these results. This results in a massive query of results that, ultimately, I only need a small subset of.
Google App Engine's api provides a count() function that returns the number of results a query fetches. However, it does not allow me to specify an offset.
How do I work around this problem?
I'm considering fetching the first item on the next page after the current page, then executing count() on another query that sorts on the values of that item, if that makes sense. Am I on the right track or am I missing something completely? I'm relatively new to app engine, so go easy! Thanks :)
UPDATE:
Thank you, Peter.
Cursors are indeed the appropriate approach to use. Here's an example for anyone looking to accomplish the same:
# For example, the following query has 27 results.
book_query = Book.all().filter("name_lowercase < ", "b" )
# Let's fetch 10 books starting at offset 0...
r = book_query.fetch(10, 0)
# This returns a cursor to the book after the last fetched result, index 10
c = book_query.cursor()
# Now let's count the number of results after our fetch, limit 100.
# To use cursors, the query must be exactly the same.
book_query2 = Book.all().filter("name_lowercase < ", "b" ).with_cursor(c)
book_query2.count(100) # Returns 17
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我还没有使用过它们,但我相信查询游标 是您正在寻找的内容。
I haven't used them yet, but I believe Query Cursors are what you are looking for.