Vector column¶
A table opened with vector_dim=N carries one more column in its records, vector: N float32 values per row
(format '<N>f4', for example '384<f4'). It is an ordinary typed column of data.bin. There is no sidecar file:
growth, sort_index, trim, the cleanup rewrite, crash recovery and the S3 head/tail upload move it together
with its row. A table pulled from S3 on another computer already has it; the stored dtype decides, no argument
is needed.
t = shdata.table('Hashs', 'D1', 'DOCS', 'NOTES', size=1_000_000, is_schemaless=True, vector_dim=384)
t.upsert([{'hash': 'a1', 'title': 'x', 'vector': emb}]) # 'embedding' is accepted too
t.upsert(rows, vectors=matrix) # or one vector per row
hits = t.query_vector(q, top_k=10) # cosine similarity, best first
hits[0] # the generic payload + '_score', '_loc'
Rules¶
- Mandatory column.
vector_dimaddsvectorwhen the table is created. A schema may also declare it (names=[..., 'vector'],formats=[..., '384<f4']). An existing table cannot gain or change the column: opening it with anothervector_dimraises, and so doesvector_dimon a table without one. Recreate it withoverwrite=True. - Rows without a vector hold NaN. They are never returned by
query_vector, and neither are all-zero vectors. Upserting a row without a vector keeps the one it has. A given vector must be finite and of the right width, otherwise the whole upsert is refused before anything is written. - Schemaless tables: the vector is stored in the record, not in the BSON payload.
get_dict_listand the query results leave it out;query_vector(..., include_vector=True)returns it under'vector'. - Local only.
df, CSV/JSON and the binary table API leave the vector column out. Over HTTP the table's other columns are served as before; the vector is read and ranked on the computer that holds the table. - Cost.
query_vectorscans every committed row (orfilter_loc) in 16 MB steps under the table lock: about 8 ms for 100k x 384 on one host. Ties at the cut-off resolve to the earliest rows.