# Retrieving Results

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

* '<mark style="color:purple;">took\_ms</mark>' – Specifies the query time in \[ms].
* '<mark style="color:purple;">similarity</mark>'  – Provides the list of results. Each item in results\['similarity'] contains the following entries –
* '<mark style="color:purple;">score</mark>' (float) – Specifies the matching score for Classic, Vector or Hybrid Search.&#x20;
* '<mark style="color:purple;">id</mark>' (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](https://docs.hyper-space.io/hyperspace-docs/projects/managing-data-collections/accessing-data).

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

{% tabs %}
{% tab title="Python" %}
{% code lineNumbers="true" %}

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

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
Object results = HyperspaceClient.search(query,
                                        size=5,              
                                        collection_name=collection_name);
JsonObject queryResponse = new Gson().toJsonTree(response).getAsJsonObject();
System.out.println(queryResponse);
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```javascript
const results = HyperspaceClient.search(query,
                                        size=5,              
                                        collection_name=collection_name);
console.log(results.similarity);
```

{% endcode %}
{% endtab %}
{% endtabs %}

\[{'score: 513.7000122070312, 'id': '78254'},\
&#x20;{'score: 512.5500126784442, 'id': '23091'},\
&#x20;{'score: 485.5471220787652, 'id': '85432'}]

Here's another example of printing results, in which the <mark style="color:purple;">took\_ms</mark> is also shown –

{% tabs %}
{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
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 ("=================================================")
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
System.out.printf("Query run time: %.2fms%n", results.getTookMs());
System.out.printf("Query run time / candidates+1 = %.2f%n", results.getTookMs() / (results.getCandidates() + 1));
System.out.println(results.getSimilarity());
System.out.println("=================================================");
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```javascript
console.log(`Query run time: ${results.took_ms.toFixed(2)}ms`);
console.log(`Query run time / candidates+1 = ${(results.took_ms / (results.candidates + 1)).toFixed(2)}`);
console.log(results.similarity);
console.log("=================================================");
```

{% endcode %}
{% endtab %}
{% endtabs %}

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'},*\
&#x20;*{'score: 512.5500126784442, 'id': '23091'},*\
&#x20;*{'score: 485.5471220787652, 'id': '85432'}]*

### Retrieving Query Results By Id

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

{% tabs %}
{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
for i, x in enumerate(results['similarity']):
    document = hyperspace_client.get_document(collection_name, x['id'])
    print(i, document)
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
 for (int i = 0; i < results.getSimilarity().size(); i++) {
            SimilarityResult x = results.getSimilarity().get(i);
            Document document = hyperspaceClient.getDocument(collectionName, x.getId());
            System.out.println(i + ": " + document);
        }
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```javascript
results.similarity.forEach((x, i) => {
    let document = hyperspaceClient.getDocument(collection_name, x.id);
    console.log(i, document);
});
```

{% endcode %}
{% endtab %}
{% endtabs %}

**Where** –

* <mark style="color:purple;">collection\_name</mark> – Specifies the Collection name from which to retrieve the documents.
* <mark style="color:purple;">id</mark> - the id of each result that was returned.

### Retrieving Query Results With Selected Fields

Provide the key <mark style="color:purple;">fields =\[...]</mark> to Hyperspace\_client.search() during the query submission to retrieve selected document fields, in addition to the document Id. See example below -

{% tabs %}
{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
results = hyperspace_client.search(query_schema, 
                                size=1,
                                collection_name=collection_name,
                                function_name="score_function",
                                fields=["FirstSeenTime", "City"])
print(results)
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```
Object results = hyperspaceClient.search(query_schema, 
                                size=1,
                                collection_name=collection_name,
                                function_name="score_function",
                                fields=["FirstSeenTime", "City"]);
console.log(results);
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```javascript
const results = hyperspaceClient.search(query_schema, 
                                size=1,
                                collection_name=collection_name,
                                function_name="score_function",
                                fields=["FirstSeenTime", "City"]);
console.log(results);
```

{% endcode %}
{% endtab %}
{% endtabs %}

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 –

*{ "candidates": 1, "similarity": \[ { "document\_id": "29", "score": 10.0, "fields": { "FirstSeenTime": 1506116398, "City": "Jakarta" } } ], "took\_ms": 33.625 }*


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.hyper-space.io/hyperspace-docs/flows/setting-up/retrieving-results.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
