Should hybrid search be used wherever possible to limit the results?
Last updated 22, Mar 2024
Question
Should hybrid search be used wherever possible to limit the results?
Answer
Hybrid searches can restrict the result space if the right filter is applied. If you have a filter that will result in a small set of candidates for the KNN
query to select from, we will not execute any fancy algorithms, but we will directly check for each candidate its distance from the query vector and return the top k. This is called AD_HOC_BF
and it is applicable for small sets of candidates since it causes random memory access to get the candidates vector in the vector index.
If you have a large set of candidates the random memory access kills performance, this is resolved by BATCHES
mode, which you can think of as KNN
queries iterator/pager. We will run the vector index query algorithm, maintain its state, and try to intersect with the filter results. If we don't have K results, we will continue from the preserved state and get another batch of nearest neighbors and so on. Empirically we do this two times on average.
For this, a "batch iterator" is implemented. First, it applies vector search over the entire vector index to get x
results, and it filters out irrelevant documents. Then, it continues the vector index search from that point and returns the next top x
results. It repeats this process until it finds k
results (the top ones) that match the filter.
Note that ADHOC_BF
will perform exact score computation regardless of the indexing methods (FLAT
or HNSW
). More to read in the documentation
Note that the FT.EXPLAIN
plan reports the same output for hybrid search regardless of the method used (BATCHES
or ADHOC_BF
). The semantics is always the same in both modes: the search uses the internal section (the INTERSECT
in this example) as a pre-filter and returns the KNN
results from the induced sub-space.
VECTOR {
INTERSECT {
@content:carbonara
TAG:@genre {
technical
}
}
} => {K=2 nearest vectors to `$vec` in vector index associated with field @embedding, yields distance as `score`}
Using range searches
Range search, instead, does not restrict the result space. It has different semantics from KNN
and its semantics is "I want all vectors within a certain radius in space from my query vector". There is no best practice with regard to range search to restrict the result space.
References
Refer to the Pre-filter query attributes