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 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.

from vespa.application import Vespa

app = Vespa(url = "")

Query the application

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

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:


And the number of documents that were returned to us:


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.

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