Cursor 类

Cursor 类提供当前这组搜索结果内的一个游标,以便根据指定的条件检索下一组结果。在索引更新时,使用游标可提高分页的性能和一致性。

下面显示了如何使用游标获取下一页结果:

# Get the first set of results, and return a cursor with that result set.
# The first cursor tells the API to return cursors in the SearchResults object.
results = index.search(search.Query(query_string='some stuff',
    options=search.QueryOptions(cursor=search.Cursor()))

# Get the next set of results with a cursor.
results = index.search(search.Query(query_string='some stuff',
    options=search.QueryOptions(cursor=results.cursor)))

如果您要从 SearchResults 中的任何一个 ScoredDocuments 继续进行搜索,请将 Cursor.per_result 设置为 True

# get the first set of results, the first cursor is used to specify
# that cursors are to be returned in the SearchResults.
results = index.search(search.Query(query_string='some stuff',
    options=search.QueryOptions(cursor=Cursor(per_result=True)))

# this shows how to access the per_document cursors returned from a search
per_document_cursor = None
for scored_document in results:
    per_document_cursor = scored_document.cursor

# get the next set of results
results = index.search(search.Query(query_string='some stuff',
    options=search.QueryOptions(cursor=per_document_cursor)))

可以将游标缓存为可用于重新构建 Cursor 的网页安全字符串。例如:

next_cursor = results.cursor
next_cursor_url_safe = next_cursor.web_safe_string

// save next_cursor_url_safe
...
// extract next_cursor_url_safe

results = index.search(
    search.Query(query_string,
        cursor=search.Cursor(web_safe_string=next_cursor_url_safe)))

构造函数

Cursor 类的构造函数定义如下:

class Cursor(web_safe_string=None, per_result=False)

构造类 Cursor 的实例。

参数

per_result

如果为 true,则为 SearchResults 中的每个 ScoredDocument 分别返回一个游标。如果为 false,则为所有 SearchResults 共同返回一个游标。如果使用 QueryOptions.offset,则该参数会被忽略,因为是由用户负责计算后续偏移量(如果有)。

web_safe_string

从搜索服务返回的游标字符串,可由搜索服务解释以获取下一组结果。

结果值

Cursor 类的新实例。

异常

ValueError

如果 API 提供的 web_safe_string 不是所需的格式,则抛出此异常。

属性

Cursor 类的实例具有以下属性:

per_result

返回是否为结果中的每个 ScoredDocument 分别返回一个游标。

web_safe_string

返回搜索服务生成的游标字符串。