Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 47 additions & 4 deletions src/it/java/io/weaviate/integration/SearchITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -801,13 +801,56 @@ public void test_rerankQueries() throws IOException {
Map.of("title", "Height-adjustable desk", "price", 349));

// Act
var got = things.query.nearText(
"office supplies",
nt -> nt.rerank(Rerank.by("price",
rank -> rank.query("cheaper first"))));
var rerank = Rerank.by("price", rank -> rank.query("cheaper first"));
var got = things.query.nearText("office supplies", nt -> nt.rerank(rerank));

// Assert: ranking not important really, just that the request was valid.
Assertions.assertThat(got.objects()).hasSize(2);

// Assert: rerank is not exclusive to vector search -- BM25, hybrid and
// fetchObjects accept it too, and the server reranks for all of them.
Assertions.assertThat(things.query.bm25("chair", bm25 -> bm25.rerank(rerank)).objects())
.as("bm25").isNotEmpty().allSatisfy(SearchITest::assertReranked);
Assertions.assertThat(things.query.hybrid("chair", hybrid -> hybrid.rerank(rerank)).objects())
.as("hybrid").isNotEmpty().allSatisfy(SearchITest::assertReranked);
Assertions.assertThat(things.query.fetchObjects(fetch -> fetch.rerank(rerank)).objects())
.as("fetchObjects").hasSize(2).allSatisfy(SearchITest::assertReranked);
}

private static void assertReranked(WeaviateObject<Map<String, Object>> object) {
Assertions.assertThat(object.queryMetadata().rerankScore())
.as("rerank score of %s", object.uuid()).isNotNull();
}

@Test
public void test_rerankScoreIsReturned() throws IOException {
// Arrange
var nsThings = ns("Things");

var things = client.collections.create(nsThings,
c -> c
.properties(Property.text("title"), Property.integer("price"))
.vectorConfig(VectorConfig.text2vecModel2Vec(
t2v -> t2v.sourceProperties("title", "price")))
.rerankerModules(new DummyReranker()));

things.data.insertMany(
Map.of("title", "Ergonomic chair", "price", 269),
Map.of("title", "Height-adjustable desk", "price", 349));

// Act
var got = things.query.fetchObjects(
fetch -> fetch.rerank(Rerank.by("title", rank -> rank.query("chair"))));

// Assert: the score which produced the ordering is readable.
Assertions.assertThat(got.objects()).hasSize(2)
.allSatisfy(obj -> Assertions.assertThat(obj.queryMetadata().rerankScore())
.as("rerank score of %s", obj.uuid()).isNotNull());

// Assert: a query without rerank leaves the score unset.
Assertions.assertThat(things.query.fetchObjects().objects()).hasSize(2)
.allSatisfy(obj -> Assertions.assertThat(obj.queryMetadata().rerankScore())
.as("not reranked").isNull());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.util.List;

import io.weaviate.client6.v1.api.collections.query.BaseQueryOptions;
import io.weaviate.client6.v1.api.collections.query.QueryObjectGrouped;
import io.weaviate.client6.v1.api.collections.query.Rerank;

public record GenerativeResponseGroup<PropertiesT>(
/** Group name. */
Expand All @@ -19,6 +21,14 @@ public record GenerativeResponseGroup<PropertiesT>(
Float maxDistance,
/** The size of the group. */
long numberOfObjects,
/**
* Score assigned to this group by the reranker module.
*
* <p>
* Only present if the query requested reranking, see
* {@link BaseQueryOptions.Builder#rerank(Rerank)}.
*/
Double rerankScore,
/** Objects retrieved in the query. */
List<QueryObjectGrouped<PropertiesT>> objects,
/** Output of the summary task for this group. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ static <PropertiesT> GenerativeResponseGrouped<PropertiesT> unmarshal(
group.getMinDistance(),
group.getMaxDistance(),
group.getNumberOfObjects(),
group.hasRerank() ? group.getRerank().getScore() : null,
objects,
generative);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public record BaseQueryOptions(
ConsistencyLevel consistencyLevel,
Filter filters,
Boost boost,
Rerank rerank,
GenerativeSearch generativeSearch,
List<String> returnProperties,
List<QueryReference> returnReferences,
Expand All @@ -40,6 +41,7 @@ private <T extends Object> BaseQueryOptions(Builder<? extends Builder<?, T>, T>
builder.consistencyLevel,
builder.filter,
builder.boost,
builder.rerank,
builder.generativeSearch,
builder.returnProperties,
builder.returnReferences,
Expand All @@ -57,6 +59,7 @@ public static abstract class Builder<SelfT extends Builder<SelfT, T>, T extends
private ConsistencyLevel consistencyLevel;
private Filter filter;
private Boost boost;
private Rerank rerank;
private GenerativeSearch generativeSearch;
private List<String> returnProperties = new ArrayList<>();
private List<QueryReference> returnReferences = new ArrayList<>();
Expand Down Expand Up @@ -152,6 +155,20 @@ public final SelfT boost(Boost boost) {
return (SelfT) this;
}

/**
* Control the ranking of the query results.
*
* <p>
* Reranking is applied by the server on top of the result set produced by the
* search operator, so it works with every operator: {@link NearText} and the
* other {@code near*} searches, {@link Bm25}, {@link Hybrid} and
* {@link FetchObjects}.
*/
public final SelfT rerank(Rerank rerank) {
this.rerank = rerank;
return (SelfT) this;
}

/** Select properties to include in the query result. */
public final SelfT returnProperties(String... properties) {
return returnProperties(Arrays.asList(properties));
Expand Down Expand Up @@ -243,6 +260,10 @@ final void appendTo(WeaviateProtoSearchGet.SearchRequest.Builder req) {
req.setBoost(boost.toProto());
}

if (rerank != null) {
rerank.appendTo(req);
}

if (generativeSearch != null) {
var generative = WeaviateProtoGenerative.GenerativeSearch.newBuilder();
generativeSearch.appendTo(generative);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ abstract class BaseVectorSearchBuilder<SelfT extends BaseVectorSearchBuilder<Sel
// Optional query parameters.
Float distance;
Float certainty;
Rerank rerank;
Diversity diversity;

/**
Expand Down Expand Up @@ -38,15 +37,6 @@ public SelfT certainty(float certainty) {
return (SelfT) this;
}

/**
* Control the ranking of the query results.
*/
@SuppressWarnings("unchecked")
public SelfT rerank(Rerank rerank) {
this.rerank = rerank;
return (SelfT) this;
}

/**
* Apply diversity selection to the query results.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public record NearAudio(
Target searchTarget,
Float distance,
Float certainty,
Rerank rerank,
Diversity diversity,
BaseQueryOptions common)
implements QueryOperator, AggregateObjectFilter {
Expand All @@ -40,7 +39,6 @@ public NearAudio(Builder builder) {
builder.media,
builder.distance,
builder.certainty,
builder.rerank,
builder.diversity,
builder.baseOptions());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public record NearDepth(
Target searchTarget,
Float distance,
Float certainty,
Rerank rerank,
Diversity diversity,
BaseQueryOptions common)
implements QueryOperator, AggregateObjectFilter {
Expand All @@ -40,7 +39,6 @@ public NearDepth(Builder builder) {
builder.media,
builder.distance,
builder.certainty,
builder.rerank,
builder.diversity,
builder.baseOptions());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public record NearImage(
Target searchTarget,
Float distance,
Float certainty,
Rerank rerank,
Diversity diversity,
BaseQueryOptions common)
implements QueryOperator, AggregateObjectFilter {
Expand All @@ -40,7 +39,6 @@ public NearImage(Builder builder) {
builder.media,
builder.distance,
builder.certainty,
builder.rerank,
builder.diversity,
builder.baseOptions());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public record NearImu(
Target searchTarget,
Float distance,
Float certainty,
Rerank rerank,
Diversity diversity,
BaseQueryOptions common)
implements QueryOperator, AggregateObjectFilter {
Expand All @@ -40,7 +39,6 @@ public NearImu(Builder builder) {
builder.media,
builder.distance,
builder.certainty,
builder.rerank,
builder.diversity,
builder.baseOptions());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public record NearObject(
String uuid,
Float distance,
Float certainty,
Rerank rerank,
Diversity diversity,
BaseQueryOptions common)
implements QueryOperator, AggregateObjectFilter {
Expand All @@ -30,7 +29,6 @@ public NearObject(Builder builder) {
builder.uuid,
builder.distance,
builder.certainty,
builder.rerank,
builder.diversity,
builder.baseOptions());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public record NearText(
Target searchTarget,
Float distance,
Float certainty,
Rerank rerank,
Move moveTo,
Move moveAway,
Diversity diversity,
Expand All @@ -44,7 +43,6 @@ public NearText(Builder builder) {
builder.searchTarget,
builder.distance,
builder.certainty,
builder.rerank,
builder.moveTo,
builder.moveAway,
builder.diversity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
public record NearThermal(Target searchTarget,
Float distance,
Float certainty,
Rerank rerank,
Diversity diversity,
BaseQueryOptions common)
implements QueryOperator, AggregateObjectFilter {
Expand All @@ -39,7 +38,6 @@ public NearThermal(Builder builder) {
builder.media,
builder.distance,
builder.certainty,
builder.rerank,
builder.diversity,
builder.baseOptions());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
public record NearVector(NearVectorTarget searchTarget,
Float distance,
Float certainty,
Rerank rerank,
Diversity diversity,
BaseQueryOptions common)
implements QueryOperator, AggregateObjectFilter {
Expand Down Expand Up @@ -44,7 +43,6 @@ public NearVector(Builder builder) {
this(builder.searchTarget,
builder.distance,
builder.certainty,
builder.rerank,
builder.diversity,
builder.baseOptions());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public record NearVideo(
Target searchTarget,
Float distance,
Float certainty,
Rerank rerank,
Diversity diversity,
BaseQueryOptions common)
implements QueryOperator, AggregateObjectFilter {
Expand All @@ -40,7 +39,6 @@ public NearVideo(Builder builder) {
builder.media,
builder.distance,
builder.certainty,
builder.rerank,
builder.diversity,
builder.baseOptions());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,31 @@ public record QueryMetadata(
/** BM25 ranking score. */
Float score,
/** Components of the BM25 ranking score. */
String explainScore) {
String explainScore,
/**
* Score assigned by the reranker module.
*
* <p>
* Only present if the query requested reranking, see
* {@link BaseQueryOptions.Builder#rerank(Rerank)}.
*/
Double rerankScore) {

private QueryMetadata(Builder builder) {
this(
builder.distance,
builder.certainty,
builder.score,
builder.explainScore);
builder.explainScore,
builder.rerankScore);
}

static class Builder implements ObjectBuilder<QueryMetadata> {
private Float distance;
private Float certainty;
private Float score;
private String explainScore;
private Double rerankScore;

final Builder distance(Float distance) {
this.distance = distance;
Expand All @@ -45,6 +55,11 @@ final Builder explainScore(String explainScore) {
return this;
}

final Builder rerankScore(Double rerankScore) {
this.rerankScore = rerankScore;
return this;
}

@Override
public final QueryMetadata build() {
return new QueryMetadata(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ default BaseQueryOptions common() {
return null;
}

/**
* Reranking requested for this query, if any.
*
* <p>
* Reranking is a common query option, so operators that carry
* {@link BaseQueryOptions} read it from there.
*/
default Rerank rerank() {
return null;
return common() != null ? common().rerank() : null;
}

/** Append QueryOperator to the request message. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ public static <PropertiesT> WeaviateProtoSearchGet.SearchRequest marshal(
if (request.operator.common() != null) {
request.operator.common().appendTo(message);
}
if (request.operator.rerank() != null) {
request.operator.rerank().appendTo(message);
}
request.operator.appendTo(message);

defaults.tenant().ifPresent(message::setTenant);
Expand Down
Loading
Loading