Google App Engine Search for Go: 如何进行分页?

huangapple go评论85阅读模式
英文:

Google App Engine Search for Go: How to paginate?

问题

如果我查询 Google App Engine Search 的 Go 相关内容,例如前 10 个结果,我如何进行后续查询以获取接下来的 10 个结果?我不知道是否有游标可用。

英文:

If I query Google App Engine Search for Go, e.g. for the first 10 results, how do I do a follow-up query that fetches the next 10 results? There are no cursors I’m aware of.

答案1

得分: 6

好消息!自从撰写这个答案以来,搜索API的游标已经完成并发布,因此它已经可用。

请查看Cursor类型。可以在SearchOptions中指定Cursor值。因此,当您调用Index.Search()时,可以传递一个带有先前从Iterator.Cursor()获取的CursorSearchOptions

以下是原始答案。


你运气不好。

通常情况下,搜索API支持游标(例如参见Java使用游标),并且在Go中也实现了游标,但是搜索的游标API是不完整且未导出的。

可以查看search/search.go的源代码:Iterator类型(由Index.List()Index.Search()方法返回)有一个未导出的searchCursor字段(当前为第602行),这正是我们认为的,它在内部使用,只是未导出。

如果你检查同一源文件(search/search.go)中SearchOptions结构体的源代码(这是在初始化/执行搜索时可以传递给Index.Search()方法的内容):

// 目前从第464行开始:
type SearchOptions struct {
    ...
    // TODO: cursor, offset, maybe others.
}

因此,计划添加对搜索游标、偏移量和其他内容的支持,只是尚未实现和导出。希望在将来的版本中添加。

英文:

EDIT: Good News! Since writing this answer, Cursors for the Search API has been completed and published, and so it's generally available.

Check out the Cursor type. A Cursor value may be specified in the SearchOptions. So when you call Index.Search(), you may pass a SearchOptions with a Cursor you obtained previously from Iterator.Cursor().

Original answer follows below.


You are out of luck.

In general Cursors are supported for the Search API (for example see Java Using cursors) and it is also implemented in Go, but the cursor API for searches is incomplete and not exported.

For evidence see the source of search/search.go: the Iterator type (returned by both Index.List() and Index.Search() methods) has an unexported searchCursor field (currently line #602) which is exactly what we think it is, and it is used internally, it is just not exported.

If you check the source of the SearchOptions struct in the same source file (search/search.go) (this is what you can pass to the Index.Search() method when initiating/performing searches):

// Currently starts at line #464:
type SearchOptions struct {
    ...
    // TODO: cursor, offset, maybe others.
}

So there is plan to add support for Search cursors, offsets and others, it is just not yet implemented and exported. Hopefully will get added in a future release.

huangapple
  • 本文由 发表于 2015年5月8日 07:43:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/30113476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定