Skip to content

Commit 0ced39a

Browse files
committed
feat(query): validate depth in sub-boosts passed to Boost.blend
1 parent fa79431 commit 0ced39a

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

  • src
    • main/java/io/weaviate/client6/v1/api/collections/query
    • test/java/io/weaviate/client6/v1/api/collections/query

src/main/java/io/weaviate/client6/v1/api/collections/query/Boost.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ public class Boost {
1515
private final Float weight;
1616
private final Integer depth;
1717

18+
// Package-private for testing.
19+
List<Condition> conditions() {
20+
return conditions;
21+
}
22+
23+
// Package-private for testing.
24+
Float weight() {
25+
return weight;
26+
}
27+
28+
// Package-private for testing.
29+
Integer depth() {
30+
return depth;
31+
}
32+
1833
private Boost(Condition condition, Float weight, Integer depth) {
1934
this(List.of(requireNonNull(condition, "condition")), weight, depth);
2035
}
@@ -64,6 +79,9 @@ public static Boost filter(Filter filter,
6479
public static Boost blend(Float weight, Integer depth, Boost... boosts) {
6580
var conditions = Arrays.stream(boosts)
6681
.<Condition>mapMulti((b, stream) -> {
82+
if (b.depth != null) {
83+
throw new IllegalArgumentException("A boost passed to Boosts.blend() cannot set it's own depth.");
84+
}
6785
b.conditions.forEach(cond -> {
6886
if (cond.weight == null && b.weight != null) {
6987
cond = cond.withWeight(b.weight);
@@ -103,6 +121,11 @@ private Condition(Object func, Float weight) {
103121
private Condition withWeight(float weight) {
104122
return new Condition(func, weight);
105123
}
124+
125+
// Package-private for testing.
126+
Float weight() {
127+
return weight;
128+
}
106129
}
107130

108131
public static class FilterBuilder extends Boost.Builder<FilterBuilder> {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package io.weaviate.client6.v1.api.collections.query;
2+
3+
import org.assertj.core.api.Assertions;
4+
import org.junit.Test;
5+
6+
import io.weaviate.client6.v1.api.collections.query.Boost.Condition;
7+
8+
public class BoostTest {
9+
@Test
10+
public void test_blend_useConditionWeight() {
11+
var boost = Boost.blend(null, 8,
12+
Boost.numericProperty("size", prop -> prop.weight(.45f)));
13+
14+
Assertions.assertThat(boost)
15+
.returns(8, Boost::depth)
16+
.returns(null, Boost::weight);
17+
Assertions.assertThat(boost.conditions()).first()
18+
.returns(.45f, Condition::weight);
19+
}
20+
21+
@Test
22+
public void test_blend_passWeightToCondition() {
23+
var boost = Boost.blend(.45f, 8,
24+
Boost.numericProperty("size"));
25+
26+
Assertions.assertThat(boost)
27+
.returns(8, Boost::depth)
28+
.returns(.45f, Boost::weight);
29+
Assertions.assertThat(boost.conditions()).first()
30+
.returns(null, Condition::weight);
31+
}
32+
33+
@Test
34+
public void test_blend_illegalDepth() {
35+
Assertions.assertThatCode(() -> {
36+
Boost.blend(1f, 2,
37+
Boost.numericProperty("size", prop -> prop.depth(3)));
38+
}).isInstanceOf(IllegalArgumentException.class);
39+
}
40+
}

0 commit comments

Comments
 (0)