Skip to content

Conversation

kderusso
Copy link
Member

Fixes a bug where knn queries on semantic_text did not respect configured filters.

Example query to test:

GET my-index-1/_search
{
  "query": {
    "knn": {
      "field": "inference_field",
      "k": 10,
      "num_candidates": 100,
      "query_vector_builder": {
        "text_embedding": {
          "model_id": "my-e5-model",
          "model_text": "test"
        }
      },
      "filter": {
        "term": { "text_field": "foo" } 
      }
    }
  }
}

@kderusso kderusso added >non-issue :Search Relevance/Ranking Scoring, rescoring, rank evaluation. auto-backport Automatically create backport pull requests when merged v9.0.0 v8.18.0 labels Jan 31, 2025
@kderusso kderusso requested a review from a team January 31, 2025 14:28
@elasticsearchmachine elasticsearchmachine added v9.1.0 Team:Search Relevance Meta label for the Search Relevance team in Elasticsearch labels Jan 31, 2025
@elasticsearchmachine
Copy link
Collaborator

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

Copy link
Contributor

@jimczi jimczi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@Mikep86 Mikep86 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch and nice fix!

Copy link
Contributor

@Samiul-TheSoccerFan Samiul-TheSoccerFan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, left some questions to gain more knowledge.

public static final NodeFeature SEMANTIC_KNN_VECTOR_QUERY_REWRITE_INTERCEPTION_SUPPORTED = new NodeFeature(
"search.semantic_knn_vector_query_rewrite_interception_supported"
);
public static final NodeFeature SEMANTIC_KNN_FILTER_FIX = new NodeFeature("search.semantic_knn_filter_fix");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this value semantic_knn_filter_fix defined somewhere, or does the NodeFeature require a unique string to identify the fix from a specific version?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's required for testing, so we only test the fix on versions that have the feature. This is the pattern we use for defining node features.

);
}

copy.addFilterQueries(original.filterQueries());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious—how do we ensure that the same filter is not added twice? Also, do we handle a different type of filter using copy.addFilterQuery(new TermsQueryBuilder(IndexFieldMapper.NAME, indices))?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is adding the filters that were already added to the request, we do not need to do further analysis as we're translating the query. Adding the filter on index name allows us to filter to only those indices we want to query.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the filter on index name allows us to filter to only those indices we want to query I missed this key point. Thank you for the clarifications.

@elasticsearchmachine
Copy link
Collaborator

💚 Backport successful

Status Branch Result
8.18
8.x
9.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto-backport Automatically create backport pull requests when merged >non-issue :Search Relevance/Ranking Scoring, rescoring, rank evaluation. Team:Search Relevance Meta label for the Search Relevance team in Elasticsearch v8.18.1 v8.19.0 v9.0.1 v9.1.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants