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.

  • 'id' (str) – 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 –

results = Hyperspace_client.search(({"params": data_point,
                                     "query": {query_string}
                                     },
                                     size=5,              
                                     collection_name=collection_name)
print(results)['similarity']) 

[{'score: 513.7000122070312, 'id': '78254'}, {'score: 512.5500126784442, 'id': '23091'}, {'score: 485.5471220787652, '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, 'id': '78254'}, {'score: 512.5500126784442, 'id': '23091'}, {'score: 485.5471220787652, 'id': '85432'}]

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

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

[{'score: 654.7001237754579, 'id': '11624'}, {'score: 567.5554227799042, 'id': '64232'}, {'score: 512.8583736787652, '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']):
    document = hyperspace_client.get_document(collection_name, x['id'])
    print(i, document)

Where

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

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

Retrieving Query Results With Selected Fields

Provide the key fields =[...] to Hyperspace_client.search() during the query submission to retrieve selected document fields, in addition to the document Id. 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) 

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

Last updated