forked from valkey-io/valkey-glide
-
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.
Signed-off-by: Yury-Fridlyand <[email protected]>
- Loading branch information
1 parent
9e3ce53
commit 7fe92e7
Showing
5 changed files
with
258 additions
and
16 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
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
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
170 changes: 170 additions & 0 deletions
170
java/integTest/src/test/java/glide/modules/VectorSearchTests.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 |
---|---|---|
@@ -0,0 +1,170 @@ | ||
/** Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0 */ | ||
package glide.modules; | ||
|
||
import static glide.TestUtilities.commonClientConfig; | ||
import static glide.TestUtilities.commonClusterClientConfig; | ||
import static glide.api.BaseClient.OK; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import glide.api.BaseClient; | ||
import glide.api.GlideClient; | ||
import glide.api.GlideClusterClient; | ||
import glide.api.models.commands.vss.FTCreateOptions.DistanceMetric; | ||
import glide.api.models.commands.vss.FTCreateOptions.FieldInfo; | ||
import glide.api.models.commands.vss.FTCreateOptions.IndexType; | ||
import glide.api.models.commands.vss.FTCreateOptions.NumericField; | ||
import glide.api.models.commands.vss.FTCreateOptions.TagField; | ||
import glide.api.models.commands.vss.FTCreateOptions.TextField; | ||
import glide.api.models.commands.vss.FTCreateOptions.VectorFieldFlat; | ||
import glide.api.models.commands.vss.FTCreateOptions.VectorFieldHnsw; | ||
import glide.api.models.exceptions.RequestException; | ||
import java.util.List; | ||
import java.util.UUID; | ||
import java.util.concurrent.ExecutionException; | ||
import lombok.Getter; | ||
import lombok.SneakyThrows; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
public class VectorSearchTests { | ||
|
||
@Getter private static List<Arguments> clients; | ||
|
||
@BeforeAll | ||
@SneakyThrows | ||
public static void init() { | ||
var standaloneClient = | ||
GlideClient.createClient(commonClientConfig().requestTimeout(5000).build()).get(); | ||
|
||
var clusterClient = | ||
GlideClusterClient.createClient(commonClusterClientConfig().requestTimeout(5000).build()) | ||
.get(); | ||
|
||
clients = List.of(Arguments.of(standaloneClient), Arguments.of(clusterClient)); | ||
} | ||
|
||
@AfterAll | ||
@SneakyThrows | ||
public static void teardown() { | ||
for (var client : clients) { | ||
((BaseClient) client.get()[0]).close(); | ||
} | ||
} | ||
|
||
@SneakyThrows | ||
@ParameterizedTest(autoCloseArguments = false) | ||
@MethodSource("getClients") | ||
public void ft_create(BaseClient client) { | ||
// create few simple indices | ||
assertEquals( | ||
OK, | ||
client | ||
.ftcreate( | ||
UUID.randomUUID().toString(), | ||
IndexType.HASH, | ||
new String[0], | ||
new FieldInfo[] { | ||
new FieldInfo("vec", "vec", VectorFieldHnsw.builder(DistanceMetric.L2, 2).build()) | ||
}) | ||
.get()); | ||
assertEquals( | ||
OK, | ||
client | ||
.ftcreate( | ||
UUID.randomUUID().toString(), | ||
IndexType.JSON, | ||
new String[] {"json:"}, | ||
new FieldInfo[] { | ||
new FieldInfo( | ||
"$.vec", "VEC", VectorFieldFlat.builder(DistanceMetric.L2, 6).build()) | ||
}) | ||
.get()); | ||
|
||
// create an index with NSFW vector with additional parameters | ||
assertEquals( | ||
OK, | ||
client | ||
.ftcreate( | ||
UUID.randomUUID().toString(), | ||
IndexType.HASH, | ||
new String[] {"docs:"}, | ||
new FieldInfo[] { | ||
new FieldInfo( | ||
"doc_embedding", | ||
VectorFieldHnsw.builder(DistanceMetric.COSINE, 1536) | ||
.numberOfEdges(40) | ||
.vectorsExaminedOnConstruction(250) | ||
.vectorsExaminedOnRuntime(40) | ||
.build()) | ||
}) | ||
.get()); | ||
|
||
// create an index with multiple fields | ||
assertEquals( | ||
OK, | ||
client | ||
.ftcreate( | ||
UUID.randomUUID().toString(), | ||
IndexType.HASH, | ||
new String[] {"blog:post:"}, | ||
new FieldInfo[] { | ||
new FieldInfo("title", new TextField()), | ||
new FieldInfo("published_at", new NumericField()), | ||
new FieldInfo("category", new TagField()) | ||
}) | ||
.get()); | ||
|
||
// create an index with multiple prefixes | ||
var name = UUID.randomUUID().toString(); | ||
assertEquals( | ||
OK, | ||
client | ||
.ftcreate( | ||
name, | ||
IndexType.HASH, | ||
new String[] {"author:details:", "book:details:"}, | ||
new FieldInfo[] { | ||
new FieldInfo("author_id", new TagField()), | ||
new FieldInfo("author_ids", new TagField()), | ||
new FieldInfo("title", new TextField()), | ||
new FieldInfo("name", new TextField()) | ||
}) | ||
.get()); | ||
|
||
// create a duplicating index | ||
var exception = | ||
assertThrows( | ||
ExecutionException.class, | ||
() -> | ||
client | ||
.ftcreate( | ||
name, | ||
IndexType.HASH, | ||
new String[0], | ||
new FieldInfo[] {new FieldInfo("title", new TextField())}) | ||
.get()); | ||
assertInstanceOf(RequestException.class, exception.getCause()); | ||
assertTrue(exception.getMessage().contains("already exists")); | ||
|
||
// create an index without fields | ||
exception = | ||
assertThrows( | ||
ExecutionException.class, | ||
() -> | ||
client | ||
.ftcreate( | ||
UUID.randomUUID().toString(), | ||
IndexType.HASH, | ||
new String[0], | ||
new FieldInfo[0]) | ||
.get()); | ||
assertInstanceOf(RequestException.class, exception.getCause()); | ||
assertTrue(exception.getMessage().contains("arguments are missing")); | ||
} | ||
} |
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