Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
import java.util.Objects;

/**
* The reranking query to use for a reranking operation. Either from the $vectorize sort clause or
* manually specified by the user.
* The reranking query to use for a reranking operation. Either from the $vectorize or $lexical sort
* clause, or manually specified by the user.
*/
public class RerankingQuery implements Recordable {

public enum Source {
/** 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,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

For a $lexical sort:

  • The rerank query defaults to the $lexical text.
  • The rerank field defaults to $lexical.
  • The rerankQuery and rerankOn options replace these defaults. This is the same rule as for a $vectorize sort and a $hybrid sort.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

that looks correct - the $lexical is used for the lexical sort the option.rerankQuery is sent to the rerank model

/** Query came from the options.rerankQuery property in the user query. */
OPTIONS,
}
Expand Down Expand Up @@ -45,15 +47,34 @@ public Source source() {
* vectorize.
* @return Constructed RerankingQuery, with the source indicating where the query came from.
*/
public static RerankingQuery create(String rerankQuery, String vectorizeText) {
public static RerankingQuery forVectorizeSort(String rerankQuery, String vectorizeText) {
return create(rerankQuery, vectorizeText, Source.VECTORIZE);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: we dont need createFor* we can just have for* when we have a factory method like thes.

}

/**
* Creates a new RerankingQuery for a $lexical sort: the <code>rerankQuery</code> option when it
* is not blank, otherwise the text of the $lexical sort.
*
* <p>Throws {@link RequestException.Code#MISSING_RERANK_QUERY_TEXT} if both are null or blank.
*
* @param rerankQuery The <code>rerankQuery</code> option, may be <code>null</code>.
* @param lexicalText The text of the $lexical sort.
* @return Constructed RerankingQuery, with the source indicating where the query came from.
*/
public static RerankingQuery forLexicalSort(String rerankQuery, String lexicalText) {
return create(rerankQuery, lexicalText, Source.LEXICAL);
}

/** The rerankQuery option when it is not blank, otherwise the sort text and its source. */
private static RerankingQuery create(String rerankQuery, String sortText, Source sortSource) {

if (rerankQuery != null && !rerankQuery.isBlank()) {
return new RerankingQuery(rerankQuery, Source.OPTIONS);
}

// will never be blank, but double-checking for safety
if (vectorizeText != null && !vectorizeText.isBlank()) {
return new RerankingQuery(vectorizeText, Source.VECTORIZE);
if (sortText != null && !sortText.isBlank()) {
return new RerankingQuery(sortText, sortSource);
}

throw RequestException.Code.MISSING_RERANK_QUERY_TEXT.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import io.stargate.sgv2.jsonapi.api.model.command.*;
import io.stargate.sgv2.jsonapi.api.model.command.clause.filter.SortDefinition;
import io.stargate.sgv2.jsonapi.api.model.command.clause.sort.FindAndRerankSort;
import io.stargate.sgv2.jsonapi.api.model.command.clause.sort.SortClause;
import io.stargate.sgv2.jsonapi.api.model.command.clause.sort.SortExpression;
import io.stargate.sgv2.jsonapi.api.model.command.impl.FindAndRerankCommand;
import io.stargate.sgv2.jsonapi.api.model.command.impl.FindCommand;
import io.stargate.sgv2.jsonapi.metrics.CommandFeatures;
import io.stargate.sgv2.jsonapi.service.embedding.operation.EmbeddingProvider;
import io.stargate.sgv2.jsonapi.service.operation.Operation;
import io.stargate.sgv2.jsonapi.service.operation.embeddings.EmbeddingDeferredAction;
Expand Down Expand Up @@ -189,11 +191,20 @@ private TaskGroupAndDeferrables<IntermediateCollectionReadTask, CollectionSchema
// Hack: See https://github.com/stargate/data-api/issues/1961
// copying the hybrid limits on the command context so the find command resolver can pick it up
// when the command runs later, so we can set the page size to be the same as the limit
// A sort without $hybrid has no hybridLimits, so we copy the read limit of its one leg
commandContext.setHybridLimits(
getOrDefault(
command.options(),
FindAndRerankCommand.Options::hybridLimits,
FindAndRerankCommand.HybridLimits.DEFAULT));
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);
}
});
Comment on lines +196 to +207

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1


var deferrables = new ArrayList<Deferrable>();

Expand Down
Loading