We’ll use the query() method to instruct the SDK to query vectors from Upstash Vector.
useUpstash\Vector\Index;useUpstash\Vector\VectorQuery;$index=newIndex( url:"<UPSTASH_VECTOR_REST_URL>", token:"<UPSTASH_VECTOR_REST_TOKEN>",);$results=$index->query(newVectorQuery( vector:[0.1,0.2,...],// "..." represents the dimension size of your vector index. topK:15,// topK is the limit number of records we want to be returned. includeMetadata:true,// (optional) if true the query results will contain metadata. includeVectors:true,// (optional) if true the query results will contain the indexed vectors. includeData:true,// (optional) if true the query results will contain the string data. filter:'',// (optional) if set, the query results will be filtered by the given filter.));
The dimension of the query vector must match the dimension of your index.
The score returned from query requests is a normalized value between 0 and 1,
where 1 indicates the highest similarity and 0 the lowest regardless of the
similarity function used.
If your index is configured with one of our embedding models, you can query the index using a simple string, which will be automatically converted into vector embeddings. See the example below:
useUpstash\Vector\Index;useUpstash\Vector\DataQuery;$index=newIndex( url:"<UPSTASH_VECTOR_REST_URL>", token:"<UPSTASH_VECTOR_REST_TOKEN>",);$results=$index->queryData(newDataQuery( data:'What is the capital of France?', topK:1,// to only return 1 result. includeData:true,));
If your index is not configured with an embedding model, this call will throw an exception.