使用Google App Engine的索引搜索返回完整的数据集

2024-05-19 06:46:20 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有办法在appengine搜索索引中获取整个数据集?下面的搜索从QueryOptions得到一个整数限制,并且这个限制总是需要存在的。在

我无法确定是否有某种特殊标志可以绕过此限制并返回整个结果集。如果查询是在没有QueryOptions的情况下进行的,那么结果集将以某种方式限制为20。在

_INDEX = search.Index(name=constants.SEARCH_INDEX)
_INDEX.search(query=search.Query(
  query,
  options=search.QueryOptions(
      limit=limit,
      sort_options=search.SortOptions(...))))

有什么想法吗?在


Tags: 数据searchindex标志方式情况整数query
2条回答

如果确实需要索引中的每个文档,而不是查询中的每个结果https://cloud.google.com/appengine/docs/python/search/#Python_Deleting_documents_from_an_index,那么可以自定义delete all示例

from google.appengine.api import search

def delete_all_in_index(index_name):
    """Delete all the docs in the given index."""
    doc_index = search.Index(name=index_name)

    # looping because get_range by default returns up to 100 documents at a time
    while True:
        # Get a list of documents populating only the doc_id field and extract the ids.
        document_ids = [document.doc_id
                        for document in doc_index.get_range(ids_only=True)]
        if not document_ids:
            break
        # Delete the documents for the given ids from the Index.
        doc_index.delete(document_ids)

所以你可能会得到这样的结果:

^{pr2}$

您可能希望在列表理解中获取文档本身,而不是先获取ID,然后再从该ID中获取文档,但是您得到了想法。在

首先,如果你窥视一下QueryOptions的构造函数,这就回答了为什么它会返回20个结果的问题:

def __init__(self, limit=20, number_found_accuracy=None, cursor=None,
               offset=None, sort_options=None, returned_fields=None,
               ids_only=False, snippeted_fields=None,
               returned_expressions=None):

我认为API之所以这样做是为了避免不必要的结果获取。如果需要在用户操作时获取更多结果,而不是总是获取所有结果,则应使用偏移量。见this。在

^{pr2}$

相关问题 更多 >

    热门问题