|
| 1 | +# ANN Parameter Tuning |
| 2 | + |
| 3 | +Approximate Nearest Neighbor (ANN) search is a powerful way to make vector search scalable and efficient. In Vespa, this is implemented by building HNSW graphs for embedding fields. |
| 4 | + |
| 5 | +For a search that uses _only_ vector similarity for retrieval, this works very well as you can just query the HNSW index and get (enough) relevant results back very fast. |
| 6 | +However, most Vespa applications are more complex and often combine vector similarity with filtering on metadata fields. |
| 7 | + |
| 8 | +There are multiple strategies in Vespa for handling queries that combine ANN with filtering, |
| 9 | +and there are parameters that control the strategy selection and the strategies themselves. |
| 10 | +While Vespa has chosen default values for these parameters that work well in most use cases, one often can benefit from further tuning these parameters for the application/use case/data set at hand. |
| 11 | + |
| 12 | +## ANN Parameter Optimizer |
| 13 | + |
| 14 | +The `vespa.evaluation` module provides a `VespaNNParameterOptimizer` class that, given a sufficient sample of queries |
| 15 | +using ANN with filtering, |
| 16 | +performs measurements to analyze the effect of various tuning parameters and, based on this, |
| 17 | +provides suggestions for these parameters. |
| 18 | +Running the optimizer can be as simple as this: |
| 19 | + |
| 20 | +```python |
| 21 | +from vespa.evaluation import VespaNNParameterOptimizer |
| 22 | + |
| 23 | +optimizer = VespaNNParameterOptimizer( |
| 24 | + app=my_vespa_app, |
| 25 | + queries=my_list_of_queries, |
| 26 | + hits=number_of_target_hits_used_in_my_queries, |
| 27 | +) |
| 28 | +report = optimizer.run() |
| 29 | + |
| 30 | +suggested_parameters = { |
| 31 | + "ranking.matching.approximateThreshold": report["approximateThreshold"][ |
| 32 | + "suggestion" |
| 33 | + ], |
| 34 | + "ranking.matching.filterFirstThreshold": report["filterFirstThreshold"][ |
| 35 | + "suggestion" |
| 36 | + ], |
| 37 | + "ranking.matching.filterFirstExploration": report["filterFirstExploration"][ |
| 38 | + "suggestion" |
| 39 | + ], |
| 40 | + "ranking.matching.postFilterThreshold": report["postFilterThreshold"][ |
| 41 | + "suggestion" |
| 42 | + ], |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +See the |
| 47 | +[example](https://vespa-engine.github.io/pyvespa/examples/ann-parameter-tuning-vespa-cloud.html) |
| 48 | +for a full guide on how to use this class and how to interpret the report it produces. |
| 49 | +See the |
| 50 | +[documentation](https://vespa-engine.github.io/pyvespa/api/vespa/evaluation.html#vespa.evaluation.VespaNNParameterOptimizer) |
| 51 | +for further details. |
| 52 | + |
0 commit comments