Skip to content

Support semantic reranking using contextual snippets instead of entire field text #129369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 68 commits into from
Jul 23, 2025

Conversation

kderusso
Copy link
Member

@kderusso kderusso commented Jun 12, 2025

Followup to the POC described in #128255

Adds the ability to rerank based on a smaller number of snippets.

Example, with a default of one snippet:

GET test/_search
{
  "retriever": {
    "text_similarity_reranker": {
      "retriever": {
        "standard": {
          "query": {
            "term": {
              "other": "lotr"
            }
          }
        }
      },
      "rank_window_size": 2,
      "field": "semantic_text",
      "inference_text": "are all who wander lost?",
      "snippets": { }
    }
  }
}

Example, specifying snippets:

GET test/_search
{
  "retriever": {
    "text_similarity_reranker": {
      "retriever": {
        "standard": {
          "query": {
            "term": {
              "other": "lotr"
            }
          }
        }
      },
      "rank_window_size": 2,
      "field": "semantic_text",
      "inference_text": "are all who wander lost?",
      "snippets": {
        "num_snippets": 3
      }
    }
  }
}

Not specifying snippets will continue to send the entire field contents into the reranker model.

@kderusso kderusso changed the title Rerank snippet POC Support semantic reranking using contextual snippets instead of entire field text Jul 2, 2025
@kderusso kderusso requested a review from jimczi July 16, 2025 13:42
Copy link

cla-checker-service bot commented Jul 21, 2025

💚 CLA has been signed

@kderusso
Copy link
Member Author

cla/check


import static org.elasticsearch.search.rank.feature.RerankSnippetConfig.DEFAULT_NUM_SNIPPETS;

public class TextSimilarityRerankingRankFeaturePhaseRankShardContext extends RerankingRankFeaturePhaseRankShardContext {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really need the separation here between TextSimilarityRerankingRankFeaturePhaseRankShardContext and RerankingRankFeaturePhaseRankShardContext? I don't see RerankingRankFeaturePhaseRankShardContext being used elsewhere so maybe we can just have TextSimilarityRerankingRankFeaturePhaseRankShardContext and implement the full logic there?

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree that it's a little extra, but it clearly conveys the fact that snippets only pertain to text similarity reranking and not other rerankers we may add in the future. If you feel strongly about it I will refactor back, but I don't think this is a bad thing per se.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we just rename RerankingRankFeaturePhaseRankShardContext into TextSimilarityRerankingRankFeaturePhaseRankShardContext? I don't see what RerankingRankFeaturePhaseRankShardContext provides as an abstract class.

Copy link
Member Author

Choose a reason for hiding this comment

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

Can we just rename RerankingRankFeaturePhaseRankShardContext into TextSimilarityRerankingRankFeaturePhaseRankShardContext? I don't see what RerankingRankFeaturePhaseRankShardContext provides as an abstract class.

@jimczi this class is referenced elsewhere including the random reranker. I personally think it's cleaner to have the separation, but if you feel strongly about it I will put snippet configuration in the parent class and remove the abstraction. I am commenting one more time instead of changing it, because I actually prefer this separation to putting things specific to text similarity that are used by the parent reranker. However, I won't die on that hill if you absolutely hate it.

*/
public class SnippetRankInput implements Writeable {

private final RerankSnippetConfig snippets;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the separation between SnippetRankInput and RerankSnippetConfig necessary? Why not injecting numSnippets and snippetQueryBuilder directly here?

Copy link
Member Author

@kderusso kderusso Jul 22, 2025

Choose a reason for hiding this comment

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

It's a little convoluted because we don't allow size of snippets in the API, I'll see if I can clean it up

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated in d56726c

private final SnippetRankInput snippetRankInput;

// Rough approximation of token size vs. characters in highlight fragments.
// TODO highlighter should be able to set fragment size by token not length
Copy link
Member Author

Choose a reason for hiding this comment

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

@jimczi Here is the TODO I referenced in my earlier response 🙂

@kderusso kderusso requested a review from jimczi July 22, 2025 18:00
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.

Thanks for adding the feature flag. I left some minor comments to address but the feature looks good to me.


import static org.elasticsearch.search.rank.feature.RerankSnippetConfig.DEFAULT_NUM_SNIPPETS;

public class TextSimilarityRerankingRankFeaturePhaseRankShardContext extends RerankingRankFeaturePhaseRankShardContext {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we just rename RerankingRankFeaturePhaseRankShardContext into TextSimilarityRerankingRankFeaturePhaseRankShardContext? I don't see what RerankingRankFeaturePhaseRankShardContext provides as an abstract class.

kderusso and others added 4 commits July 22, 2025 16:31
…inference/rank/textsimilarity/TextSimilarityRankBuilder.java

Co-authored-by: Jim Ferenczi <[email protected]>
…inference/rank/textsimilarity/TextSimilarityRerankingRankFeaturePhaseRankShardContext.java

Co-authored-by: Jim Ferenczi <[email protected]>
@afoucret afoucret dismissed their stale review July 23, 2025 09:16

Notr relevant

@kderusso kderusso merged commit 37c27e0 into elastic:main Jul 23, 2025
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>enhancement :SearchOrg/Relevance Label for the Search (solution/org) Relevance team Team:Search - Relevance The Search organization Search Relevance team Team:SearchOrg Meta label for the Search Org (Enterprise Search) v9.2.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants