-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Add RerankRequestChunker #130485
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
dan-rubinstein
merged 17 commits into
elastic:main
from
dan-rubinstein:rerank-request-chunker
Sep 29, 2025
Merged
Add RerankRequestChunker #130485
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
849147e
Add RerankRequestChunker
dan-rubinstein c41d54c
Merge branch 'main' into rerank-request-chunker
elasticmachine da4c939
Add chunking strategy generation
dan-rubinstein 004ca8f
Merge branch 'main' into rerank-request-chunker
davidkyle 5ec620a
Merge branch 'main' into rerank-request-chunker
elasticmachine 4ff8eb0
Adding unit tests and fixing token/word ratio
dan-rubinstein ec78b87
Merge branch 'main' into rerank-request-chunker
elasticmachine 9ef8917
Add configurable values for long document handling strategy and maxim…
dan-rubinstein 24497ae
Adding back sentence overlap for rerank chunking strategy
dan-rubinstein 1fea365
Merge branch 'main' into rerank-request-chunker
elasticmachine 8396214
Merge branch 'main' into rerank-request-chunker
elasticmachine 8b97711
Adding unit tests, transport version, and feature flag
dan-rubinstein 833ef02
Update docs/changelog/130485.yaml
dan-rubinstein 77701e1
Merge branch 'main' of github.com:elastic/elasticsearch into rerank-r…
dan-rubinstein 344e121
Adding unit tests and refactoring code with clearer naming conventions
dan-rubinstein 02c9d0a
Merge branch 'main' of github.com:elastic/elasticsearch into rerank-r…
dan-rubinstein d68bf09
Merge branch 'main' of github.com:elastic/elasticsearch into rerank-r…
dan-rubinstein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| pr: 130485 | ||
| summary: Add `RerankRequestChunker` | ||
| area: Machine Learning | ||
| type: enhancement | ||
| issues: [] |
1 change: 1 addition & 0 deletions
1
...ain/resources/transport/definitions/referable/elastic_reranker_chunking_configuration.csv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 9180000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| ml_inference_google_model_garden_added,9179000 | ||
| elastic_reranker_chunking_configuration,9180000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...erence/src/main/java/org/elasticsearch/xpack/inference/chunking/RerankRequestChunker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.inference.chunking; | ||
|
|
||
| import com.ibm.icu.text.BreakIterator; | ||
|
|
||
| import org.elasticsearch.action.ActionListener; | ||
| import org.elasticsearch.inference.ChunkingSettings; | ||
| import org.elasticsearch.inference.InferenceServiceResults; | ||
| import org.elasticsearch.xpack.core.inference.results.RankedDocsResults; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| public class RerankRequestChunker { | ||
| private final List<String> inputs; | ||
| private final List<RerankChunks> rerankChunks; | ||
|
|
||
| public RerankRequestChunker(String query, List<String> inputs, Integer maxChunksPerDoc) { | ||
| this.inputs = inputs; | ||
| this.rerankChunks = chunk(inputs, buildChunkingSettingsForElasticRerank(query), maxChunksPerDoc); | ||
| } | ||
|
|
||
| private List<RerankChunks> chunk(List<String> inputs, ChunkingSettings chunkingSettings, Integer maxChunksPerDoc) { | ||
| var chunker = ChunkerBuilder.fromChunkingStrategy(chunkingSettings.getChunkingStrategy()); | ||
| var chunks = new ArrayList<RerankChunks>(); | ||
| for (int i = 0; i < inputs.size(); i++) { | ||
| var chunksForInput = chunker.chunk(inputs.get(i), chunkingSettings); | ||
| if (maxChunksPerDoc != null && chunksForInput.size() > maxChunksPerDoc) { | ||
| chunksForInput = chunksForInput.subList(0, maxChunksPerDoc); | ||
| } | ||
|
|
||
| for (var chunk : chunksForInput) { | ||
| chunks.add(new RerankChunks(i, inputs.get(i).substring(chunk.start(), chunk.end()))); | ||
| } | ||
| } | ||
| return chunks; | ||
| } | ||
|
|
||
| public List<String> getChunkedInputs() { | ||
| List<String> chunkedInputs = new ArrayList<>(); | ||
| for (RerankChunks chunk : rerankChunks) { | ||
| chunkedInputs.add(chunk.chunkString()); | ||
| } | ||
|
|
||
| return chunkedInputs; | ||
| } | ||
|
|
||
| public ActionListener<InferenceServiceResults> parseChunkedRerankResultsListener(ActionListener<InferenceServiceResults> listener) { | ||
| return ActionListener.wrap(results -> { | ||
| if (results instanceof RankedDocsResults rankedDocsResults) { | ||
| listener.onResponse(parseRankedDocResultsForChunks(rankedDocsResults)); | ||
|
|
||
| } else { | ||
| listener.onFailure(new IllegalArgumentException("Expected RankedDocsResults but got: " + results.getClass())); | ||
| } | ||
|
|
||
| }, listener::onFailure); | ||
| } | ||
|
|
||
| private RankedDocsResults parseRankedDocResultsForChunks(RankedDocsResults rankedDocsResults) { | ||
| List<RankedDocsResults.RankedDoc> topRankedDocs = new ArrayList<>(); | ||
| Set<Integer> docIndicesSeen = new HashSet<>(); | ||
|
|
||
| List<RankedDocsResults.RankedDoc> rankedDocs = new ArrayList<>(rankedDocsResults.getRankedDocs()); | ||
| rankedDocs.sort((r1, r2) -> Float.compare(r2.relevanceScore(), r1.relevanceScore())); | ||
| for (RankedDocsResults.RankedDoc rankedDoc : rankedDocs) { | ||
| int chunkIndex = rankedDoc.index(); | ||
| int docIndex = rerankChunks.get(chunkIndex).docIndex(); | ||
|
|
||
| if (docIndicesSeen.contains(docIndex) == false) { | ||
| // Create a ranked doc with the full input string and the index for the document instead of the chunk | ||
| RankedDocsResults.RankedDoc updatedRankedDoc = new RankedDocsResults.RankedDoc( | ||
| docIndex, | ||
| rankedDoc.relevanceScore(), | ||
| inputs.get(docIndex) | ||
| ); | ||
| topRankedDocs.add(updatedRankedDoc); | ||
| docIndicesSeen.add(docIndex); | ||
| } | ||
| } | ||
|
|
||
| return new RankedDocsResults(topRankedDocs); | ||
| } | ||
|
|
||
| public record RerankChunks(int docIndex, String chunkString) {}; | ||
|
|
||
| private ChunkingSettings buildChunkingSettingsForElasticRerank(String query) { | ||
| var wordIterator = BreakIterator.getWordInstance(); | ||
| wordIterator.setText(query); | ||
| var queryWordCount = ChunkerUtils.countWords(0, query.length(), wordIterator); | ||
| return ChunkingSettingsBuilder.buildChunkingSettingsForElasticRerank(queryWordCount); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.