Skip to content

Conversation

@jimczi
Copy link
Contributor

@jimczi jimczi commented Sep 10, 2025

This change makes all filters in the knn query eligible for query caching. By default, Lucene considers some simple filters (e.g., term queries) too cheap to cache. In the context of vector search, these filters are eagerly materialized as bitsets, which makes them significantly more expensive to evaluate on every request. Forcing them to be cacheable avoids repeated recomputation.

This is a stop-gap change to support simple use cases such as a single term query used as a filter in knn. The long-term solution is to move this decision logic into the Lucene knn codec itself, but that will require more time.

Benchmark Results

Dataset: 20M 128D vectors, term filter matching ~80% of documents.

With this change:

Precision  QPS      P50 (ms)   P95 (ms)
0.91       632.8    5.763      9.900

Without this change:

Precision  QPS      P50 (ms)   P95 (ms)
0.91        68.2    82.52      193.92

This change makes all filters in the `knn` query eligible for query caching.
By default, Lucene considers some simple filters (e.g., term queries) too cheap to cache. In the context of vector search, these filters are eagerly materialized as bitsets, which makes them significantly more expensive to evaluate on every request. Forcing them to be cacheable avoids repeated recomputation.

This is a stop-gap change to support simple use cases such as a single term query used as a filter in `knn`. The long-term solution is to move this decision logic into the Lucene `knn` codec itself, but that will require more time.

### Benchmark Results

Dataset: **20M 128D vectors**, term filter matching \~80% of documents.

**With this change:**

```
Precision  QPS      P50 (ms)   P95 (ms)
0.91       632.8    5.763      9.900
```

**Without this change:**

```
Precision  QPS      P50 (ms)   P95 (ms)
0.91        68.2    82.52      193.92
```
@elasticsearchmachine elasticsearchmachine added the Team:Search Relevance Meta label for the Search Relevance team in Elasticsearch label Sep 10, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-search-relevance (Team:Search Relevance)

@elasticsearchmachine
Copy link
Collaborator

Hi @jimczi, I've created a changelog YAML for you.

@benwtrent benwtrent self-requested a review September 10, 2025 15:48
@jimczi jimczi merged commit ad5df9d into elastic:main Sep 11, 2025
34 checks passed
@jimczi jimczi deleted the knn_query_filter_cache branch September 11, 2025 08:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

>enhancement :Search Relevance/Vectors Vector search Team:Search Relevance Meta label for the Search Relevance team in Elasticsearch v9.2.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants