forked from weaviate/java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
114 additions
and
147 deletions.
There are no files selected for viewing
This file contains 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
255 changes: 108 additions & 147 deletions
255
src/test/java/io/weaviate/client/v1/graphql/query/argument/HybridArgumentTest.java
This file contains 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,154 +1,115 @@ | ||
package io.weaviate.client.v1.graphql.query.argument; | ||
|
||
import java.util.LinkedHashMap; | ||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class HybridArgumentTest { | ||
|
||
@Test | ||
public void shouldCreateArgument() { | ||
HybridArgument hybrid = HybridArgument.builder() | ||
.query("I'm a simple string") | ||
.build(); | ||
|
||
String str = hybrid.build(); | ||
|
||
assertThat(str).isEqualTo("hybrid:{query:\"I'm a simple string\"}"); | ||
} | ||
|
||
@Test | ||
public void shouldCreateArgumentWithVectorAndAlpha() { | ||
HybridArgument hybrid = HybridArgument.builder() | ||
.query("I'm a simple string") | ||
.vector(new Float[]{.1f, .2f, .3f}) | ||
.alpha(.567f) | ||
.build(); | ||
|
||
String str = hybrid.build(); | ||
|
||
assertThat(str).isEqualTo("hybrid:{query:\"I'm a simple string\" " + | ||
"vector:[0.1,0.2,0.3] " + | ||
"alpha:0.567}"); | ||
} | ||
|
||
@Test | ||
public void shouldCreateArgumentWithVectorAndTargetVectors() { | ||
HybridArgument hybrid = HybridArgument.builder() | ||
.query("I'm a simple string") | ||
.vector(new Float[]{.1f, .2f, .3f}) | ||
.targetVectors(new String[]{"vector1"}) | ||
.build(); | ||
|
||
String str = hybrid.build(); | ||
|
||
assertThat(str).isEqualTo("hybrid:{query:\"I'm a simple string\" " + | ||
"vector:[0.1,0.2,0.3] " + | ||
"targetVectors:[\"vector1\"]}"); | ||
} | ||
|
||
@Test | ||
public void shouldCreateArgumentWithChars() { | ||
HybridArgument hybrid = HybridArgument.builder() | ||
.query("\"I'm a complex string\" says the {'`:string:`'}") | ||
.build(); | ||
|
||
String str = hybrid.build(); | ||
|
||
assertThat(str).isEqualTo("hybrid:{query:\"\\\"I'm a complex string\\\" says the {'`:string:`'}\"}"); | ||
} | ||
|
||
@Test | ||
public void shouldCreateArgumentWithFusionTypeRanked() { | ||
HybridArgument hybrid = HybridArgument.builder() | ||
.query("I'm a simple string") | ||
.fusionType(FusionType.RANKED) | ||
.build(); | ||
|
||
String str = hybrid.build(); | ||
|
||
assertThat(str).isEqualTo("hybrid:{query:\"I'm a simple string\" " + | ||
"fusionType:rankedFusion}"); | ||
} | ||
|
||
@Test | ||
public void shouldCreateArgumentWithFusionTypeRelativeScore() { | ||
HybridArgument hybrid = HybridArgument.builder() | ||
.query("I'm a simple string") | ||
.fusionType(FusionType.RELATIVE_SCORE) | ||
.build(); | ||
|
||
String str = hybrid.build(); | ||
|
||
assertThat(str).isEqualTo("hybrid:{query:\"I'm a simple string\" " + | ||
"fusionType:relativeScoreFusion}"); | ||
} | ||
|
||
@Test | ||
public void shouldCreateArgumentWithProperties() { | ||
HybridArgument hybrid = HybridArgument.builder() | ||
.query("I'm a simple string") | ||
.properties(new String[]{"prop1", "prop2"}) | ||
.build(); | ||
|
||
String str = hybrid.build(); | ||
|
||
assertThat(str).isEqualTo("hybrid:{query:\"I'm a simple string\" " + | ||
"properties:[\"prop1\",\"prop2\"]}"); | ||
} | ||
|
||
@Test | ||
public void shouldCreateArgumentWithNearVectorSearches() { | ||
NearVectorArgument nearVector = NearVectorArgument.builder() | ||
.vector(new Float[]{ .1f, .2f, .3f }) | ||
.certainty(0.9f) | ||
.build(); | ||
|
||
HybridArgument hybrid = HybridArgument.builder() | ||
.query("I'm a simple string") | ||
.searches(HybridArgument.Searches.builder().nearVector(nearVector).build()) | ||
.build(); | ||
|
||
String str = hybrid.build(); | ||
|
||
assertThat(str).isEqualTo("hybrid:{query:\"I'm a simple string\" searches:{nearVector:{vector:[0.1,0.2,0.3] certainty:0.9}}}"); | ||
} | ||
|
||
@Test | ||
public void shouldCreateArgumentWithNearTextSearches() { | ||
NearTextArgument nearText = NearTextArgument.builder() | ||
.concepts(new String[]{"concept"}) | ||
.certainty(0.9f) | ||
.build(); | ||
|
||
HybridArgument hybrid = HybridArgument.builder() | ||
.query("I'm a simple string") | ||
.searches(HybridArgument.Searches.builder().nearText(nearText).build()) | ||
.build(); | ||
|
||
String str = hybrid.build(); | ||
import java.util.LinkedHashMap; | ||
import java.util.stream.Stream; | ||
|
||
assertThat(str).isEqualTo("hybrid:{query:\"I'm a simple string\" searches:{nearText:{concepts:[\"concept\"] certainty:0.9}}}"); | ||
} | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
@Test | ||
public void shouldCreateArgumentWithTargets() { | ||
// given | ||
LinkedHashMap<String, Float> weights = new LinkedHashMap<>(); | ||
weights.put("t1", 0.8f); | ||
weights.put("t2", 0.2f); | ||
Targets targets = Targets.builder() | ||
.targetVectors(new String[]{ "t1", "t2" }) | ||
.combinationMethod(Targets.CombinationMethod.minimum) | ||
.weights(weights) | ||
.build(); | ||
HybridArgument hybrid = HybridArgument.builder() | ||
.query("I'm a simple string").targets(targets) | ||
.build(); | ||
// when | ||
String str = hybrid.build(); | ||
// then | ||
assertThat(str).isEqualTo("hybrid:{query:\"I'm a simple string\" targets:{combinationMethod:minimum targetVectors:[\"t1\",\"t2\"] weights:{t1:0.8 t2:0.2}}}"); | ||
} | ||
public class HybridArgumentTest { | ||
static Stream<Arguments> testCases() { | ||
return Stream.of( | ||
Arguments.of( | ||
"simple query", | ||
HybridArgument.builder() | ||
.query("I'm a simple string") | ||
.build(), | ||
"hybrid:{query:\"I'm a simple string\"}"), | ||
Arguments.of( | ||
"vector and alpha", | ||
HybridArgument.builder() | ||
.query("I'm a simple string") | ||
.vector(new Float[] { .1f, .2f, .3f }) | ||
.alpha(.567f) | ||
.build(), | ||
"hybrid:{query:\"I'm a simple string\" vector:[0.1,0.2,0.3] alpha:0.567}"), | ||
Arguments.of( | ||
"vector and target vectors", | ||
HybridArgument.builder() | ||
.query("I'm a simple string") | ||
.vector(new Float[] { .1f, .2f, .3f }) | ||
.targetVectors(new String[] { "vector1" }) | ||
.build(), | ||
"hybrid:{query:\"I'm a simple string\" vector:[0.1,0.2,0.3] targetVectors:[\"vector1\"]}"), | ||
Arguments.of( | ||
"with escaped characters", | ||
HybridArgument.builder() | ||
.query("\"I'm a complex string\" says the {'`:string:`'}") | ||
.build(), | ||
"hybrid:{query:\"\\\"I'm a complex string\\\" says the {'`:string:`'}\"}"), | ||
Arguments.of( | ||
"fusion type ranked", | ||
HybridArgument.builder() | ||
.query("I'm a simple string") | ||
.fusionType(FusionType.RANKED) | ||
.build(), | ||
"hybrid:{query:\"I'm a simple string\" fusionType:rankedFusion}"), | ||
Arguments.of( | ||
"fusion type relative score", | ||
HybridArgument.builder() | ||
.query("I'm a simple string") | ||
.fusionType(FusionType.RELATIVE_SCORE) | ||
.build(), | ||
"hybrid:{query:\"I'm a simple string\" fusionType:relativeScoreFusion}"), | ||
Arguments.of( | ||
"specify properties to search on", | ||
HybridArgument.builder() | ||
.query("I'm a simple string") | ||
.properties(new String[] { "prop1", "prop2" }) | ||
.build(), | ||
"hybrid:{query:\"I'm a simple string\" properties:[\"prop1\",\"prop2\"]}"), | ||
Arguments.of( | ||
"nearVector search", | ||
HybridArgument.builder().query("I'm a simple string") | ||
.searches(HybridArgument.Searches.builder() | ||
.nearVector(NearVectorArgument | ||
.builder() | ||
// @formatter:off | ||
.vector(new Float[] {.1f, .2f, .3f }) | ||
// @formatter:on | ||
.certainty(0.9f) | ||
.build()) | ||
.build()) | ||
.build(), | ||
"hybrid:{query:\"I'm a simple string\" searches:{nearVector:{vector:[0.1,0.2,0.3] certainty:0.9}}}"), | ||
Arguments.of( | ||
"nearText search", | ||
HybridArgument.builder().query("I'm a simple string") | ||
// @formatter:off | ||
.searches( | ||
HybridArgument.Searches.builder().nearText( | ||
NearTextArgument.builder() | ||
.concepts(new String[] { "concept" }) | ||
.certainty(0.9f) | ||
.build()) | ||
.build()) | ||
// @formatter:on | ||
.build(), | ||
"hybrid:{query:\"I'm a simple string\" searches:{nearText:{concepts:[\"concept\"] certainty:0.9}}}"), | ||
Arguments.of( | ||
"target vectors", | ||
HybridArgument.builder().query("I'm a simple string") | ||
.targets(Targets.builder() | ||
// @formatter:off | ||
.targetVectors(new String[] { "t1", "t2" }) | ||
.combinationMethod(Targets.CombinationMethod.minimum) | ||
.weights(new LinkedHashMap<String, Float>() {{ | ||
put("t1", 0.8f); | ||
put("t2", 0.2f); | ||
}}) | ||
.build()) | ||
// @formatter:on | ||
.build(), | ||
"hybrid:{query:\"I'm a simple string\" targets:{combinationMethod:minimum targetVectors:[\"t1\",\"t2\"] weights:{t1:0.8 t2:0.2}}}")); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("testCases") | ||
void test(String name, HybridArgument hybrid, String expected) { | ||
String actual = hybrid.build(); | ||
assertThat(actual).as(name).isEqualTo(expected); | ||
} | ||
} |