Skip to content

Latest commit

 

History

History
167 lines (149 loc) · 11.9 KB

SEARCH.md

File metadata and controls

167 lines (149 loc) · 11.9 KB

Vector search (Redis Stack–style FT.*)

KiviDB implements Redis Stack–compatible search and vector commands, so existing clients and tutorials largely work unchanged. Use them for full-text (BM25), tags / numerics, and vector ANN (FLAT or HNSW) on hash fields.

Quick start

  1. Create an index on keys with a prefix, declaring at least one VECTOR field (HNSW or FLAT) and optional TEXT / TAG / NUMERIC fields:
redis-cli -p 6380 FT.CREATE idx ON HASH PREFIX 1 doc: SCHEMA \
  vec VECTOR HNSW 10 TYPE FLOAT32 DIM 128 DISTANCE_METRIC COSINE M 16 EF_CONSTRUCTION 128
  1. Store vectors as raw FLOAT32 little-endian bytes in a hash field (DIM × 4 bytes), the same convention as Redis Stack and redis-py / numpy blobs. Use your client's binary-safe HSET (the integration tests under tests/e2e/ have examples).

  2. Query with KNN (Dialect 2), passing the query vector as a named binary parameter (<blob> stands for the query vector's raw bytes, which a shell cannot type: send this from a client library, as in step 2):

redis-cli -p 6380 FT.SEARCH idx '*=>[KNN 10 @vec $q]' PARAMS 2 q <blob> DIALECT 2

The full command surface, filters, hybrid search and edge cases are in COMMANDS.md (search for FT.CREATE, FT.SEARCH, FT.HYBRID, and so on).

Index builds and EF_RUNTIME

  • Vectors are linked into an HNSW graph by helper threads, as many as search-index-threads says (--search-index-threads, or in the configuration file; read at startup, CONFIG GET shows it): a number, 0 meaning each write links its own vector as 1.2 did, or auto, the default. auto starts none under a CPU quota (a container's --cpus, a Kubernetes CPU limit) or on fewer than 8 CPUs. Otherwise it starts one fewer than the CPUs (seven on 8, fifteen on 16), and how many of them link depends on what the server is doing:

    • while it serves clients, (CPUs - 2) / 2 of them: three on 8, seven on 16;
    • while it does nothing but take the writes that fill the backlogs (a bulk load, FT.CREATE over existing keys, a replica's full sync), all of them.

    The server counts as serving when, besides the commands that wrote vectors, it served 500 commands a second, or its threads used a CPU's worth of time besides the links and the commands that wrote vectors. The helpers look every 50 ms. They go back to the serving count at the first look that finds clients. They take the others in again after 200 ms without any. A number starts exactly that many, all of which always link. They start on the first write that needs them and run as SCHED_IDLE on Linux, taking CPU time the workers leave; the log says how many there are, and FT.INFO shows hnsw_index_threads (started) and hnsw_index_threads_active (linking now).

    • On a server of fewer than 8 CPUs, builds run at 1.2's speed by default. search-index-threads 1 (or more) trades serving latency for build speed there. Measured with every CPU of a 4-vCPU server busy (memtier GET/SET, one client loading 384-d vectors in pipelines, one HSET every 2 ms from another client): GET/SET p99 1.6 ms with none, 2.1 ms with one helper, 2.6 ms with two; the build alone 1,540, 3,190 and 3,620 documents a second. A writer whose connection shares the bulk loader's worker waits behind the loader's pipelines with or without helpers (its median 15-25 ms with none, 12-16 ms with one); on the other connections one helper raised the HSET p99 from 2.3-2.7 ms to 4.5-4.7 ms.
    • Under a CPU limit the helpers would spend the quota the workers need and the whole server would be throttled (GET/SET p99 8-9 ms against 0.35 ms beside a load with --cpus 2 and one helper), so there builds run at 1.2's speed unless search-index-threads is given a number.
    • On a server whose CPUs the workers keep busy the backlog stays full and each write links its own vector too (measured with every server CPU busy: the backlog held its limit the whole time).
  • A write puts its vector in the graph's backlog and returns; a search measures every vector of the backlog exactly, beside what it finds in the graph, so a document is found from the moment its HSET is answered, as before. The backlog holds at most 1,024 vectors, and at most 128 Ki floats (85 vectors of 1,536 dimensions, 341 of 384; never fewer than 64): a writer that finds it full links its vector itself. The walk that indexes the documents already there (FT.CREATE on existing keys, a restart, a replica's full sync) hands its vectors over the same way. A graph of fewer than 1,000 nodes links each vector in the write.

  • FT.INFO shows the backlog as hnsw_backlog (and backlog per field under hnsw_indexes), and as hnsw_backlog_bytes the memory it holds, the helpers' queued jobs included: at most one a waiting key, so writes over the same keys do not grow it. A graph dropped (FT.DROPINDEX, FLUSHALL) or replaced by a compaction is freed at once, whatever its jobs. hnsw_live_count counts the graph's nodes, so it trails the documents while the backlog is not empty; num_docs does not.

  • A KNN search looks at no fewer than 50 candidates, whatever EF_RUNTIME says (the default is 10, as in Redis). So the default answers with the recall Redis gives at about EF_RUNTIME 50, and costs about what that costs; compare the two at the same recall, not the same EF_RUNTIME.

  • Measured numbers are in PERFORMANCE.md.

GEOSHAPE fields

A GEOSHAPE field holds a shape in WKT, as in Redis 8.10: POINT(x y) or POLYGON((x y, …), (hole), …), nothing else (LINESTRING, MULTI… and the rest are refused). Its coordinate system follows the type: GEOSHAPE FLAT (the plane) or GEOSHAPE SPHERICAL (the default: lon lat in degrees). It takes SORTABLE, NOINDEX and INDEXMISSING.

On a FLAT field a coordinate is 0 or between 1e-100 and 1e100 in magnitude, so that no product the checks and predicates form can overflow or underflow. A stored value outside that leaves its document in the index without a shape, counted in hash_indexing_failures; a query's shape outside it is refused. SPHERICAL coordinates are angles and have no such limit.

FT.CREATE shapes ON HASH PREFIX 1 shape: SCHEMA geom GEOSHAPE FLAT
HSET shape:1 geom "POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))"
FT.SEARCH shapes "@geom:[WITHIN $area]" PARAMS 2 area "POLYGON((-1 -1, 11 -1, 11 11, -1 11, -1 -1))" DIALECT 2
  • Queries: @field:[WITHIN $p], [CONTAINS $p], [INTERSECTS $p] and [DISJOINT $p], the shape always a parameter, from DIALECT 2 on. In dialect 1 (the default, search-default-dialect) and with the WKT written in the query, the clause is a syntax error, as in Redis. WITHIN and CONTAINS do not count a point on an edge or a corner: such a point intersects the shape but is not within it. A point is never within, nor contains, another point. A shape clause scores 0.
  • Values that are no shape: a HASH value that is not WKT, a polygon that crosses itself, has a spike, has no area, or has a hole outside it or inside another hole, leaves its document in the index without a shape in that field, and counts as a failure in FT.INFO's hash_indexing_failures, with Redis's error. POINT EMPTY is refused too (Redis keeps it, with coordinates of no use). On JSON the field takes a string; a number, boolean, object or array (even of strings) leaves the whole document out of the index, as in Redis.
  • Edges and rounding: a point a rounding step from an edge is on it; the step grows with the coordinates, so near 1e9 a point 1e-7 inside an edge is on it, as in Redis.
  • SPHERICAL: an edge is the shorter great-circle arc between its points, so a polygon with an edge across the antimeridian is the narrow one there. Coordinates are not range checked, as in Redis. Redis measures edges on the WGS84 ellipsoid with an approximation; the two can decide a point within about 0.01 degree of a long edge differently (north of the edge from 0 60 to 90 60, Redis puts it near latitude 67.8 at longitude 45, the great circle at 67.79). A SPHERICAL polygon, stored or queried, must fit within 44.5 degrees of the middle of its points (about 4,900 km): a larger stored one is a failure when it is written (the document stays, without a shape), and a larger query shape is an error. Redis takes larger ones; this keeps any two shapes that may meet comparable in one projection.
  • Limits: a shape has at most 32,768 points. Checking a polygon takes at most 16,777,216 steps (pairs of its segments, and the points its holes are located by). A query has at most 16 shape clauses. Its shape clauses together take at most 134,217,728 steps (shapes looked at, segment pairs, point tests) over the documents they measure, TIMEOUT 0 too, and the query answers an error past that. That is about a second of one worker for the costliest shapes (a release build on a 4-CPU Azure D4s_v5), and search-geoshape-max-steps (CONFIG SET, the configuration file) sets it; a TIMEOUT ends them sooner, with Timeout limit was reached. In MULTI/EXEC and in a script run alone, where every other command waits for it, the steps are a sixteenth of that. FT.HYBRID gives each of its two queries half. What the clauses keep of their answers (a bit and 16 bytes a matching document) is at most 64 MiB a query, an error past that. Bounding boxes come first: a document whose box rules it out costs no segment loop. DISJOINT finds only documents that hold a shape, as in Redis.
  • Storage: each document's shape is parsed once, when it is written, and kept with the document's other postings; a query reads no WKT of the documents it measures. The shapes are counted in the index's memory (used_memory, maxmemory) and in FT.INFO's postings_shape_bytes and geoshapes_sz_mb. FT.INFO lists the field as type GEOSHAPE coord_system FLAT; FT.EXPLAIN writes the clause GEOSHAPE{2 <wkt>}, the number being the predicate (1 CONTAINS, 2 WITHIN, 3 DISJOINT, 4 INTERSECTS).

Persistence and restarts

  • Native snapshots (dump.kdb) store the keys, the index schemas, and, when you SAVE / BGSAVE with indexes populated, the HNSW graph of each vector field, so search quality after a restart matches the graph you had before shutdown.
  • While search indexes are being rebuilt at startup, most commands return LOADING until the rebuild finishes; PING, INFO and a few other commands still work.
  • Documents that a query finds level (equal SORTBY values, the same score, vectors as near) come in the order they were written, as in Redis: SORTBY … ASC first written first, DESC the reverse. A write that names a field of the index makes its document the last written. Snapshots keep that order (their document order section), the AOF replays it, and a replica has its master's.
  • Limits of that order: HEXPIRE of an indexed field leaves the document where it is; with KNN and k below the number of vectors at the same distance, which of those are returned is not decided by the order they were written in; with aof-preamble no, the order after an AOF rewrite is that of the rewritten file; FT.HYBRID does not use it; a replica fed from its master's stream can order level documents written at the same time from different connections otherwise than its master, so a LIMIT page that ends among them can differ; an index of vector fields only keeps the order of the keys; the documents written after an FT.CREATE while its first walk is still filling the index, saved in that window, load in the order of their keys. A RENAME keeps the document's place, as in Redis. FT.AGGREGATE gives groups in the order of their first documents written.
  • After a large ingest, run SAVE (or schedule BGSAVE) so the next start reloads the graph from disk instead of rebuilding it from the hashes.