Retrieving Results

The results can be accessed through the Python dictionary object returned by Hyperspace_client.search(), which has the following entries –

  • 'took_ms' – Specifies the query time in [ms].

  • 'similarity' – Provides the list of results. Each item in Results['similarity'] contains the following entries –

  • 'score' (float) – Specifies the matching score for Classic, Vector or Hybrid Search.

  • 'vector_id' (int) – The unique identifier of a document. This is the same id that you assigned to this data when it was uploaded. You can use this identifier to retrieve the document, as described in Get Document.

Here is an example of what results might look like if they were printed on the screen –

print(results)['similarity']) 

[{'score: 513.7000122070312, 'vector_id': '78254'}, {'score: 512.5500126784442, 'vector_id': '23091'}, {'score: 485.5471220787652, 'vector_id': '85432'}]

Here's another example of printing results, in which the took_ms is also shown –

print(f"Query run time: {results['took_ms']:.2f}ms")
print(f'Query run time / candidates+1 = { results["took_ms"] /
                                         (results["candidates"] + 1):.2f}')
print(results['similarity'])
print ("=================================================")

The following is printed–

Results for collection data-2023-09-06 ================================== Query run time: 2.82ms Query run time / candidates+1 = 2.82

[{'score: 513.7000122070312, 'vector_id': '78254'}, {'score: 512.5500126784442, 'vector_id': '23091'}, {'score: 485.5471220787652, 'vector_id': '85432'}]

==================================

Results for collection data-2023-09-06 ================================== Query run time: 2.82ms query run time / candidates+1 = 2.82

[{'score: 654.7001237754579, 'vector_id': '11624'}, {'score: 567.5554227799042, 'vector_id': '64232'}, {'score: 512.8583736787652, 'vector_id': '95332'}]

Retrieving Query Results By Id

Use the following to retrieve and print the results of the search.

for i, x in enumerate(results['similarity']):
    vector = hyperspace_client.get_document(collection_name, x['vector_id'])
    print(i, vector)

Where

  • collection_name – Specifies the Collection name from which to retrieve the documents.

  • vector_id - the id of each result that was returned.

Retrieving Query Results With Selected Fields

Query results can be modified to include any document fields, instead of just 'vector_id' by default. To do this, provide the key fields to Hyperspace_client.search during the query submission. See example below -

search_api_response = hyperspace_api_instance.search(query_schema, 
                                                     size=1,
                                                     collection_name=collection_name,
                                                     function_name="score_function",
                                                     fields=["FirstSeenTime", "City"])

The results object will now include a field named "fields" that contains the user selected fields. Here is an example of what results might look like now if they were printed on the screen –

print(results)['similarity']) 

{ "candidates": 1, "similarity": [ { "document_id": "29", "score": 10.0, "fields": { "FirstSeenTime": 1506116398, "City": "Jakarta" } } ], "took_ms": 33.625 }

Last updated

#108: Max's Nov 6 changes

Change request updated