Search Parameters#
new_search and get_next_k_items share four arguments, k,
search_exp, max_increments and exclude_vec. This page explains how
they work together.
How a search runs#
The index is a tree. A search keeps a queue of nodes to visit, ordered by how
close each node’s representative is to the query, and always visits the closest
one next. Visiting a leaf node scores every item in it and adds the items to the
query’s buffer. The search stops scanning leaf nodes at the limits below, sorts the
buffer, and returns the best k items. The rest stay buffered for
get_next_k_items.
kHow many
(score, item_id)pairs to return. Fewer come back when the search does not find that many, whether because too few items were scanned or becauseexclude_vecfiltered some out.search_expHow many leaf nodes to scan in this call. More leaf nodes give better results and a slower search. Results are exact within the scanned leaf nodes, so a closer item in a leaf node that wasn’t scanned is missed.
max_incrementsWhat to do when
search_expleaf nodes gave fewer thankitems. The search doublessearch_expand keeps going, at mostmax_incrementstimes.0never doubles, and-1doubles untilkitems are found or the whole tree has been scanned.exclude_vecItem ids to leave out of the results. The query keeps them as its exclude set, also when it is saved and resumed, so
get_next_k_itemsonly needs ids to add to the set. Added ids are also removed from the items found but not yet returned.
Example#
With k=10, search_exp=4 and max_increments=2, the search scans 4
leaf nodes. If they hold 10 or more items, it stops there. Otherwise it doubles
to 8 leaf nodes in total, then 16, and stops after that even if it still has
fewer than 10 items.
Scores#
Lower is always better. For Metric.L2 the score is the Euclidean distance.
For Metric.IP it is the negated inner product, so a strong match has a large
negative score.
Paging with get_next_k_items#
get_next_k_items returns the next k buffered items. If fewer than k
are buffered and unvisited nodes remain, it first scans search_exp more
leaf nodes, with the same doubling rule. A later page can hold an item that
scores better than one returned earlier, when it comes from a leaf node scanned
later.
A query is saved to disk when close() runs or when it is evicted from
memory. If it still has results left, a new Index on the same path can
resume it with its id (see Quickstart). A query that was never saved
cannot be resumed.