# Import necessary libraries
import os
import shutil
import numpy as np
import tiledb.vector_search as vs
# Set the index URI for this tutorial
= os.path.expanduser("~/time_traveling")
index_uri
# Set the query vector
= np.array([[2, 2, 2]], dtype=np.float32)
query_vector
# Clean up previous data
if os.path.exists(index_uri):
shutil.rmtree(index_uri)
Time Traveling
We recommend running this tutorial, as well as the other various tutorials in the Tutorials section, inside TileDB Cloud. This will allow you to quickly experiment avoiding all the installation, deployment, and configuration hassles. Sign up for the free tier, spin up a TileDB Cloud notebook with a Python kernel, and follow the tutorial instructions. If you wish to learn how to run tutorials locally on your machine, read the Tutorials: Running Locally tutorial.
This tutorial shows how you can perform time traveling with TileDB-Vector-Search (i.e., how to query different index “views” over the various versions that were created via vector updates or deletions). We recommend reading the following sections before proceeding with this tutorial:
Setup
First, import the appropriate libraries, set the index URI, create a query vector, and delete any previously created data.
Next, create an empty index:
Populate index
Perform a series of updates, starting with adding some new vectors in bulk. Note that here you will provide timestamp = 1
as a parameter to the update_batch
command. In other tutorials, this parameter was omitted, and a default timestamp was set to the current time (in milliseconds since epoch).
Next, update the values of some existing vectors one by one at timestamp = 2
and timestamp = 3
, respectively. Then perform a bulk update at timestamp = 4
.
Delete some vectors at timestamp = 5
:
Query over timestamps
In order to time travel, you need to “open” the index via one of the following ways:
- At a specific timestamp (which will you give you a “view” of the array “as of” that timestamp).
- At a timestamp range (which will give you a “view” of the array considering only updates and deletions that occurred within the timestamp range).
First, query the index at timestamp=10
(i.e., a timestamp after all the performed updates and deletions), which is equivalent to not providing any timestamp (which sets by default the timestamp to the current time). The result reflects the current state of the index, with all the changes considered.
Now, query the index at timestamp=0
(i.e., before any change occurred). In that case, there will be no results.
Next, query the index at timestamp=1
(i.e., considering only the very first bulk insertions), which will ignore the two vector value updates and deletions.
Finally, query with timestamp=(1,2)
, which considers the initial bulk vector insertion, and only the update of vector with external id equal to 1
. That will ignore the update of the vector with external id equal to 2
and the two vector deletions.
Clean up
Clean up in the end by removing the index:
What’s next?
Now that you learned about how to time travel with TileDB-Vector-Search, we recommend reading the Tutorials: Consolidation section.