Connect to a running Vespa instance

Connect and interact with the CORD-19 search app.

Open In Colab

This self-contained tutorial will show you how to connect to a pre-existing Vespa instance. We will use the https://cord19.vespa.ai/ app as an example. You can run this tutorial yourself in Google Colab by clicking on the badge located at the top of the tutorial.

Install pyvespa

[ ]:
!pip install pyvespa

Connect to a running Vespa application

We can connect to a running Vespa application by creating an instance of Vespa with the appropriate url. The resulting app will then be used to communicate with the application.

[1]:
from vespa.application import Vespa

app = Vespa(url = "https://api.cord19.vespa.ai")

Query the application

We have full flexibility to specify the request body based on the Vespa query language.

[2]:
body = {
  'yql': 'select cord_uid, title, abstract from sources * where userQuery();',
  'hits': 5,
  'query': 'Is remdesivir an effective treatment for COVID-19?',
  'type': 'any',
  'ranking': 'bm25'
}
query_result = app.query(body)

Inspect the query result

We can see the number of documents that were retrieved by Vespa:

[3]:
query_result.number_documents_retrieved
[3]:
268858

And the number of documents that were returned to us:

[4]:
len(query_result.hits)
[4]:
5

We can then retrieve specific information from the hit list thorugh the query_result.hits or access the entire Vespa response through query_result.json.

[5]:
[hit["fields"]["cord_uid"] for hit in query_result.hits]
[5]:
['2lwzhqer', '8n6eybze', '8n6eybze', '8art2tyj', 'oud5ioks']