Repository navigation
Support findAndRerank single leg retrieval - #2678
Conversation
📈 Unit Test Coverage Delta vs Main Branch
|
Unit Test Coverage Report
|
📈 Integration Test Coverage Delta vs Main Branch (dse69-it)
|
Integration Test Coverage Report (dse69-it)
|
📈 Integration Test Coverage Delta vs Main Branch (hcd-it)
|
Integration Test Coverage Report (hcd-it)
|
120c421 to
23b7dbb
Compare
| /** Query came from the $vectorize sort in the user query. This is the default. */ | ||
| VECTORIZE, | ||
| /** Query came from the $lexical sort in the user query, when the sort is only $lexical. */ | ||
| LEXICAL, |
There was a problem hiding this comment.
For a $lexical sort:
- The rerank query defaults to the
$lexicaltext. - The rerank field defaults to
$lexical. - The
rerankQueryandrerankOnoptions replace these defaults. This is the same rule as for a$vectorizesort and a$hybridsort.
Example 1, no options:
{"findAndRerank": {
"filter": {"country": "nz"},
"sort": {"$lexical": "cheese balls"},
"options": {}
}}Lexical read: "I like cheese!". Rerank query: "cheese balls". Rerank field: $lexical.
Example 2, with options:
{"findAndRerank": {
"filter": {},
"sort": {"$lexical": "I like cheese!"},
"options": {
"rerankQuery": "who loves cheese the most?",
"rerankOn": "food_preferences",
"rerankCandidateLimit": 100,
"limit": 10
}
}}The lexical read uses "I like cheese!" and reads up to 100 documents. The reranker compares "who loves cheese the most?" to food_preferences and returns the top 10.
LMK if we don't want to do this
There was a problem hiding this comment.
that looks correct - the $lexical is used for the lexical sort the option.rerankQuery is sent to the rerank model
| switch (command.sortClause()) { | ||
| case FindAndRerankSort.MultiLeg ignored -> | ||
| getOrDefault( | ||
| command.options(), | ||
| FindAndRerankCommand.Options::hybridLimits, | ||
| FindAndRerankCommand.HybridLimits.DEFAULT); | ||
| case FindAndRerankSort.SingleLeg ignored -> { | ||
| var readLimit = plan.legs().getFirst().readLimit(); | ||
| yield new FindAndRerankCommand.HybridLimits( | ||
| readLimit, readLimit, CommandFeatures.EMPTY); | ||
| } | ||
| }); |
There was a problem hiding this comment.
Just reuse the hack to keep things moving, will have another PR to handle this properly.
Test: IT sortWithoutHybridReadsRerankCandidateLimitDocuments and unit test setsReadLimitOfOneLegAsHybridLimitsOnCommandContext ensure it works
| // error order of the command: e.g. {"$hybrid": ""} with no rerankQuery fails with | ||
| // MISSING_RERANK_QUERY_TEXT, not MISSING_HYBRID_SORT_VALUE | ||
| // MISSING_RERANK_QUERY_TEXT, not MISSING_HYBRID_SORT_VALUE. And a command with no sort and no | ||
| // options fails with MISSING_RERANK_QUERY_TEXT, not UNSUPPORTED_FIND_AND_RERANK_SORT. |
There was a problem hiding this comment.
can have another PR to make the error order better
| case FindAndRerankSort.VectorLeg(VectorizeSortExpression ignored) -> | ||
| new FindAndRerankPlan.RerankLeg(RerankLegType.VECTOR_LEG, null, null, readLimit); | ||
| case FindAndRerankSort.VectorLeg(VectorSortExpression(var vector)) -> | ||
| new FindAndRerankPlan.RerankLeg(RerankLegType.VECTOR_LEG, vector, null, readLimit); | ||
| case FindAndRerankSort.LexicalLeg(LexicalSortExpression(var lexicalText)) -> | ||
| new FindAndRerankPlan.RerankLeg(RerankLegType.LEXICAL_LEG, null, lexicalText, readLimit); |
There was a problem hiding this comment.
RerankLeg is an enum tag plus fields that are only valid for some tags (vector, lexicalText). The constructor checks them at runtime. We can change it to a sealed interface with one record per leg, so that an invalid leg cannot be created.
Then we do not need the nulls or the constructor checks, and the builder can switch on the leg and deconstruct it.
LMK if we think it's a good idea to have another PR to refactor
There was a problem hiding this comment.
yes I think this is the sort of pattern we discussed - Enums may become hierarchies, and.... I think the inner classes having the same name makes it harder to understand. Even though the outer class has Sort or Plan in it, I think the inner classes need that as well. e.g. FindAndRerankSort.VectorLegSort and FindAndRerankPlan.RerankLegPlan
| return sort instanceof FindAndRerankSort.LexicalLeg(LexicalSortExpression(var lexicalText)) | ||
| ? RerankingQuery.createForLexicalSort(rerankQueryOption, lexicalText) | ||
| : RerankingQuery.createForVectorizeSort(rerankQueryOption, vectorizeText(sort)); |
There was a problem hiding this comment.
Maybe we can make this better, I just used the simplest way to keep things moving. LMK if we want to improve.
There was a problem hiding this comment.
I did not think decomposition was usable like this. Not sure we can make this any cleaner
| if (isVectorizeSort(command.sortClause())) { | ||
| // use the vectorize field, unless the user has overridden | ||
| finalRerankField = isRerankOn ? rerankOn : VECTOR_EMBEDDING_TEXT_FIELD; | ||
| } else if (isRerankOn) { | ||
| // user has to provide a field to rerank on | ||
| finalRerankField = rerankOn; | ||
| } else { | ||
| throw RequestException.Code.MISSING_RERANK_ON.get(); |
There was a problem hiding this comment.
reorganized a bit to make it easier to understand, lmk if we don't want to change
| case FindAndRerankSort.FilterLeg ignored -> | ||
| throw SortException.Code.UNSUPPORTED_FIND_AND_RERANK_SORT.get(); |
There was a problem hiding this comment.
will have the next PR to support FilterLeg, to keep this PR small
|
Thanks Hazel, a few thoughts:
|
amorton
left a comment
There was a problem hiding this comment.
+1 I have not done a deep dive on tests though
| case FindAndRerankSort.SingleLeg ignored -> | ||
| throw SortException.Code.UNSUPPORTED_FIND_AND_RERANK_SORT.get(); | ||
| case FindAndRerankSort.SingleLeg singleLeg -> | ||
| List.of(singleLeg(singleLeg, rerankCandidateLimit)); |
There was a problem hiding this comment.
nit: which singleLeg is passed to the singleLeg function ? the singleLeg or the singleLeg ? :) On a more serious note, rename the var or function to make it a little easier to read. I think rename the functions to something like build*
| /** | ||
| * Validate the user-supplied rerankCandidateLimit: it cannot be used with a $hybrid sort, and | ||
| * must be inside the dynamic OperationsConfig bounds. | ||
| * The read limit for a sort without $hybrid: the user-supplied rerankCandidateLimit, or the |
There was a problem hiding this comment.
nit: comments to explain the logic like this are better inside the function next to the code they are for. Because it is more likely to get updated and checked, it's easier to read things like "use rerankCandidateLimit from X because Y" or "not defined on Y so use it from Z"
| } | ||
|
|
||
| /** | ||
| * The one leg of a sort without $hybrid, it reads at most <code>readLimit</code> documents. |
There was a problem hiding this comment.
nit: the comments are a bit too conversational the java standard used to be something like:
First sentence: what does this function do ?
extra paragraphs for extra explaining
@params for the params
then linline comments where they make sense.
Just watch out that code comments are for humans as well as machines.
| case FindAndRerankSort.VectorLeg(VectorSortExpression(var vector)) -> | ||
| new FindAndRerankPlan.RerankLeg(RerankLegType.VECTOR_LEG, vector, null, readLimit); |
There was a problem hiding this comment.
+1 this code looks really clean :)
| case FindAndRerankSort.VectorLeg(VectorizeSortExpression ignored) -> | ||
| new FindAndRerankPlan.RerankLeg(RerankLegType.VECTOR_LEG, null, null, readLimit); | ||
| case FindAndRerankSort.VectorLeg(VectorSortExpression(var vector)) -> | ||
| new FindAndRerankPlan.RerankLeg(RerankLegType.VECTOR_LEG, vector, null, readLimit); | ||
| case FindAndRerankSort.LexicalLeg(LexicalSortExpression(var lexicalText)) -> | ||
| new FindAndRerankPlan.RerankLeg(RerankLegType.LEXICAL_LEG, null, lexicalText, readLimit); |
There was a problem hiding this comment.
yes I think this is the sort of pattern we discussed - Enums may become hierarchies, and.... I think the inner classes having the same name makes it harder to understand. Even though the outer class has Sort or Plan in it, I think the inner classes need that as well. e.g. FindAndRerankSort.VectorLegSort and FindAndRerankPlan.RerankLegPlan
| /** Query came from the $vectorize sort in the user query. This is the default. */ | ||
| VECTORIZE, | ||
| /** Query came from the $lexical sort in the user query, when the sort is only $lexical. */ | ||
| LEXICAL, |
There was a problem hiding this comment.
that looks correct - the $lexical is used for the lexical sort the option.rerankQuery is sent to the rerank model
| */ | ||
| public static RerankingQuery create(String rerankQuery, String vectorizeText) { | ||
| public static RerankingQuery createForVectorizeSort(String rerankQuery, String vectorizeText) { | ||
| return create(rerankQuery, vectorizeText, Source.VECTORIZE); |
There was a problem hiding this comment.
nit: we dont need createFor* we can just have for* when we have a factory method like thes.
| switch (command.sortClause()) { | ||
| case FindAndRerankSort.MultiLeg ignored -> | ||
| getOrDefault( | ||
| command.options(), | ||
| FindAndRerankCommand.Options::hybridLimits, | ||
| FindAndRerankCommand.HybridLimits.DEFAULT); | ||
| case FindAndRerankSort.SingleLeg ignored -> { | ||
| var readLimit = plan.legs().getFirst().readLimit(); | ||
| yield new FindAndRerankCommand.HybridLimits( | ||
| readLimit, readLimit, CommandFeatures.EMPTY); | ||
| } | ||
| }); |
| case FindAndRerankSort.HybridText hybridText -> hybridText.text(); | ||
| case FindAndRerankSort.HybridObject hybridObject -> hybridObject.vectorize(); | ||
| case FindAndRerankSort.SingleLeg ignored -> null; | ||
| case FindAndRerankSort.VectorLeg(VectorizeSortExpression(var text)) -> text; |
There was a problem hiding this comment.
Do you think this function, and few below should move to be default methods on the FindAndRerankSort interface ?
In a classic Object Orientated approach we would define the method as virtual and implement it in the appropirate sub classes.
In this new data orientated approach I am not 100% sure where behavour like this should sit. OR we could say "the only thing dealing with this is the planner, and we keep behaviour out of our data model". Can leave as is for now, more thinking about how we want to handle new data modeling techniques.
There was a problem hiding this comment.
will rethink about this later
| if (isVectorizeSort(command.sortClause())) { | ||
| // use the vectorize field, unless the user has overridden | ||
| finalRerankField = isRerankOn ? rerankOn : VECTOR_EMBEDDING_TEXT_FIELD; | ||
| } else if (isRerankOn) { | ||
| // user has to provide a field to rerank on | ||
| finalRerankField = rerankOn; | ||
| } else { | ||
| throw RequestException.Code.MISSING_RERANK_ON.get(); |
What this PR does:
findAndRerank now supports a sort single leg retrieval:
$vectorize,$vectoror$lexicalrerankCandidateLimitdocuments (or the configured default).$vectorizeand$lexicalgive the default rerank query and rerank field. A$vectorsort needs thererankQueryandrerankOnoptions. The options always win.Out of scope:
Which issue(s) this PR fixes:
Fixes #2602
Checklist