處理搜尋結果

查詢呼叫正常完成時,會以 SearchResults 物件的形式傳回結果。結果物件會顯示索引中找到的相符文件數量,以及傳回的相符文件數量。同時也會提供相符 ScoredDocuments 的清單。由於每次呼叫搜尋時,系統都會傳回限定數量的文件,因此清單通常只會包含部分相符文件。使用偏移或游標時,您可以一次擷取所有相符文件,也可以一次擷取部分文件。

結果

def query_results(index, query_string):
    result = index.search(query_string)
    total_matches = result.number_found
    list_of_docs = result.results
    number_of_docs_returned = len(list_of_docs)
    return total_matches, list_of_docs, number_of_docs_returned

limit 查詢選項的值而定,結果中傳回的相符文件數量可能會小於找到的數量。請注意,如果找到的準確度低於找到的數量,系統會估算找到的數量。無論您如何設定搜尋選項,search() 呼叫最多只會找出 10,000 個相符的文件。

如果找到的文件數量超過傳回的文件數量,且您想擷取所有文件,請使用偏移或游標重複搜尋,如下所述。

計分文件

搜尋結果會包含符合查詢的 [ScoredDocuments]/appengine/docs/standard/services/search/python/scoreddocumentclass) 清單。您可以疊代處理清單中的每個文件:

for scored_document in results:
        print(scored_document)

根據預設,評分文件會包含已建立索引的原始文件所有欄位。如果查詢選項指定了 returned_fields ,則只有這些欄位會顯示在文件的 fields 屬性中。如果您已指定 returned_expressions snippeted_fields 來建立任何運算欄位,則這些欄位會個別顯示在文件的 expressions 屬性中。

使用位移

如果搜尋找到的文件數量超過一次可傳回的數量,請使用偏移量為相符文件清單建立索引。舉例來說,預設查詢限制為 20 份文件。第一次執行搜尋 (位移為 0) 並擷取前 20 份文件後,請將位移設為 20,然後再次執行相同的搜尋,即可擷取接下來的 20 份文件。請持續重複搜尋,每次都將偏移量增加傳回的文件數量:

def query_offset(index, query_string):
    offset = 0

    while True:
        # Build the query using the current offset.
        options = search.QueryOptions(offset=offset)
        query = search.Query(query_string=query_string, options=options)

        # Get the results
        results = index.search(query)

        number_retrieved = len(results.results)
        if number_retrieved == 0:
            break

        # Add the number of documents found to the offset, so that the next
        # iteration will grab the next page of documents.
        offset += number_retrieved

        # Process the matched documents
        for document in results:
            print(document)

疊代處理非常大型的結果集時,使用位移可能會效率不彰。

使用游標

您也可以使用游標來擷取某個子範圍的結果。如果您打算在連續頁面中顯示搜尋結果,並確保不會略過任何文件 (索引可能會在查詢之間修改),游標就非常實用。疊代處理大型結果集時,游標的效率也較高。

如要使用游標,您必須建立初始游標,並將其納入查詢選項。游標有兩種形式:「根據查詢」以及「根據結果」。每個查詢游標都會導致搜尋呼叫傳回的結果物件,與個別游標建立關聯。每個結果的游標會導致游標與結果中的每個評分文件建立關聯。

使用根據查詢的游標

根據預設,新建構的游標為根據查詢的游標。這個游標會保留搜尋結果中最後一個傳回文件的位置。每次搜尋時都會更新。如要列舉索引中所有相符的文件,請執行相同的搜尋,直到結果傳回空值游標為止:

def query_cursor(index, query_string):
    cursor = search.Cursor()

    while cursor:
        # Build the query using the cursor.
        options = search.QueryOptions(cursor=cursor)
        query = search.Query(query_string=query_string, options=options)

        # Get the results and the next cursor
        results = index.search(query)
        cursor = results.cursor

        for document in results:
            print(document)

使用根據結果的游標

如要建立每個結果的游標,請在建立初始游標時,將游標per_result屬性設為 true。搜尋傳回時,每份文件都會有相關聯的游標。您可以使用該游標指定新的搜尋,結果會從特定文件開始。請注意,將每個結果的游標傳遞至搜尋時,結果本身不會有相關聯的每個查詢游標;result.getCursor() 會傳回空值,因此您無法使用這項功能測試是否已擷取所有相符項目。

def query_per_document_cursor(index, query_string):
    cursor = search.Cursor(per_result=True)

    # Build the query using the cursor.
    options = search.QueryOptions(cursor=cursor)
    query = search.Query(query_string=query_string, options=options)

    # Get the results.
    results = index.search(query)

    document_cursor = None
    for document in results:
        # discover some document of interest and grab its cursor, for this
        # sample we'll just use the first document.
        document_cursor = document.cursor
        break

    # Start the next search from the document of interest.
    if document_cursor is None:
        return

    options = search.QueryOptions(cursor=document_cursor)
    query = search.Query(query_string=query_string, options=options)
    results = index.search(query)

    for document in results:
        print(document)

儲存和還原游標

您可以將游標序列化為網頁安全字串並加以儲存,然後還原以供日後使用:

def saving_and_restoring_cursor(cursor):
    # Convert the cursor to a web-safe string.
    cursor_string = cursor.web_safe_string
    # Restore the cursor from a web-safe string.
    cursor = search.Cursor(web_safe_string=cursor_string)