diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..d19e182d --- /dev/null +++ b/.editorconfig @@ -0,0 +1,18 @@ +root = true + +[*] +charset = utf-8 +indent_style = tab +indent_size = 4 + +[*.{md,markdown,json,js,xml,yml}] +indent_style = space +indent_size = 2 + +[*.{md,markdown}] +trim_trailing_whitespace = false +max_line_length = 80 + +[*.{sh,bat,ps1}] +trim_trailing_whitespace = true +insert_final_newline = true diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..3776e59c --- /dev/null +++ b/.gitattributes @@ -0,0 +1,11 @@ +# Auto detect text files and perform LF normalization +* text=auto eol=lf + +# Set default behavior for command prompt diff. +# This gives output on command line taking java language constructs into consideration (e.g showing class name) +*.java text diff=java + +# Set windows specific files explicitly to crlf line ending +*.cmd eol=crlf +*.bat eol=crlf +*.ps1 eol=crlf \ No newline at end of file diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 00000000..9aafe3a8 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,65 @@ +name: Build + +on: + push: + branches: [ "master" ] + pull_request_target: + branches: [ "master" ] + +permissions: + contents: read + checks: write + +jobs: + build: + name: Build + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + + - name: Build + uses: gradle/gradle-build-action@v2 + with: + gradle-version: 8.5 + arguments: build + + - name: Test + uses: gradle/gradle-build-action@v2 + with: + gradle-version: 8.5 + arguments: test + + - name: Test Results + uses: mikepenz/action-junit-report@v4 + if: always() + with: + fail_on_failure: true + require_tests: true + report_paths: '**/build/test-results/test/TEST-*.xml' + + - name: Upload Jars + uses: actions/upload-artifact@v3 + with: + name: QdrantJava + path: build/libs + + publish: + runs-on: ubuntu-latest + needs: test + steps: + - name: Deploy javadoc to Github Pages + uses: dev-vince/actions-publish-javadoc@v1.0.1 + with: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + java-version: "8" + java-distribution: "adopt" # The distributor of the target JDK. See https://github.com/actions/setup-java for more information. + project: maven # The project type. + branch: "gh-pages" # The branch for the javadoc contents. diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml deleted file mode 100644 index 4b345eba..00000000 --- a/.github/workflows/cd.yml +++ /dev/null @@ -1,42 +0,0 @@ -name: Test And Deploy - -permissions: - contents: write - -on: - push: - branches: - - master - -jobs: - test: - runs-on: ubuntu-latest - strategy: - matrix: - java-version: ["8", "11", "16", "17", "21"] - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-java@v3 - with: - java-version: ${{ matrix.java-version }} - distribution: "temurin" - - name: Set execute permission - run: | - chmod +x ./tools/mvn_test.sh - - name: Maven tests - run: | - ./tools/mvn_test.sh - shell: bash - - publish: - runs-on: ubuntu-latest - needs: test - steps: - - name: Deploy javadoc to Github Pages - uses: dev-vince/actions-publish-javadoc@v1.0.1 - with: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - java-version: "8" - java-distribution: "adopt" # The distributor of the target JDK. See https://github.com/actions/setup-java for more information. - project: maven # The project type. - branch: "gh-pages" # The branch for the javadoc contents. diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml deleted file mode 100644 index cd540363..00000000 --- a/.github/workflows/test.yml +++ /dev/null @@ -1,29 +0,0 @@ -name: Maven Tests - -on: - pull_request: - types: - - opened - - edited - - synchronize - - reopened - -jobs: - test: - runs-on: ubuntu-latest - strategy: - matrix: - java-version: ["8", "11", "16", "17", "21"] - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-java@v3 - with: - java-version: ${{ matrix.java-version }} - distribution: "temurin" - - name: Set execute permission - run: | - chmod +x ./tools/mvn_test.sh - - name: Maven tests - run: | - ./tools/mvn_test.sh - shell: bash diff --git a/.gitignore b/.gitignore index 94c668b5..b5136074 100644 --- a/.gitignore +++ b/.gitignore @@ -1,23 +1,47 @@ -target/ -pom.xml.tag -pom.xml.releaseBackup -pom.xml.versionsBackup -pom.xml.next -release.properties -dependency-reduced-pom.xml -buildNumber.properties -.mvn/timing.properties -# https://github.com/takari/maven-wrapper#usage-without-binary-jar -.mvn/wrapper/maven-wrapper.jar +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ -# Eclipse m2e generated files -# Eclipse Core -.project -# JDT-specific (Eclipse Java Development Tools) +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +.idea/uiDesigner.xml +.idea/codeStyles/codeStyleConfig.xml +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated .classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ -# VS Code -.vscode/* +### Mac OS ### +.DS_Store -# Others -.DS_Store \ No newline at end of file +### Project specific ### +protos diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..13566b81 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 00000000..2a11f8b9 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +client \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 00000000..e7c302f9 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,29 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..11303e26 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..94a25f7f --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000..05b20cb8 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,124 @@ +# Contributing to Java client for Qdrant + +We love your input! We want to make contributing to this project as easy and transparent as possible, whether it's: + +- Reporting a bug +- Discussing the current state of the code +- Submitting a fix +- Proposing new features + +## We Develop with GitHub + +We use github to host code, to track issues and feature requests, as well as accept pull requests. + +We Use [GitHub Flow](https://docs.github.com/en/get-started/quickstart/github-flow), so all code changes +happen through Pull Requests. Pull requests are the best way to propose changes to the codebase. + +It's usually best to open an issue first to discuss a feature or bug before opening a pull request. +Doing so can save time and help further ascertain the crux of an issue. + +1. See if there is an existing issue +2. Fork the repo and create your branch from `master`. +3. If you've added code that should be tested, add tests. +4. Ensure the test suite passes. +5. Issue that pull request! + +### Any contributions you make will be under the Apache License 2.0 + +In short, when you submit code changes, your submissions are understood to be under the +same [Apache License 2.0](https://choosealicense.com/licenses/apache-2.0/) that covers the project. +Feel free to contact the maintainers if that's a concern. + +## Report bugs using GitHub's [issues](https://github.com/qdrant/java-client/issues) + +We use GitHub issues to track public bugs. Report a bug by +[opening a new issue](https://github.com/qdrant/java-client/issues/new); it's that easy! + +**Great Bug Reports** tend to have: + +- A quick summary and/or background +- Steps to reproduce + - Be specific! + - Give sample code if you can. +- What you expected would happen +- What actually happens +- Notes (possibly including why you think this might be happening, or stuff you tried that didn't work) + +## Coding Styleguide + +If you are modifying code, make sure it has no warnings when building. + +## License + +By contributing, you agree that your contributions will be licensed under its Apache License 2.0. + +# Building the solution + +The solution uses several open source software tools: + +## Docker + +Qdrant docker image is used to run integration tests. Be sure to +[install docker](https://docs.docker.com/engine/install/) and have it running when running tests. + +## Gradle + +[Gradle](https://docs.gradle.org/current/userguide/userguide.html) is used as the build automation tool for the solution. +To get started after cloning the solution, it's best to run the gradlew wrapper script in the root + +for Windows + +``` +.\gradlew.bat build +``` + +for OSX/Linux + +``` +./gradlew build +``` + +This will + +- Pull down all the dependencies for the build process as well as the solution +- Run the default build task for the solution + +You can also compile the solution within IntelliJ or other IDEs if you prefer. + +## Tests + +jUnit5 tests are run as part of the default build task. These can also be run with + +``` +./gradlew test +``` + +## Updating the client + +A large portion of the client is generated from the upstream qdrant proto definitions, which are +downloaded locally as needed, based on the version defined by `qdrantProtosVersion` in gradle.properties +in the root directory. + +When a new qdrant version is released upstream, update the `qdrantProtosVersion` value to the new version, +then run the build script + +for Windows + +``` +.\gradlew.bat build +``` + +for OSX/Linux + +``` +./gradlew build +``` + +A specific version of the qdrant docker image can be targeted by modifying the `qdrantVersion` +in gradle.properties. + +The generated files do not form part of the checked in source code. Instead, they are generated +and emitted into the build/generated/source directory, and included in compilation. + +If upstream changes to proto definitions change the API of generated code, you may need +to fix compilation errors in code that relies on that generated code. \ No newline at end of file diff --git a/README.md b/README.md index 12b4950c..2c8757c5 100644 --- a/README.md +++ b/README.md @@ -31,133 +31,153 @@ Java client library with handy utility methods and overloads for interfacing wit To install the library, add the following lines to your build config file. #### Maven + ```xml io.qdrant client - 1.0 + 1.7-SNAPSHOT ``` #### Scala SBT + ```sbt -libraryDependencies += "io.qdrant" % "client" % "1.0" +libraryDependencies += "io.qdrant" % "client" % "1.7-SNAPSHOT" ``` #### Gradle + ```gradle -implementation 'io.qdrant:client:1.0' +implementation 'io.qdrant:client:1.7-SNAPSHOT' ``` ## 📖 Documentation + - [`QdrantClient` Reference](https://qdrant.github.io/java-client/io/qdrant/client/QdrantClient.html#constructor-detail) -- [Utility Methods Reference](https://qdrant.github.io/java-client/io/qdrant/client/utils/package-summary.html) -## 🔌 Connecting to Qdrant +## 🔌 Getting started -> [!NOTE] -> The library uses Qdrant's GRPC interface. The default port being `6334`. -> -> Uses `TLS` if the URL protocol is `https`, plaintext otherwise. +### Creating a client -#### Connecting to a local Qdrant instance -```java -import io.qdrant.client.QdrantClient; +A client can be instantiated with -QdrantClient client = new QdrantClient("http://localhost:6334"); +```java +QdrantClient client = + new QdrantClient(QdrantGrpcClient.newBuilder("localhost").build()); ``` +which creates a client that will connect to Qdrant on https://localhost:6334. -#### Connecting to Qdrant cloud -```java -import io.qdrant.client.QdrantClient; +Internally, the high level client uses a low level gRPC client to interact with +Qdrant. Additional constructor overloads provide more control over how the gRPC +client is configured. The following example configures a client to use TLS, +validating the certificate using the root CA to verify the server's identity +instead of the system's default, and also configures API key authentication: -QdrantClient client = new QdrantClient("https://xyz-eg.eu-central.aws.cloud.qdrant.io:6334", ""); +```java +ManagedChannel channel = Grpc.newChannelBuilder( + "localhost:6334", + TlsChannelCredentials.newBuilder() + .trustManager(new File("ssl/ca.crt")) + .build()) +.build(); + +QdrantClient client = new QdrantClient( + QdrantGrpcClient.newBuilder(channel) + .withApiKey("") + .build()); ``` -## 🧪 Example Usage -
-Click to expand example - +The client implements [`AutoCloseable`](https://docs.oracle.com/javase/8/docs/api/java/lang/AutoCloseable.html), +though a client will typically be created once and used for the lifetime of the +application. When a client is constructed by passing a `ManagedChannel`, the +client does not shut down the channel on close by default. The client can be +configured to shut down the channel on close with -#### You can connect to Qdrant by instantiating a [QdrantClient](https://qdrant.github.io/java-client/io/qdrant/client/QdrantClient.html) instance. ```java -import io.qdrant.client.QdrantClient; +ManagedChannel channel = Grpc.newChannelBuilder( + "localhost:6334", + TlsChannelCredentials.create()) +.build(); + +QdrantClient client = new QdrantClient( + QdrantGrpcClient.newBuilder(channel, true) + .withApiKey("") + .build()); +``` -QdrantClient client = new QdrantClient("http://localhost:6334"); +All client methods return `ListenableFuture`. -System.out.println(client.listCollections()); -``` -*Output*: -``` -collections { -name: "Documents" -} -collections { -name: "some_collection" -} -time: 7.04541E-4 -``` +### Working with collections -#### We can now perform operations on the DB. Like creating a collection, adding a point. -The library offers handy utility methods for constructing GRPC structures. +Once a client has been created, create a new collection ```java -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import io.qdrant.client.utils.*; +client.createCollectionAsync("my_collection", + VectorParams.newBuilder() + .setDistance(Distance.Cosine) + .setSize(4) + .build()) + .get(); +``` -String collectionName = "Documents"; +Insert vectors into a collection -client.recreateCollection(collectionName, 6, Distance.Cosine); +```java +// import static convenience methods +import static io.qdrant.client.PointIdFactory.id; +import static io.qdrant.client.ValueFactory.value; +import static io.qdrant.client.VectorsFactory.vector; + +Random random = new Random(); +List points = IntStream.range(1, 101) + .mapToObj(i -> PointStruct.newBuilder() + .setId(id(i)) + .setVectors(vector(IntStream.range(1, 101) + .mapToObj(v -> random.nextFloat()) + .collect(Collectors.toList()))) + .putAllPayload(ImmutableMap.of( + "color", value("red"), + "rand_number", value(i % 10)) + ) + .build() + ) + .collect(Collectors.toList()); + +UpdateResult updateResult = client.upsertAsync("my_collection", points).get(); +``` -Map map = new HashMap<>(); -map.put("name", "John Doe"); -map.put("age", 42); -map.put("married", true); +Search for similar vectors -PointStruct point = - PointUtil.point( - 0, - VectorUtil.toVector(0.0f, 0.1f, 0.2f, 0.3f, 0.4f, 0.5f), - PayloadUtil.toPayload(map)); -List points = Arrays.asList(point); -client.upsertPoints(collectionName, points, null); +```java +List queryVector = IntStream.range(1, 101) + .mapToObj(v -> random.nextFloat()) + .collect(Collectors.toList()); + +List points = client.searchAsync(SearchPoints.newBuilder() + .setCollectionName("my_collection") + .addAllVector(queryVector) + .setLimit(5) + .build() +).get(); ``` -#### Performing a search on the vectors with filtering +Search for similar vectors with filtering condition + ```java -import io.qdrant.client.grpc.Points.Filter; -import io.qdrant.client.grpc.Points.SearchPoints; -import io.qdrant.client.grpc.Points.SearchResponse; - -import io.qdrant.client.utils.*; - -Filter filter = FilterUtil.must(FilterUtil.fieldCondition("age", FilterUtil.match(42))); - -SearchPoints request = SearchPoints.newBuilder() - .setCollectionName(collectionName) - .addAllVector(Arrays.asList(0.0f, 0.1f, 0.2f, 0.3f, 0.4f, 0.5f)) - .setFilter(filter) - .setWithPayload(SelectorUtil.withPayload()) - .setLimit(10) - .build(); -SearchResponse result = client.searchPoints(request); - -ScoredPoint result = results.getResult(0); - -System.out.println("Similarity: " + result.getScore()); -System.out.println("Payload: " + PayloadUtil.toMap(result.getPayload())); -``` -*Output*: +// import static convenience methods +import static io.qdrant.client.ConditionFactory.range; + +List points = client.searchAsync(SearchPoints.newBuilder() + .setCollectionName("my_collection") + .addAllVector(queryVector) + .setFilter(Filter.newBuilder() + .addMust(range("rand_number", Range.newBuilder().setGte(3).build())) + .build()) + .setLimit(5) + .build() +).get(); ``` -Similarity: 0.9999999 -Payload: {name=John Doe, married=true, age=42} -``` - -
## ⚖️ LICENSE diff --git a/build.gradle b/build.gradle new file mode 100644 index 00000000..722d6c0e --- /dev/null +++ b/build.gradle @@ -0,0 +1,231 @@ +import net.ltgt.gradle.errorprone.CheckSeverity +import org.ajoberstar.grgit.Grgit +import org.apache.commons.compress.archivers.tar.TarArchiveEntry +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream +import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream +import org.apache.http.client.methods.HttpGet +import org.apache.http.impl.client.HttpClients + +import java.nio.file.Files +import java.nio.file.Paths +import java.nio.file.StandardOpenOption +import java.time.ZoneOffset +import java.util.regex.Pattern + +plugins { + id 'java-library' + id 'idea' + id 'signing' + id 'maven-publish' + + id 'com.google.protobuf' version '0.9.4' + id "net.ltgt.errorprone" version '3.1.0' +} + +group = 'io.qdrant' +version = packageVersion +description = 'Official Java client for Qdrant vector database' + +repositories { + // google mirror for maven + maven { + url 'https://maven-central.storage-download.googleapis.com/maven2/' + } + mavenCentral() + mavenLocal() +} + +java { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 + + withJavadocJar() + withSourcesJar() +} + +tasks.withType(JavaCompile).configureEach { + // exclude generated code from error prone checks + options.errorprone.excludedPaths.set(".*/build/generated/.*") + options.errorprone { + //noinspection GroovyAssignabilityCheck + check("NullAway", CheckSeverity.ERROR) + option("NullAway:AnnotatedPackages", "com.uber") + } +} + +javadoc { + // exclude code generated from protos + exclude 'io/qdrant/client/grpc/**' + exclude 'grpc/**' +} + +sourcesJar { + // exclude generated duplicate of com/qdrant/client/grpc/CollectionsGrpc.java + duplicatesStrategy = DuplicatesStrategy.EXCLUDE +} + +jar { + doFirst { + def git = Grgit.open(Map.of('currentDir', rootProject.rootDir)) + // add qdrant version from which client is generated. + jar.manifest.attributes['X-Qdrant-Version'] = qdrantProtosVersion + // add git revision and commit time to jar manifest + jar.manifest.attributes['X-Git-Revision'] = git.head().id + jar.manifest.attributes['X-Git-Commit-Time'] = git.head().dateTime.withZoneSameLocal(ZoneOffset.UTC) + git.close() + } +} + +def grpcVersion = '1.59.0' +def protobufVersion = '3.24.0' +def protocVersion = protobufVersion +def testContainersVersion = '1.19.2' +def jUnitVersion = '5.8.1' + +dependencies { + errorprone "com.uber.nullaway:nullaway:0.10.18" + + implementation "io.grpc:grpc-protobuf:${grpcVersion}" + implementation "io.grpc:grpc-services:${grpcVersion}" + implementation "io.grpc:grpc-stub:${grpcVersion}" + implementation "com.google.guava:guava:30.1-jre" + implementation "org.slf4j:slf4j-api:2.0.7" + + compileOnly "org.apache.tomcat:annotations-api:6.0.53" + compileOnly "com.google.code.findbugs:jsr305:3.0.2" + + errorprone "com.google.errorprone:error_prone_core:2.23.0" + + runtimeOnly "io.grpc:grpc-netty-shaded:${grpcVersion}" + + testImplementation "io.grpc:grpc-testing:${grpcVersion}" + testImplementation "org.junit.jupiter:junit-jupiter-api:${jUnitVersion}" + testImplementation "org.mockito:mockito-core:3.4.0" + testImplementation "org.testcontainers:testcontainers:${testContainersVersion}" + testImplementation "org.testcontainers:junit-jupiter:${testContainersVersion}" + + testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${jUnitVersion}" +} + +tasks.register('downloadProtos') { + // gradle detects changes to this output dir since last run to determine whether to run this task + outputs.dir(new File(rootProject.rootDir, "protos/${qdrantProtosVersion}")) + + doLast { + def outputDirectory = outputs.files.singleFile.toPath().toString() + def protoFileRegex = Pattern.compile(".*?lib/api/src/grpc/proto/.*?.proto") + try (def httpClient = HttpClients.createDefault()) { + def url = "https://api.github.com/repos/qdrant/qdrant/tarball/refs/tags/${qdrantProtosVersion}" + logger.debug("downloading protos from {}", url) + def response = httpClient.execute(new HttpGet(url)) + try (InputStream tarballStream = response.getEntity().getContent()) { + try (TarArchiveInputStream tarInput = new TarArchiveInputStream(new GzipCompressorInputStream(tarballStream))) { + TarArchiveEntry entry + while ((entry = tarInput.getNextTarEntry()) != null) { + if (!entry.isDirectory() && protoFileRegex.matcher(entry.getName()).matches()) { + def lines = new ArrayList() + def lineNum = -1 + def seenJavaPackage = false + def br = new BufferedReader(new InputStreamReader(tarInput)) + String line + while ((line = br.readLine()) != null) { + lines.add(line) + if (line == "package qdrant;") { + lineNum = lines.size() + } else if (line.startsWith("option java_package")) { + seenJavaPackage = true + } + } + // patch in java package to qdrant protos + if (!seenJavaPackage && lineNum != -1) { + lines.add(lineNum, "option java_package = \"io.qdrant.client.grpc\";") + } + + def fileName = Paths.get(entry.getName()).getFileName().toString() + def dest = java.nio.file.Path.of(outputDirectory, fileName) + logger.debug("writing {} to {}", fileName, dest) + Files.write(dest, lines, StandardOpenOption.CREATE) + } + } + } + } + } + } +} + +processResources { + dependsOn downloadProtos +} + +extractIncludeProto { + dependsOn downloadProtos +} + +protobuf { + protoc { artifact = "com.google.protobuf:protoc:${protocVersion}" } + plugins { + grpc { artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}" } + } + generateProtoTasks { + all()*.plugins { grpc {} } + } +} + +// Inform IDEs like IntelliJ IDEA, Eclipse or NetBeans about the generated code. +sourceSets { + main { + proto { + // include protos from outside the sourceSets + //noinspection GroovyAssignabilityCheck + srcDir "protos/${qdrantProtosVersion}" + } + java { + srcDirs 'build/generated/source/proto/main/grpc' + srcDirs 'build/generated/source/proto/main/java' + } + } +} + +test { + useJUnitPlatform() + + // Set system property to use as docker image version for integration tests + systemProperty 'qdrantVersion', qdrantVersion +} + +def organization = 'qdrant' +def repository = 'java-client' + +publishing { + publications { + mavenJava(MavenPublication) { + from components.java + pom { + description = "${project.description}" + url = "https://github.com/${organization}/${repository}" + licenses { + license { + name = 'The Apache License, Version 2.0' + url = 'http://www.apache.org/licenses/LICENSE-2.0.txt' + distribution = 'repo' + } + } + developers { + developer { + id = 'qdrant' + name = 'Qdrant and Contributors' + email = 'info@qdrant.com' + } + } + scm { + connection = "scm:git:git://github.com/${organization}/${repository}.git" + developerConnection = "scm:git:ssh://github.com/${organization}/${repository}.git" + url = "https://github.com/${organization}/${repository}" + } + } + } + } + repositories { + mavenLocal() + } +} \ No newline at end of file diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle new file mode 100644 index 00000000..d90e0954 --- /dev/null +++ b/buildSrc/build.gradle @@ -0,0 +1,9 @@ +dependencies { + implementation 'org.ajoberstar.grgit:grgit-gradle:5.0.0' + implementation 'org.apache.httpcomponents:httpclient:4.5.14' + implementation 'org.apache.commons:commons-compress:1.23.0' +} + +repositories { + mavenCentral() +} \ No newline at end of file diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 00000000..44e0858b --- /dev/null +++ b/gradle.properties @@ -0,0 +1,8 @@ +# The version of qdrant to use to download protos +qdrantProtosVersion=v1.7.0 + +# The version of qdrant docker image to run integration tests against +qdrantVersion=v1.7.0 + +# The version of the client to generate +packageVersion=1.7-SNAPSHOT \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..033e24c4 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..1af9e093 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100644 index 00000000..fcb6fca1 --- /dev/null +++ b/gradlew @@ -0,0 +1,248 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 00000000..93e3f59f --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,92 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/pom.xml b/pom.xml deleted file mode 100644 index b728c7c6..00000000 --- a/pom.xml +++ /dev/null @@ -1,136 +0,0 @@ - - - 4.0.0 - - io.qdrant - client - - 1.0 - - - - io.grpc - grpc-netty-shaded - 1.59.0 - runtime - - - io.grpc - grpc-protobuf - 1.59.0 - - - io.grpc - grpc-stub - 1.59.0 - - - org.apache.tomcat - annotations-api - 6.0.53 - provided - - - - org.junit.jupiter - junit-jupiter-engine - 5.5.2 - test - - - - - - - - kr.motd.maven - os-maven-plugin - 1.7.1 - - - - - org.xolstice.maven.plugins - protobuf-maven-plugin - 0.6.1 - - com.google.protobuf:protoc:3.24.0:exe:${os.detected.classifier} - grpc-java - - io.grpc:protoc-gen-grpc-java:1.59.0:exe:${os.detected.classifier} - - - - - compile - compile-custom - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.2.1 - - io.qdrant.client.Main - - - - org.apache.maven.plugins - maven-surefire-plugin - 3.0.0-M3 - - - org.apache.maven.plugins - maven-javadoc-plugin - 3.6.2 - - - maven-compiler-plugin - 3.11.0 - - 1.8 - 1.8 - - - - org.apache.maven.plugins - maven-source-plugin - 2.2.1 - - - attach-sources - - jar-no-fork - - - - - - com.coveo - fmt-maven-plugin - 2.9 - - - - format - - - - - - - - - ossrh - https://s01.oss.sonatype.org/content/repositories/snapshots - - - ossrh - https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ - - - \ No newline at end of file diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 00000000..dd1a777f --- /dev/null +++ b/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'client' + diff --git a/src/main/java/io/qdrant/client/ApiKeyCredentials.java b/src/main/java/io/qdrant/client/ApiKeyCredentials.java new file mode 100644 index 00000000..d3a02e4f --- /dev/null +++ b/src/main/java/io/qdrant/client/ApiKeyCredentials.java @@ -0,0 +1,35 @@ +package io.qdrant.client; + +import io.grpc.CallCredentials; +import io.grpc.Metadata; +import io.grpc.Status; + +import java.util.concurrent.Executor; + +/** + * API key authentication credentials + */ +public class ApiKeyCredentials extends CallCredentials { + private final String apiKey; + + /** + * Instantiates a new instance of {@link ApiKeyCredentials} + * @param apiKey The API key to use for authentication + */ + public ApiKeyCredentials(String apiKey) { + this.apiKey = apiKey; + } + + @Override + public void applyRequestMetadata(RequestInfo requestInfo, Executor appExecutor, MetadataApplier applier) { + appExecutor.execute(() -> { + try { + Metadata headers = new Metadata(); + headers.put(Metadata.Key.of("api-key", Metadata.ASCII_STRING_MARSHALLER), apiKey); + applier.apply(headers); + } catch (Throwable e) { + applier.fail(Status.UNAUTHENTICATED.withCause(e)); + } + }); + } +} diff --git a/src/main/java/io/qdrant/client/ConditionFactory.java b/src/main/java/io/qdrant/client/ConditionFactory.java new file mode 100644 index 00000000..5af72f55 --- /dev/null +++ b/src/main/java/io/qdrant/client/ConditionFactory.java @@ -0,0 +1,373 @@ +package io.qdrant.client; + +import java.util.List; + +import static io.qdrant.client.grpc.Points.Condition; +import static io.qdrant.client.grpc.Points.FieldCondition; +import static io.qdrant.client.grpc.Points.Filter; +import static io.qdrant.client.grpc.Points.GeoBoundingBox; +import static io.qdrant.client.grpc.Points.GeoLineString; +import static io.qdrant.client.grpc.Points.GeoPoint; +import static io.qdrant.client.grpc.Points.GeoPolygon; +import static io.qdrant.client.grpc.Points.GeoRadius; +import static io.qdrant.client.grpc.Points.HasIdCondition; +import static io.qdrant.client.grpc.Points.IsEmptyCondition; +import static io.qdrant.client.grpc.Points.IsNullCondition; +import static io.qdrant.client.grpc.Points.Match; +import static io.qdrant.client.grpc.Points.NestedCondition; +import static io.qdrant.client.grpc.Points.PointId; +import static io.qdrant.client.grpc.Points.Range; +import static io.qdrant.client.grpc.Points.RepeatedIntegers; +import static io.qdrant.client.grpc.Points.RepeatedStrings; +import static io.qdrant.client.grpc.Points.ValuesCount; + +/** + * Convenience methods for constructing {@link Condition} + */ +public final class ConditionFactory { + private ConditionFactory() { + } + + /** + * Match all records with the provided id + * @param id The id to match + * @return a new instance of {@link Condition} + */ + public static Condition hasId(PointId id) { + return Condition.newBuilder() + .setHasId(HasIdCondition.newBuilder() + .addHasId(id) + .build()) + .build(); + } + + /** + * Match all records with the provided ids + * @param ids The ids to match + * @return a new instance of {@link Condition} + */ + public static Condition hasId(List ids) { + return Condition.newBuilder() + .setHasId(HasIdCondition.newBuilder() + .addAllHasId(ids) + .build()) + .build(); + } + + /** + * Match all records where the given field either does not exist, or has null or empty value. + * @param field The name of the field + * @return a new instance of {@link Condition} + */ + public static Condition isEmpty(String field) { + return Condition.newBuilder() + .setIsEmpty(IsEmptyCondition.newBuilder() + .setKey(field) + .build()) + .build(); + } + + /** + * Match all records where the given field is null. + * @param field The name of the field + * @return a new instance of {@link Condition} + */ + public static Condition isNull(String field) { + return Condition.newBuilder() + .setIsNull(IsNullCondition.newBuilder() + .setKey(field) + .build()) + .build(); + } + + /** + * Match records where the given field matches the given keyword + * @param field The name of the field + * @param keyword The keyword to match + * @return a new instance of {@link Condition} + */ + public static Condition matchKeyword(String field, String keyword) { + return Condition.newBuilder() + .setField(FieldCondition.newBuilder() + .setKey(field) + .setMatch(Match.newBuilder() + .setKeyword(keyword) + .build()) + .build()) + .build(); + } + + /** + * Match records where the given field matches the given text. + * @param field The name of the field + * @param text The text to match + * @return a new instance of {@link Condition} + */ + public static Condition matchText(String field, String text) { + return Condition.newBuilder() + .setField(FieldCondition.newBuilder() + .setKey(field) + .setMatch(Match.newBuilder() + .setText(text) + .build()) + .build()) + .build(); + } + + /** + * Match records where the given field matches the given boolean value. + * @param field The name of the field + * @param value The value to match + * @return a new instance of {@link Condition} + */ + public static Condition match(String field, boolean value) { + return Condition.newBuilder() + .setField(FieldCondition.newBuilder() + .setKey(field) + .setMatch(Match.newBuilder() + .setBoolean(value) + .build()) + .build()) + .build(); + } + + /** + * Match records where the given field matches the given long value. + * @param field The name of the field + * @param value The value to match + * @return a new instance of {@link Condition} + */ + public static Condition match(String field, long value) { + return Condition.newBuilder() + .setField(FieldCondition.newBuilder() + .setKey(field) + .setMatch(Match.newBuilder() + .setInteger(value) + .build()) + .build()) + .build(); + } + + /** + * Match records where the given field matches any of the given keywords. + * @param field The name of the field + * @param keywords The keywords to match + * @return a new instance of {@link Condition} + */ + public static Condition matchKeywords(String field, List keywords) { + return Condition.newBuilder() + .setField(FieldCondition.newBuilder() + .setKey(field) + .setMatch(Match.newBuilder() + .setKeywords(RepeatedStrings.newBuilder().addAllStrings(keywords).build()) + .build()) + .build()) + .build(); + } + + /** + * Match records where the given field matches any of the given values. + * @param field The name of the field + * @param values The values to match + * @return a new instance of {@link Condition} + */ + public static Condition matchValues(String field, List values) { + return Condition.newBuilder() + .setField(FieldCondition.newBuilder() + .setKey(field) + .setMatch(Match.newBuilder() + .setIntegers(RepeatedIntegers.newBuilder().addAllIntegers(values).build()) + .build()) + .build()) + .build(); + } + + /** + * Match records where the given field does not match any of the given keywords. + * @param field The name of the field + * @param keywords The keywords not to match + * @return a new instance of {@link Condition} + */ + public static Condition matchExceptKeywords(String field, List keywords) { + return Condition.newBuilder() + .setField(FieldCondition.newBuilder() + .setKey(field) + .setMatch(Match.newBuilder() + .setExceptKeywords(RepeatedStrings.newBuilder().addAllStrings(keywords).build()) + .build()) + .build()) + .build(); + } + + /** + * Match records where the given field does not match any of the given values. + * @param field The name of the field + * @param values The values not to match + * @return a new instance of {@link Condition} + */ + public static Condition matchExceptValues(String field, List values) { + return Condition.newBuilder() + .setField(FieldCondition.newBuilder() + .setKey(field) + .setMatch(Match.newBuilder() + .setExceptIntegers(RepeatedIntegers.newBuilder().addAllIntegers(values).build()) + .build()) + .build()) + .build(); + } + + /** + * Match records where the given nested field matches the given condition. + * @param field The name of the nested field. + * @param condition The condition to match. + * @return a new instance of {@link Condition} + */ + public static Condition nested(String field, Condition condition) { + return Condition.newBuilder() + .setNested(NestedCondition.newBuilder() + .setKey(field) + .setFilter(Filter.newBuilder() + .addMust(condition) + .build()) + .build()) + .build(); + } + + /** + * Match records where the given nested field matches the given filter. + * @param field The name of the nested field. + * @param filter The filter to match. + * @return a new instance of {@link Condition} + */ + public static Condition nested(String field, Filter filter) { + return Condition.newBuilder() + .setNested(NestedCondition.newBuilder() + .setKey(field) + .setFilter(filter)) + .build(); + } + + /** + * Match records where the given field matches the given range. + * @param field The name of the nested field. + * @param range The range to match. + * @return a new instance of {@link Condition} + */ + public static Condition range(String field, Range range) { + return Condition.newBuilder() + .setField(FieldCondition.newBuilder() + .setKey(field) + .setRange(range) + .build()) + .build(); + } + + /** + * Match records where the given field has values inside a circle centred at a given latitude and longitude + * with a given radius. + * @param field The name of the field. + * @param latitude The latitude of the center. + * @param longitude The longitude of the center. + * @param radius The radius in meters. + * @return a new instance of {@link Condition} + */ + public static Condition geoRadius(String field, double latitude, double longitude, float radius) { + return Condition.newBuilder() + .setField(FieldCondition.newBuilder() + .setKey(field) + .setGeoRadius(GeoRadius.newBuilder() + .setCenter(GeoPoint.newBuilder() + .setLat(latitude) + .setLon(longitude) + .build()) + .setRadius(radius) + .build()) + .build()) + .build(); + } + + /** + * Match records where the given field has values inside a bounding box specified by the top left and + * bottom right coordinates. + * @param field The name of the field. + * @param topLeftLatitude The latitude of the top left point. + * @param topLeftLongitude The longitude of the top left point. + * @param bottomRightLatitude The latitude of the bottom right point. + * @param bottomRightLongitude The longitude of the bottom right point. + * @return a new instance of {@link Condition} + */ + public static Condition geoBoundingBox( + String field, + double topLeftLatitude, + double topLeftLongitude, + double bottomRightLatitude, + double bottomRightLongitude) { + return Condition.newBuilder() + .setField(FieldCondition.newBuilder() + .setKey(field) + .setGeoBoundingBox(GeoBoundingBox.newBuilder() + .setTopLeft(GeoPoint.newBuilder() + .setLat(topLeftLatitude) + .setLon(topLeftLongitude) + .build()) + .setBottomRight(GeoPoint.newBuilder() + .setLat(bottomRightLatitude) + .setLon(bottomRightLongitude) + .build()) + .build()) + .build()) + .build(); + } + + /** + * Matches records where the given field has values inside the provided polygon. A polygon always has an exterior + * ring and may optionally have interior rings, which represent independent areas or holes. + * When defining a ring, you must pick either a clockwise or counterclockwise ordering for your points. + * The first and last point of the polygon must be the same. + * @param field The name of the field. + * @param exterior The exterior ring of the polygon. + * @param interiors The interior rings of the polygon. + * @return a new instance of {@link Condition} + */ + public static Condition geoPolygon(String field, GeoLineString exterior, List interiors) { + GeoPolygon.Builder geoPolygonBuilder = GeoPolygon.newBuilder() + .setExterior(exterior); + + if (!interiors.isEmpty()) { + geoPolygonBuilder.addAllInteriors(interiors); + } + + return Condition.newBuilder() + .setField(FieldCondition.newBuilder() + .setKey(field) + .setGeoPolygon(geoPolygonBuilder.build()) + .build()) + .build(); + } + + /** + * Matches records where the given field has a count of values within the specified count range + * @param field The name of the field. + * @param valuesCount The count range to match. + * @return a new instance of {@link Condition} + */ + public static Condition valuesCount(String field, ValuesCount valuesCount) { + return Condition.newBuilder() + .setField(FieldCondition.newBuilder() + .setKey(field) + .setValuesCount(valuesCount) + .build()) + .build(); + } + + /** + * Nests a filter + * @param filter The filter to nest. + * @return a new instance of {@link Condition} + */ + public static Condition filter(Filter filter) { + return Condition.newBuilder() + .setFilter(filter) + .build(); + } +} diff --git a/src/main/java/io/qdrant/client/PointIdFactory.java b/src/main/java/io/qdrant/client/PointIdFactory.java new file mode 100644 index 00000000..ef5cad05 --- /dev/null +++ b/src/main/java/io/qdrant/client/PointIdFactory.java @@ -0,0 +1,31 @@ +package io.qdrant.client; + +import java.util.UUID; + +import static io.qdrant.client.grpc.Points.PointId; + +/** + * Convenience methods for constructing {@link PointId} + */ +public final class PointIdFactory { + private PointIdFactory() { + } + + /** + * Creates a point id from a {@link long} + * @param id The id + * @return a new instance of {@link PointId} + */ + public static PointId id(long id) { + return PointId.newBuilder().setNum(id).build(); + } + + /** + * Creates a point id from a {@link UUID} + * @param id The id + * @return a new instance of {@link PointId} + */ + public static PointId id(UUID id) { + return PointId.newBuilder().setUuid(id.toString()).build(); + } +} diff --git a/src/main/java/io/qdrant/client/QdrantClient.java b/src/main/java/io/qdrant/client/QdrantClient.java index 7bd73bab..680f29ed 100644 --- a/src/main/java/io/qdrant/client/QdrantClient.java +++ b/src/main/java/io/qdrant/client/QdrantClient.java @@ -1,1391 +1,2421 @@ package io.qdrant.client; -import io.grpc.Deadline; -import io.grpc.ManagedChannel; -import io.grpc.ManagedChannelBuilder; -import io.qdrant.client.grpc.Collections; +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; +import com.google.common.util.concurrent.FutureCallback; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.MoreExecutors; import io.qdrant.client.grpc.CollectionsGrpc; -import io.qdrant.client.grpc.JsonWithInt.Value; -import io.qdrant.client.grpc.Points; import io.qdrant.client.grpc.PointsGrpc; -import io.qdrant.client.grpc.QdrantGrpc; -import io.qdrant.client.grpc.QdrantOuterClass; import io.qdrant.client.grpc.SnapshotsGrpc; -import io.qdrant.client.grpc.SnapshotsService; -import io.qdrant.client.grpc.SnapshotsService.SnapshotDescription; -import io.qdrant.client.utils.PointUtil; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.URL; -import java.nio.file.Path; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; import java.time.Duration; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; -import javax.annotation.Nullable; - -/** Client for interfacing with the Qdrant service. */ +import java.util.stream.Collectors; + +import static io.qdrant.client.grpc.Collections.AliasDescription; +import static io.qdrant.client.grpc.Collections.AliasOperations; +import static io.qdrant.client.grpc.Collections.ChangeAliases; +import static io.qdrant.client.grpc.Collections.CollectionDescription; +import static io.qdrant.client.grpc.Collections.CollectionInfo; +import static io.qdrant.client.grpc.Collections.CollectionOperationResponse; +import static io.qdrant.client.grpc.Collections.CreateAlias; +import static io.qdrant.client.grpc.Collections.CreateCollection; +import static io.qdrant.client.grpc.Collections.DeleteAlias; +import static io.qdrant.client.grpc.Collections.DeleteCollection; +import static io.qdrant.client.grpc.Collections.GetCollectionInfoRequest; +import static io.qdrant.client.grpc.Collections.GetCollectionInfoResponse; +import static io.qdrant.client.grpc.Collections.ListAliasesRequest; +import static io.qdrant.client.grpc.Collections.ListAliasesResponse; +import static io.qdrant.client.grpc.Collections.ListCollectionAliasesRequest; +import static io.qdrant.client.grpc.Collections.ListCollectionsRequest; +import static io.qdrant.client.grpc.Collections.ListCollectionsResponse; +import static io.qdrant.client.grpc.Collections.PayloadIndexParams; +import static io.qdrant.client.grpc.Collections.PayloadSchemaType; +import static io.qdrant.client.grpc.Collections.RenameAlias; +import static io.qdrant.client.grpc.Collections.UpdateCollection; +import static io.qdrant.client.grpc.Collections.VectorParams; +import static io.qdrant.client.grpc.Collections.VectorParamsMap; +import static io.qdrant.client.grpc.Collections.VectorsConfig; +import static io.qdrant.client.grpc.JsonWithInt.Value; +import static io.qdrant.client.grpc.Points.BatchResult; +import static io.qdrant.client.grpc.Points.ClearPayloadPoints; +import static io.qdrant.client.grpc.Points.CountPoints; +import static io.qdrant.client.grpc.Points.CountResponse; +import static io.qdrant.client.grpc.Points.CreateFieldIndexCollection; +import static io.qdrant.client.grpc.Points.DeleteFieldIndexCollection; +import static io.qdrant.client.grpc.Points.DeletePayloadPoints; +import static io.qdrant.client.grpc.Points.DeletePointVectors; +import static io.qdrant.client.grpc.Points.DeletePoints; +import static io.qdrant.client.grpc.Points.FieldType; +import static io.qdrant.client.grpc.Points.Filter; +import static io.qdrant.client.grpc.Points.GetPoints; +import static io.qdrant.client.grpc.Points.GetResponse; +import static io.qdrant.client.grpc.Points.PointGroup; +import static io.qdrant.client.grpc.Points.PointId; +import static io.qdrant.client.grpc.Points.PointStruct; +import static io.qdrant.client.grpc.Points.PointVectors; +import static io.qdrant.client.grpc.Points.PointsIdsList; +import static io.qdrant.client.grpc.Points.PointsOperationResponse; +import static io.qdrant.client.grpc.Points.PointsSelector; +import static io.qdrant.client.grpc.Points.ReadConsistency; +import static io.qdrant.client.grpc.Points.RecommendBatchPoints; +import static io.qdrant.client.grpc.Points.RecommendBatchResponse; +import static io.qdrant.client.grpc.Points.RecommendGroupsResponse; +import static io.qdrant.client.grpc.Points.RecommendPointGroups; +import static io.qdrant.client.grpc.Points.RecommendPoints; +import static io.qdrant.client.grpc.Points.RecommendResponse; +import static io.qdrant.client.grpc.Points.RetrievedPoint; +import static io.qdrant.client.grpc.Points.ScoredPoint; +import static io.qdrant.client.grpc.Points.ScrollPoints; +import static io.qdrant.client.grpc.Points.ScrollResponse; +import static io.qdrant.client.grpc.Points.SearchBatchPoints; +import static io.qdrant.client.grpc.Points.SearchBatchResponse; +import static io.qdrant.client.grpc.Points.SearchGroupsResponse; +import static io.qdrant.client.grpc.Points.SearchPointGroups; +import static io.qdrant.client.grpc.Points.SearchPoints; +import static io.qdrant.client.grpc.Points.SearchResponse; +import static io.qdrant.client.grpc.Points.SetPayloadPoints; +import static io.qdrant.client.grpc.Points.UpdatePointVectors; +import static io.qdrant.client.grpc.Points.UpdateResult; +import static io.qdrant.client.grpc.Points.UpsertPoints; +import static io.qdrant.client.grpc.Points.VectorsSelector; +import static io.qdrant.client.grpc.Points.WithPayloadSelector; +import static io.qdrant.client.grpc.Points.WithVectorsSelector; +import static io.qdrant.client.grpc.Points.WriteOrdering; +import static io.qdrant.client.grpc.Points.WriteOrderingType; +import static io.qdrant.client.grpc.QdrantGrpc.QdrantFutureStub; +import static io.qdrant.client.grpc.QdrantOuterClass.HealthCheckReply; +import static io.qdrant.client.grpc.QdrantOuterClass.HealthCheckRequest; +import static io.qdrant.client.grpc.SnapshotsService.CreateFullSnapshotRequest; +import static io.qdrant.client.grpc.SnapshotsService.CreateSnapshotRequest; +import static io.qdrant.client.grpc.SnapshotsService.CreateSnapshotResponse; +import static io.qdrant.client.grpc.SnapshotsService.DeleteFullSnapshotRequest; +import static io.qdrant.client.grpc.SnapshotsService.DeleteSnapshotRequest; +import static io.qdrant.client.grpc.SnapshotsService.DeleteSnapshotResponse; +import static io.qdrant.client.grpc.SnapshotsService.ListFullSnapshotsRequest; +import static io.qdrant.client.grpc.SnapshotsService.ListSnapshotsRequest; +import static io.qdrant.client.grpc.SnapshotsService.ListSnapshotsResponse; +import static io.qdrant.client.grpc.SnapshotsService.SnapshotDescription; + +/** + * Client for the Qdrant vector database. + */ public class QdrantClient implements AutoCloseable { - private QdrantGrpc.QdrantBlockingStub qdrantStub; - private CollectionsGrpc.CollectionsBlockingStub collectionsStub; - private PointsGrpc.PointsBlockingStub pointsStub; - private SnapshotsGrpc.SnapshotsBlockingStub snapshotsStub; - private ManagedChannel channel; - - /** - * Constructs a new QdrantClient with the specified URL and API key
- * Uses TLS if the URL is https, otherwise uses plaintext. - * - * @param url The URL of the Qdrant service. - * @param apiKey The API key for authentication. - * @throws MalformedURLException If the URL is malformed. - * @throws IllegalArgumentException If the protocol is invalid. - */ - public QdrantClient(String url, String apiKey) - throws MalformedURLException, IllegalArgumentException { - TokenInterceptor interceptor = new TokenInterceptor(apiKey); - ManagedChannel channel = createManagedChannel(url, interceptor); - initializeStubs(channel, null); - } - - /** - * Constructs a new QdrantClient with the specified URL and API key
- * Uses TLS if the URL is https, otherwise uses plaintext. - * - * @param url The URL of the Qdrant service. - * @param apiKey The API key for authentication. - * @param timeout The timeout for the gRPC requests. - * @throws MalformedURLException If the URL is malformed. - * @throws IllegalArgumentException If the protocol is invalid. - */ - public QdrantClient(String url, String apiKey, Duration timeout) - throws MalformedURLException, IllegalArgumentException { - TokenInterceptor interceptor = new TokenInterceptor(apiKey); - ManagedChannel channel = createManagedChannel(url, interceptor); - initializeStubs(channel, timeout); - } - - /** - * Constructs a new QdrantClient with the specified URL
- * Uses TLS if the URL is https, otherwise uses plaintext. - * - * @param url the URL of the Qdrant service - * @throws MalformedURLException If the URL is malformed - * @throws IllegalArgumentException If the protocol is invalid. - */ - public QdrantClient(String url) throws MalformedURLException, IllegalArgumentException { - ManagedChannel channel = createManagedChannel(url, null); - initializeStubs(channel, null); - } - - /** - * Constructs a new QdrantClient with the specified URL
- * Uses TLS if the URL is https, otherwise uses plaintext. - * - * @param url the URL of the Qdrant service - * @param timeout The timeout for the gRPC requests. - * @throws MalformedURLException If the URL is malformed - * @throws IllegalArgumentException If the protocol is invalid. - */ - public QdrantClient(String url, Duration timeout) - throws MalformedURLException, IllegalArgumentException { - ManagedChannel channel = createManagedChannel(url, null); - initializeStubs(channel, timeout); - } - - /** - * Creates a managed channel based on the provided URL and interceptor. - * - * @param url The URL of the gRPC server. - * @param interceptor The token interceptor to be added to the channel. - * @return The created managed channel. - * @throws MalformedURLException If the provided URL is malformed. - * @throws IllegalArgumentException If the provided protocol is invalid. - */ - private ManagedChannel createManagedChannel(String url, @Nullable TokenInterceptor interceptor) - throws MalformedURLException, IllegalArgumentException { - URL parsedUrl = new URL(url); - - ManagedChannelBuilder channelBuilder = - ManagedChannelBuilder.forAddress(parsedUrl.getHost(), parsedUrl.getPort()); - - switch (parsedUrl.getProtocol().toUpperCase()) { - case "HTTPS": - // TLS is enabled by default - // Specifying explicitly for clarity - channelBuilder.useTransportSecurity(); - break; - case "HTTP": - channelBuilder.usePlaintext(); - break; - default: - throw new IllegalArgumentException( - "Invalid protocol. Only 'http' and 'https' are supported for gRPC URLs."); - } - - if (interceptor != null) { - // Add token interceptor if apiKey is provided - channelBuilder.intercept(interceptor); - } - - return channelBuilder.build(); - } - - /** - * Initializes the gRPC stubs for Qdrant client. - * - * @param channel The managed channel used for communication. - */ - private void initializeStubs(ManagedChannel channel, @Nullable Duration timeout) { - this.qdrantStub = - QdrantGrpc.newBlockingStub(channel) - .withDeadline( - timeout == null ? null : Deadline.after(timeout.toMillis(), TimeUnit.MILLISECONDS)); - this.collectionsStub = - CollectionsGrpc.newBlockingStub(channel) - .withDeadline( - timeout == null ? null : Deadline.after(timeout.toMillis(), TimeUnit.MILLISECONDS)); - this.pointsStub = - PointsGrpc.newBlockingStub(channel) - .withDeadline( - timeout == null ? null : Deadline.after(timeout.toMillis(), TimeUnit.MILLISECONDS)); - this.snapshotsStub = - SnapshotsGrpc.newBlockingStub(channel) - .withDeadline( - timeout == null ? null : Deadline.after(timeout.toMillis(), TimeUnit.MILLISECONDS)); - this.channel = channel; - } - - /** - * Retrieves a list of collections. - * - * @return The response containing the list of collections. - */ - public Collections.ListCollectionsResponse listCollections() { - Collections.ListCollectionsRequest request = - Collections.ListCollectionsRequest.newBuilder().build(); - return collectionsStub.list(request); - } - - /** - * Performs a health check on the Qdrant service. - * - * @return The health check reply from the Qdrant service. - */ - public QdrantOuterClass.HealthCheckReply healthCheck() { - QdrantOuterClass.HealthCheckRequest request = - QdrantOuterClass.HealthCheckRequest.newBuilder().build(); - return qdrantStub.healthCheck(request); - } - - /** - * Checks if a collection with the given name exists. - * - * @param collectionName The name of the collection to check. - * @return True if the collection exists, false otherwise. - */ - public boolean hasCollection(String collectionName) { - return listCollections().getCollectionsList().stream() - .anyMatch(c -> c.getName().equals(collectionName)); - } - - /** - * Creates a new collection with the specified name, vector size, and distance metric. - * - * @param collectionName The name of the collection to be created. - * @param vectorSize The size of the vectors in the collection. - * @param distance The distance metric to be used for vector comparison. - * @return The response containing the operation status. - */ - public Collections.CollectionOperationResponse createCollection( - String collectionName, long vectorSize, Collections.Distance distance) { - Collections.VectorParams.Builder params = - Collections.VectorParams.newBuilder().setDistance(distance).setSize(vectorSize); - Collections.VectorsConfig config = - Collections.VectorsConfig.newBuilder().setParams(params).build(); - Collections.CreateCollection details = - Collections.CreateCollection.newBuilder() - .setVectorsConfig(config) - .setCollectionName(collectionName) - .build(); - return createCollection(details); - } - - /** - * Creates a new collection with the specified name, vector size, and distance metric. - * - * @param collectionName The name of the collection to be created. - * @param vectorsConfig The vectors configuration of the collection. - * @return The response containing the operation status. - */ - public Collections.CollectionOperationResponse createCollection( - String collectionName, Collections.VectorsConfig vectorsConfig) { - - Collections.CreateCollection details = - Collections.CreateCollection.newBuilder() - .setVectorsConfig(vectorsConfig) - .setCollectionName(collectionName) - .build(); - return createCollection(details); - } - - /** - * Creates a new collection with the specified details. - * - * @param details The details of the collection to be created. - * @return The response containing the operation status. - */ - public Collections.CollectionOperationResponse createCollection( - Collections.CreateCollection details) { - return collectionsStub.create(details); - } - - /** - * Deletes and creates a new collection with the specified name, vector size, and distance metric. - * - * @param collectionName The name of the collection to be created. - * @param vectorSize The size of the vectors in the collection. - * @param distance The distance metric to be used for vector comparison. - * @return The response containing the operation status. - */ - public Collections.CollectionOperationResponse recreateCollection( - String collectionName, long vectorSize, Collections.Distance distance) { - - Collections.VectorParams.Builder params = - Collections.VectorParams.newBuilder().setDistance(distance).setSize(vectorSize); - Collections.VectorsConfig config = - Collections.VectorsConfig.newBuilder().setParams(params).build(); - Collections.CreateCollection details = - Collections.CreateCollection.newBuilder() - .setVectorsConfig(config) - .setCollectionName(collectionName) - .build(); - return recreateCollection(details); - } - - /** - * Deletes and creates a new collection with the specified details. - * - * @param details The details of the collection to be created. - * @return The response containing the operation status. - */ - public Collections.CollectionOperationResponse recreateCollection( - Collections.CreateCollection details) { - deleteCollection(details.getCollectionName()); - return collectionsStub.create(details); - } - - /** - * Updates a collection with the specified details. - * - * @param details The details of the update operation. - * @return The response containing the operation status. - */ - public Collections.CollectionOperationResponse updateCollection( - Collections.UpdateCollection details) { - return collectionsStub.update(details); - } - - /** - * Deletes a collection with the specified name. - * - * @param collectionName the name of the collection to be deleted - * @return the response of the collection deletion operation - */ - public Collections.CollectionOperationResponse deleteCollection(String collectionName) { - Collections.DeleteCollection request = - Collections.DeleteCollection.newBuilder().setCollectionName(collectionName).build(); - return collectionsStub.delete(request); - } - - /** - * Retrieves information about a collection. - * - * @param collectionName The name of the collection. - * @return The response containing the collection information. - */ - public Collections.GetCollectionInfoResponse getCollectionInfo(String collectionName) { - Collections.GetCollectionInfoRequest request = - Collections.GetCollectionInfoRequest.newBuilder().setCollectionName(collectionName).build(); - return collectionsStub.get(request); - } - - /** - * Creates an alias for a collection. - * - * @param collectionName The name of the collection. - * @param aliasName The name of the alias. - * @return The response of the collection operation. - */ - public Collections.CollectionOperationResponse createAlias( - String collectionName, String aliasName) { - Collections.CreateAlias createAlias = - Collections.CreateAlias.newBuilder() - .setCollectionName(collectionName) - .setAliasName(aliasName) - .build(); - Collections.AliasOperations operations = - Collections.AliasOperations.newBuilder().setCreateAlias(createAlias).build(); - Collections.ChangeAliases changeAliases = - Collections.ChangeAliases.newBuilder().addActions(operations).build(); - - return this.updateAliases(changeAliases); - } - - /** - * Deletes an alias with the specified name. - * - * @param aliasName the name of the alias to be deleted - * @return the response of the collection operation - */ - public Collections.CollectionOperationResponse deleteAlias(String aliasName) { - Collections.DeleteAlias deleteAlias = - Collections.DeleteAlias.newBuilder().setAliasName(aliasName).build(); - Collections.AliasOperations operations = - Collections.AliasOperations.newBuilder().setDeleteAlias(deleteAlias).build(); - Collections.ChangeAliases changeAliases = - Collections.ChangeAliases.newBuilder().addActions(operations).build(); - - return this.updateAliases(changeAliases); - } - - /** - * Renames an alias in the Qdrant collection. - * - * @param oldAliasName The current name of the alias. - * @param newAliasName The new name for the alias. - * @return The response containing the result of the alias rename operation. - */ - public Collections.CollectionOperationResponse renameAlias( - String oldAliasName, String newAliasName) { - Collections.RenameAlias renameAlias = - Collections.RenameAlias.newBuilder() - .setOldAliasName(oldAliasName) - .setNewAliasName(newAliasName) - .build(); - Collections.AliasOperations operations = - Collections.AliasOperations.newBuilder().setRenameAlias(renameAlias).build(); - Collections.ChangeAliases changeAliases = - Collections.ChangeAliases.newBuilder().addActions(operations).build(); - return this.updateAliases(changeAliases); - } - - /** - * Updates the aliases for collections. - * - * @param details The details of the aliases to be changed. - * @return The response of the collection operation. - */ - public Collections.CollectionOperationResponse updateAliases(Collections.ChangeAliases details) { - return collectionsStub.updateAliases(details); - } - - /** - * Retrieves the list of aliases for a given collection. - * - * @param collectionName The name of the collection. - * @return The response containing the list of aliases. - */ - public Collections.ListAliasesResponse listCollectionAliases(String collectionName) { - Collections.ListCollectionAliasesRequest request = - Collections.ListCollectionAliasesRequest.newBuilder() - .setCollectionName(collectionName) - .build(); - return collectionsStub.listCollectionAliases(request); - } - - /** - * Retrieves a list of aliases. - * - * @return The response containing the list of aliases. - */ - public Collections.ListAliasesResponse listAliases() { - Collections.ListAliasesRequest request = Collections.ListAliasesRequest.newBuilder().build(); - return collectionsStub.listAliases(request); - } - - /** - * Retrieves the cluster information for a specific collection. - * - * @param collectionName The name of the collection. - * @return The cluster information for the collection. - */ - public Collections.CollectionClusterInfoResponse getCollectionClusterInfo(String collectionName) { - Collections.CollectionClusterInfoRequest request = - Collections.CollectionClusterInfoRequest.newBuilder() - .setCollectionName(collectionName) - .build(); - return collectionsStub.collectionClusterInfo(request); - } - - /** - * Updates the cluster setup for a collection. - * - * @param collectionName The name of the collection. - * @param request The request object containing the updated cluster setup. - * @return The response object indicating the success or failure of the update operation. - */ - public Collections.UpdateCollectionClusterSetupResponse updateCollectionClusterSetup( - String collectionName, Collections.UpdateCollectionClusterSetupRequest request) { - return collectionsStub.updateCollectionClusterSetup(request); - } - - /** Internal batch update method */ - private Points.UpdateBatchResponse batchUpdate( - String collectionName, - List operations, - @Nullable Points.WriteOrderingType ordering, - Boolean wait) { - Points.UpdateBatchPoints.Builder request = - Points.UpdateBatchPoints.newBuilder() - .setCollectionName(collectionName) - .setWait(wait) - .addAllOperations(operations); - - if (ordering != null) { - request.setOrdering(PointUtil.ordering(ordering)); - } - return pointsStub.updateBatch(request.build()); - } - - /** - * Performs a batch update operation on a collection. Does not wait for the operation to complete - * before returning. - * - * @param collectionName The name of the collection. - * @param operations The list of update operations to be performed. - * @return The response of the batch update operation. - */ - public Points.UpdateBatchResponse batchUpdate( - String collectionName, - List operations, - @Nullable Points.WriteOrderingType ordering) { - return batchUpdate(collectionName, operations, ordering, false); - } - - /** - * Performs a batch update operation on a collection. Waits for the operation to complete before - * returning. - * - * @param collectionName The name of the collection. - * @param operations The list of update operations to be performed. - * @return The response of the batch update operation. - */ - public Points.UpdateBatchResponse batchUpdateBlocking( - String collectionName, - List operations, - @Nullable Points.WriteOrderingType ordering) { - return batchUpdate(collectionName, operations, ordering, true); - } - - /** Internal upsert method */ - private Points.PointsOperationResponse upsertPoints( - String collectionName, - List points, - @Nullable Points.WriteOrderingType ordering, - Boolean wait) { - Points.UpsertPoints.Builder request = - Points.UpsertPoints.newBuilder() - .setCollectionName(collectionName) - .addAllPoints(points) - .setWait(wait); - - if (ordering != null) { - request.setOrdering(PointUtil.ordering(ordering)); - } - return pointsStub.upsert(request.build()); - } - - /** - * Upserts the given points into the specified collection. Does not wait for the operation to - * complete before returning. - * - * @param collectionName The name of the collection. - * @param points The list of points to be upserted. - * @param ordering The write ordering for the upsert operation. - * @return The response of the upsert operation. - */ - public Points.PointsOperationResponse upsertPoints( - String collectionName, - List points, - @Nullable Points.WriteOrderingType ordering) { - return upsertPoints(collectionName, points, ordering, false); - } - - /** - * Upserts the given points into the specified collection. Waits for the operation to complete - * before returning. - * - * @param collectionName The name of the collection. - * @param points The list of points to be upserted. - * @param ordering The write ordering for the upsert operation. - * @return The response of the upsert operation. - */ - public Points.PointsOperationResponse upsertPointsBlocking( - String collectionName, - List points, - @Nullable Points.WriteOrderingType ordering) { - return upsertPoints(collectionName, points, ordering, true); - } - - /** Internal batch upsert method */ - private Points.PointsOperationResponse upsertPointsBatch( - String collectionName, - List points, - @Nullable Points.WriteOrderingType ordering, - Boolean wait, - int chunkSize) { - int listSize = points.size(); - double timeTaken = 0; - Points.UpdateResult result = null; - - for (int i = 0; i < listSize; i += chunkSize) { - int end = Math.min(i + chunkSize, listSize); - List chunk = points.subList(i, end); - Points.PointsOperationResponse response = upsertPoints(collectionName, chunk, ordering, wait); - timeTaken += response.getTime(); - result = response.getResult(); - } - return Points.PointsOperationResponse.newBuilder().setTime(timeTaken).setResult(result).build(); - } - - /** - * Upserts a batch of points in the specified collection. Does not wait for the operation to - * complete before returning. - * - * @param collectionName The name of the collection. - * @param points The list of points to upsert. - * @return The response of the points operation. - */ - public Points.PointsOperationResponse upsertPointsBatch( - String collectionName, - List points, - @Nullable Points.WriteOrderingType ordering, - int chunkSize) { - return upsertPointsBatch(collectionName, points, ordering, false, chunkSize); - } - - /** - * Upserts a batch of points in the specified collection. Waits for the operation to complete - * before returning. - * - * @param collectionName The name of the collection. - * @param points The list of points to upsert. - * @return The response of the points operation. - */ - public Points.PointsOperationResponse upsertPointsBatchBlocking( - String collectionName, - List points, - @Nullable Points.WriteOrderingType ordering, - int chunkSize) { - return upsertPointsBatch(collectionName, points, ordering, true, chunkSize); - } - - /** Internal update method */ - private Points.PointsOperationResponse setPayload( - String collectionName, - Points.PointsSelector points, - Map payload, - @Nullable Points.WriteOrderingType ordering, - Boolean wait) { - Points.SetPayloadPoints.Builder request = - Points.SetPayloadPoints.newBuilder() - .setCollectionName(collectionName) - .setPointsSelector(points) - .putAllPayload(payload) - .setWait(wait); - - if (ordering != null) { - request.setOrdering(PointUtil.ordering(ordering)); - } - return pointsStub.setPayload(request.build()); - } - - /** - * Sets the payload of the specified points in a collection. Does not wait for the operation to - * complete before returning. - * - * @param collectionName The name of the collection. - * @param points The selector for the points to be updated. - * @param payload The new payload to be assigned to the points. - * @param ordering The ordering of the write operation. - * @return The response of the points operation. - */ - public Points.PointsOperationResponse setPayload( - String collectionName, - Points.PointsSelector points, - Map payload, - @Nullable Points.WriteOrderingType ordering) { - return setPayload(collectionName, points, payload, ordering, false); - } - - /** - * Sets the payload of the specified points in a collection. Waits for the operation to complete - * before returning. - * - * @param collectionName The name of the collection. - * @param points The selector for the points to be updated. - * @param payload The new payload to be assigned to the points. - * @param ordering The ordering of the write operation. - * @return The response of the points operation. - */ - public Points.PointsOperationResponse setPayloadBlocking( - String collectionName, - Points.PointsSelector points, - Map payload, - @Nullable Points.WriteOrderingType ordering) { - return setPayload(collectionName, points, payload, ordering, true); - } - - /** Internal payload overwrite method */ - private Points.PointsOperationResponse overwritePayload( - String collectionName, - Points.PointsSelector points, - Map payload, - @Nullable Points.WriteOrderingType ordering, - Boolean wait) { - Points.SetPayloadPoints.Builder request = - Points.SetPayloadPoints.newBuilder() - .setCollectionName(collectionName) - .setPointsSelector(points) - .putAllPayload(payload) - .setWait(wait); - - if (ordering != null) { - request.setOrdering(PointUtil.ordering(ordering)); - } - return pointsStub.overwritePayload(request.build()); - } - - /** - * Overwrites the payload of the specified points in a collection. Does not wait for the operation - * to complete before returning. - * - * @param collectionName The name of the collection. - * @param points The selector for the points to be overwritten. - * @param payload The new payload to be assigned to the points. - * @param ordering The ordering of the write operation. - * @return The response of the points operation. - */ - public Points.PointsOperationResponse overwritePayload( - String collectionName, - Points.PointsSelector points, - Map payload, - @Nullable Points.WriteOrderingType ordering) { - return overwritePayload(collectionName, points, payload, ordering, false); - } - - /** - * Overwrites the payload of the specified points in a collection. Waits for the operation to - * complete before returning. - * - * @param collectionName The name of the collection. - * @param points The selector for the points to be overwritten. - * @param payload The new payload to be assigned to the points. - * @param ordering The ordering of the write operation. - * @return The response of the points operation. - */ - public Points.PointsOperationResponse overwritePayloadBlocking( - String collectionName, - Points.PointsSelector points, - Map payload, - @Nullable Points.WriteOrderingType ordering) { - return overwritePayload(collectionName, points, payload, ordering, true); - } - - /** Internal payload delete method */ - private Points.PointsOperationResponse deletePayload( - String collectionName, - Points.PointsSelector points, - List keys, - @Nullable Points.WriteOrderingType ordering, - Boolean wait) { - Points.DeletePayloadPoints.Builder request = - Points.DeletePayloadPoints.newBuilder() - .setCollectionName(collectionName) - .addAllKeys(keys) - .setWait(wait) - .setPointsSelector(points); - - if (ordering != null) { - request.setOrdering(PointUtil.ordering(ordering)); - } - return pointsStub.deletePayload(request.build()); - } - - /** - * Deletes the payload associated with the specified collection, points, keys, and ordering. Does - * not wait for the operation to complete before returning. - * - * @param collectionName The name of the collection. - * @param points The points selector. - * @param keys The list of keys. - * @param ordering The write ordering. - * @return The response of the points operation. - */ - public Points.PointsOperationResponse deletePayload( - String collectionName, - Points.PointsSelector points, - List keys, - @Nullable Points.WriteOrderingType ordering) { - return deletePayload(collectionName, points, keys, ordering, false); - } - - /** - * Deletes the payload associated with the specified collection, points, keys, and ordering. Waits - * for the operation to complete before returning. - * - * @param collectionName The name of the collection. - * @param points The points selector. - * @param keys The list of keys. - * @param ordering The write ordering. - * @return The response of the points operation. - */ - public Points.PointsOperationResponse deletePayloadBlocking( - String collectionName, - Points.PointsSelector points, - List keys, - @Nullable Points.WriteOrderingType ordering) { - return deletePayload(collectionName, points, keys, ordering, true); - } - - /** Internal payload clear method */ - private Points.PointsOperationResponse clearPayload( - String collectionName, - Points.PointsSelector points, - @Nullable Points.WriteOrderingType ordering, - Boolean wait) { - Points.ClearPayloadPoints.Builder request = - Points.ClearPayloadPoints.newBuilder() - .setCollectionName(collectionName) - .setPoints(points) - .setWait(wait); - - if (ordering != null) { - request.setOrdering(PointUtil.ordering(ordering)); - } - - return pointsStub.clearPayload(request.build()); - } - - /** - * Clears the payload associated with the specified collection, points and ordering. Does not wait - * for the operation to complete before returning. - * - * @param collectionName The name of the collection. - * @param points The points to be cleared. - * @return The response of the clearPayload operation. - */ - public Points.PointsOperationResponse clearPayload( - String collectionName, - Points.PointsSelector points, - @Nullable Points.WriteOrderingType ordering) { - return clearPayload(collectionName, points, ordering, false); - } - - /** - * Clears the payload associated with the specified collection, points and ordering. Waits for the - * operation to complete before returning. - * - * @param collectionName The name of the collection. - * @param points The points to be cleared. - * @return The response of the clearPayload operation. - */ - public Points.PointsOperationResponse clearPayloadBlocking( - String collectionName, - Points.PointsSelector points, - @Nullable Points.WriteOrderingType ordering) { - return clearPayload(collectionName, points, ordering, true); - } - - /** - * Retrieves points from a collection. - * - * @param collectionName The name of the collection. - * @param points The IDs of the points to retrieve. - * @param withVectors The selector for including vectors in the response. - * @param withPayload The selector for including payload in the response. - * @param readConsistency The read consistency level for the operation. - * @return The response containing the retrieved points. - */ - public Points.GetResponse getPoints( - String collectionName, - Iterable points, - Points.WithVectorsSelector withVectors, - Points.WithPayloadSelector withPayload, - @Nullable Points.ReadConsistencyType readConsistency) { - Points.GetPoints.Builder request = - Points.GetPoints.newBuilder() - .setCollectionName(collectionName) - .addAllIds(points) - .setWithVectors(withVectors) - .setWithPayload(withPayload); - - if (readConsistency != null) { - request.setReadConsistency(PointUtil.consistency(readConsistency)); - } - - return pointsStub.get(request.build()); - } - - /** - * Performs a search operation on the points. - * - * @param request The search request containing the query parameters. - * @return The response containing the search results. - */ - public Points.SearchResponse searchPoints(Points.SearchPoints request) { - return pointsStub.search(request); - } - - /** - * Performs a batch search for points. - * - * @param request The search request containing the batch points to search for. - * @return The response containing the search results. - */ - public Points.SearchBatchResponse searchBatchPoints(Points.SearchBatchPoints request) { - return pointsStub.searchBatch(request); - } - - /** - * Searches for point groups based on the given request. - * - * @param request The search request containing the criteria for searching point groups. - * @return The response containing the search results for point groups. - */ - public Points.SearchGroupsResponse searchGroups(Points.SearchPointGroups request) { - return pointsStub.searchGroups(request); - } - - /** Internal delete method */ - private Points.PointsOperationResponse deletePoints( - String collectionName, - Points.PointsSelector points, - @Nullable Points.WriteOrderingType ordering, - Boolean wait) { - Points.DeletePoints.Builder request = - Points.DeletePoints.newBuilder() - .setCollectionName(collectionName) - .setPoints(points) - .setWait(wait); - - if (ordering != null) { - request.setOrdering(PointUtil.ordering(ordering)); - } - return pointsStub.delete(request.build()); - } - - /** - * Deletes points from a collection. Does not wait for the operation to complete before returning. - * - * @param collectionName The name of the collection from which points will be deleted. - * @param points The selector for the points to be deleted. - * @param ordering The ordering of the write operation. - * @return The response of the points deletion operation. - */ - public Points.PointsOperationResponse deletePoints( - String collectionName, - Points.PointsSelector points, - @Nullable Points.WriteOrderingType ordering) { - return deletePoints(collectionName, points, ordering, false); - } - - /** - * Deletes points from a collection. Waits for the operation to complete before returning. - * - * @param collectionName The name of the collection from which points will be deleted. - * @param points The selector for the points to be deleted. - * @param ordering The ordering of the write operation. - * @return The response of the points deletion operation. - */ - public Points.PointsOperationResponse deletePointsBlocking( - String collectionName, - Points.PointsSelector points, - @Nullable Points.WriteOrderingType ordering) { - return deletePoints(collectionName, points, ordering, true); - } - - /** Internal delete vectors method */ - private Points.PointsOperationResponse deleteVectors( - String collectionName, - Points.PointsSelector points, - Points.VectorsSelector vectors, - @Nullable Points.WriteOrderingType ordering, - Boolean wait) { - Points.DeletePointVectors.Builder requests = - Points.DeletePointVectors.newBuilder() - .setCollectionName(collectionName) - .setPointsSelector(points) - .setVectors(vectors) - .setWait(wait); - - if (ordering != null) { - requests.setOrdering(PointUtil.ordering(ordering)); - } - return pointsStub.deleteVectors(requests.build()); - } - - /** - * Deletes vectors from a collection. Does not wait for the operation to complete before - * returning. - * - * @param collectionName The name of the collection. - * @param points The selector for points to delete. - * @param vectors The selector for vectors to delete. - * @param ordering The write ordering for the operation. - * @return The response of the delete operation. - */ - public Points.PointsOperationResponse deleteVectors( - String collectionName, - Points.PointsSelector points, - Points.VectorsSelector vectors, - @Nullable Points.WriteOrderingType ordering) { - return deleteVectors(collectionName, points, vectors, ordering, false); - } - - /** - * Deletes vectors from a collection. Waits for the operation to complete before returning. - * - * @param collectionName The name of the collection. - * @param points The selector for points to delete. - * @param vectors The selector for vectors to delete. - * @param ordering The write ordering for the operation. - * @return The response of the delete operation. - */ - public Points.PointsOperationResponse deleteVectorsBlocking( - String collectionName, - Points.PointsSelector points, - Points.VectorsSelector vectors, - @Nullable Points.WriteOrderingType ordering) { - return deleteVectors(collectionName, points, vectors, ordering, true); - } - - /** Internal update vectors method */ - private Points.PointsOperationResponse updateVectors( - String collectionName, - Iterable points, - @Nullable Points.WriteOrderingType ordering, - Boolean wait) { - Points.UpdatePointVectors.Builder request = - Points.UpdatePointVectors.newBuilder() - .setCollectionName(collectionName) - .addAllPoints(points) - .setWait(wait); - - if (ordering != null) { - request.setOrdering(PointUtil.ordering(ordering)); - } - return pointsStub.updateVectors(request.build()); - } - - /** - * Updates the vectors of points in a collection. Does not wait for the operation to complete - * before returning. - * - * @param collectionName The name of the collection. - * @param points An iterable of point vectors to update. - * @param ordering The write ordering for the update operation. - * @return The response of the points operation. - */ - public Points.PointsOperationResponse updateVectors( - String collectionName, - Iterable points, - @Nullable Points.WriteOrderingType ordering) { - return updateVectors(collectionName, points, ordering, false); - } - - /** - * Updates the vectors of points in a collection. Waits for the operation to complete before - * returning. - * - * @param collectionName The name of the collection. - * @param points An iterable of point vectors to update. - * @param ordering The write ordering for the update operation. - * @return The response of the points operation. - */ - public Points.PointsOperationResponse updateVectorsBlocking( - String collectionName, - Iterable points, - @Nullable Points.WriteOrderingType ordering) { - return updateVectors(collectionName, points, ordering, true); - } - - /** - * Retrieve points from a collection based on filters. - * - * @param request The search request containing the query parameters. - * @return The response containing the scroll results. - */ - public Points.ScrollResponse scroll(Points.ScrollPoints request) { - return pointsStub.scroll(request); - } - - /** - * Recommends points based on the given positive/negative points recommendation request. - * - * @param request The points recommendation request. - * @return The recommendation response. - */ - public Points.RecommendResponse recommend(Points.RecommendPoints request) { - return pointsStub.recommend(request); - } - - /** - * Recommends points batch based on the given positive/negative points recommendation request. - * - * @param request The batch recommendation points request. - * @return The response containing the recommended points. - */ - public Points.RecommendBatchResponse recommendBatch(Points.RecommendBatchPoints request) { - return pointsStub.recommendBatch(request); - } - - /** - * Recommends groups based on the given positive/negative points recommendation request. - * - * @param request The request containing the point groups to recommend. - * @return The response containing the recommended groups. - */ - public Points.RecommendGroupsResponse recommendGroups(Points.RecommendPointGroups request) { - return pointsStub.recommendGroups(request); - } - - /** - * Counts the number of points in a collection based on the given filters. - * - * @param collectionName The name of the collection. - * @param filter The filter to be applied. - * @return The response containing the points count result. - */ - public Points.CountResponse count(String collectionName, Points.Filter filter) { - Points.CountPoints request = - Points.CountPoints.newBuilder().setCollectionName(collectionName).setFilter(filter).build(); - return pointsStub.count(request); - } - - /** - * Counts the number of points in a collection based on the given filters. - * - * @param request The request containing the filters and options. - * @return The response containing the points count result. - */ - public Points.CountResponse count(Points.CountPoints request) { - return pointsStub.count(request); - } - - /** Internal update batch method */ - private Points.UpdateBatchResponse updateBatchPoints( - String collecionName, - Iterable operations, - @Nullable Points.WriteOrderingType ordering, - Boolean wait) { - Points.UpdateBatchPoints.Builder request = - Points.UpdateBatchPoints.newBuilder() - .setCollectionName(collecionName) - .addAllOperations(operations) - .setWait(wait); - - if (ordering != null) { - request.setOrdering(PointUtil.ordering(ordering)); - } - return pointsStub.updateBatch(request.build()); - } - - /** - * Updates a batch of points in a collection. Does not wait for the operation to complete before - * returning. - * - * @param collecionName The name of the collection. - * @param operations The operations to be performed on the points. - * @param ordering The ordering of the write operations. - * @return The response of the batch points update operation. - */ - public Points.UpdateBatchResponse updateBatchPoints( - String collecionName, - Iterable operations, - @Nullable Points.WriteOrderingType ordering) { - return updateBatchPoints(collecionName, operations, ordering, false); - } - - /** - * Updates a batch of points in a collection. Waits for the operation to complete before - * returning. - * - * @param collectionName The name of the collection. - * @param operations The operations to be performed on the points. - * @param ordering The ordering of the write operations. - * @return The response of the batch points update operation. - */ - public Points.UpdateBatchResponse updateBatchPointsBlocking( - String collectionName, - Iterable operations, - @Nullable Points.WriteOrderingType ordering) { - return updateBatchPoints(collectionName, operations, ordering, true); - } - - /** Internal create field index method */ - private Points.PointsOperationResponse createFieldIndex( - String collectionName, - String fieldName, - Points.FieldType fieldType, - Collections.PayloadIndexParams fieldIndexParams, - @Nullable Points.WriteOrderingType ordering, - Boolean wait) { - Points.CreateFieldIndexCollection.Builder request = - Points.CreateFieldIndexCollection.newBuilder() - .setCollectionName(collectionName) - .setFieldName(fieldName) - .setFieldType(fieldType) - .setFieldIndexParams(fieldIndexParams) - .setWait(wait); - - if (ordering != null) { - request.setOrdering(PointUtil.ordering(ordering)); - } - return pointsStub.createFieldIndex(request.build()); - } - - /** - * Creates a field index in the specified collection with the given parameters. Does not wait for - * the operation to complete before returning. - * - * @param collectionName The name of the collection. - * @param fieldName The name of the field. - * @param fieldType The type of the field. - * @param fieldIndexParams The index parameters for the field. - * @param ordering The write ordering for the field. - * @return The response of the field index creation operation. - */ - public Points.PointsOperationResponse createFieldIndex( - String collectionName, - String fieldName, - Points.FieldType fieldType, - Collections.PayloadIndexParams fieldIndexParams, - @Nullable Points.WriteOrderingType ordering) { - return createFieldIndex( - collectionName, fieldName, fieldType, fieldIndexParams, ordering, false); - } - - /** - * Creates a field index in the specified collection with the given parameters. Waits for the - * operation to complete before returning. - * - * @param collectionName The name of the collection. - * @param fieldName The name of the field. - * @param fieldType The type of the field. - * @param fieldIndexParams The index parameters for the field. - * @param ordering The write ordering for the field. - * @return The response of the field index creation operation. - */ - public Points.PointsOperationResponse createFieldIndexBlocking( - String collectionName, - String fieldName, - Points.FieldType fieldType, - Collections.PayloadIndexParams fieldIndexParams, - @Nullable Points.WriteOrderingType ordering) { - return createFieldIndex(collectionName, fieldName, fieldType, fieldIndexParams, ordering, true); - } - - /** Internal delete field index method */ - private Points.PointsOperationResponse deleteFieldIndex( - String collectionName, - String fieldName, - @Nullable Points.WriteOrderingType ordering, - Boolean wait) { - Points.DeleteFieldIndexCollection.Builder request = - Points.DeleteFieldIndexCollection.newBuilder() - .setCollectionName(collectionName) - .setFieldName(fieldName) - .setWait(wait); - - if (ordering != null) { - request.setOrdering(PointUtil.ordering(ordering)); - } - return pointsStub.deleteFieldIndex(request.build()); - } - - /** - * Deletes the field index for a given collection and field name. Does not wait for the operation - * to complete before returning. - * - * @param collectionName The name of the collection. - * @param fieldName The name of the field. - * @param ordering The write ordering for the operation. - * @return The response of the delete operation. - */ - public Points.PointsOperationResponse deleteFieldIndex( - String collectionName, String fieldName, @Nullable Points.WriteOrderingType ordering) { - return deleteFieldIndex(collectionName, fieldName, ordering, false); - } - - /** - * Deletes the field index for a given collection and field name. Waits for the operation to - * complete before returning. - * - * @param collectionName The name of the collection. - * @param fieldName The name of the field. - * @param ordering The write ordering for the operation. - * @return The response of the delete operation. - */ - public Points.PointsOperationResponse deleteFieldIndexBlocking( - String collectionName, String fieldName, @Nullable Points.WriteOrderingType ordering) { - return deleteFieldIndex(collectionName, fieldName, ordering, true); - } - - /** - * Creates a snapshot of a collection. - * - * @param collectionName the name of the collection - * @return The response containing information about the created snapshot - */ - public SnapshotsService.CreateSnapshotResponse createSnapshot(String collectionName) { - SnapshotsService.CreateSnapshotRequest request = - SnapshotsService.CreateSnapshotRequest.newBuilder() - .setCollectionName(collectionName) - .build(); - return snapshotsStub.create(request); - } - - /** - * Retrieves a list of snapshots for a given collection. - * - * @param collectionName the name of the collection - * @return The response containing the list of snapshots - */ - public SnapshotsService.ListSnapshotsResponse listSnapshots(String collectionName) { - SnapshotsService.ListSnapshotsRequest request = - SnapshotsService.ListSnapshotsRequest.newBuilder() - .setCollectionName(collectionName) - .build(); - return snapshotsStub.list(request); - } - - /** - * Deletes a snapshot with the specified name from the given collection. - * - * @param collectionName The name of the collection. - * @param snapshotName The name of the snapshot to be deleted. - * @return The response indicating the success or failure of the snapshot deletion. - */ - public SnapshotsService.DeleteSnapshotResponse deleteSnapshot( - String collectionName, String snapshotName) { - SnapshotsService.DeleteSnapshotRequest request = - SnapshotsService.DeleteSnapshotRequest.newBuilder() - .setCollectionName(collectionName) - .setSnapshotName(snapshotName) - .build(); - return snapshotsStub.delete(request); - } - - /** - * Creates a full snapshot of the Qdrant database. - * - * @return The response indicating the status of the snapshot creation. - */ - public SnapshotsService.CreateSnapshotResponse createFullSnapshot() { - SnapshotsService.CreateFullSnapshotRequest request = - SnapshotsService.CreateFullSnapshotRequest.newBuilder().build(); - return snapshotsStub.createFull(request); - } - - /** - * Retrieves a list of full snapshots for a given collection. - * - * @return The response containing the list of full snapshots. - */ - public SnapshotsService.ListSnapshotsResponse listFullSnapshots() { - SnapshotsService.ListFullSnapshotsRequest request = - SnapshotsService.ListFullSnapshotsRequest.newBuilder().build(); - return snapshotsStub.listFull(request); - } - - /** - * Deletes a full snapshot. - * - * @param snapshotName the name of the snapshot to delete. - * @return The response indicating the status of the snapshot deletion. - */ - public SnapshotsService.DeleteSnapshotResponse deleteFullSnapshot(String snapshotName) { - SnapshotsService.DeleteFullSnapshotRequest request = - SnapshotsService.DeleteFullSnapshotRequest.newBuilder() - .setSnapshotName(snapshotName) - .build(); - return snapshotsStub.deleteFull(request); - } - - /** - * Downloads a snapshot of a collection from the specified REST API URI and saves it to the given - * output path. - * - * @param outPath The path where the snapshot will be saved. - * @param collectionName The name of the collection. - * @param snapshotName The name of the snapshot. If null, the latest snapshot will be downloaded. - * @param restApiUri The URI of the REST API. If null, the default URI "http://localhost:6333" - * will be used. - * @throws RuntimeException If an error occurs while downloading the snapshot. - */ - public void downloadSnapshot( - Path outPath, - String collectionName, - @Nullable String snapshotName, - @Nullable String restApiUri) { - try { - String resolvedSnapshotName; - - if (snapshotName != null) { - resolvedSnapshotName = snapshotName; - } else { - // Get the latest(0th) snapshot of the collection - List snapshots = - listSnapshots(collectionName).getSnapshotDescriptionsList(); - if (snapshots.isEmpty()) { - throw new RuntimeException("No snapshots found"); - } - resolvedSnapshotName = - listSnapshots(collectionName).getSnapshotDescriptionsList().get(0).getName(); - } - - String uri; - if (restApiUri != null) { - uri = - String.format( - "%s/collections/%s/snapshots/%s", restApiUri, collectionName, resolvedSnapshotName); - } else { - uri = - String.format( - "http://localhost:6333/collections/%s/snapshots/%s", - collectionName, resolvedSnapshotName); - } - - URL url = new URL(uri); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - - if (connection.getResponseCode() == 200) { - try (InputStream in = connection.getInputStream(); - FileOutputStream fileOut = new FileOutputStream(outPath.toFile())) { - - byte[] buffer = new byte[8192]; - int bytesRead; - while ((bytesRead = in.read(buffer)) != -1) { - fileOut.write(buffer, 0, bytesRead); - } - - System.out.println("Downloaded successfully"); - } - } else { - System.err.println("Download failed. HTTP Status Code: " + connection.getResponseCode()); - } - } catch (IOException e) { - throw new RuntimeException("Error downloading snapshot " + e.getMessage()); - } - } - - @Override - public void close() throws InterruptedException { - this.channel.shutdown().awaitTermination(10, TimeUnit.SECONDS); - } + private static final Logger logger = LoggerFactory.getLogger(QdrantClient.class); + private final QdrantGrpcClient grpcClient; + + /** + * Creates a new instance of {@link QdrantClient} + * + * @param grpcClient The low-level gRPC client to use. + */ + public QdrantClient(QdrantGrpcClient grpcClient) { + this.grpcClient = grpcClient; + } + + /** + * Gets detailed information about the qdrant cluster. + * + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture healthCheckAsync() { + return healthCheckAsync(null); + } + + /** + * Gets detailed information about the qdrant cluster. + * + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture healthCheckAsync(@Nullable Duration timeout) { + QdrantFutureStub qdrant = timeout != null + ? this.grpcClient.qdrant().withDeadlineAfter(timeout.toMillis(), TimeUnit.MILLISECONDS) + : this.grpcClient.qdrant(); + return qdrant.healthCheck(HealthCheckRequest.getDefaultInstance()); + } + + //region Collections + + /** + * Creates a new collection with the given parameters + * + * @param collectionName The name of the collection. + * @param vectorParams The vector parameters + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture createCollectionAsync( + String collectionName, + VectorParams vectorParams) { + return createCollectionAsync(collectionName, vectorParams, null); + } + + /** + * Creates a new collection with the given parameters + * + * @param collectionName The name of the collection. + * @param vectorParams The vector parameters + * @param timeout The timeout for the call + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture createCollectionAsync( + String collectionName, + VectorParams vectorParams, + @Nullable Duration timeout) { + return createCollectionAsync(CreateCollection.newBuilder() + .setCollectionName(collectionName) + .setVectorsConfig(VectorsConfig.newBuilder() + .setParams(vectorParams) + .build()) + .build(), + timeout); + } + + /** + * Creates a new collection with the given parameters + * + * @param collectionName The name of the collection. + * @param namedVectorParams The named vector parameters + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture createCollectionAsync( + String collectionName, + Map namedVectorParams) { + return createCollectionAsync(collectionName, namedVectorParams, null); + } + + /** + * Creates a new collection with the given parameters + * + * @param collectionName The name of the collection. + * @param namedVectorParams The named vector parameters + * @param timeout The timeout for the call + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture createCollectionAsync( + String collectionName, + Map namedVectorParams, + @Nullable Duration timeout) { + return createCollectionAsync(CreateCollection.newBuilder() + .setCollectionName(collectionName) + .setVectorsConfig(VectorsConfig.newBuilder() + .setParamsMap(VectorParamsMap.newBuilder().putAllMap(namedVectorParams).build()) + .build()) + .build(), + timeout); + } + + /** + * Creates a new collection with the given parameters + * + * @param createCollection The collection creation parameters + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture createCollectionAsync(CreateCollection createCollection) { + return createCollectionAsync(createCollection, null); + } + + /** + * Creates a new collection with the given parameters + * + * @param createCollection The collection creation parameters + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture createCollectionAsync(CreateCollection createCollection, @Nullable Duration timeout) { + String collectionName = createCollection.getCollectionName(); + Preconditions.checkArgument(!collectionName.isEmpty(), "Collection name must not be empty"); + logger.debug("Create collection '{}'", collectionName); + ListenableFuture future = getCollections(timeout).create(createCollection); + addLogFailureCallback(future, "Create collection"); + return Futures.transform(future, response -> { + if (!response.getResult()) { + logger.error("Collection '{}' could not be created", collectionName); + throw new QdrantException("Collection '" + collectionName + "' could not be created"); + } + return response; + }, MoreExecutors.directExecutor()); + } + + /** + * Deletes a collection if one exists, and creates a new collection with the given parameters. + * + * @param collectionName The name of the collection. + * @param vectorParams The vector parameters + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture recreateCollectionAsync( + String collectionName, + VectorParams vectorParams) { + return recreateCollectionAsync(collectionName, vectorParams, null); + } + + /** + * Deletes a collection if one exists, and creates a new collection with the given parameters. + * + * @param collectionName The name of the collection. + * @param vectorParams The vector parameters + * @param timeout The timeout for the call + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture recreateCollectionAsync( + String collectionName, + VectorParams vectorParams, + @Nullable Duration timeout) { + return recreateCollectionAsync(CreateCollection.newBuilder() + .setCollectionName(collectionName) + .setVectorsConfig(VectorsConfig.newBuilder() + .setParams(vectorParams) + .build()) + .build(), + timeout); + } + + /** + * Deletes a collection if one exists, and creates a new collection with the given parameters. + * + * @param collectionName The name of the collection. + * @param namedVectorParams The named vector parameters + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture recreateCollectionAsync( + String collectionName, + Map namedVectorParams) { + return recreateCollectionAsync(collectionName, namedVectorParams, null); + } + + /** + * Deletes a collection if one exists, and creates a new collection with the given parameters. + * + * @param collectionName The name of the collection. + * @param namedVectorParams The named vector parameters + * @param timeout The timeout for the call + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture recreateCollectionAsync( + String collectionName, + Map namedVectorParams, + @Nullable Duration timeout) { + return recreateCollectionAsync(CreateCollection.newBuilder() + .setCollectionName(collectionName) + .setVectorsConfig(VectorsConfig.newBuilder() + .setParamsMap(VectorParamsMap.newBuilder().putAllMap(namedVectorParams).build()) + .build()) + .build(), + timeout); + } + + /** + * Deletes a collection if one exists, and creates a new collection with the given parameters. + * + * @param createCollection The collection creation parameters + * @return a new instance of {@link CollectionOperationResponse} + */ + public ListenableFuture recreateCollectionAsync(CreateCollection createCollection) { + return recreateCollectionAsync(createCollection, null); + } + + /** + * Deletes a collection if one exists, and creates a new collection with the given parameters. + * + * @param createCollection The collection creation parameters + * @param timeout The timeout for the call. + * @return a new instance of {@link CollectionOperationResponse} + */ + public ListenableFuture recreateCollectionAsync(CreateCollection createCollection, @Nullable Duration timeout) { + return Futures.transformAsync( + deleteCollectionAsync(createCollection.getCollectionName(), timeout), + input -> createCollectionAsync(createCollection, timeout), + MoreExecutors.directExecutor()); + } + + /** + * Gets detailed information about an existing collection. + * + * @param collectionName The name of the collection. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture getCollectionInfoAsync(String collectionName) { + return getCollectionInfoAsync(collectionName, null); + } + + /** + * Gets detailed information about an existing collection. + * + * @param collectionName The name of the collection. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture getCollectionInfoAsync(String collectionName, @Nullable Duration timeout) { + logger.debug("Get collection info for '{}'", collectionName); + GetCollectionInfoRequest request = GetCollectionInfoRequest.newBuilder() + .setCollectionName(collectionName) + .build(); + ListenableFuture future = getCollections(timeout).get(request); + addLogFailureCallback(future, "Get collection info"); + return Futures.transform(future, GetCollectionInfoResponse::getResult, MoreExecutors.directExecutor()); + } + + /** + * Deletes a collection and all its associated data. + * + * @param collectionName The name of the collection + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture deleteCollectionAsync(String collectionName) { + return deleteCollectionAsync(collectionName, null); + } + + /** + * Deletes a collection and all its associated data. + * + * @param collectionName The name of the collection + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture deleteCollectionAsync(String collectionName, @Nullable Duration timeout) { + Preconditions.checkArgument(!collectionName.isEmpty(), "Collection name must not be empty"); + logger.debug("Delete collection '{}'", collectionName); + + DeleteCollection deleteCollection = DeleteCollection.newBuilder() + .setCollectionName(collectionName) + .build(); + ListenableFuture future = getCollections(timeout).delete(deleteCollection); + addLogFailureCallback(future, "Delete collection"); + + return Futures.transform(future, response -> { + if (!response.getResult()) { + logger.error("Collection '{}' could not be deleted", collectionName); + throw new QdrantException("Collection '" + collectionName + "' could not be deleted"); + } + return response; + }, MoreExecutors.directExecutor()); + } + + /** + * Gets the names of all existing collections + * + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture> listCollectionsAsync() { + return listCollectionsAsync(null); + } + + /** + * Gets the names of all existing collections + * + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture> listCollectionsAsync(@Nullable Duration timeout) { + logger.debug("List collections"); + + ListenableFuture future = + getCollections(timeout).list(ListCollectionsRequest.getDefaultInstance()); + + addLogFailureCallback(future, "List collection"); + return Futures.transform(future, response -> + response.getCollectionsList() + .stream() + .map(CollectionDescription::getName) + .collect(Collectors.toList()), MoreExecutors.directExecutor()); + } + + /** + * Update parameters of the collection + * + * @param updateCollection The update parameters. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture updateCollectionAsync(UpdateCollection updateCollection) { + return updateCollectionAsync(updateCollection, null); + } + + /** + * Update parameters of the collection + * + * @param updateCollection The update parameters. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture updateCollectionAsync(UpdateCollection updateCollection, @Nullable Duration timeout) { + String collectionName = updateCollection.getCollectionName(); + Preconditions.checkArgument(!collectionName.isEmpty(), "Collection name must not be empty"); + logger.debug("Update collection '{}'", collectionName); + + ListenableFuture future = getCollections(timeout).update(updateCollection); + addLogFailureCallback(future, "Update collection"); + return Futures.transform(future, response -> { + if (!response.getResult()) { + logger.error("Collection '{}' could not be updated", collectionName); + throw new QdrantException("Collection '" + collectionName + "' could not be updated"); + } + return response; + }, MoreExecutors.directExecutor()); + } + + //endregion + + //region Alias Management + + /** + * Creates an alias for a given collection. + * + * @param aliasName The alias to be created. + * @param collectionName The collection for which the alias is to be created. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture createAliasAsync(String aliasName, String collectionName) { + return createAliasAsync(aliasName, collectionName, null); + } + + /** + * Creates an alias for a given collection. + * + * @param aliasName The alias to be created. + * @param collectionName The collection for which the alias is to be created. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture createAliasAsync(String aliasName, String collectionName, @Nullable Duration timeout) { + return updateAliasesAsync(ImmutableList.of(AliasOperations.newBuilder() + .setCreateAlias(CreateAlias.newBuilder() + .setAliasName(aliasName) + .setCollectionName(collectionName) + .build()) + .build()), + timeout); + } + + /** + * Renames an existing alias. + * + * @param oldAliasName The old alias name. + * @param newAliasName The new alias name. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture renameAliasAsync(String oldAliasName, String newAliasName) { + return renameAliasAsync(oldAliasName, newAliasName, null); + } + + /** + * Renames an existing alias. + * + * @param oldAliasName The old alias name. + * @param newAliasName The new alias name. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture renameAliasAsync(String oldAliasName, String newAliasName, @Nullable Duration timeout) { + return updateAliasesAsync(ImmutableList.of(AliasOperations.newBuilder() + .setRenameAlias(RenameAlias.newBuilder() + .setOldAliasName(oldAliasName) + .setNewAliasName(newAliasName) + .build()) + .build()), + timeout); + } + + /** + * Deletes an alias. + * + * @param aliasName The alias to be deleted. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture deleteAliasAsync(String aliasName) { + return deleteAliasAsync(aliasName, null); + } + + /** + * Deletes an alias. + * + * @param aliasName The alias to be deleted. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture deleteAliasAsync(String aliasName, @Nullable Duration timeout) { + return updateAliasesAsync(ImmutableList.of(AliasOperations.newBuilder() + .setDeleteAlias(DeleteAlias.newBuilder() + .setAliasName(aliasName) + .build()) + .build()), + timeout); + } + + /** + * Update the aliases of existing collections. + * + * @param aliasOperations The list of operations to perform. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture updateAliasesAsync(List aliasOperations) { + return updateAliasesAsync(aliasOperations, null); + } + + /** + * Update the aliases of existing collections. + * + * @param aliasOperations The list of operations to perform. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture updateAliasesAsync(List aliasOperations, @Nullable Duration timeout) { + ChangeAliases request = ChangeAliases.newBuilder() + .addAllActions(aliasOperations) + .build(); + + if (logger.isDebugEnabled()) { + for (AliasOperations aliasOperation : aliasOperations) { + switch (aliasOperation.getActionCase()) { + case CREATE_ALIAS: + CreateAlias createAlias = aliasOperation.getCreateAlias(); + logger.debug("Create alias '{}' for collection '{}'", + createAlias.getAliasName(), + createAlias.getCollectionName()); + break; + case RENAME_ALIAS: + RenameAlias renameAlias = aliasOperation.getRenameAlias(); + logger.debug("Rename alias '{}' to '{}'", + renameAlias.getOldAliasName(), + renameAlias.getNewAliasName()); + break; + case DELETE_ALIAS: + DeleteAlias deleteAlias = aliasOperation.getDeleteAlias(); + logger.debug("Delete alias '{}'", deleteAlias.getAliasName()); + break; + case ACTION_NOT_SET: + break; + } + } + } + + ListenableFuture future = getCollections(timeout).updateAliases(request); + addLogFailureCallback(future, "Update aliases"); + return Futures.transform(future, response -> { + if (!response.getResult()) { + logger.error("Alias update operation could not be performed"); + throw new QdrantException("Alias update could not be performed"); + } + return response; + }, MoreExecutors.directExecutor()); + } + + /** + * Gets a list of all aliases for a collection. + * + * @param collectionName The name of the collection. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture> listCollectionAliasesAsync(String collectionName) { + return listCollectionAliasesAsync(collectionName, null); + } + + /** + * Gets a list of all aliases for a collection. + * + * @param collectionName The name of the collection. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture> listCollectionAliasesAsync(String collectionName, @Nullable Duration timeout) { + Preconditions.checkArgument(!collectionName.isEmpty(), "Collection name must not be empty"); + logger.debug("List aliases for collection '{}'", collectionName); + + ListCollectionAliasesRequest request = ListCollectionAliasesRequest.newBuilder() + .setCollectionName(collectionName) + .build(); + + ListenableFuture future = getCollections(timeout).listCollectionAliases(request); + addLogFailureCallback(future, "List collection aliases"); + return Futures.transform(future, response -> response.getAliasesList() + .stream() + .map(AliasDescription::getAliasName) + .collect(Collectors.toList()), MoreExecutors.directExecutor()); + } + + /** + * Gets a list of all aliases for all existing collections. + * + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture> listAliasesAsync() { + return listAliasesAsync(null); + } + + /** + * Gets a list of all aliases for all existing collections. + * + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture> listAliasesAsync(@Nullable Duration timeout) { + logger.debug("List all aliases"); + ListenableFuture future = getCollections(timeout).listAliases(ListAliasesRequest.getDefaultInstance()); + addLogFailureCallback(future, "List aliases"); + return Futures.transform(future, ListAliasesResponse::getAliasesList, MoreExecutors.directExecutor()); + } + + //endregion + + //region Point Management + + /** + * Perform insert and updates on points. If a point with a given ID already exists, it will be overwritten. + * + * @param collectionName The name of the collection. + * @param points The points to be upserted + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture upsertAsync( + String collectionName, + List points) { + return upsertAsync(collectionName, points, null); + } + + /** + * Perform insert and updates on points. If a point with a given ID already exists, it will be overwritten. + * The call waits for the changes to be applied. + * + * @param collectionName The name of the collection. + * @param points The points to be upserted + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture upsertAsync( + String collectionName, + List points, + @Nullable Duration timeout) { + return upsertAsync( + UpsertPoints.newBuilder() + .setCollectionName(collectionName) + .addAllPoints(points) + .setWait(true) + .build(), + timeout); + } + + /** + * Perform insert and updates on points. If a point with a given ID already exists, it will be overwritten. + * + * @param request The upsert points request + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture upsertAsync(UpsertPoints request) { + return upsertAsync(request, null); + } + + /** + * Perform insert and updates on points. If a point with a given ID already exists, it will be overwritten. + * + * @param request The upsert points request + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture upsertAsync( + UpsertPoints request, + @Nullable Duration timeout) { + String collectionName = request.getCollectionName(); + Preconditions.checkArgument(!collectionName.isEmpty(), "Collection name must not be empty"); + logger.debug("Upsert {} points into '{}'", request.getPointsList().size(), collectionName); + ListenableFuture future = getPoints(timeout).upsert(request); + addLogFailureCallback(future, "Upsert"); + return Futures.transform(future, PointsOperationResponse::getResult, MoreExecutors.directExecutor()); + } + + /** + * Deletes points. + * The call waits for the changes to be applied. + * + * @param collectionName The name of the collection. + * @param ids The ids of points to delete. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture deleteAsync( + String collectionName, + List ids) { + return deleteAsync(collectionName, ids, null); + } + + /** + * Deletes points. + * The call waits for the changes to be applied. + * + * @param collectionName The name of the collection. + * @param ids The ids of points to delete. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture deleteAsync( + String collectionName, + List ids, + @Nullable Duration timeout) { + return deleteAsync(DeletePoints.newBuilder() + .setCollectionName(collectionName) + .setPoints(PointsSelector.newBuilder().setPoints(PointsIdsList.newBuilder().addAllIds(ids).build()).build()) + .setWait(true) + .build(), + timeout); + } + + /** + * Deletes points. + * The call waits for the changes to be applied. + * + * @param collectionName The name of the collection. + * @param filter A filter selecting the points to be deleted. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture deleteAsync( + String collectionName, + Filter filter) { + return deleteAsync(collectionName, filter, null); + } + + /** + * Deletes points. + * + * @param collectionName The name of the collection. + * @param filter A filter selecting the points to be deleted. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture deleteAsync( + String collectionName, + Filter filter, + @Nullable Duration timeout) { + return deleteAsync(DeletePoints.newBuilder() + .setCollectionName(collectionName) + .setPoints(PointsSelector.newBuilder().setFilter(filter).build()) + .setWait(true) + .build(), + timeout); + } + + /** + * Deletes points. + * + * @param request The delete points request + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture deleteAsync(DeletePoints request) { + return deleteAsync(request, null); + } + + /** + * Deletes points. + * + * @param request The delete points request + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture deleteAsync( + DeletePoints request, + @Nullable Duration timeout) { + String collectionName = request.getCollectionName(); + Preconditions.checkArgument(!collectionName.isEmpty(), "Collection name must not be empty"); + logger.debug("Delete from '{}'", collectionName); + ListenableFuture future = getPoints(timeout).delete(request); + addLogFailureCallback(future, "Delete"); + return Futures.transform(future, PointsOperationResponse::getResult, MoreExecutors.directExecutor()); + } + + /** + * Retrieves points. Includes all payload, excludes vectors. + * + * @param collectionName The name of the collection. + * @param id The id of a point to retrieve + * @param readConsistency Options for specifying read consistency guarantees. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture> retrieveAsync( + String collectionName, + PointId id, + @Nullable ReadConsistency readConsistency + ) { + return retrieveAsync( + collectionName, + id, + true, + false, + readConsistency + ); + } + + /** + * Retrieves points. + * + * @param collectionName The name of the collection. + * @param id The id of a point to retrieve + * @param withPayload Whether to include the payload or not. + * @param withVectors Whether to include the vectors or not. + * @param readConsistency Options for specifying read consistency guarantees. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture> retrieveAsync( + String collectionName, + PointId id, + boolean withPayload, + boolean withVectors, + @Nullable ReadConsistency readConsistency + ) { + return retrieveAsync( + collectionName, + ImmutableList.of(id), + WithPayloadSelectorFactory.enable(withPayload), + WithVectorsSelectorFactory.enable(withVectors), + readConsistency + ); + } + + /** + * Retrieves points. Includes all payload, excludes vectors. + * + * @param collectionName The name of the collection. + * @param ids The list of ids of points to retrieve + * @param readConsistency Options for specifying read consistency guarantees. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture> retrieveAsync( + String collectionName, + List ids, + @Nullable ReadConsistency readConsistency + ) { + return retrieveAsync( + collectionName, + ids, + true, + false, + readConsistency + ); + } + + /** + * Retrieves points. + * + * @param collectionName The name of the collection. + * @param ids The list of ids of points to retrieve + * @param withPayload Whether to include the payload or not. + * @param withVectors Whether to include the vectors or not. + * @param readConsistency Options for specifying read consistency guarantees. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture> retrieveAsync( + String collectionName, + List ids, + boolean withPayload, + boolean withVectors, + @Nullable ReadConsistency readConsistency + ) { + return retrieveAsync( + collectionName, + ids, + WithPayloadSelectorFactory.enable(withPayload), + WithVectorsSelectorFactory.enable(withVectors), + readConsistency + ); + } + + /** + * Retrieves points. + * + * @param collectionName The name of the collection. + * @param ids The list of ids of points to retrieve + * @param payloadSelector Options for specifying which payload to include or not. + * @param vectorsSelector Options for specifying which vectors to include into response. + * @param readConsistency Options for specifying read consistency guarantees. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture> retrieveAsync( + String collectionName, + List ids, + WithPayloadSelector payloadSelector, + WithVectorsSelector vectorsSelector, + @Nullable ReadConsistency readConsistency + ) { + return retrieveAsync(collectionName, ids, payloadSelector, vectorsSelector, readConsistency, null); + } + + /** + * Retrieves points. + * + * @param collectionName The name of the collection. + * @param ids The list of ids of points to retrieve + * @param payloadSelector Options for specifying which payload to include or not. + * @param vectorsSelector Options for specifying which vectors to include into response. + * @param readConsistency Options for specifying read consistency guarantees. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture> retrieveAsync( + String collectionName, + List ids, + WithPayloadSelector payloadSelector, + WithVectorsSelector vectorsSelector, + @Nullable ReadConsistency readConsistency, + @Nullable Duration timeout + ) { + logger.debug("Retrieve points from '{}'", collectionName); + GetPoints.Builder requestBuilder = GetPoints.newBuilder() + .setCollectionName(collectionName) + .addAllIds(ids) + .setWithPayload(payloadSelector) + .setWithVectors(vectorsSelector); + + if (readConsistency != null) { + requestBuilder.setReadConsistency(readConsistency); + } + + ListenableFuture future = getPoints(timeout).get(requestBuilder.build()); + addLogFailureCallback(future, "Retrieve"); + return Futures.transform(future, GetResponse::getResultList, MoreExecutors.directExecutor()); + } + + //region Update Vectors + + /** + * Update named vectors for point. + * + * @param collectionName The name of the collection. + * @param points The list of points and vectors to update. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture updateVectorsAsync( + String collectionName, + List points + ) { + return updateVectorsAsync(collectionName, points, null, null, null); + } + + /** + * Update named vectors for point. + * + * @param collectionName The name of the collection. + * @param points The list of points and vectors to update. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture updateVectorsAsync( + String collectionName, + List points, + @Nullable Duration timeout + ) { + return updateVectorsAsync(collectionName, points, null, null, timeout); + } + + /** + * Update named vectors for point. + * + * @param collectionName The name of the collection. + * @param points The list of points and vectors to update. + * @param wait Whether to wait until the changes have been applied. Defaults to true. + * @param ordering Write ordering guarantees. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture updateVectorsAsync( + String collectionName, + List points, + @Nullable Boolean wait, + @Nullable WriteOrderingType ordering, + @Nullable Duration timeout + ) { + logger.debug("Update vectors in '{}'", collectionName); + UpdatePointVectors.Builder requestBuilder = UpdatePointVectors.newBuilder() + .setCollectionName(collectionName) + .addAllPoints(points) + .setWait(wait == null || wait); + + if (ordering != null) { + requestBuilder.setOrdering(WriteOrdering.newBuilder().setType(ordering).build()); + } + + ListenableFuture future = getPoints(timeout).updateVectors(requestBuilder.build()); + addLogFailureCallback(future, "Update vectors"); + return Futures.transform(future, PointsOperationResponse::getResult, MoreExecutors.directExecutor()); + } + + //endregion + + //region Delete Vectors + + /** + * Delete named vectors for points. + * + * @param collectionName The name of the collection. + * @param vectors The list of vector names to delete. + * @param filter A filter selecting the points to be deleted. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture deleteVectorsAsync( + String collectionName, + List vectors, + Filter filter + ) { + return deleteVectorsAsync( + collectionName, + vectors, + PointsSelector.newBuilder().setFilter(filter).build(), + null, + null, + null + ); + } + + /** + * Delete named vectors for points. + * + * @param collectionName The name of the collection. + * @param vectors The list of vector names to delete. + * @param filter A filter selecting the points to be deleted. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture deleteVectorsAsync( + String collectionName, + List vectors, + Filter filter, + @Nullable Duration timeout + ) { + return deleteVectorsAsync( + collectionName, + vectors, + PointsSelector.newBuilder().setFilter(filter).build(), + null, + null, + timeout + ); + } + + /** + * Delete named vectors for points. + * + * @param collectionName The name of the collection. + * @param vectors The list of vector names to delete. + * @param filter A filter selecting the points to be deleted. + * @param wait Whether to wait until the changes have been applied. Defaults to true. + * @param ordering Write ordering guarantees. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture deleteVectorsAsync( + String collectionName, + List vectors, + Filter filter, + @Nullable Boolean wait, + @Nullable WriteOrderingType ordering, + @Nullable Duration timeout + ) { + return deleteVectorsAsync( + collectionName, + vectors, + PointsSelector.newBuilder().setFilter(filter).build(), + wait, + ordering, + timeout + ); + } + + /** + * Delete named vectors for points. + * + * @param collectionName The name of the collection. + * @param vectors The list of vector names to delete. + * @param ids The list of ids to delete. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture deleteVectorsAsync( + String collectionName, + List vectors, + List ids + ) { + return deleteVectorsAsync( + collectionName, + vectors, + PointsSelector.newBuilder() + .setPoints(PointsIdsList.newBuilder().addAllIds(ids).build()) + .build(), + null, + null, + null + ); + } + + /** + * Delete named vectors for points. + * + * @param collectionName The name of the collection. + * @param vectors The list of vector names to delete. + * @param ids The list of ids to delete. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture deleteVectorsAsync( + String collectionName, + List vectors, + List ids, + @Nullable Duration timeout + ) { + return deleteVectorsAsync( + collectionName, + vectors, + PointsSelector.newBuilder() + .setPoints(PointsIdsList.newBuilder().addAllIds(ids).build()) + .build(), + null, + null, + timeout + ); + } + + /** + * Delete named vectors for points. + * + * @param collectionName The name of the collection. + * @param vectors The list of vector names to delete. + * @param ids The list of ids to delete. + * @param wait Whether to wait until the changes have been applied. Defaults to true. + * @param ordering Write ordering guarantees. + * @param timeout The timeout for the call + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture deleteVectorsAsync( + String collectionName, + List vectors, + List ids, + @Nullable Boolean wait, + @Nullable WriteOrderingType ordering, + @Nullable Duration timeout + ) { + return deleteVectorsAsync( + collectionName, + vectors, + PointsSelector.newBuilder() + .setPoints(PointsIdsList.newBuilder().addAllIds(ids).build()) + .build(), + wait, + ordering, + timeout + ); + } + + private ListenableFuture deleteVectorsAsync( + String collectionName, + List vectors, + PointsSelector pointsSelector, + @Nullable Boolean wait, + @Nullable WriteOrderingType ordering, + @Nullable Duration timeout + ) { + logger.debug("Delete vectors in '{}'", collectionName); + DeletePointVectors.Builder requestBuilder = DeletePointVectors.newBuilder() + .setCollectionName(collectionName) + .setVectors(VectorsSelector.newBuilder() + .addAllNames(vectors) + .build()) + .setPointsSelector(pointsSelector) + .setWait(wait == null || wait); + + if (ordering != null) { + requestBuilder.setOrdering(WriteOrdering.newBuilder().setType(ordering).build()); + } + + ListenableFuture future = getPoints(timeout).deleteVectors(requestBuilder.build()); + addLogFailureCallback(future, "Delete vectors"); + return Futures.transform(future, PointsOperationResponse::getResult, MoreExecutors.directExecutor()); + } + + //endregion + + //endregion + + //region Set Payload + + /** + * Sets the payload for all points in the collection. + * + * @param collectionName The name of the collection. + * @param payload New payload values + * @param wait Whether to wait until the changes have been applied. Defaults to true. + * @param ordering Write ordering guarantees. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture setPayloadAsync( + String collectionName, + Map payload, + @Nullable Boolean wait, + @Nullable WriteOrderingType ordering, + @Nullable Duration timeout + ) { + return setPayloadAsync( + collectionName, + payload, + (PointsSelector) null, + wait, + ordering, + timeout + ); + } + + /** + * Sets the payload for the given id. + * + * @param collectionName The name of the collection. + * @param payload New payload values + * @param id The id for which to set the payload. + * @param wait Whether to wait until the changes have been applied. Defaults to true. + * @param ordering Write ordering guarantees. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture setPayloadAsync( + String collectionName, + Map payload, + PointId id, + @Nullable Boolean wait, + @Nullable WriteOrderingType ordering, + @Nullable Duration timeout + ) { + return setPayloadAsync( + collectionName, + payload, + PointsSelector.newBuilder().setPoints(PointsIdsList.newBuilder().addIds(id).build()).build(), + wait, + ordering, + timeout + ); + } + + /** + * Sets the payload for the given ids. + * + * @param collectionName The name of the collection. + * @param payload New payload values + * @param ids The ids for which to set the payload. + * @param wait Whether to wait until the changes have been applied. Defaults to true. + * @param ordering Write ordering guarantees. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture setPayloadAsync( + String collectionName, + Map payload, + List ids, + @Nullable Boolean wait, + @Nullable WriteOrderingType ordering, + @Nullable Duration timeout + ) { + return setPayloadAsync( + collectionName, + payload, + PointsSelector.newBuilder().setPoints(PointsIdsList.newBuilder().addAllIds(ids).build()).build(), + wait, + ordering, + timeout + ); + } + + /** + * Sets the payload for the given ids. + * + * @param collectionName The name of the collection. + * @param payload New payload values + * @param filter A filter selecting the points to be set. + * @param wait Whether to wait until the changes have been applied. Defaults to true. + * @param ordering Write ordering guarantees. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture setPayloadAsync( + String collectionName, + Map payload, + Filter filter, + @Nullable Boolean wait, + @Nullable WriteOrderingType ordering, + @Nullable Duration timeout + ) { + return setPayloadAsync( + collectionName, + payload, + PointsSelector.newBuilder().setFilter(filter).build(), + wait, + ordering, + timeout + ); + } + + private ListenableFuture setPayloadAsync( + String collectionName, + Map payload, + @Nullable PointsSelector pointsSelector, + @Nullable Boolean wait, + @Nullable WriteOrderingType ordering, + @Nullable Duration timeout + ) { + SetPayloadPoints.Builder requestBuilder = SetPayloadPoints.newBuilder() + .setCollectionName(collectionName) + .setWait(wait == null || wait) + .putAllPayload(payload); + + if (pointsSelector != null) { + requestBuilder.setPointsSelector(pointsSelector); + } + + if (ordering != null) { + requestBuilder.setOrdering(WriteOrdering.newBuilder().setType(ordering).build()); + } + + logger.debug("Set payload in '{}'", collectionName); + ListenableFuture future = getPoints(timeout).setPayload(requestBuilder.build()); + addLogFailureCallback(future, "Set payload"); + return Futures.transform(future, PointsOperationResponse::getResult, MoreExecutors.directExecutor()); + } + + //endregion + + //region Overwrite payload + + /** + * Overwrites the payload for all points in the collection. + * + * @param collectionName The name of the collection. + * @param payload New payload values + * @param wait Whether to wait until the changes have been applied. Defaults to true. + * @param ordering Write ordering guarantees. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture overwritePayloadAsync( + String collectionName, + Map payload, + @Nullable Boolean wait, + @Nullable WriteOrderingType ordering, + @Nullable Duration timeout + ) { + return overwritePayloadAsync( + collectionName, + payload, + (PointsSelector) null, + wait, + ordering, + timeout + ); + } + + /** + * Overwrites the payload for the given id. + * + * @param collectionName The name of the collection. + * @param payload New payload values + * @param id The id for which to overwrite the payload. + * @param wait Whether to wait until the changes have been applied. Defaults to true. + * @param ordering Write ordering guarantees. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture overwritePayloadAsync( + String collectionName, + Map payload, + PointId id, + @Nullable Boolean wait, + @Nullable WriteOrderingType ordering, + @Nullable Duration timeout + ) { + return overwritePayloadAsync( + collectionName, + payload, + PointsSelector.newBuilder().setPoints(PointsIdsList.newBuilder().addIds(id).build()).build(), + wait, + ordering, + timeout + ); + } + + /** + * Overwrites the payload for the given ids. + * + * @param collectionName The name of the collection. + * @param payload New payload values + * @param ids The ids for which to overwrite the payload. + * @param wait Whether to wait until the changes have been applied. Defaults to true. + * @param ordering Write ordering guarantees. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture overwritePayloadAsync( + String collectionName, + Map payload, + List ids, + @Nullable Boolean wait, + @Nullable WriteOrderingType ordering, + @Nullable Duration timeout + ) { + return overwritePayloadAsync( + collectionName, + payload, + PointsSelector.newBuilder().setPoints(PointsIdsList.newBuilder().addAllIds(ids).build()).build(), + wait, + ordering, + timeout + ); + } + + /** + * Overwrites the payload for the given ids. + * + * @param collectionName The name of the collection. + * @param payload New payload values + * @param filter A filter selecting the points for which to overwrite the payload. + * @param wait Whether to wait until the changes have been applied. Defaults to true. + * @param ordering Write ordering guarantees. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture overwritePayloadAsync( + String collectionName, + Map payload, + Filter filter, + @Nullable Boolean wait, + @Nullable WriteOrderingType ordering, + @Nullable Duration timeout + ) { + return overwritePayloadAsync( + collectionName, + payload, + PointsSelector.newBuilder().setFilter(filter).build(), + wait, + ordering, + timeout + ); + } + + private ListenableFuture overwritePayloadAsync( + String collectionName, + Map payload, + @Nullable PointsSelector pointsSelector, + @Nullable Boolean wait, + @Nullable WriteOrderingType ordering, + @Nullable Duration timeout + ) { + SetPayloadPoints.Builder requestBuilder = SetPayloadPoints.newBuilder() + .setCollectionName(collectionName) + .setWait(wait == null || wait) + .putAllPayload(payload); + + if (pointsSelector != null) { + requestBuilder.setPointsSelector(pointsSelector); + } + + if (ordering != null) { + requestBuilder.setOrdering(WriteOrdering.newBuilder().setType(ordering).build()); + } + + logger.debug("Overwrite payload in '{}'", collectionName); + ListenableFuture future = getPoints(timeout).overwritePayload(requestBuilder.build()); + addLogFailureCallback(future, "Overwrite payload"); + return Futures.transform(future, PointsOperationResponse::getResult, MoreExecutors.directExecutor()); + } + + //endregion + + //region Delete Payload + + /** + * Delete specified key payload for all points. + * + * @param collectionName The name of the collection. + * @param keys List of keys to delete. + * @param wait Whether to wait until the changes have been applied. Defaults to true. + * @param ordering Write ordering guarantees. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture deletePayloadAsync( + String collectionName, + List keys, + @Nullable Boolean wait, + @Nullable WriteOrderingType ordering, + @Nullable Duration timeout + ) { + return deletePayloadAsync( + collectionName, + keys, + (PointsSelector) null, + wait, + ordering, + timeout + ); + } + + /** + * Delete specified key payload for the given id. + * + * @param collectionName The name of the collection. + * @param keys List of keys to delete. + * @param id The id for which to delete the payload. + * @param wait Whether to wait until the changes have been applied. Defaults to true. + * @param ordering Write ordering guarantees. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture deletePayloadAsync( + String collectionName, + List keys, + PointId id, + @Nullable Boolean wait, + @Nullable WriteOrderingType ordering, + @Nullable Duration timeout + ) { + return deletePayloadAsync( + collectionName, + keys, + PointsSelector.newBuilder().setPoints(PointsIdsList.newBuilder().addIds(id).build()).build(), + wait, + ordering, + timeout + ); + } + + /** + * Delete specified key payload for the given ids. + * + * @param collectionName The name of the collection. + * @param keys List of keys to delete. + * @param ids The ids for which to delete the payload. + * @param wait Whether to wait until the changes have been applied. Defaults to true. + * @param ordering Write ordering guarantees. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture deletePayloadAsync( + String collectionName, + List keys, + List ids, + @Nullable Boolean wait, + @Nullable WriteOrderingType ordering, + @Nullable Duration timeout + ) { + return deletePayloadAsync( + collectionName, + keys, + PointsSelector.newBuilder().setPoints(PointsIdsList.newBuilder().addAllIds(ids).build()).build(), + wait, + ordering, + timeout + ); + } + + /** + * Delete specified key payload for the given ids. + * + * @param collectionName The name of the collection. + * @param keys List of keys to delete. + * @param filter A filter selecting the points to for which to delete the payload. + * @param wait Whether to wait until the changes have been applied. Defaults to true. + * @param ordering Write ordering guarantees. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture deletePayloadAsync( + String collectionName, + List keys, + Filter filter, + @Nullable Boolean wait, + @Nullable WriteOrderingType ordering, + @Nullable Duration timeout + ) { + return deletePayloadAsync( + collectionName, + keys, + PointsSelector.newBuilder().setFilter(filter).build(), + wait, + ordering, + timeout + ); + } + + private ListenableFuture deletePayloadAsync( + String collectionName, + List keys, + @Nullable PointsSelector pointsSelector, + @Nullable Boolean wait, + @Nullable WriteOrderingType ordering, + @Nullable Duration timeout + ) { + DeletePayloadPoints.Builder requestBuilder = DeletePayloadPoints.newBuilder() + .setCollectionName(collectionName) + .setWait(wait == null || wait) + .addAllKeys(keys); + + if (pointsSelector != null) { + requestBuilder.setPointsSelector(pointsSelector); + } + + if (ordering != null) { + requestBuilder.setOrdering(WriteOrdering.newBuilder().setType(ordering).build()); + } + + logger.debug("Delete payload in '{}'", collectionName); + ListenableFuture future = getPoints(timeout).deletePayload(requestBuilder.build()); + addLogFailureCallback(future, "Delete payload"); + return Futures.transform(future, PointsOperationResponse::getResult, MoreExecutors.directExecutor()); + } + + //endregion + + //region Clear Payload + + /** + * Remove all payload for all points. + * + * @param collectionName The name of the collection. + * @param wait Whether to wait until the changes have been applied. Defaults to true. + * @param ordering Write ordering guarantees. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture clearPayloadAsync( + String collectionName, + @Nullable Boolean wait, + @Nullable WriteOrderingType ordering, + @Nullable Duration timeout + ) { + return clearPayloadAsync( + collectionName, + (PointsSelector) null, + wait, + ordering, + timeout + ); + } + + /** + * Removes all payload for the given id. + * + * @param collectionName The name of the collection. + * @param id The id for which to remove the payload. + * @param wait Whether to wait until the changes have been applied. Defaults to true. + * @param ordering Write ordering guarantees. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture clearPayloadAsync( + String collectionName, + PointId id, + @Nullable Boolean wait, + @Nullable WriteOrderingType ordering, + @Nullable Duration timeout + ) { + return clearPayloadAsync( + collectionName, + PointsSelector.newBuilder().setPoints(PointsIdsList.newBuilder().addIds(id).build()).build(), + wait, + ordering, + timeout + ); + } + + /** + * Removes all payload for the given ids. + * + * @param collectionName The name of the collection. + * @param ids The ids for which to remove the payload. + * @param wait Whether to wait until the changes have been applied. Defaults to true. + * @param ordering Write ordering guarantees. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture clearPayloadAsync( + String collectionName, + List ids, + @Nullable Boolean wait, + @Nullable WriteOrderingType ordering, + @Nullable Duration timeout + ) { + return clearPayloadAsync( + collectionName, + PointsSelector.newBuilder().setPoints(PointsIdsList.newBuilder().addAllIds(ids).build()).build(), + wait, + ordering, + timeout + ); + } + + /** + * Removes all payload for the given ids. + * + * @param collectionName The name of the collection. + * @param filter A filter selecting the points for which to remove the payload. + * @param wait Whether to wait until the changes have been applied. Defaults to true. + * @param ordering Write ordering guarantees. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture clearPayloadAsync( + String collectionName, + Filter filter, + @Nullable Boolean wait, + @Nullable WriteOrderingType ordering, + @Nullable Duration timeout + ) { + return clearPayloadAsync( + collectionName, + PointsSelector.newBuilder().setFilter(filter).build(), + wait, + ordering, + timeout + ); + } + + private ListenableFuture clearPayloadAsync( + String collectionName, + @Nullable PointsSelector pointsSelector, + @Nullable Boolean wait, + @Nullable WriteOrderingType ordering, + @Nullable Duration timeout + ) { + ClearPayloadPoints.Builder requestBuilder = ClearPayloadPoints.newBuilder() + .setCollectionName(collectionName) + .setWait(wait == null || wait); + + if (pointsSelector != null) { + requestBuilder.setPoints(pointsSelector); + } + + if (ordering != null) { + requestBuilder.setOrdering(WriteOrdering.newBuilder().setType(ordering).build()); + } + + logger.debug("Clear payload in '{}'", collectionName); + ListenableFuture future = getPoints(timeout).clearPayload(requestBuilder.build()); + addLogFailureCallback(future, "Clear payload"); + return Futures.transform(future, PointsOperationResponse::getResult, MoreExecutors.directExecutor()); + } + + //endregion + + /** + * Creates a payload field index in a collection. + * + * @param collectionName The name of the collection. + * @param field The field name to index. + * @param schemaType The schema type of the field. + * @param indexParams Payload index parameters. + * @param wait Whether to wait until the changes have been applied. Defaults to true. + * @param ordering Write ordering guarantees. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture createPayloadIndexAsync( + String collectionName, + String field, + PayloadSchemaType schemaType, + @Nullable PayloadIndexParams indexParams, + @Nullable Boolean wait, + @Nullable WriteOrderingType ordering, + @Nullable Duration timeout + ) { + CreateFieldIndexCollection.Builder requestBuilder = CreateFieldIndexCollection.newBuilder() + .setCollectionName(collectionName) + .setFieldName(field) + .setWait(wait == null || wait); + + switch (schemaType) { + case Keyword: + requestBuilder.setFieldType(FieldType.FieldTypeKeyword); + break; + case Integer: + requestBuilder.setFieldType(FieldType.FieldTypeInteger); + break; + case Float: + requestBuilder.setFieldType(FieldType.FieldTypeFloat); + break; + case Geo: + requestBuilder.setFieldType(FieldType.FieldTypeGeo); + break; + case Text: + requestBuilder.setFieldType(FieldType.FieldTypeText); + break; + case Bool: + requestBuilder.setFieldType(FieldType.FieldTypeBool); + break; + default: + throw new IllegalArgumentException("Invalid schemaType: '" + schemaType + "'"); + } + + if (indexParams != null) { + requestBuilder.setFieldIndexParams(indexParams); + } + + if (ordering != null) { + requestBuilder.setOrdering(WriteOrdering.newBuilder().setType(ordering).build()); + } + + logger.debug("Create payload field index for '{}' in '{}'", field, collectionName); + ListenableFuture future = getPoints(timeout).createFieldIndex(requestBuilder.build()); + addLogFailureCallback(future, "Create payload field index"); + return Futures.transform(future, PointsOperationResponse::getResult, MoreExecutors.directExecutor()); + } + + /** + * Deletes a payload field index in a collection. + * + * @param collectionName The name of the collection. + * @param field The field name to index. + * @param wait Whether to wait until the changes have been applied. Defaults to true. + * @param ordering Write ordering guarantees. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture deletePayloadIndexAsync( + String collectionName, + String field, + @Nullable Boolean wait, + @Nullable WriteOrderingType ordering, + @Nullable Duration timeout + ) { + DeleteFieldIndexCollection.Builder requestBuilder = DeleteFieldIndexCollection.newBuilder() + .setCollectionName(collectionName) + .setFieldName(field) + .setWait(wait == null || wait); + + if (ordering != null) { + requestBuilder.setOrdering(WriteOrdering.newBuilder().setType(ordering).build()); + } + + logger.debug("Delete payload field index for '{}' in '{}'", field, collectionName); + ListenableFuture future = getPoints(timeout).deleteFieldIndex(requestBuilder.build()); + addLogFailureCallback(future, "Delete payload field index"); + return Futures.transform(future, PointsOperationResponse::getResult, MoreExecutors.directExecutor()); + } + + /** + * Retrieves closest points based on vector similarity and the given filtering conditions. + * + * @param request the search request + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture> searchAsync(SearchPoints request) { + return searchAsync(request, null); + } + + /** + * Retrieves closest points based on vector similarity and the given filtering conditions. + * + * @param request the search request + * @param timeout the timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture> searchAsync(SearchPoints request, @Nullable Duration timeout) { + Preconditions.checkArgument( + !request.getCollectionName().isEmpty(), + "Collection name must not be empty"); + Preconditions.checkArgument( + !request.getVectorList().isEmpty(), + "Vector must not be empty"); + + logger.debug("Search on '{}'", request.getCollectionName()); + ListenableFuture future = getPoints(timeout).search(request); + addLogFailureCallback(future, "Search"); + return Futures.transform(future, SearchResponse::getResultList, MoreExecutors.directExecutor()); + } + + /** + * Retrieves closest points based on vector similarity and the given filtering conditions. + * + * @param collectionName The name of the collection + * @param searches The searches to be performed in the batch. + * @param readConsistency Options for specifying read consistency guarantees. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture> searchBatchAsync( + String collectionName, + List searches, + @Nullable ReadConsistency readConsistency + ) { + return searchBatchAsync(collectionName, searches, readConsistency, null); + } + + /** + * Retrieves closest points based on vector similarity and the given filtering conditions. + * + * @param collectionName The name of the collection + * @param searches The searches to be performed in the batch. + * @param readConsistency Options for specifying read consistency guarantees. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture> searchBatchAsync( + String collectionName, + List searches, + @Nullable ReadConsistency readConsistency, + @Nullable Duration timeout + ) { + // TODO: Workaround for https://github.com/qdrant/qdrant/issues/2880 + searches = Lists.transform( + searches, + searchPoints -> searchPoints.toBuilder().setCollectionName(collectionName).build()); + + SearchBatchPoints.Builder requestBuilder = SearchBatchPoints.newBuilder() + .setCollectionName(collectionName) + .addAllSearchPoints(searches); + + if (readConsistency != null) { + requestBuilder.setReadConsistency(readConsistency); + } + + logger.debug("Search batch on '{}'", collectionName); + ListenableFuture future = getPoints(timeout).searchBatch(requestBuilder.build()); + addLogFailureCallback(future, "Search batch"); + return Futures.transform(future, SearchBatchResponse::getResultList, MoreExecutors.directExecutor()); + } + + /** + * Retrieves closest points based on vector similarity and the given filtering conditions, grouped by a given field. + * + * @param request The search group request + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture> searchGroupsAsync(SearchPointGroups request) { + return searchGroupsAsync(request, null); + } + + /** + * Retrieves closest points based on vector similarity and the given filtering conditions, grouped by a given field. + * + * @param request The search group request + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture> searchGroupsAsync(SearchPointGroups request, @Nullable Duration timeout) { + Preconditions.checkArgument( + !request.getCollectionName().isEmpty(), + "Collection name must not be empty"); + logger.debug("Search groups on '{}'", request.getCollectionName()); + ListenableFuture future = getPoints(timeout).searchGroups(request); + addLogFailureCallback(future, "Search groups"); + return Futures.transform( + future, + response -> response.getResult().getGroupsList(), + MoreExecutors.directExecutor()); + } + + /** + * Iterates over all or filtered points. + * + * @param request The scroll request + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture scrollAsync(ScrollPoints request) { + return scrollAsync(request, null); + } + + /** + * Iterates over all or filtered points. + * + * @param request The scroll request. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture scrollAsync(ScrollPoints request, @Nullable Duration timeout) { + Preconditions.checkArgument( + !request.getCollectionName().isEmpty(), + "Collection name must not be empty"); + logger.debug("Scroll on '{}'", request.getCollectionName()); + ListenableFuture future = getPoints(timeout).scroll(request); + addLogFailureCallback(future, "Scroll"); + return future; + } + + /** + * Look for the points which are closer to stored positive examples and at the same time further to negative + * examples. + * + * @param request The recommend request + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture> recommendAsync(RecommendPoints request) { + return recommendAsync(request, null); + } + + /** + * Look for the points which are closer to stored positive examples and at the same time further to negative + * examples. + * + * @param request The recommend request. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture> recommendAsync(RecommendPoints request, @Nullable Duration timeout) { + Preconditions.checkArgument( + !request.getCollectionName().isEmpty(), + "Collection name must not be empty"); + logger.debug("Recommend on '{}'", request.getCollectionName()); + ListenableFuture future = getPoints(timeout).recommend(request); + addLogFailureCallback(future, "Recommend"); + return Futures.transform( + future, + RecommendResponse::getResultList, + MoreExecutors.directExecutor()); + } + + /** + * Look for the points which are closer to stored positive examples and at the same time further to negative + * examples. + * + * @param collectionName The name of the collection. + * @param recommendSearches The list of recommendation searches. + * @param readConsistency Options for specifying read consistency guarantees. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture> recommendBatchAsync( + String collectionName, + List recommendSearches, + @Nullable ReadConsistency readConsistency) { + return recommendBatchAsync(collectionName, recommendSearches, readConsistency, null); + } + + /** + * Look for the points which are closer to stored positive examples and at the same time further to negative + * examples. + * + * @param collectionName The name of the collection. + * @param recommendSearches The list of recommendation searches. + * @param readConsistency Options for specifying read consistency guarantees. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture> recommendBatchAsync( + String collectionName, + List recommendSearches, + @Nullable ReadConsistency readConsistency, + @Nullable Duration timeout) { + Preconditions.checkArgument(!collectionName.isEmpty(), "Collection name must not be empty"); + + // TODO: Workaround for https://github.com/qdrant/qdrant/issues/2880 + recommendSearches = Lists.transform( + recommendSearches, + recommendPoints -> recommendPoints.toBuilder().setCollectionName(collectionName).build()); + + RecommendBatchPoints.Builder requestBuilder = RecommendBatchPoints.newBuilder() + .setCollectionName(collectionName) + .addAllRecommendPoints(recommendSearches); + + if (readConsistency != null) { + requestBuilder.setReadConsistency(readConsistency); + } + + logger.debug("Recommend batch on '{}'", collectionName); + ListenableFuture future = getPoints(timeout).recommendBatch(requestBuilder.build()); + addLogFailureCallback(future, "Recommend batch"); + return Futures.transform( + future, + RecommendBatchResponse::getResultList, + MoreExecutors.directExecutor()); + } + + /** + * Look for the points which are closer to stored positive examples and at the same time further to negative + * examples, grouped by a given field + * + * @param request The recommend groups request + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture> recommendGroupsAsync(RecommendPointGroups request) { + return recommendGroupsAsync(request, null); + } + + /** + * Look for the points which are closer to stored positive examples and at the same time further to negative + * examples, grouped by a given field + * + * @param request The recommend groups request + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture> recommendGroupsAsync(RecommendPointGroups request, @Nullable Duration timeout) { + String collectionName = request.getCollectionName(); + Preconditions.checkArgument(!collectionName.isEmpty(), "Collection name must not be empty"); + logger.debug("Recommend groups on '{}'", collectionName); + ListenableFuture future = getPoints(timeout).recommendGroups(request); + addLogFailureCallback(future, "Recommend groups"); + return Futures.transform( + future, + response -> response.getResult().getGroupsList(), + MoreExecutors.directExecutor()); + } + + /** + * Count the points in a collection. The count is exact + * + * @param collectionName The name of the collection. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture countAsync(String collectionName) { + return countAsync(collectionName, null, null, null); + } + + /** + * Count the points in a collection. The count is exact + * + * @param collectionName The name of the collection. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture countAsync(String collectionName, @Nullable Duration timeout) { + return countAsync(collectionName, null, null, timeout); + } + + /** + * Count the points in a collection with the given filtering conditions. + * + * @param collectionName The name of the collection. + * @param filter Filter conditions - return only those points that satisfy the specified conditions. + * @param exact If true, returns the exact count, + * if false, returns an approximate count. Defaults to true. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture countAsync( + String collectionName, + @Nullable Filter filter, + @Nullable Boolean exact) { + return countAsync(collectionName, filter, exact, null); + } + + /** + * Count the points in a collection with the given filtering conditions. + * + * @param collectionName The name of the collection. + * @param filter Filter conditions - return only those points that satisfy the specified conditions. + * @param exact If true, returns the exact count, + * if false, returns an approximate count. Defaults to true. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture countAsync( + String collectionName, + @Nullable Filter filter, + @Nullable Boolean exact, + @Nullable Duration timeout) { + Preconditions.checkArgument(!collectionName.isEmpty(), "Collection name must not be empty"); + CountPoints.Builder requestBuilder = CountPoints.newBuilder() + .setCollectionName(collectionName) + .setExact(exact == null || exact); + + if (filter != null) { + requestBuilder.setFilter(filter); + } + + logger.debug("Count on '{}'", collectionName); + ListenableFuture future = getPoints(timeout).count(requestBuilder.build()); + addLogFailureCallback(future, "Count"); + return Futures.transform(future, response -> response.getResult().getCount(), MoreExecutors.directExecutor()); + } + + //region Snapshot Management + + /** + * Create snapshot for a given collection. + * + * @param collectionName The name of the collection. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture createSnapshotAsync(String collectionName) { + return createSnapshotAsync(collectionName, null); + } + + /** + * Create snapshot for a given collection. + * + * @param collectionName The name of the collection. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture createSnapshotAsync(String collectionName, @Nullable Duration timeout) { + Preconditions.checkArgument(!collectionName.isEmpty(), "Collection name must not be empty"); + logger.debug("Create snapshot of '{}'", collectionName); + ListenableFuture future = getSnapshots(timeout).create( + CreateSnapshotRequest.newBuilder() + .setCollectionName(collectionName) + .build()); + addLogFailureCallback(future, "Create snapshot"); + return Futures.transform(future, CreateSnapshotResponse::getSnapshotDescription, MoreExecutors.directExecutor()); + } + + /** + * Get list of snapshots for a collection. + * + * @param collectionName The name of the collection. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture> listSnapshotAsync(String collectionName) { + return listSnapshotAsync(collectionName, null); + } + + /** + * Get list of snapshots for a collection. + * + * @param collectionName The name of the collection. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture> listSnapshotAsync(String collectionName, @Nullable Duration timeout) { + Preconditions.checkArgument(!collectionName.isEmpty(), "Collection name must not be empty"); + logger.debug("List snapshots of '{}'", collectionName); + ListenableFuture future = getSnapshots(timeout).list(ListSnapshotsRequest.newBuilder() + .setCollectionName(collectionName) + .build()); + addLogFailureCallback(future, "List snapshots"); + return Futures.transform(future, ListSnapshotsResponse::getSnapshotDescriptionsList, MoreExecutors.directExecutor()); + } + + /** + * Delete snapshot for a given collection. + * + * @param collectionName The name of the collection. + * @param snapshotName The name of the snapshot. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture deleteSnapshotAsync(String collectionName, String snapshotName) { + return deleteSnapshotAsync(collectionName, snapshotName, null); + } + + /** + * Delete snapshot for a given collection. + * + * @param collectionName The name of the collection. + * @param snapshotName The name of the snapshot. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture deleteSnapshotAsync(String collectionName, String snapshotName, @Nullable Duration timeout) { + Preconditions.checkArgument(!collectionName.isEmpty(), "Collection name must not be empty"); + Preconditions.checkArgument(!snapshotName.isEmpty(), "Snapshot name must not be empty"); + logger.debug("Delete snapshot '{}' of '{}'", snapshotName, collectionName); + ListenableFuture future = getSnapshots(timeout).delete(DeleteSnapshotRequest.newBuilder() + .setCollectionName(collectionName) + .setSnapshotName(snapshotName) + .build()); + addLogFailureCallback(future, "Delete snapshot"); + return future; + } + + /** + * Create snapshot for a whole storage. + * + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture createFullSnapshotAsync() { + return createFullSnapshotAsync(null); + } + + /** + * Create snapshot for a whole storage. + * + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture createFullSnapshotAsync(@Nullable Duration timeout) { + logger.debug("Create full snapshot for a whole storage"); + ListenableFuture future = + getSnapshots(timeout).createFull(CreateFullSnapshotRequest.getDefaultInstance()); + addLogFailureCallback(future, "Create full snapshot"); + return Futures.transform(future, CreateSnapshotResponse::getSnapshotDescription, MoreExecutors.directExecutor()); + } + + /** + * Get list of snapshots for a whole storage. + * + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture> listFullSnapshotAsync() { + return listFullSnapshotAsync(null); + } + + /** + * Get list of snapshots for a whole storage. + * + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture> listFullSnapshotAsync(@Nullable Duration timeout) { + logger.debug("List full snapshots for a whole storage"); + ListenableFuture future = + getSnapshots(timeout).listFull(ListFullSnapshotsRequest.getDefaultInstance()); + addLogFailureCallback(future, "List full snapshots"); + return Futures.transform(future, ListSnapshotsResponse::getSnapshotDescriptionsList, MoreExecutors.directExecutor()); + } + + /** + * Delete snapshot for a whole storage. + * + * @param snapshotName The name of the snapshot. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture deleteFullSnapshotAsync(String snapshotName) { + return deleteFullSnapshotAsync(snapshotName, null); + } + + /** + * Delete snapshot for a whole storage. + * + * @param snapshotName The name of the snapshot. + * @param timeout The timeout for the call. + * @return a new instance of {@link ListenableFuture} + */ + public ListenableFuture deleteFullSnapshotAsync(String snapshotName, @Nullable Duration timeout) { + Preconditions.checkArgument(!snapshotName.isEmpty(), "Snapshot name must not be empty"); + logger.debug("Delete full snapshot '{}'", snapshotName); + ListenableFuture future = getSnapshots(timeout).deleteFull(DeleteFullSnapshotRequest.newBuilder() + .setSnapshotName(snapshotName) + .build()); + addLogFailureCallback(future, "Delete full snapshot"); + return future; + } + + //endregion + + @Override + public void close() { + grpcClient.close(); + } + + private void addLogFailureCallback(ListenableFuture future, String message) { + Futures.addCallback(future, new FutureCallback() { + @Override + public void onSuccess(V result) { + } + + @Override + public void onFailure(Throwable t) { + logger.error(message + " operation failed", t); + } + }, MoreExecutors.directExecutor()); + } + + private CollectionsGrpc.CollectionsFutureStub getCollections(@Nullable Duration timeout) { + return timeout != null + ? this.grpcClient.collections().withDeadlineAfter(timeout.toMillis(), TimeUnit.MILLISECONDS) + : this.grpcClient.collections(); + } + + private PointsGrpc.PointsFutureStub getPoints(@Nullable Duration timeout) { + return timeout != null + ? this.grpcClient.points().withDeadlineAfter(timeout.toMillis(), TimeUnit.MILLISECONDS) + : this.grpcClient.points(); + } + + private SnapshotsGrpc.SnapshotsFutureStub getSnapshots(@Nullable Duration timeout) { + return timeout != null + ? this.grpcClient.snapshots().withDeadlineAfter(timeout.toMillis(), TimeUnit.MILLISECONDS) + : this.grpcClient.snapshots(); + } } diff --git a/src/main/java/io/qdrant/client/QdrantException.java b/src/main/java/io/qdrant/client/QdrantException.java new file mode 100644 index 00000000..740b0bf5 --- /dev/null +++ b/src/main/java/io/qdrant/client/QdrantException.java @@ -0,0 +1,14 @@ +package io.qdrant.client; + +/** + * An exception when interacting with qdrant + */ +public class QdrantException extends RuntimeException { + /** + * Instantiates a new instance of {@link QdrantException} + * @param message The exception message + */ + public QdrantException(String message) { + super(message); + } +} diff --git a/src/main/java/io/qdrant/client/QdrantGrpcClient.java b/src/main/java/io/qdrant/client/QdrantGrpcClient.java new file mode 100644 index 00000000..0015ddb6 --- /dev/null +++ b/src/main/java/io/qdrant/client/QdrantGrpcClient.java @@ -0,0 +1,219 @@ +package io.qdrant.client; + +import io.qdrant.client.grpc.CollectionsGrpc; +import io.qdrant.client.grpc.PointsGrpc; +import io.qdrant.client.grpc.QdrantGrpc; +import io.qdrant.client.grpc.QdrantGrpc.QdrantFutureStub; +import io.qdrant.client.grpc.SnapshotsGrpc; +import io.grpc.CallCredentials; +import io.grpc.Deadline; +import io.grpc.ManagedChannel; +import io.grpc.ManagedChannelBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; +import java.time.Duration; +import java.util.concurrent.TimeUnit; + +import static io.qdrant.client.grpc.CollectionsGrpc.CollectionsFutureStub; +import static io.qdrant.client.grpc.PointsGrpc.PointsFutureStub; +import static io.qdrant.client.grpc.SnapshotsGrpc.SnapshotsFutureStub; + +/** + * Low-level gRPC client for qdrant vector database. + */ +public class QdrantGrpcClient implements AutoCloseable { + private static final Logger logger = LoggerFactory.getLogger(QdrantGrpcClient.class); + @Nullable + private final CallCredentials callCredentials; + private final ManagedChannel channel; + private final boolean shutdownChannelOnClose; + @Nullable + private final Duration timeout; + + QdrantGrpcClient( + ManagedChannel channel, + boolean shutdownChannelOnClose, + @Nullable CallCredentials callCredentials, + @Nullable Duration timeout) { + this.callCredentials = callCredentials; + this.channel = channel; + this.shutdownChannelOnClose = shutdownChannelOnClose; + this.timeout = timeout; + } + + /** + * Creates a new builder to build a client. + * @param channel The channel for communication. This channel is not shutdown by the client and must be managed + * by the caller. + * @return a new instance of {@link Builder} + */ + public static Builder newBuilder(ManagedChannel channel) { + return new Builder(channel, false); + } + + /** + * Creates a new builder to build a client. + * @param channel The channel for communication. + * @param shutdownChannelOnClose Whether the channel is shutdown on client close. + * @return a new instance of {@link Builder} + */ + public static Builder newBuilder(ManagedChannel channel, boolean shutdownChannelOnClose) { + return new Builder(channel, shutdownChannelOnClose); + } + + /** + * Creates a new builder to build a client. + * @param host The host to connect to. The default gRPC port 6334 is used. + * @return a new instance of {@link Builder} + */ + public static Builder newBuilder(String host) { + return new Builder(host, 6334, true); + } + + /** + * Creates a new builder to build a client. The client uses Transport Layer Security by default. + * @param host The host to connect to. + * @param port The port to connect to. + * @return a new instance of {@link Builder} + */ + public static Builder newBuilder(String host, int port) { + return new Builder(host, port, true); + } + + /** + * Creates a new builder to build a client. + * @param host The host to connect to. + * @param port The port to connect to. + * @param useTransportLayerSecurity Whether the client uses Transport Layer Security (TLS) to secure communications. + * Running without TLS should only be used for testing purposes. + * @return a new instance of {@link Builder} + */ + public static Builder newBuilder(String host, int port, boolean useTransportLayerSecurity) { + return new Builder(host, port, useTransportLayerSecurity); + } + + /** + * Gets the client for qdrant services + * @return a new instance of {@link QdrantFutureStub} + */ + public QdrantGrpc.QdrantFutureStub qdrant() { + return QdrantGrpc.newFutureStub(channel) + .withCallCredentials(callCredentials) + .withDeadline(timeout != null ? Deadline.after(timeout.toMillis(), TimeUnit.MILLISECONDS) : null); + } + + /** + * Gets the client for points + * @return a new instance of {@link PointsFutureStub} + */ + public PointsFutureStub points() { + return PointsGrpc.newFutureStub(channel) + .withCallCredentials(callCredentials) + .withDeadline(timeout != null ? Deadline.after(timeout.toMillis(), TimeUnit.MILLISECONDS) : null); + } + + /** + * Gets the client for collections + * @return a new instance of {@link CollectionsFutureStub} + */ + public CollectionsFutureStub collections() { + return CollectionsGrpc.newFutureStub(channel) + .withCallCredentials(callCredentials) + .withDeadline(timeout != null ? Deadline.after(timeout.toMillis(), TimeUnit.MILLISECONDS) : null); + } + + /** + * Gets the client for snapshots + * @return a new instance of {@link SnapshotsFutureStub} + */ + public SnapshotsFutureStub snapshots() { + return SnapshotsGrpc.newFutureStub(channel) + .withCallCredentials(callCredentials) + .withDeadline(timeout != null ? Deadline.after(timeout.toMillis(), TimeUnit.MILLISECONDS) : null); + } + + @Override + public void close() { + if (shutdownChannelOnClose && !channel.isShutdown() && !channel.isTerminated()) { + try { + channel.shutdown().awaitTermination(5, TimeUnit.SECONDS); + } catch (InterruptedException e) { + logger.warn("exception thrown when shutting down channel", e); + } + } + } + + /** + * builder for {@link QdrantGrpcClient} + */ + public static class Builder { + private final ManagedChannel channel; + private final boolean shutdownChannelOnClose; + @Nullable + private CallCredentials callCredentials; + @Nullable + private Duration timeout; + + Builder(ManagedChannel channel, boolean shutdownChannelOnClose) { + this.channel = channel; + this.shutdownChannelOnClose = shutdownChannelOnClose; + } + + Builder(String host, int port, boolean useTransportLayerSecurity) { + this.channel = createChannel(host, port, useTransportLayerSecurity); + this.shutdownChannelOnClose = true; + } + + /** + * Sets the API key to use for authentication + * @param apiKey The API key to use. + * @return this + */ + public Builder withApiKey(String apiKey) { + this.callCredentials = new ApiKeyCredentials(apiKey); + return this; + } + + /** + * Sets a default timeout for all requests. + * @param timeout The timeout. + * @return this + */ + public Builder withTimeout(@Nullable Duration timeout) { + this.timeout = timeout; + return this; + } + + /** + * Sets the credential data that will be propagated to the server via request metadata for each RPC. + * @param callCredentials The call credentials to use. + * @return this + */ + public Builder withCallCredentials(@Nullable CallCredentials callCredentials) { + this.callCredentials = callCredentials; + return this; + } + + /** + * Builds a new instance of {@link QdrantGrpcClient} + * @return a new instance of {@link QdrantGrpcClient} + */ + public QdrantGrpcClient build() { + return new QdrantGrpcClient(channel, shutdownChannelOnClose, callCredentials, timeout); + } + + private static ManagedChannel createChannel(String host, int port, boolean useTransportLayerSecurity) { + ManagedChannelBuilder channelBuilder = ManagedChannelBuilder.forAddress(host, port); + + if (useTransportLayerSecurity) { + channelBuilder.useTransportSecurity(); + } else { + channelBuilder.usePlaintext(); + } + + return channelBuilder.build(); + } + } +} diff --git a/src/main/java/io/qdrant/client/TokenInterceptor.java b/src/main/java/io/qdrant/client/TokenInterceptor.java deleted file mode 100644 index f735ed42..00000000 --- a/src/main/java/io/qdrant/client/TokenInterceptor.java +++ /dev/null @@ -1,37 +0,0 @@ -package io.qdrant.client; - -import io.grpc.CallOptions; -import io.grpc.Channel; -import io.grpc.ClientCall; -import io.grpc.ClientInterceptor; -import io.grpc.ForwardingClientCall.SimpleForwardingClientCall; -import io.grpc.Metadata; -import io.grpc.MethodDescriptor; - -/** Interceptor for adding an API key to the headers of gRPC requests. */ -final class TokenInterceptor implements ClientInterceptor { - private final String apiKey; - private final Metadata.Key API_KEY = - Metadata.Key.of("api-key", Metadata.ASCII_STRING_MARSHALLER); - - /** - * Constructs a new TokenInterceptor with the specified API key. - * - * @param apiKey the API key to be added to the headers - */ - TokenInterceptor(String apiKey) { - this.apiKey = apiKey; - } - - @Override - public ClientCall interceptCall( - MethodDescriptor method, CallOptions callOptions, Channel next) { - return new SimpleForwardingClientCall(next.newCall(method, callOptions)) { - @Override - public void start(Listener responseListener, Metadata headers) { - headers.put(API_KEY, apiKey); - super.start(responseListener, headers); - } - }; - } -} diff --git a/src/main/java/io/qdrant/client/ValueFactory.java b/src/main/java/io/qdrant/client/ValueFactory.java new file mode 100644 index 00000000..f2264cd2 --- /dev/null +++ b/src/main/java/io/qdrant/client/ValueFactory.java @@ -0,0 +1,70 @@ +package io.qdrant.client; + +import java.util.List; + +import static io.qdrant.client.grpc.JsonWithInt.ListValue; +import static io.qdrant.client.grpc.JsonWithInt.NullValue; +import static io.qdrant.client.grpc.JsonWithInt.Value; + +/** + * Convenience methods for constructing {@link Value} + */ +public final class ValueFactory { + private ValueFactory() { + } + + /** + * Creates a value from a {@link String} + * @param value The value + * @return a new instance of {@link io.qdrant.client.grpc.JsonWithInt.Value} + */ + public static Value value(String value) { + return Value.newBuilder().setStringValue(value).build(); + } + + /** + * Creates a value from a {@link long} + * @param value The value + * @return a new instance of {@link io.qdrant.client.grpc.JsonWithInt.Value} + */ + public static Value value(long value) { + return Value.newBuilder().setIntegerValue(value).build(); + } + + /** + * Creates a value from a {@link double} + * @param value The value + * @return a new instance of {@link io.qdrant.client.grpc.JsonWithInt.Value} + */ + public static Value value(double value) { + return Value.newBuilder().setDoubleValue(value).build(); + } + + /** + * Creates a value from a {@link boolean} + * @param value The value + * @return a new instance of {@link io.qdrant.client.grpc.JsonWithInt.Value} + */ + public static Value value(boolean value) { + return Value.newBuilder().setBoolValue(value).build(); + } + + /** + * Creates a null value + * @return a new instance of {@link io.qdrant.client.grpc.JsonWithInt.Value} + */ + public static Value nullValue() { + return Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build(); + } + + /** + * Creates a value from a list of values + * @param values The list of values + * @return a new instance of {@link io.qdrant.client.grpc.JsonWithInt.Value} + */ + public static Value list(List values) { + return Value.newBuilder() + .setListValue(ListValue.newBuilder().addAllValues(values).build()) + .build(); + } +} diff --git a/src/main/java/io/qdrant/client/VectorsFactory.java b/src/main/java/io/qdrant/client/VectorsFactory.java new file mode 100644 index 00000000..0e2c3486 --- /dev/null +++ b/src/main/java/io/qdrant/client/VectorsFactory.java @@ -0,0 +1,60 @@ +package io.qdrant.client; + +import com.google.common.collect.Maps; +import com.google.common.primitives.Floats; + +import java.util.List; +import java.util.Map; + +import static io.qdrant.client.grpc.Points.NamedVectors; +import static io.qdrant.client.grpc.Points.Vector; +import static io.qdrant.client.grpc.Points.Vectors; + +/** + * Convenience methods for constructing {@link Vectors} + */ +public final class VectorsFactory { + private VectorsFactory() { + } + + /** + * Creates named vectors + * @param values A map of vector names to values + * @return a new instance of {@link Vectors} + */ + public static Vectors namedVectors(Map> values) { + return Vectors.newBuilder() + .setVectors(NamedVectors.newBuilder() + .putAllVectors(Maps.transformValues(values, v -> Vector.newBuilder() + .addAllData(v) + .build())) + ) + .build(); + } + + /** + * Creates a vector + * @param values A list of values + * @return a new instance of {@link Vectors} + */ + public static Vectors vector(List values) { + return Vectors.newBuilder() + .setVector(Vector.newBuilder() + .addAllData(values) + .build()) + .build(); + } + + /** + * Creates a vector + * @param values A list of values + * @return a new instance of {@link Vectors} + */ + public static Vectors vector(float... values) { + return Vectors.newBuilder() + .setVector(Vector.newBuilder() + .addAllData(Floats.asList(values)) + .build()) + .build(); + } +} diff --git a/src/main/java/io/qdrant/client/WithPayloadSelectorFactory.java b/src/main/java/io/qdrant/client/WithPayloadSelectorFactory.java new file mode 100644 index 00000000..02c61363 --- /dev/null +++ b/src/main/java/io/qdrant/client/WithPayloadSelectorFactory.java @@ -0,0 +1,46 @@ +package io.qdrant.client; + +import java.util.List; + +import static io.qdrant.client.grpc.Points.PayloadExcludeSelector; +import static io.qdrant.client.grpc.Points.PayloadIncludeSelector; +import static io.qdrant.client.grpc.Points.WithPayloadSelector; + +/** + * Convenience methods for constructing {@link WithPayloadSelector} + */ +public final class WithPayloadSelectorFactory { + private WithPayloadSelectorFactory() { + } + + /** + * Whether to include all payload in response. + * @param enable if true, to include all payload, if false, none. + * @return a new instance of {@link WithPayloadSelector} + */ + public static WithPayloadSelector enable(boolean enable) { + return WithPayloadSelector.newBuilder().setEnable(enable).build(); + } + + /** + * Which payload fields to include in response. + * @param fields the list of fields to include. + * @return a new instance of {@link WithPayloadSelector} + */ + public static WithPayloadSelector include(List fields) { + return WithPayloadSelector.newBuilder() + .setInclude(PayloadIncludeSelector.newBuilder().addAllFields(fields).build()) + .build(); + } + + /** + * Which payload fields to exclude in response. + * @param fields the list of fields to exclude. + * @return a new instance of {@link WithPayloadSelector} + */ + public static WithPayloadSelector exclude(List fields) { + return WithPayloadSelector.newBuilder() + .setExclude(PayloadExcludeSelector.newBuilder().addAllFields(fields).build()) + .build(); + } +} diff --git a/src/main/java/io/qdrant/client/WithVectorsSelectorFactory.java b/src/main/java/io/qdrant/client/WithVectorsSelectorFactory.java new file mode 100644 index 00000000..60cb69ad --- /dev/null +++ b/src/main/java/io/qdrant/client/WithVectorsSelectorFactory.java @@ -0,0 +1,35 @@ +package io.qdrant.client; + +import io.qdrant.client.grpc.Points; + +import java.util.List; + +import static io.qdrant.client.grpc.Points.WithVectorsSelector; + +/** + * Convenience methods for constructing {@link WithVectorsSelector} + */ +public final class WithVectorsSelectorFactory { + private WithVectorsSelectorFactory() { + } + + /** + * Whether to include vectors in response. + * @param enable if true, to include vectors, if false, none. + * @return a new instance of {@link WithVectorsSelector} + */ + public static WithVectorsSelector enable(boolean enable) { + return WithVectorsSelector.newBuilder().setEnable(enable).build(); + } + + /** + * List of named vectors to include in response. + * @param names The names of vectors. + * @return a new instance of {@link WithVectorsSelector} + */ + public static WithVectorsSelector include(List names) { + return WithVectorsSelector.newBuilder() + .setInclude(Points.VectorsSelector.newBuilder().addAllNames(names)) + .build(); + } +} diff --git a/src/main/java/io/qdrant/client/package-info.java b/src/main/java/io/qdrant/client/package-info.java new file mode 100644 index 00000000..05885bd9 --- /dev/null +++ b/src/main/java/io/qdrant/client/package-info.java @@ -0,0 +1,7 @@ +/** + * package + */ +@ParametersAreNonnullByDefault +package io.qdrant.client; + +import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/src/main/java/io/qdrant/client/utils/FilterUtil.java b/src/main/java/io/qdrant/client/utils/FilterUtil.java deleted file mode 100644 index 8e452f3e..00000000 --- a/src/main/java/io/qdrant/client/utils/FilterUtil.java +++ /dev/null @@ -1,375 +0,0 @@ -package io.qdrant.client.utils; - -import io.qdrant.client.grpc.Points.Condition; -import io.qdrant.client.grpc.Points.FieldCondition; -import io.qdrant.client.grpc.Points.Filter; -import io.qdrant.client.grpc.Points.GeoBoundingBox; -import io.qdrant.client.grpc.Points.GeoLineString; -import io.qdrant.client.grpc.Points.GeoPoint; -import io.qdrant.client.grpc.Points.GeoPolygon; -import io.qdrant.client.grpc.Points.GeoRadius; -import io.qdrant.client.grpc.Points.HasIdCondition; -import io.qdrant.client.grpc.Points.IsEmptyCondition; -import io.qdrant.client.grpc.Points.IsNullCondition; -import io.qdrant.client.grpc.Points.Match; -import io.qdrant.client.grpc.Points.NestedCondition; -import io.qdrant.client.grpc.Points.PointId; -import io.qdrant.client.grpc.Points.Range; -import io.qdrant.client.grpc.Points.RepeatedIntegers; -import io.qdrant.client.grpc.Points.RepeatedStrings; -import io.qdrant.client.grpc.Points.ValuesCount; -import java.util.Arrays; -import java.util.List; - -/** Utility class for creating filter conditions and filters. */ -public class FilterUtil { - - /** - * Creates a {@link Condition} with a {@link FieldCondition} for text matching. - * - * @param key The key of the field. - * @param match The text match criteria. - * @return The created condition. - */ - public static Condition fieldCondition(String key, Match match) { - FieldCondition fieldCondition = FieldCondition.newBuilder().setKey(key).setMatch(match).build(); - - return Condition.newBuilder().setField(fieldCondition).build(); - } - - /** - * Creates a {@link Condition} with a {@link FieldCondition} for range matching. - * - * @param key The key of the field. - * @param range The range criteria. - * @return The created condition. - */ - public static Condition fieldCondition(String key, Range range) { - FieldCondition fieldCondition = FieldCondition.newBuilder().setKey(key).setRange(range).build(); - - return Condition.newBuilder().setField(fieldCondition).build(); - } - - /** - * Creates a {@link Condition} with a {@link FieldCondition} for geo bounding box matching. - * - * @param key The key of the field. - * @param geoBoundingBox The geo bounding box criteria. - * @return The created condition. - */ - public static Condition fieldCondition(String key, GeoBoundingBox geoBoundingBox) { - FieldCondition fieldCondition = - FieldCondition.newBuilder().setKey(key).setGeoBoundingBox(geoBoundingBox).build(); - - return Condition.newBuilder().setField(fieldCondition).build(); - } - - /** - * Creates a {@link Condition} with a {@link FieldCondition} for geo radius matching. - * - * @param key The key of the field. - * @param geoRadius The geo radius criteria. - * @return The created condition. - */ - public static Condition fieldCondition(String key, GeoRadius geoRadius) { - FieldCondition fieldCondition = - FieldCondition.newBuilder().setKey(key).setGeoRadius(geoRadius).build(); - - return Condition.newBuilder().setField(fieldCondition).build(); - } - - /** - * Creates a {@link Condition} with a {@link FieldCondition} for values count matching. - * - * @param key The key of the field. - * @param valuesCount The values count criteria. - * @return The created condition. - */ - public static Condition fieldCondition(String key, ValuesCount valuesCount) { - FieldCondition fieldCondition = - FieldCondition.newBuilder().setKey(key).setValuesCount(valuesCount).build(); - - return Condition.newBuilder().setField(fieldCondition).build(); - } - - /** - * Creates a {@link Condition} with a {@link FieldCondition} for geo polygon matching. - * - * @param key The key of the field. - * @param geoPolygon The geo polygon criteria. - * @return The created condition. - */ - public static Condition fieldCondition(String key, GeoPolygon geoPolygon) { - FieldCondition fieldCondition = - FieldCondition.newBuilder().setKey(key).setGeoPolygon(geoPolygon).build(); - - return Condition.newBuilder().setField(fieldCondition).build(); - } - - /** - * Creates a {@link Match} condition for string matching. If the text contains a space, it is - * considered a full-text match. Else it is considered a keyword match. - * - * @param text The text to match. - * @return The created match condition. - */ - public static Match match(String text) { - return text.contains(" ") - ? Match.newBuilder().setText(text).build() - : Match.newBuilder().setKeyword(text).build(); - } - - /** - * Creates a {@link Match} condition for integer matching. - * - * @param value The integer value to match. - * @return The created match condition. - */ - public static Match match(long value) { - return Match.newBuilder().setInteger(value).build(); - } - - /** - * Creates a {@link Match} condition for boolean matching. - * - * @param value The boolean value to match. - * @return The created match condition. - */ - public static Match match(boolean value) { - return Match.newBuilder().setBoolean(value).build(); - } - - /** - * Creates a {@link Match} condition with a list of keywords for matching. - * - * @param keywords The list of keywords to match. - * @return The created match condition. - */ - public static Match matchWithKeywords(List keywords) { - return Match.newBuilder() - .setKeywords(RepeatedStrings.newBuilder().addAllStrings(keywords).build()) - .build(); - } - - /** - * Creates a {@link Match} condition with a list of integers for matching. - * - * @param integers The list of integers to match. - * @return The created match condition. - */ - public static Match matchWithIntegers(List integers) { - return Match.newBuilder() - .setIntegers(RepeatedIntegers.newBuilder().addAllIntegers(integers).build()) - .build(); - } - - /** - * Creates a {@link GeoBoundingBox} based on the top-left and bottom-right {@link GeoPoint}s. - * - * @param topLeft The top-left point of the bounding box. - * @param bottomRight The bottom-right point of the bounding box. - * @return The created geo bounding box. - */ - public static GeoBoundingBox geoBoundingBox(GeoPoint topLeft, GeoPoint bottomRight) { - return GeoBoundingBox.newBuilder().setTopLeft(topLeft).setBottomRight(bottomRight).build(); - } - - /** - * Creates a {@link GeoRadius} based on the center {@link GeoPoint} and radius. - * - * @param center The center point of the radius. - * @param radius The radius value. - * @return The created geo radius. - */ - public static GeoRadius geoRadius(GeoPoint center, float radius) { - return GeoRadius.newBuilder().setCenter(center).setRadius(radius).build(); - } - - /** - * Creates a {@link GeoPolygon} based on the exterior {@link GeoLineString} and a list of interior - * {@link GeoLineString}s. - * - * @param exterior The exterior line string. - * @param interiors The list of interior line strings. - * @return The created geo polygon. - */ - public static GeoPolygon geoPolygon(GeoLineString exterior, List interiors) { - return GeoPolygon.newBuilder().setExterior(exterior).addAllInteriors(interiors).build(); - } - - /** - * Creates a {@link Range} for numeric range matching. - * - * @param lt The less than value. - * @param gt The greater than value. - * @param gte The greater than or equal to value. - * @param lte The less than or equal to value. - * @return The created range. - */ - public static Range range(double lt, double gt, double gte, double lte) { - return Range.newBuilder().setLt(lt).setGt(gt).setGte(gte).setLte(lte).build(); - } - - /** - * Creates a {@link ValuesCount} for values count matching. - * - * @param lt The less than value. - * @param gt The greater than value. - * @param gte The greater than or equal to value. - * @param lte The less than or equal to value. - * @return The created values count. - */ - public static ValuesCount valuesCount(long lt, long gt, long gte, long lte) { - return ValuesCount.newBuilder().setLt(lt).setGt(gt).setGte(gte).setLte(lte).build(); - } - - /** - * Creates a {@link Condition} with a {@link Filter} as a subcondition. - * - * @param filter The filter criteria. - * @return The created condition. - */ - public static Condition filterCondition(Filter filter) { - return Condition.newBuilder().setFilter(filter).build(); - } - - /** - * Creates a {@link Condition} with a {@link NestedCondition} based on a key and a nested filter. - * - * @param key The key of the nested condition. - * @param filter The nested filter criteria. - * @return The created condition. - */ - public static Condition nestedCondition(String key, Filter filter) { - validateNestedFilter(filter); - - NestedCondition nestedCondition = - NestedCondition.newBuilder().setKey(key).setFilter(filter).build(); - - return Condition.newBuilder().setNested(nestedCondition).build(); - } - - private static void validateNestedFilter(Filter filter) { - validateNoHasId(filter.getMustList()); - validateNoHasId(filter.getMustNotList()); - validateNoHasId(filter.getShouldList()); - } - - private static void validateNoHasId(List conditions) { - for (Condition condition : conditions) { - if (condition.hasHasId()) { - throw new IllegalArgumentException("Nested filter cannot have a HasIdCondition"); - } - } - } - - /** - * Creates a {@link Condition} with an {@link IsEmptyCondition} based on a key for empty condition - * matching. - * - * @param key The key of the field. - * @return The created condition. - */ - public static Condition isEmptyCondition(String key) { - IsEmptyCondition isEmptyCondition = IsEmptyCondition.newBuilder().setKey(key).build(); - - return Condition.newBuilder().setIsEmpty(isEmptyCondition).build(); - } - - /** - * Creates a {@link Condition} with an {@link IsNullCondition} based on a key for null condition - * matching. - * - * @param key The key of the field. - * @return The created condition. - */ - public static Condition isNullCondition(String key) { - IsNullCondition isNullCondition = IsNullCondition.newBuilder().setKey(key).build(); - - return Condition.newBuilder().setIsNull(isNullCondition).build(); - } - - /** - * Creates a {@link Condition} with a {@link HasIdCondition} based on a list of point IDs for ID - * condition matching. - * - * @param pointIds The list of point IDs. - * @return The created condition. - */ - public static Condition hasIdCondition(List pointIds) { - HasIdCondition hasIdCondition = HasIdCondition.newBuilder().addAllHasId(pointIds).build(); - - return Condition.newBuilder().setHasId(hasIdCondition).build(); - } - - /** - * Creates a {@link GeoPoint} based on latitude and longitude values. - * - * @param latitude The latitude value. - * @param longitude The longitude value. - * @return The created geo point. - */ - public static GeoPoint geoPoint(double latitude, double longitude) { - return GeoPoint.newBuilder().setLat(latitude).setLon(longitude).build(); - } - - /** - * Creates a {@link Filter} with "must" conditions. - * - * @param mustConditions The list of "must" conditions. - * @return The created filter. - */ - public static Filter must(Condition... mustConditions) { - return Filter.newBuilder().addAllMust(Arrays.asList(mustConditions)).build(); - } - - /** - * Creates a {@link Filter} with "must not" conditions. - * - * @param mustNotConditions The list of "must not" conditions. - * @return The created filter. - */ - public static Filter mustNot(Condition... mustNotConditions) { - return Filter.newBuilder().addAllMustNot(Arrays.asList(mustNotConditions)).build(); - } - - /** - * Creates a {@link Filter} with "should" conditions. - * - * @param shouldConditions The list of "should" conditions. - * @return The created filter. - */ - public static Filter should(Condition... shouldConditions) { - return Filter.newBuilder().addAllShould(Arrays.asList(shouldConditions)).build(); - } - - /** - * Creates a {@link Filter} with "must" conditions. - * - * @param mustConditions The list of "must" conditions. - * @return The created filter. - */ - public static Filter must(List mustConditions) { - return Filter.newBuilder().addAllMust(mustConditions).build(); - } - - /** - * Creates a {@link Filter} with "must not" conditions. - * - * @param mustNotConditions The list of "must not" conditions. - * @return The created filter. - */ - public static Filter mustNot(List mustNotConditions) { - return Filter.newBuilder().addAllMustNot(mustNotConditions).build(); - } - - /** - * Creates a {@link Filter} with "should" conditions. - * - * @param shouldConditions The list of "should" conditions. - * @return The created filter. - */ - public static Filter should(List shouldConditions) { - return Filter.newBuilder().addAllShould(shouldConditions).build(); - } -} diff --git a/src/main/java/io/qdrant/client/utils/PayloadUtil.java b/src/main/java/io/qdrant/client/utils/PayloadUtil.java deleted file mode 100644 index 01fa6938..00000000 --- a/src/main/java/io/qdrant/client/utils/PayloadUtil.java +++ /dev/null @@ -1,154 +0,0 @@ -package io.qdrant.client.utils; - -import io.qdrant.client.grpc.JsonWithInt.ListValue; -import io.qdrant.client.grpc.JsonWithInt.NullValue; -import io.qdrant.client.grpc.JsonWithInt.Struct; -import io.qdrant.client.grpc.JsonWithInt.Value; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** Utility class for working with Payloads. */ -public class PayloadUtil { - - /** - * Converts a map to a payload struct. - * - * @param inputMap The input map to convert. - * @return The converted payload struct. - */ - public static Struct toPayloadStruct(Map inputMap) { - Struct.Builder structBuilder = Struct.newBuilder(); - Map map = toPayload(inputMap); - structBuilder.putAllFields(map); - return structBuilder.build(); - } - - /** - * Converts a map to a payload map. - * - * @param inputMap The input map to convert. - * @return The converted payload map. - */ - public static Map toPayload(Map inputMap) { - Map map = new HashMap<>(); - for (Map.Entry entry : inputMap.entrySet()) { - String fieldName = entry.getKey(); - Object value = entry.getValue(); - - Value.Builder valueBuilder = Value.newBuilder(); - setValue(valueBuilder, value); - map.put(fieldName, valueBuilder.build()); - } - return map; - } - - /** - * Converts a payload struct to a Java Map. - * - * @param struct The payload struct to convert. - * @return The converted hash map. - */ - public static Map toMap(Struct struct) { - Map structMap = toMap(struct.getFieldsMap()); - return structMap; - } - - /** - * Converts a payload map to a Java Map. - * - * @param payload The payload map to convert. - * @return The converted hash map. - */ - public static Map toMap(Map payload) { - Map hashMap = new HashMap<>(); - for (Map.Entry entry : payload.entrySet()) { - String fieldName = entry.getKey(); - Value fieldValue = entry.getValue(); - - Object value = valueToObject(fieldValue); - hashMap.put(fieldName, value); - } - return hashMap; - } - - /** - * Sets the value of a Value.Builder based on the given object. - * - * @param valueBuilder The Value.Builder to set the value for. - * @param value The object value to set. - */ - static void setValue(Value.Builder valueBuilder, Object value) { - if (value == null) { - valueBuilder.setNullValue(NullValue.NULL_VALUE); - } else if (value instanceof String) { - valueBuilder.setStringValue((String) value); - } else if (value instanceof Integer) { - valueBuilder.setIntegerValue((Integer) value); - } else if (value instanceof Double) { - valueBuilder.setDoubleValue((Double) value); - } else if (value instanceof Boolean) { - valueBuilder.setBoolValue((Boolean) value); - } else if (value instanceof Map) { - valueBuilder.setStructValue(toPayloadStruct((Map) value)); - } else if (value instanceof List) { - valueBuilder.setListValue(listToListValue((List) value)); - } - } - - /** - * Converts a list to a ListValue.Builder. - * - * @param list The list to convert. - * @return The converted ListValue.Builder. - */ - static ListValue.Builder listToListValue(List list) { - ListValue.Builder listValueBuilder = ListValue.newBuilder(); - - for (Object element : list) { - Value.Builder elementBuilder = Value.newBuilder(); - setValue(elementBuilder, element); - listValueBuilder.addValues(elementBuilder.build()); - } - - return listValueBuilder; - } - - /** - * Converts a ListValue to an array of objects. - * - * @param listValue The ListValue to convert. - * @return The converted array of objects. - */ - static Object listValueToList(ListValue listValue) { - return listValue.getValuesList().stream().map(PayloadUtil::valueToObject).toArray(); - } - - /** - * Converts a Value to an object. - * - * @param value The Value to convert. - * @return The converted object. - */ - static Object valueToObject(Value value) { - if (value.hasStringValue()) { - return value.getStringValue(); - } else if (value.hasIntegerValue()) { - // int64 is converted to long - // We need to cast it to int - return (int) value.getIntegerValue(); - } else if (value.hasDoubleValue()) { - return value.getDoubleValue(); - } else if (value.hasBoolValue()) { - return value.getBoolValue(); - } else if (value.hasNullValue()) { - return null; - } else if (value.hasStructValue()) { - return toMap(value.getStructValue()); - } else if (value.hasListValue()) { - return listValueToList(value.getListValue()); - } - - return null; - } -} diff --git a/src/main/java/io/qdrant/client/utils/PointUtil.java b/src/main/java/io/qdrant/client/utils/PointUtil.java deleted file mode 100644 index e74b233f..00000000 --- a/src/main/java/io/qdrant/client/utils/PointUtil.java +++ /dev/null @@ -1,233 +0,0 @@ -package io.qdrant.client.utils; - -import io.qdrant.client.grpc.JsonWithInt.Value; -import io.qdrant.client.grpc.Points.Filter; -import io.qdrant.client.grpc.Points.NamedVectors; -import io.qdrant.client.grpc.Points.PointId; -import io.qdrant.client.grpc.Points.PointStruct; -import io.qdrant.client.grpc.Points.PointsIdsList; -import io.qdrant.client.grpc.Points.PointsSelector; -import io.qdrant.client.grpc.Points.ReadConsistency; -import io.qdrant.client.grpc.Points.ReadConsistencyType; -import io.qdrant.client.grpc.Points.Vector; -import io.qdrant.client.grpc.Points.Vectors; -import io.qdrant.client.grpc.Points.WriteOrdering; -import io.qdrant.client.grpc.Points.WriteOrderingType; -import java.util.Arrays; -import java.util.Map; -import java.util.UUID; -import java.util.stream.Collectors; - -/** Utility class for working with Points. */ -public class PointUtil { - - /** - * Creates a {@link PointsSelector} with point IDs specified as long values. - * - * @param pointIds The array of point IDs. - * @return The created PointsSelector. - */ - public static PointsSelector createPointsSelector(long... pointIds) { - - PointsIdsList pointsIdsList = - PointsIdsList.newBuilder() - .addAllIds( - Arrays.stream(pointIds).mapToObj(PointUtil::pointId).collect(Collectors.toList())) - .build(); - - return PointsSelector.newBuilder().setPoints(pointsIdsList).build(); - } - - /** - * Creates a {@link PointsSelector} with point IDs specified as strings. - * - * @param pointIds The array of point IDs. - * @return The created PointsSelector. - */ - public static PointsSelector createPointsSelector(String... pointIds) { - - // Validating the UUIDs - Arrays.stream(pointIds).forEach((String id) -> UUID.fromString(id)); - - // mapToObj() couldn't resolve the method overloads of createPointId() - // Using map() instead - PointsIdsList pointsIdsList = - PointsIdsList.newBuilder() - .addAllIds( - Arrays.stream(pointIds) - .map((String id) -> PointUtil.pointId(id)) - .collect(Collectors.toList())) - .build(); - - return PointsSelector.newBuilder().setPoints(pointsIdsList).build(); - } - - /** - * Creates a {@link PointsSelector} with point IDs specified as an iterable of {@link PointId}. - * - * @param pointIds The iterable of point IDs. - * @return The created PointsSelector. - */ - public static PointsSelector createPointsSelector(Iterable pointIds) { - PointsIdsList pointsIdsList = PointsIdsList.newBuilder().addAllIds(pointIds).build(); - - return PointsSelector.newBuilder().setPoints(pointsIdsList).build(); - } - - /** - * Creates a {@link PointsSelector} with a filter condition. - * - * @param filter The filter condition. - * @return The created PointsSelector. - */ - public static PointsSelector pointsSelector(Filter filter) { - return PointsSelector.newBuilder().setFilter(filter).build(); - } - - /** - * Creates a {@link PointId} with a long value. - * - * @param num The long value for the point ID. - * @return The created PointId. - * @throws IllegalArgumentException if the provided long value is negative. - */ - public static PointId pointId(long num) { - if (num < 0) { - throw new IllegalArgumentException("Point ID must be an unsigned integer or UUID"); - } - return PointId.newBuilder().setNum(num).build(); - } - - /** - * Creates a {@link PointId} with a string representing a UUID. - * - * @param uuid The string representation of the UUID. - * @return The created PointId. - * @throws IllegalArgumentException if the provided string is not a valid UUID. - */ - public static PointId pointId(String uuid) { - UUID.fromString(uuid); // Throws IllegalArgumentException if the string is not a valid UUID - return PointId.newBuilder().setUuid(uuid).build(); - } - - /** - * Creates a {@link PointId} with a {@link UUID}. - * - * @param uuid The UUID. - * @return The created PointId. - */ - public static PointId pointId(UUID uuid) { - return PointId.newBuilder().setUuid(uuid.toString()).build(); - } - - /** - * Creates a {@link PointStruct} with a UUID, a {@link Vector}, and payload data. - * - * @param uuid The UUID. - * @param vector The vector data. - * @param payload The payload data. - * @return The created PointStruct. - */ - public static PointStruct point(UUID uuid, Vector vector, Map payload) { - - return point(pointId(uuid), vector, payload); - } - - /** - * Creates a {@link PointStruct} with a long ID, a {@link Vector}, and payload data. - * - * @param id The long ID. - * @param vector The vector data. - * @param payload The payload data. - * @return The created PointStruct. - */ - public static PointStruct point(long id, Vector vector, Map payload) { - - return point(pointId(id), vector, payload); - } - - /** - * Creates a {@link PointStruct} with a UUID string, a {@link Vector}, and payload data. - * - * @param uuid The UUID string. - * @param vector The vector data. - * @param payload The payload data. - * @return The created PointStruct. - */ - public static PointStruct point(String uuid, Vector vector, Map payload) { - - return point(pointId(uuid), vector, payload); - } - - /** - * Creates a {@link PointStruct} with a {@link PointId}, a {@link Vector}, and payload data. - * - * @param id The PointId. - * @param vector The vector data. - * @param payload The payload data. - * @return The created PointStruct. - */ - public static PointStruct point(PointId id, Vector vector, Map payload) { - PointStruct.Builder builder = - PointStruct.newBuilder().setId(id).setVectors(Vectors.newBuilder().setVector(vector)); - if (payload != null) { - builder.putAllPayload(payload); - } - return builder.build(); - } - - /** - * Creates a named {@link PointStruct} with a long ID, vector name, vector data, and payload data. - * - * @param id The long ID. - * @param vectorName The name of the vector. - * @param vectorData The vector data. - * @param payload The payload data. - * @return The created PointStruct. - */ - public static PointStruct namedPoint( - Long id, String vectorName, float[] vectorData, Map payload) { - return namedPoint(pointId(id), vectorName, vectorData, payload); - } - - /** - * Creates a named {@link PointStruct} with a {@link PointId}, vector name, vector data, and - * payload data. - * - * @param id The PointId. - * @param vectorName The name of the vector. - * @param vectorData The vector data. - * @param payload The payload data. - * @return The created PointStruct. - */ - public static PointStruct namedPoint( - PointId id, String vectorName, float[] vectorData, Map payload) { - NamedVectors vectors = VectorUtil.namedVector(vectorName, vectorData); - PointStruct.Builder builder = - PointStruct.newBuilder().setId(id).setVectors(Vectors.newBuilder().setVectors(vectors)); - if (payload != null) { - builder.putAllPayload(payload); - } - return builder.build(); - } - - /** - * Creates a {@link WriteOrdering} with the specified ordering type. - * - * @param orderingType The ordering type. - * @return The created WriteOrdering. - */ - public static WriteOrdering ordering(WriteOrderingType orderingType) { - return WriteOrdering.newBuilder().setType(orderingType).build(); - } - - /** - * Creates a {@link ReadConsistency} with the specified consistency type. - * - * @param consistencyType The consistency type. - * @return The created ReadConsistency. - */ - public static ReadConsistency consistency(ReadConsistencyType consistencyType) { - return ReadConsistency.newBuilder().setType(consistencyType).build(); - } -} diff --git a/src/main/java/io/qdrant/client/utils/SelectorUtil.java b/src/main/java/io/qdrant/client/utils/SelectorUtil.java deleted file mode 100644 index 519e6c35..00000000 --- a/src/main/java/io/qdrant/client/utils/SelectorUtil.java +++ /dev/null @@ -1,92 +0,0 @@ -package io.qdrant.client.utils; - -import io.qdrant.client.grpc.Points.Filter; -import io.qdrant.client.grpc.Points.PayloadIncludeSelector; -import io.qdrant.client.grpc.Points.PointId; -import io.qdrant.client.grpc.Points.PointsIdsList; -import io.qdrant.client.grpc.Points.PointsSelector; -import io.qdrant.client.grpc.Points.VectorsSelector; -import io.qdrant.client.grpc.Points.WithPayloadSelector; -import io.qdrant.client.grpc.Points.WithVectorsSelector; -import java.util.Arrays; -import java.util.List; - -/** Utility class for working with Selectors. */ -public class SelectorUtil { - - /** - * Creates a {@link WithPayloadSelector} with the enable flag set to true. - * - * @return The created {@link WithPayloadSelector} object. - */ - public static WithPayloadSelector withPayload() { - return WithPayloadSelector.newBuilder().setEnable(true).build(); - } - - /** - * Creates a {@link WithVectorsSelector} with the enable flag set to true. - * - * @return The created {@link WithVectorsSelector} object. - */ - public static WithVectorsSelector withVectors() { - return WithVectorsSelector.newBuilder().setEnable(true).build(); - } - - /** - * Creates a {@link WithPayloadSelector} with the specified fields included in the payload. - * - * @param fields The fields to include in the payload. - * @return The created {@link WithPayloadSelector} object. - */ - public static WithPayloadSelector withPayload(String... fields) { - PayloadIncludeSelector include = - PayloadIncludeSelector.newBuilder().addAllFields(Arrays.asList(fields)).build(); - return WithPayloadSelector.newBuilder().setInclude(include).build(); - } - - /** - * Creates a {@link WithVectorsSelector} with the specified vector fields included. - * - * @param vectors The names of the vectors to include. - * @return The created {@link WithVectorsSelector} object. - */ - public static WithVectorsSelector withVectors(String... vectors) { - VectorsSelector include = - VectorsSelector.newBuilder().addAllNames(Arrays.asList(vectors)).build(); - return WithVectorsSelector.newBuilder().setInclude(include).build(); - } - - /** - * Creates a {@link PointsSelector} with the specified list of point IDs. - * - * @param ids The list of point IDs. - * @return The created {@link PointsSelector} object. - */ - public static PointsSelector idsSelector(List ids) { - return PointsSelector.newBuilder() - .setPoints(PointsIdsList.newBuilder().addAllIds(ids).build()) - .build(); - } - - /** - * Creates a {@link PointsSelector} with the specified array of point IDs. - * - * @param ids The array of point IDs. - * @return The created {@link PointsSelector} object. - */ - public static PointsSelector idsSelector(PointId... ids) { - return PointsSelector.newBuilder() - .setPoints(PointsIdsList.newBuilder().addAllIds(Arrays.asList(ids)).build()) - .build(); - } - - /** - * Creates a {@link PointsSelector} with the specified Filter. - * - * @param filter The Filter for the selector. - * @return The created {@link PointsSelector} object. - */ - public static PointsSelector filterSelector(Filter filter) { - return PointsSelector.newBuilder().setFilter(filter).build(); - } -} diff --git a/src/main/java/io/qdrant/client/utils/VectorUtil.java b/src/main/java/io/qdrant/client/utils/VectorUtil.java deleted file mode 100644 index d9657bf0..00000000 --- a/src/main/java/io/qdrant/client/utils/VectorUtil.java +++ /dev/null @@ -1,131 +0,0 @@ -package io.qdrant.client.utils; - -import io.qdrant.client.grpc.Points.NamedVectors; -import io.qdrant.client.grpc.Points.PointVectors; -import io.qdrant.client.grpc.Points.Vector; -import io.qdrant.client.grpc.Points.Vectors; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Random; - -/** Utility class for working with vector data. */ -public class VectorUtil { - - /** - * Creates a vector from a list of floats. - * - * @param vector The list of floats representing the vector. - * @return The created vector. - */ - public static Vector toVector(List vector) { - Vector.Builder vectorBuilder = Vector.newBuilder(); - return vectorBuilder.addAllData(vector).build(); - } - - /** - * Creates a vector from an array of floats. - * - * @param vector The array of floats representing the vector. - * @return The created vector. - */ - public static Vector toVector(float... vector) { - Vector.Builder vectorBuilder = Vector.newBuilder(); - for (Float f : vector) { - vectorBuilder.addData(f); - } - return vectorBuilder.build(); - } - - /** - * Creates a named vector from a list of floats. - * - * @param name The name of the vector. - * @param vector The list of floats representing the vector. - * @return The created named vector. - */ - public static NamedVectors namedVector(String name, List vector) { - NamedVectors.Builder namedVectorBuilder = NamedVectors.newBuilder(); - return namedVectorBuilder.putVectors(name, toVector(vector)).build(); - } - - /** - * Creates a named vector from an array of floats. - * - * @param name The name of the vector. - * @param vector The array of floats representing the vector. - * @return The created named vector. - */ - public static NamedVectors namedVector(String name, float... vector) { - NamedVectors.Builder namedVectorBuilder = NamedVectors.newBuilder(); - return namedVectorBuilder.putVectors(name, toVector(vector)).build(); - } - - /** - * Creates named vectors from a map of vector names to vectors. - * - * @param vectors The map of vector names to vectors. - * @return The created named vectors. - */ - public static NamedVectors namedVectors(Map vectors) { - NamedVectors.Builder namedVectorBuilder = NamedVectors.newBuilder(); - return namedVectorBuilder.putAllVectors(vectors).build(); - } - - /** - * Creates point vectors from an ID, name, and an array of floats. - * - * @param id The ID of the point vectors. - * @param name The name of the point vectors. - * @param vector The array of floats representing the point vectors. - * @return The created point vectors. - */ - public static PointVectors pointVectors(String id, String name, float... vector) { - PointVectors.Builder pointVectorsBuilder = PointVectors.newBuilder(); - Vectors vectors = Vectors.newBuilder().setVectors(namedVector(name, vector)).build(); - return pointVectorsBuilder.setId(PointUtil.pointId(id)).setVectors(vectors).build(); - } - - /** - * Generates dummy embeddings of the specified size. - * - * @param size The size of the embeddings to generate. - * @return An array of floats representing the generated embeddings. - * @throws IllegalArgumentException If the size is less than or equal to zero. - */ - public static List dummyEmbeddings(int size) { - if (size <= 0) { - throw new IllegalArgumentException("Size must be greater than zero"); - } - - List embeddings = new ArrayList<>(); - Random random = new Random(); - - for (int i = 0; i < size; i++) { - embeddings.add(random.nextFloat()); - } - - return embeddings; - } - - /** - * Generates a dummy vector of the specified size. - * - * @param size The size of the vector. - * @return The generated dummy vector. - */ - public static Vector dummyVector(int size) { - return toVector(dummyEmbeddings(size)); - } - - /** - * Generates a dummy named vector of the specified size. - * - * @param name The name of the vector. - * @param size The size of the vector. - * @return The generated dummy vector. - */ - public static NamedVectors dummyNamedVector(String name, int size) { - return namedVector(name, dummyEmbeddings(size)); - } -} diff --git a/src/main/proto/collections.proto b/src/main/proto/collections.proto deleted file mode 100644 index 67373bed..00000000 --- a/src/main/proto/collections.proto +++ /dev/null @@ -1,448 +0,0 @@ -syntax = "proto3"; -package qdrant; - -option java_package = "io.qdrant.client.grpc"; - -message VectorParams { - uint64 size = 1; // Size of the vectors - Distance distance = 2; // Distance function used for comparing vectors - optional HnswConfigDiff hnsw_config = 3; // Configuration of vector HNSW graph. If omitted - the collection configuration will be used - optional QuantizationConfig quantization_config = 4; // Configuration of vector quantization config. If omitted - the collection configuration will be used - optional bool on_disk = 5; // If true - serve vectors from disk. If set to false, the vectors will be loaded in RAM. -} - -message VectorParamsDiff { - optional HnswConfigDiff hnsw_config = 1; // Update params for HNSW index. If empty object - it will be unset - optional QuantizationConfigDiff quantization_config = 2; // Update quantization params. If none - it is left unchanged. - optional bool on_disk = 3; // If true - serve vectors from disk. If set to false, the vectors will be loaded in RAM. -} - -message VectorParamsMap { - map map = 1; -} - -message VectorParamsDiffMap { - map map = 1; -} - -message VectorsConfig { - oneof config { - VectorParams params = 1; - VectorParamsMap params_map = 2; - } -} - -message VectorsConfigDiff { - oneof config { - VectorParamsDiff params = 1; - VectorParamsDiffMap params_map = 2; - } -} - -message GetCollectionInfoRequest { - string collection_name = 1; // Name of the collection -} - -message ListCollectionsRequest { -} - -message CollectionDescription { - string name = 1; // Name of the collection -} - -message GetCollectionInfoResponse { - CollectionInfo result = 1; - double time = 2; // Time spent to process -} - -message ListCollectionsResponse { - repeated CollectionDescription collections = 1; - double time = 2; // Time spent to process -} - -enum Distance { - UnknownDistance = 0; - Cosine = 1; - Euclid = 2; - Dot = 3; -} - -enum CollectionStatus { - UnknownCollectionStatus = 0; - Green = 1; // All segments are ready - Yellow = 2; // Optimization in process - Red = 3; // Something went wrong -} - -enum PayloadSchemaType { - UnknownType = 0; - Keyword = 1; - Integer = 2; - Float = 3; - Geo = 4; - Text = 5; - Bool = 6; -} - -enum QuantizationType { - UnknownQuantization = 0; - Int8 = 1; -} - -enum CompressionRatio { - x4 = 0; - x8 = 1; - x16 = 2; - x32 = 3; - x64 = 4; -} - -message OptimizerStatus { - bool ok = 1; - string error = 2; -} - -message HnswConfigDiff { - /* - Number of edges per node in the index graph. Larger the value - more accurate the search, more space required. - */ - optional uint64 m = 1; - /* - Number of neighbours to consider during the index building. Larger the value - more accurate the search, more time required to build the index. - */ - optional uint64 ef_construct = 2; - /* - Minimal size (in KiloBytes) of vectors for additional payload-based indexing. - If the payload chunk is smaller than `full_scan_threshold` additional indexing won't be used - - in this case full-scan search should be preferred by query planner and additional indexing is not required. - Note: 1 Kb = 1 vector of size 256 - */ - optional uint64 full_scan_threshold = 3; - /* - Number of parallel threads used for background index building. If 0 - auto selection. - */ - optional uint64 max_indexing_threads = 4; - /* - Store HNSW index on disk. If set to false, the index will be stored in RAM. - */ - optional bool on_disk = 5; - /* - Number of additional payload-aware links per node in the index graph. If not set - regular M parameter will be used. - */ - optional uint64 payload_m = 6; -} - -message WalConfigDiff { - optional uint64 wal_capacity_mb = 1; // Size of a single WAL block file - optional uint64 wal_segments_ahead = 2; // Number of segments to create in advance -} - -message OptimizersConfigDiff { - /* - The minimal fraction of deleted vectors in a segment, required to perform segment optimization - */ - optional double deleted_threshold = 1; - /* - The minimal number of vectors in a segment, required to perform segment optimization - */ - optional uint64 vacuum_min_vector_number = 2; - /* - Target amount of segments the optimizer will try to keep. - Real amount of segments may vary depending on multiple parameters: - - - Amount of stored points. - - Current write RPS. - - It is recommended to select the default number of segments as a factor of the number of search threads, - so that each segment would be handled evenly by one of the threads. - */ - optional uint64 default_segment_number = 3; - /* - Do not create segments larger this size (in kilobytes). - Large segments might require disproportionately long indexation times, - therefore it makes sense to limit the size of segments. - - If indexing speed is more important - make this parameter lower. - If search speed is more important - make this parameter higher. - Note: 1Kb = 1 vector of size 256 - If not set, will be automatically selected considering the number of available CPUs. - */ - optional uint64 max_segment_size = 4; - /* - Maximum size (in kilobytes) of vectors to store in-memory per segment. - Segments larger than this threshold will be stored as read-only memmaped file. - - Memmap storage is disabled by default, to enable it, set this threshold to a reasonable value. - - To disable memmap storage, set this to `0`. - - Note: 1Kb = 1 vector of size 256 - */ - optional uint64 memmap_threshold = 5; - /* - Maximum size (in kilobytes) of vectors allowed for plain index, exceeding this threshold will enable vector indexing - - Default value is 20,000, based on . - - To disable vector indexing, set to `0`. - - Note: 1kB = 1 vector of size 256. - */ - optional uint64 indexing_threshold = 6; - /* - Interval between forced flushes. - */ - optional uint64 flush_interval_sec = 7; - /* - Max number of threads, which can be used for optimization. If 0 - `NUM_CPU - 1` will be used - */ - optional uint64 max_optimization_threads = 8; -} - -message ScalarQuantization { - QuantizationType type = 1; // Type of quantization - optional float quantile = 2; // Number of bits to use for quantization - optional bool always_ram = 3; // If true - quantized vectors always will be stored in RAM, ignoring the config of main storage -} - -message ProductQuantization { - CompressionRatio compression = 1; // Compression ratio - optional bool always_ram = 2; // If true - quantized vectors always will be stored in RAM, ignoring the config of main storage -} - -message BinaryQuantization { - optional bool always_ram = 1; // If true - quantized vectors always will be stored in RAM, ignoring the config of main storage -} - -message QuantizationConfig { - oneof quantization { - ScalarQuantization scalar = 1; - ProductQuantization product = 2; - BinaryQuantization binary = 3; - } -} - -message Disabled { - -} - -message QuantizationConfigDiff { - oneof quantization { - ScalarQuantization scalar = 1; - ProductQuantization product = 2; - Disabled disabled = 3; - BinaryQuantization binary = 4; - } -} - -message CreateCollection { - string collection_name = 1; // Name of the collection - reserved 2; // Deprecated - reserved 3; // Deprecated - optional HnswConfigDiff hnsw_config = 4; // Configuration of vector index - optional WalConfigDiff wal_config = 5; // Configuration of the Write-Ahead-Log - optional OptimizersConfigDiff optimizers_config = 6; // Configuration of the optimizers - optional uint32 shard_number = 7; // Number of shards in the collection, default is 1 for standalone, otherwise equal to the number of nodes. Minimum is 1 - optional bool on_disk_payload = 8; // If true - point's payload will not be stored in memory - optional uint64 timeout = 9; // Wait timeout for operation commit in seconds, if not specified - default value will be supplied - optional VectorsConfig vectors_config = 10; // Configuration for vectors - optional uint32 replication_factor = 11; // Number of replicas of each shard that network tries to maintain, default = 1 - optional uint32 write_consistency_factor = 12; // How many replicas should apply the operation for us to consider it successful, default = 1 - optional string init_from_collection = 13; // Specify name of the other collection to copy data from - optional QuantizationConfig quantization_config = 14; // Quantization configuration of vector -} - -message UpdateCollection { - string collection_name = 1; // Name of the collection - optional OptimizersConfigDiff optimizers_config = 2; // New configuration parameters for the collection. This operation is blocking, it will only proceed once all current optimizations are complete - optional uint64 timeout = 3; // Wait timeout for operation commit in seconds if blocking, if not specified - default value will be supplied - optional CollectionParamsDiff params = 4; // New configuration parameters for the collection - optional HnswConfigDiff hnsw_config = 5; // New HNSW parameters for the collection index - optional VectorsConfigDiff vectors_config = 6; // New vector parameters - optional QuantizationConfigDiff quantization_config = 7; // Quantization configuration of vector -} - -message DeleteCollection { - string collection_name = 1; // Name of the collection - optional uint64 timeout = 2; // Wait timeout for operation commit in seconds, if not specified - default value will be supplied -} - -message CollectionOperationResponse { - bool result = 1; // if operation made changes - double time = 2; // Time spent to process -} - -message CollectionParams { - reserved 1; // Deprecated - reserved 2; // Deprecated - uint32 shard_number = 3; // Number of shards in collection - bool on_disk_payload = 4; // If true - point's payload will not be stored in memory - optional VectorsConfig vectors_config = 5; // Configuration for vectors - optional uint32 replication_factor = 6; // Number of replicas of each shard that network tries to maintain - optional uint32 write_consistency_factor = 7; // How many replicas should apply the operation for us to consider it successful - optional uint32 read_fan_out_factor = 8; // Fan-out every read request to these many additional remote nodes (and return first available response) -} - -message CollectionParamsDiff { - optional uint32 replication_factor = 1; // Number of replicas of each shard that network tries to maintain - optional uint32 write_consistency_factor = 2; // How many replicas should apply the operation for us to consider it successful - optional bool on_disk_payload = 3; // If true - point's payload will not be stored in memory - optional uint32 read_fan_out_factor = 4; // Fan-out every read request to these many additional remote nodes (and return first available response) -} - -message CollectionConfig { - CollectionParams params = 1; // Collection parameters - HnswConfigDiff hnsw_config = 2; // Configuration of vector index - OptimizersConfigDiff optimizer_config = 3; // Configuration of the optimizers - WalConfigDiff wal_config = 4; // Configuration of the Write-Ahead-Log - optional QuantizationConfig quantization_config = 5; // Configuration of the vector quantization -} - -enum TokenizerType { - Unknown = 0; - Prefix = 1; - Whitespace = 2; - Word = 3; - Multilingual = 4; -} - -message TextIndexParams { - TokenizerType tokenizer = 1; // Tokenizer type - optional bool lowercase = 2; // If true - all tokens will be lowercase - optional uint64 min_token_len = 3; // Minimal token length - optional uint64 max_token_len = 4; // Maximal token length -} - -message PayloadIndexParams { - oneof index_params { - TextIndexParams text_index_params = 1; // Parameters for text index - } -} - -message PayloadSchemaInfo { - PayloadSchemaType data_type = 1; // Field data type - optional PayloadIndexParams params = 2; // Field index parameters - optional uint64 points = 3; // Number of points indexed within this field indexed -} - -message CollectionInfo { - CollectionStatus status = 1; // operating condition of the collection - OptimizerStatus optimizer_status = 2; // status of collection optimizers - uint64 vectors_count = 3; // number of vectors in the collection - uint64 segments_count = 4; // Number of independent segments - reserved 5; // Deprecated - reserved 6; // Deprecated - CollectionConfig config = 7; // Configuration - map payload_schema = 8; // Collection data types - uint64 points_count = 9; // number of points in the collection - optional uint64 indexed_vectors_count = 10; // number of indexed vectors in the collection. -} - -message ChangeAliases { - repeated AliasOperations actions = 1; // List of actions - optional uint64 timeout = 2; // Wait timeout for operation commit in seconds, if not specified - default value will be supplied -} - -message AliasOperations { - oneof action { - CreateAlias create_alias = 1; - RenameAlias rename_alias = 2; - DeleteAlias delete_alias = 3; - } -} - -message CreateAlias { - string collection_name = 1; // Name of the collection - string alias_name = 2; // New name of the alias -} - -message RenameAlias { - string old_alias_name = 1; // Name of the alias to rename - string new_alias_name = 2; // Name of the alias -} - -message DeleteAlias { - string alias_name = 1; // Name of the alias -} - -message ListAliasesRequest { -} - -message ListCollectionAliasesRequest { - string collection_name = 1; // Name of the collection -} - -message AliasDescription { - string alias_name = 1; // Name of the alias - string collection_name = 2; // Name of the collection -} - -message ListAliasesResponse { - repeated AliasDescription aliases = 1; - double time = 2; // Time spent to process -} - -message CollectionClusterInfoRequest { - string collection_name = 1; // Name of the collection -} - -enum ReplicaState { - Active = 0; // Active and sound - Dead = 1; // Failed for some reason - Partial = 2; // The shard is partially loaded and is currently receiving data from other shards - Initializing = 3; // Collection is being created - Listener = 4; // A shard which receives data, but is not used for search; Useful for backup shards -} - -message LocalShardInfo { - uint32 shard_id = 1; // Local shard id - uint64 points_count = 2; // Number of points in the shard - ReplicaState state = 3; // Is replica active -} - -message RemoteShardInfo { - uint32 shard_id = 1; // Local shard id - uint64 peer_id = 2; // Remote peer id - ReplicaState state = 3; // Is replica active -} - -message ShardTransferInfo { - uint32 shard_id = 1; // Local shard id - uint64 from = 2; - uint64 to = 3; - bool sync = 4; // If `true` transfer is a synchronization of a replicas; If `false` transfer is a moving of a shard from one peer to another -} - -message CollectionClusterInfoResponse { - uint64 peer_id = 1; // ID of this peer - uint64 shard_count = 2; // Total number of shards - repeated LocalShardInfo local_shards = 3; // Local shards - repeated RemoteShardInfo remote_shards = 4; // Remote shards - repeated ShardTransferInfo shard_transfers = 5; // Shard transfers -} - -message MoveShard { - uint32 shard_id = 1; // Local shard id - uint64 from_peer_id = 2; - uint64 to_peer_id = 3; -} - -message Replica { - uint32 shard_id = 1; - uint64 peer_id = 2; -} - -message UpdateCollectionClusterSetupRequest { - string collection_name = 1; // Name of the collection - oneof operation { - MoveShard move_shard = 2; - MoveShard replicate_shard = 3; - MoveShard abort_transfer = 4; - Replica drop_replica = 5; - } - optional uint64 timeout = 6; // Wait timeout for operation commit in seconds, if not specified - default value will be supplied -} - -message UpdateCollectionClusterSetupResponse { - bool result = 1; -} diff --git a/src/main/proto/collections_service.proto b/src/main/proto/collections_service.proto deleted file mode 100644 index d5a106ad..00000000 --- a/src/main/proto/collections_service.proto +++ /dev/null @@ -1,50 +0,0 @@ -syntax = "proto3"; - -import "collections.proto"; - -option java_package = "io.qdrant.client.grpc"; - -package qdrant; - -service Collections { - /* - Get detailed information about specified existing collection - */ - rpc Get (GetCollectionInfoRequest) returns (GetCollectionInfoResponse) {} - /* - Get list name of all existing collections - */ - rpc List (ListCollectionsRequest) returns (ListCollectionsResponse) {} - /* - Create new collection with given parameters - */ - rpc Create (CreateCollection) returns (CollectionOperationResponse) {} - /* - Update parameters of the existing collection - */ - rpc Update (UpdateCollection) returns (CollectionOperationResponse) {} - /* - Drop collection and all associated data - */ - rpc Delete (DeleteCollection) returns (CollectionOperationResponse) {} - /* - Update Aliases of the existing collection - */ - rpc UpdateAliases (ChangeAliases) returns (CollectionOperationResponse) {} - /* - Get list of all aliases for a collection - */ - rpc ListCollectionAliases (ListCollectionAliasesRequest) returns (ListAliasesResponse) {} - /* - Get list of all aliases for all existing collections - */ - rpc ListAliases (ListAliasesRequest) returns (ListAliasesResponse) {} - /* - Get cluster information for a collection - */ - rpc CollectionClusterInfo (CollectionClusterInfoRequest) returns (CollectionClusterInfoResponse) {} - /* - Update cluster setup for a collection - */ - rpc UpdateCollectionClusterSetup (UpdateCollectionClusterSetupRequest) returns (UpdateCollectionClusterSetupResponse) {} -} diff --git a/src/main/proto/json_with_int.proto b/src/main/proto/json_with_int.proto deleted file mode 100644 index c9fecb06..00000000 --- a/src/main/proto/json_with_int.proto +++ /dev/null @@ -1,63 +0,0 @@ -// Fork of the google.protobuf.Value with explicit support for integer values - -syntax = "proto3"; - -package qdrant; - -option java_package = "io.qdrant.client.grpc"; - -// `Struct` represents a structured data value, consisting of fields -// which map to dynamically typed values. In some languages, `Struct` -// might be supported by a native representation. For example, in -// scripting languages like JS a struct is represented as an -// object. The details of that representation are described together -// with the proto support for the language. -// -// The JSON representation for `Struct` is a JSON object. -message Struct { - // Unordered map of dynamically typed values. - map fields = 1; -} - -// `Value` represents a dynamically typed value which can be either -// null, a number, a string, a boolean, a recursive struct value, or a -// list of values. A producer of value is expected to set one of those -// variants, absence of any variant indicates an error. -// -// The JSON representation for `Value` is a JSON value. -message Value { - // The kind of value. - oneof kind { - // Represents a null value. - NullValue null_value = 1; - // Represents a double value. - double double_value = 2; - // Represents an integer value - int64 integer_value = 3; - // Represents a string value. - string string_value = 4; - // Represents a boolean value. - bool bool_value = 5; - // Represents a structured value. - Struct struct_value = 6; - // Represents a repeated `Value`. - ListValue list_value = 7; - } -} - -// `NullValue` is a singleton enumeration to represent the null value for the -// `Value` type union. -// -// The JSON representation for `NullValue` is JSON `null`. -enum NullValue { - // Null value. - NULL_VALUE = 0; -} - -// `ListValue` is a wrapper around a repeated field of values. -// -// The JSON representation for `ListValue` is a JSON array. -message ListValue { - // Repeated field of dynamically typed values. - repeated Value values = 1; -} diff --git a/src/main/proto/points.proto b/src/main/proto/points.proto deleted file mode 100644 index b7b5ec4c..00000000 --- a/src/main/proto/points.proto +++ /dev/null @@ -1,646 +0,0 @@ -syntax = "proto3"; - -package qdrant; - -option java_package = "io.qdrant.client.grpc"; - -import "json_with_int.proto"; -import "collections.proto"; - - -enum WriteOrderingType { - Weak = 0; // Write operations may be reordered, works faster, default - Medium = 1; // Write operations go through dynamically selected leader, may be inconsistent for a short period of time in case of leader change - Strong = 2; // Write operations go through the permanent leader, consistent, but may be unavailable if leader is down -} - -message WriteOrdering { - WriteOrderingType type = 1; // Write ordering guarantees -} - -enum ReadConsistencyType { - All = 0; // Send request to all nodes and return points which are present on all of them - Majority = 1; // Send requests to all nodes and return points which are present on majority of them - Quorum = 2; // Send requests to half + 1 nodes, return points which are present on all of them -} - -message ReadConsistency { - oneof value { - ReadConsistencyType type = 1; // Common read consistency configurations - uint64 factor = 2; // Send request to a specified number of nodes, and return points which are present on all of them - } -} - -// --------------------------------------------- -// ------------- Point Id Requests ------------- -// --------------------------------------------- - -message PointId { - oneof point_id_options { - uint64 num = 1; // Numerical ID of the point - string uuid = 2; // UUID - } -} - -message Vector { - repeated float data = 1; -} - -// --------------------------------------------- -// ---------------- RPC Requests --------------- -// --------------------------------------------- - -message UpsertPoints { - string collection_name = 1; // name of the collection - optional bool wait = 2; // Wait until the changes have been applied? - repeated PointStruct points = 3; - optional WriteOrdering ordering = 4; // Write ordering guarantees -} - -message DeletePoints { - string collection_name = 1; // name of the collection - optional bool wait = 2; // Wait until the changes have been applied? - PointsSelector points = 3; // Affected points - optional WriteOrdering ordering = 4; // Write ordering guarantees -} - -message GetPoints { - string collection_name = 1; // name of the collection - repeated PointId ids = 2; // List of points to retrieve - reserved 3; // deprecated "with_vector" field - WithPayloadSelector with_payload = 4; // Options for specifying which payload to include or not - optional WithVectorsSelector with_vectors = 5; // Options for specifying which vectors to include into response - optional ReadConsistency read_consistency = 6; // Options for specifying read consistency guarantees -} - -message UpdatePointVectors { - string collection_name = 1; // name of the collection - optional bool wait = 2; // Wait until the changes have been applied? - repeated PointVectors points = 3; // List of points and vectors to update - optional WriteOrdering ordering = 4; // Write ordering guarantees -} - -message PointVectors { - PointId id = 1; // ID to update vectors for - Vectors vectors = 2; // Named vectors to update, leave others intact -} - -message DeletePointVectors { - string collection_name = 1; // name of the collection - optional bool wait = 2; // Wait until the changes have been applied? - PointsSelector points_selector = 3; // Affected points - VectorsSelector vectors = 4; // List of vector names to delete - optional WriteOrdering ordering = 5; // Write ordering guarantees -} - -message SetPayloadPoints { - string collection_name = 1; // name of the collection - optional bool wait = 2; // Wait until the changes have been applied? - map payload = 3; // New payload values - reserved 4; // List of point to modify, deprecated - optional PointsSelector points_selector = 5; // Affected points - optional WriteOrdering ordering = 6; // Write ordering guarantees -} - -message DeletePayloadPoints { - string collection_name = 1; // name of the collection - optional bool wait = 2; // Wait until the changes have been applied? - repeated string keys = 3; // List of keys to delete - reserved 4; // Affected points, deprecated - optional PointsSelector points_selector = 5; // Affected points - optional WriteOrdering ordering = 6; // Write ordering guarantees -} - -message ClearPayloadPoints { - string collection_name = 1; // name of the collection - optional bool wait = 2; // Wait until the changes have been applied? - PointsSelector points = 3; // Affected points - optional WriteOrdering ordering = 4; // Write ordering guarantees -} - -enum FieldType { - FieldTypeKeyword = 0; - FieldTypeInteger = 1; - FieldTypeFloat = 2; - FieldTypeGeo = 3; - FieldTypeText = 4; - FieldTypeBool = 5; -} - -message CreateFieldIndexCollection { - string collection_name = 1; // name of the collection - optional bool wait = 2; // Wait until the changes have been applied? - string field_name = 3; // Field name to index - optional FieldType field_type = 4; // Field type. - optional PayloadIndexParams field_index_params = 5; // Payload index params. - optional WriteOrdering ordering = 6; // Write ordering guarantees -} - -message DeleteFieldIndexCollection { - string collection_name = 1; // name of the collection - optional bool wait = 2; // Wait until the changes have been applied? - string field_name = 3; // Field name to delete - optional WriteOrdering ordering = 4; // Write ordering guarantees -} - -message PayloadIncludeSelector { - repeated string fields = 1; // List of payload keys to include into result -} - -message PayloadExcludeSelector { - repeated string fields = 1; // List of payload keys to exclude from the result -} - -message WithPayloadSelector { - oneof selector_options { - bool enable = 1; // If `true` - return all payload, if `false` - none - PayloadIncludeSelector include = 2; - PayloadExcludeSelector exclude = 3; - } -} - -message NamedVectors { - map vectors = 1; -} - -message Vectors { - oneof vectors_options { - Vector vector = 1; - NamedVectors vectors = 2; - } -} - -message VectorsSelector { - repeated string names = 1; // List of vectors to include into result -} - -message WithVectorsSelector { - oneof selector_options { - bool enable = 1; // If `true` - return all vectors, if `false` - none - VectorsSelector include = 2; // List of payload keys to include into result - } -} - -message QuantizationSearchParams { - /* - If set to true, search will ignore quantized vector data - */ - optional bool ignore = 1; - - /* - If true, use original vectors to re-score top-k results. If ignored, qdrant decides automatically does rescore enabled or not. - */ - optional bool rescore = 2; - - /* - Oversampling factor for quantization. - - Defines how many extra vectors should be pre-selected using quantized index, - and then re-scored using original vectors. - - For example, if `oversampling` is 2.4 and `limit` is 100, then 240 vectors will be pre-selected using quantized index, - and then top-100 will be returned after re-scoring. - */ - optional double oversampling = 3; -} - -message SearchParams { - /* - Params relevant to HNSW index. Size of the beam in a beam-search. - Larger the value - more accurate the result, more time required for search. - */ - optional uint64 hnsw_ef = 1; - - /* - Search without approximation. If set to true, search may run long but with exact results. - */ - optional bool exact = 2; - - /* - If set to true, search will ignore quantized vector data - */ - optional QuantizationSearchParams quantization = 3; - /* - If enabled, the engine will only perform search among indexed or small segments. - Using this option prevents slow searches in case of delayed index, but does not - guarantee that all uploaded vectors will be included in search results - */ - optional bool indexed_only = 4; -} - -message SearchPoints { - string collection_name = 1; // name of the collection - repeated float vector = 2; // vector - Filter filter = 3; // Filter conditions - return only those points that satisfy the specified conditions - uint64 limit = 4; // Max number of result - reserved 5; // deprecated "with_vector" field - WithPayloadSelector with_payload = 6; // Options for specifying which payload to include or not - SearchParams params = 7; // Search config - optional float score_threshold = 8; // If provided - cut off results with worse scores - optional uint64 offset = 9; // Offset of the result - optional string vector_name = 10; // Which vector to use for search, if not specified - use default vector - optional WithVectorsSelector with_vectors = 11; // Options for specifying which vectors to include into response - optional ReadConsistency read_consistency = 12; // Options for specifying read consistency guarantees -} - -message SearchBatchPoints { - string collection_name = 1; // Name of the collection - repeated SearchPoints search_points = 2; - optional ReadConsistency read_consistency = 3; // Options for specifying read consistency guarantees -} - -message WithLookup { - string collection = 1; // Name of the collection to use for points lookup - optional WithPayloadSelector with_payload = 2; // Options for specifying which payload to include (or not) - optional WithVectorsSelector with_vectors = 3; // Options for specifying which vectors to include (or not) -} - - -message SearchPointGroups { - string collection_name = 1; // Name of the collection - repeated float vector = 2; // Vector to compare against - Filter filter = 3; // Filter conditions - return only those points that satisfy the specified conditions - uint32 limit = 4; // Max number of result - WithPayloadSelector with_payload = 5; // Options for specifying which payload to include or not - SearchParams params = 6; // Search config - optional float score_threshold = 7; // If provided - cut off results with worse scores - optional string vector_name = 8; // Which vector to use for search, if not specified - use default vector - optional WithVectorsSelector with_vectors = 9; // Options for specifying which vectors to include into response - string group_by = 10; // Payload field to group by, must be a string or number field. If there are multiple values for the field, all of them will be used. One point can be in multiple groups. - uint32 group_size = 11; // Maximum amount of points to return per group - optional ReadConsistency read_consistency = 12; // Options for specifying read consistency guarantees - optional WithLookup with_lookup = 13; // Options for specifying how to use the group id to lookup points in another collection -} - -message ScrollPoints { - string collection_name = 1; - Filter filter = 2; // Filter conditions - return only those points that satisfy the specified conditions - optional PointId offset = 3; // Start with this ID - optional uint32 limit = 4; // Max number of result - reserved 5; // deprecated "with_vector" field - WithPayloadSelector with_payload = 6; // Options for specifying which payload to include or not - optional WithVectorsSelector with_vectors = 7; // Options for specifying which vectors to include into response - optional ReadConsistency read_consistency = 8; // Options for specifying read consistency guarantees -} - -// How to use positive and negative vectors to find the results, default is `AverageVector`: -enum RecommendStrategy { - // Average positive and negative vectors and create a single query with the formula - // `query = avg_pos + avg_pos - avg_neg`. Then performs normal search. - AverageVector = 0; - - // Uses custom search objective. Each candidate is compared against all - // examples, its score is then chosen from the `max(max_pos_score, max_neg_score)`. - // If the `max_neg_score` is chosen then it is squared and negated. - BestScore = 1; -} - -message LookupLocation { - string collection_name = 1; - optional string vector_name = 2; // Which vector to use for search, if not specified - use default vector -} - -message RecommendPoints { - string collection_name = 1; // name of the collection - repeated PointId positive = 2; // Look for vectors closest to the vectors from these points - repeated PointId negative = 3; // Try to avoid vectors like the vector from these points - Filter filter = 4; // Filter conditions - return only those points that satisfy the specified conditions - uint64 limit = 5; // Max number of result - reserved 6; // deprecated "with_vector" field - WithPayloadSelector with_payload = 7; // Options for specifying which payload to include or not - SearchParams params = 8; // Search config - optional float score_threshold = 9; // If provided - cut off results with worse scores - optional uint64 offset = 10; // Offset of the result - optional string using = 11; // Define which vector to use for recommendation, if not specified - default vector - optional WithVectorsSelector with_vectors = 12; // Options for specifying which vectors to include into response - optional LookupLocation lookup_from = 13; // Name of the collection to use for points lookup, if not specified - use current collection - optional ReadConsistency read_consistency = 14; // Options for specifying read consistency guarantees - optional RecommendStrategy strategy = 16; // How to use the example vectors to find the results - repeated Vector positive_vectors = 17; // Look for vectors closest to those - repeated Vector negative_vectors = 18; // Try to avoid vectors like this -} - -message RecommendBatchPoints { - string collection_name = 1; // Name of the collection - repeated RecommendPoints recommend_points = 2; - optional ReadConsistency read_consistency = 3; // Options for specifying read consistency guarantees -} - -message RecommendPointGroups { - string collection_name = 1; // Name of the collection - repeated PointId positive = 2; // Look for vectors closest to the vectors from these points - repeated PointId negative = 3; // Try to avoid vectors like the vector from these points - Filter filter = 4; // Filter conditions - return only those points that satisfy the specified conditions - uint32 limit = 5; // Max number of groups in result - WithPayloadSelector with_payload = 6; // Options for specifying which payload to include or not - SearchParams params = 7; // Search config - optional float score_threshold = 8; // If provided - cut off results with worse scores - optional string using = 9; // Define which vector to use for recommendation, if not specified - default vector - optional WithVectorsSelector with_vectors = 10; // Options for specifying which vectors to include into response - optional LookupLocation lookup_from = 11; // Name of the collection to use for points lookup, if not specified - use current collection - string group_by = 12; // Payload field to group by, must be a string or number field. If there are multiple values for the field, all of them will be used. One point can be in multiple groups. - uint32 group_size = 13; // Maximum amount of points to return per group - optional ReadConsistency read_consistency = 14; // Options for specifying read consistency guarantees - optional WithLookup with_lookup = 15; // Options for specifying how to use the group id to lookup points in another collection - optional RecommendStrategy strategy = 17; // How to use the example vectors to find the results - repeated Vector positive_vectors = 18; // Look for vectors closest to those - repeated Vector negative_vectors = 19; // Try to avoid vectors like this -} - -message CountPoints { - string collection_name = 1; // name of the collection - Filter filter = 2; // Filter conditions - return only those points that satisfy the specified conditions - optional bool exact = 3; // If `true` - return exact count, if `false` - return approximate count -} - -message PointsUpdateOperation { - message PointStructList { - repeated PointStruct points = 1; - } - message SetPayload { - map payload = 1; - optional PointsSelector points_selector = 2; // Affected points - } - message DeletePayload { - repeated string keys = 1; - optional PointsSelector points_selector = 2; // Affected points - } - message UpdateVectors { - repeated PointVectors points = 1; // List of points and vectors to update - } - message DeleteVectors { - PointsSelector points_selector = 1; // Affected points - VectorsSelector vectors = 2; // List of vector names to delete - } - - oneof operation { - PointStructList upsert = 1; - PointsSelector delete = 2; - SetPayload set_payload = 3; - SetPayload overwrite_payload = 4; - DeletePayload delete_payload = 5; - PointsSelector clear_payload = 6; - UpdateVectors update_vectors = 7; - DeleteVectors delete_vectors = 8; - } -} - -message UpdateBatchPoints { - string collection_name = 1; // name of the collection - optional bool wait = 2; // Wait until the changes have been applied? - repeated PointsUpdateOperation operations = 3; - optional WriteOrdering ordering = 4; // Write ordering guarantees -} - -// --------------------------------------------- -// ---------------- RPC Response --------------- -// --------------------------------------------- - -message PointsOperationResponse { - UpdateResult result = 1; - double time = 2; // Time spent to process -} - -message UpdateResult { - uint64 operation_id = 1; // Number of operation - UpdateStatus status = 2; // Operation status -} - -enum UpdateStatus { - UnknownUpdateStatus = 0; - Acknowledged = 1; // Update is received, but not processed yet - Completed = 2; // Update is applied and ready for search -} - -message ScoredPoint { - PointId id = 1; // Point id - map payload = 2; // Payload - float score = 3; // Similarity score - reserved 4; // deprecated "vector" field - uint64 version = 5; // Last update operation applied to this point - optional Vectors vectors = 6; // Vectors to search -} - -message GroupId { - oneof kind { - // Represents a double value. - uint64 unsigned_value = 1; - // Represents an integer value - int64 integer_value = 2; - // Represents a string value. - string string_value = 3; - } -} - -message PointGroup { - GroupId id = 1; // Group id - repeated ScoredPoint hits = 2; // Points in the group - RetrievedPoint lookup = 3; // Point(s) from the lookup collection that matches the group id -} - -message GroupsResult { - repeated PointGroup groups = 1; // Groups -} - -message SearchResponse { - repeated ScoredPoint result = 1; - double time = 2; // Time spent to process -} - -message BatchResult { - repeated ScoredPoint result = 1; -} - -message SearchBatchResponse { - repeated BatchResult result = 1; - double time = 2; // Time spent to process -} - -message SearchGroupsResponse { - GroupsResult result = 1; - double time = 2; // Time spent to process -} - -message CountResponse { - CountResult result = 1; - double time = 2; // Time spent to process -} - -message ScrollResponse { - optional PointId next_page_offset = 1; // Use this offset for the next query - repeated RetrievedPoint result = 2; - double time = 3; // Time spent to process -} - -message CountResult { - uint64 count = 1; -} - -message RetrievedPoint { - PointId id = 1; - map payload = 2; - reserved 3; // deprecated "vector" field - optional Vectors vectors = 4; -} - -message GetResponse { - repeated RetrievedPoint result = 1; - double time = 2; // Time spent to process -} - -message RecommendResponse { - repeated ScoredPoint result = 1; - double time = 2; // Time spent to process -} - -message RecommendBatchResponse { - repeated BatchResult result = 1; - double time = 2; // Time spent to process -} - -message RecommendGroupsResponse { - GroupsResult result = 1; - double time = 2; // Time spent to process -} - -message UpdateBatchResponse { - repeated UpdateResult result = 1; - double time = 2; // Time spent to process -} - -// --------------------------------------------- -// ------------- Filter Conditions ------------- -// --------------------------------------------- - -message Filter { - repeated Condition should = 1; // At least one of those conditions should match - repeated Condition must = 2; // All conditions must match - repeated Condition must_not = 3; // All conditions must NOT match -} - -message Condition { - oneof condition_one_of { - FieldCondition field = 1; - IsEmptyCondition is_empty = 2; - HasIdCondition has_id = 3; - Filter filter = 4; - IsNullCondition is_null = 5; - NestedCondition nested = 6; - } -} - -message IsEmptyCondition { - string key = 1; -} - -message IsNullCondition { - string key = 1; -} - -message HasIdCondition { - repeated PointId has_id = 1; -} - -message NestedCondition { - string key = 1; // Path to nested object - Filter filter = 2; // Filter condition -} - -message FieldCondition { - string key = 1; - Match match = 2; // Check if point has field with a given value - Range range = 3; // Check if points value lies in a given range - GeoBoundingBox geo_bounding_box = 4; // Check if points geolocation lies in a given area - GeoRadius geo_radius = 5; // Check if geo point is within a given radius - ValuesCount values_count = 6; // Check number of values for a specific field - GeoPolygon geo_polygon = 7; // Check if geo point is within a given polygon -} - -message Match { - oneof match_value { - string keyword = 1; // Match string keyword - int64 integer = 2; // Match integer - bool boolean = 3; // Match boolean - string text = 4; // Match text - RepeatedStrings keywords = 5; // Match multiple keywords - RepeatedIntegers integers = 6; // Match multiple integers - RepeatedIntegers except_integers = 7; // Match any other value except those integers - RepeatedStrings except_keywords = 8; // Match any other value except those keywords - } -} - -message RepeatedStrings { - repeated string strings = 1; -} - -message RepeatedIntegers { - repeated int64 integers = 1; -} - -message Range { - optional double lt = 1; - optional double gt = 2; - optional double gte = 3; - optional double lte = 4; -} - -message GeoBoundingBox { - GeoPoint top_left = 1; // north-west corner - GeoPoint bottom_right = 2; // south-east corner -} - -message GeoRadius { - GeoPoint center = 1; // Center of the circle - float radius = 2; // In meters -} - -message GeoLineString { - repeated GeoPoint points = 1; // Ordered sequence of GeoPoints representing the line -} - -// For a valid GeoPolygon, both the exterior and interior GeoLineStrings must consist of a minimum of 4 points. -// Additionally, the first and last points of each GeoLineString must be the same. -message GeoPolygon { - GeoLineString exterior = 1; // The exterior line bounds the surface - repeated GeoLineString interiors = 2; // Interior lines (if present) bound holes within the surface -} - -message ValuesCount { - optional uint64 lt = 1; - optional uint64 gt = 2; - optional uint64 gte = 3; - optional uint64 lte = 4; -} - -// --------------------------------------------- -// -------------- Points Selector -------------- -// --------------------------------------------- - -message PointsSelector { - oneof points_selector_one_of { - PointsIdsList points = 1; - Filter filter = 2; - } -} - -message PointsIdsList { - repeated PointId ids = 1; -} - -// --------------------------------------------- -// ------------------- Point ------------------- -// --------------------------------------------- - - -message PointStruct { - PointId id = 1; - reserved 2; // deprecated "vector" field - map payload = 3; - optional Vectors vectors = 4; -} - - -message GeoPoint { - double lon = 1; - double lat = 2; -} diff --git a/src/main/proto/points_service.proto b/src/main/proto/points_service.proto deleted file mode 100644 index 79c15937..00000000 --- a/src/main/proto/points_service.proto +++ /dev/null @@ -1,93 +0,0 @@ -syntax = "proto3"; - -import "points.proto"; - -package qdrant; - -option java_package = "io.qdrant.client.grpc"; - -import "google/protobuf/struct.proto"; - -service Points { - /* - Perform insert + updates on points. If a point with a given ID already exists - it will be overwritten. - */ - rpc Upsert (UpsertPoints) returns (PointsOperationResponse) {} - /* - Delete points - */ - rpc Delete (DeletePoints) returns (PointsOperationResponse) {} - /* - Retrieve points - */ - rpc Get (GetPoints) returns (GetResponse) {} - /* - Update named vectors for point - */ - rpc UpdateVectors (UpdatePointVectors) returns (PointsOperationResponse) {} - /* - Delete named vectors for points - */ - rpc DeleteVectors (DeletePointVectors) returns (PointsOperationResponse) {} - /* - Set payload for points - */ - rpc SetPayload (SetPayloadPoints) returns (PointsOperationResponse) {} - /* - Overwrite payload for points - */ - rpc OverwritePayload (SetPayloadPoints) returns (PointsOperationResponse) {} - /* - Delete specified key payload for points - */ - rpc DeletePayload (DeletePayloadPoints) returns (PointsOperationResponse) {} - /* - Remove all payload for specified points - */ - rpc ClearPayload (ClearPayloadPoints) returns (PointsOperationResponse) {} - /* - Create index for field in collection - */ - rpc CreateFieldIndex (CreateFieldIndexCollection) returns (PointsOperationResponse) {} - /* - Delete field index for collection - */ - rpc DeleteFieldIndex (DeleteFieldIndexCollection) returns (PointsOperationResponse) {} - /* - Retrieve closest points based on vector similarity and given filtering conditions - */ - rpc Search (SearchPoints) returns (SearchResponse) {} - /* - Retrieve closest points based on vector similarity and given filtering conditions - */ - rpc SearchBatch (SearchBatchPoints) returns (SearchBatchResponse) {} - /* - Retrieve closest points based on vector similarity and given filtering conditions, grouped by a given field - */ - rpc SearchGroups (SearchPointGroups) returns (SearchGroupsResponse) {} - /* - Iterate over all or filtered points - */ - rpc Scroll (ScrollPoints) returns (ScrollResponse) {} - /* - Look for the points which are closer to stored positive examples and at the same time further to negative examples. - */ - rpc Recommend (RecommendPoints) returns (RecommendResponse) {} - /* - Look for the points which are closer to stored positive examples and at the same time further to negative examples. - */ - rpc RecommendBatch (RecommendBatchPoints) returns (RecommendBatchResponse) {} - /* - Look for the points which are closer to stored positive examples and at the same time further to negative examples, grouped by a given field - */ - rpc RecommendGroups (RecommendPointGroups) returns (RecommendGroupsResponse) {} - /* - Count points in collection with given filtering conditions - */ - rpc Count (CountPoints) returns (CountResponse) {} - - /* - Perform multiple update operations in one request - */ - rpc UpdateBatch (UpdateBatchPoints) returns (UpdateBatchResponse) {} -} diff --git a/src/main/proto/qdrant.proto b/src/main/proto/qdrant.proto deleted file mode 100644 index 97b525e7..00000000 --- a/src/main/proto/qdrant.proto +++ /dev/null @@ -1,20 +0,0 @@ -syntax = "proto3"; - -import "collections_service.proto"; -import "points_service.proto"; -import "snapshots_service.proto"; - -package qdrant; - -option java_package = "io.qdrant.client.grpc"; - -service Qdrant { - rpc HealthCheck (HealthCheckRequest) returns (HealthCheckReply) {} -} - -message HealthCheckRequest {} - -message HealthCheckReply { - string title = 1; - string version = 2; -} diff --git a/src/main/proto/snapshots_service.proto b/src/main/proto/snapshots_service.proto deleted file mode 100644 index e858a333..00000000 --- a/src/main/proto/snapshots_service.proto +++ /dev/null @@ -1,77 +0,0 @@ -syntax = "proto3"; - -package qdrant; - -import "google/protobuf/struct.proto"; -import "google/protobuf/timestamp.proto"; - -option java_package = "io.qdrant.client.grpc"; - -service Snapshots { - /* - Create collection snapshot - */ - rpc Create (CreateSnapshotRequest) returns (CreateSnapshotResponse) {} - /* - List collection snapshots - */ - rpc List (ListSnapshotsRequest) returns (ListSnapshotsResponse) {} - /* - Delete collection snapshots - */ - rpc Delete (DeleteSnapshotRequest) returns (DeleteSnapshotResponse) {} - /* - Create full storage snapshot - */ - rpc CreateFull (CreateFullSnapshotRequest) returns (CreateSnapshotResponse) {} - /* - List full storage snapshots - */ - rpc ListFull (ListFullSnapshotsRequest) returns (ListSnapshotsResponse) {} - /* - List full storage snapshots - */ - rpc DeleteFull (DeleteFullSnapshotRequest) returns (DeleteSnapshotResponse) {} - -} - -message CreateFullSnapshotRequest {} - -message ListFullSnapshotsRequest {} - -message DeleteFullSnapshotRequest { - string snapshot_name = 1; // Name of the full snapshot -} - -message CreateSnapshotRequest { - string collection_name = 1; // Name of the collection -} - -message ListSnapshotsRequest { - string collection_name = 1; // Name of the collection -} - -message DeleteSnapshotRequest { - string collection_name = 1; // Name of the collection - string snapshot_name = 2; // Name of the collection snapshot -} - -message SnapshotDescription { - string name = 1; // Name of the snapshot - google.protobuf.Timestamp creation_time = 2; // Creation time of the snapshot - int64 size = 3; // Size of the snapshot in bytes -} - -message CreateSnapshotResponse { - SnapshotDescription snapshot_description = 1; - double time = 2; // Time spent to process -} - -message ListSnapshotsResponse { - repeated SnapshotDescription snapshot_descriptions = 1; - double time = 2; // Time spent to process -} - -message DeleteSnapshotResponse { - double time = 1; // Time spent to process -} diff --git a/src/test/java/io/qdrant/client/ApiKeyTest.java b/src/test/java/io/qdrant/client/ApiKeyTest.java new file mode 100644 index 00000000..2c2f26c3 --- /dev/null +++ b/src/test/java/io/qdrant/client/ApiKeyTest.java @@ -0,0 +1,66 @@ +package io.qdrant.client; + +import io.grpc.Grpc; +import io.grpc.InsecureChannelCredentials; +import io.grpc.ManagedChannel; +import io.grpc.Status; +import io.grpc.StatusRuntimeException; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import io.qdrant.client.container.QdrantContainer; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; + +import static io.qdrant.client.grpc.QdrantOuterClass.HealthCheckReply; +import static io.qdrant.client.grpc.QdrantOuterClass.HealthCheckRequest; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +@Testcontainers +public class ApiKeyTest { + @Container + private static final QdrantContainer QDRANT_CONTAINER = new QdrantContainer().withApiKey("password!"); + private ManagedChannel channel; + + @BeforeEach + public void setup() { + channel = Grpc.newChannelBuilder( + QDRANT_CONTAINER.getGrpcHostAddress(), + InsecureChannelCredentials.create()) + .build(); + } + + @AfterEach + public void teardown() throws InterruptedException { + channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS); + } + + @Test + public void client_with_api_key_can_connect() throws ExecutionException, InterruptedException { + HealthCheckReply healthCheckReply; + try (QdrantGrpcClient grpcClient = QdrantGrpcClient.newBuilder(channel).withApiKey("password!").build()) { + healthCheckReply = grpcClient.qdrant().healthCheck(HealthCheckRequest.getDefaultInstance()).get(); + } + + assertNotNull(healthCheckReply.getTitle()); + assertNotNull(healthCheckReply.getVersion()); + } + + @Test + public void client_without_api_key_cannot_connect() { + try (QdrantGrpcClient grpcClient = QdrantGrpcClient.newBuilder(channel).build()) { + ExecutionException executionException = assertThrows( + ExecutionException.class, + () -> grpcClient.qdrant().healthCheck(HealthCheckRequest.getDefaultInstance()).get()); + Throwable cause = executionException.getCause(); + assertEquals(StatusRuntimeException.class, cause.getClass()); + StatusRuntimeException statusRuntimeException = (StatusRuntimeException) cause; + assertEquals(Status.Code.PERMISSION_DENIED, statusRuntimeException.getStatus().getCode()); + } + } +} diff --git a/src/test/java/io/qdrant/client/CollectionsTest.java b/src/test/java/io/qdrant/client/CollectionsTest.java new file mode 100644 index 00000000..26779d22 --- /dev/null +++ b/src/test/java/io/qdrant/client/CollectionsTest.java @@ -0,0 +1,254 @@ +package io.qdrant.client; + +import io.qdrant.client.grpc.Collections; +import io.grpc.Grpc; +import io.grpc.InsecureChannelCredentials; +import io.grpc.ManagedChannel; +import io.grpc.Status; +import io.grpc.StatusRuntimeException; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import io.qdrant.client.container.QdrantContainer; + +import java.util.Comparator; +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +import static io.qdrant.client.grpc.Collections.AliasDescription; +import static io.qdrant.client.grpc.Collections.CollectionInfo; +import static io.qdrant.client.grpc.Collections.CollectionStatus; +import static io.qdrant.client.grpc.Collections.CreateCollection; +import static io.qdrant.client.grpc.Collections.Distance; +import static io.qdrant.client.grpc.Collections.VectorParams; +import static io.qdrant.client.grpc.Collections.VectorsConfig; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@Testcontainers +class CollectionsTest { + @Container + private static final QdrantContainer QDRANT_CONTAINER = new QdrantContainer(); + private QdrantClient client; + private ManagedChannel channel; + private String testName; + + private static CreateCollection getCreateCollection(String collectionName) { + return CreateCollection.newBuilder() + .setCollectionName(collectionName) + .setVectorsConfig(VectorsConfig.newBuilder() + .setParams(VectorParams.newBuilder() + .setDistance(Distance.Cosine) + .setSize(4) + .build()) + .build()) + .build(); + } + + @BeforeEach + public void setup(TestInfo testInfo) { + testName = testInfo.getDisplayName().replace("()", ""); + channel = Grpc.newChannelBuilder( + QDRANT_CONTAINER.getGrpcHostAddress(), + InsecureChannelCredentials.create()) + .build(); + QdrantGrpcClient grpcClient = QdrantGrpcClient.newBuilder(channel).build(); + client = new QdrantClient(grpcClient); + } + + @AfterEach + public void teardown() throws Exception { + List collectionNames = client.listCollectionsAsync().get(); + for (String collectionName : collectionNames) { + client.deleteCollectionAsync(collectionName).get(); + } + client.close(); + channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS); + } + + @Test + void createCollection() throws ExecutionException, InterruptedException { + client.createCollectionAsync( + testName, + VectorParams.newBuilder() + .setDistance(Distance.Cosine) + .setSize(4) + .build()).get(); + + List collections = client.listCollectionsAsync().get(); + assertTrue(collections.contains(testName)); + } + + @Test + void createCollection_for_existing_collection() throws ExecutionException, InterruptedException { + CreateCollection createCollection = getCreateCollection(testName); + client.createCollectionAsync(createCollection).get(); + + ExecutionException exception = assertThrows(ExecutionException.class, () -> + client.createCollectionAsync(createCollection).get()); + assertEquals(StatusRuntimeException.class, exception.getCause().getClass()); + } + + @Test + void createCollection_with_no_collection_name() { + CreateCollection createCollection = CreateCollection.newBuilder() + .setVectorsConfig(VectorsConfig.newBuilder() + .setParams(VectorParams.newBuilder() + .setDistance(Distance.Cosine) + .setSize(4) + .build()) + .build()) + .build(); + + assertThrows( + IllegalArgumentException.class, + () -> client.createCollectionAsync(createCollection).get()); + } + + @Test + public void recreateCollection() throws ExecutionException, InterruptedException { + CreateCollection createCollection = getCreateCollection(testName); + client.createCollectionAsync(createCollection).get(); + client.recreateCollectionAsync(createCollection).get(); + + List collections = client.listCollectionsAsync().get(); + assertTrue(collections.contains(testName)); + } + + @Test + public void updateCollection() throws ExecutionException, InterruptedException { + CreateCollection createCollection = getCreateCollection(testName); + client.createCollectionAsync(createCollection).get(); + client.updateCollectionAsync(Collections.UpdateCollection.newBuilder() + .setCollectionName(testName) + .setVectorsConfig(Collections.VectorsConfigDiff.newBuilder() + .setParams(Collections.VectorParamsDiff.newBuilder() + .setOnDisk(false) + .build()) + .build()) + .build()).get(); + } + + @Test + public void updateCollection_with_no_changes() throws ExecutionException, InterruptedException { + CreateCollection createCollection = getCreateCollection(testName); + client.createCollectionAsync(createCollection).get(); + client.updateCollectionAsync(Collections.UpdateCollection.newBuilder() + .setCollectionName(testName) + .build()).get(); + } + + @Test + public void deleteCollection() throws ExecutionException, InterruptedException { + CreateCollection createCollection = getCreateCollection(testName); + client.createCollectionAsync(createCollection).get(); + client.deleteCollectionAsync(testName).get(); + + List collections = client.listCollectionsAsync().get(); + assertTrue(collections.isEmpty()); + } + + @Test + public void deleteCollection_with_missing_collection() { + ExecutionException exception = assertThrows(ExecutionException.class, () -> client.deleteCollectionAsync(testName).get()); + Throwable cause = exception.getCause(); + assertEquals(QdrantException.class, cause.getClass()); + } + + @Test + public void getCollectionInfo() throws ExecutionException, InterruptedException { + CreateCollection createCollection = getCreateCollection(testName); + client.createCollectionAsync(createCollection).get(); + CollectionInfo collectionInfo = client.getCollectionInfoAsync(testName).get(); + assertEquals(CollectionStatus.Green, collectionInfo.getStatus()); + } + + @Test + public void getCollectionInfo_with_missing_collection() { + ExecutionException exception = assertThrows(ExecutionException.class, () -> + client.getCollectionInfoAsync(testName).get()); + Throwable cause = exception.getCause(); + assertEquals(StatusRuntimeException.class, cause.getClass()); + StatusRuntimeException underlyingException = (StatusRuntimeException) cause; + assertEquals(Status.Code.NOT_FOUND, underlyingException.getStatus().getCode()); + } + + @Test + public void createAlias() throws ExecutionException, InterruptedException { + CreateCollection createCollection = getCreateCollection(testName); + client.createCollectionAsync(createCollection).get(); + client.createAliasAsync("alias_1", testName).get(); + } + + @Test + public void listAliases() throws ExecutionException, InterruptedException { + CreateCollection createCollection = getCreateCollection(testName); + client.createCollectionAsync(createCollection).get(); + client.createAliasAsync("alias_1", testName).get(); + client.createAliasAsync("alias_2", testName).get(); + + List aliasDescriptions = client.listAliasesAsync().get(); + + List sorted = aliasDescriptions.stream() + .sorted(Comparator.comparing(AliasDescription::getAliasName)) + .collect(Collectors.toList()); + + assertEquals(2, sorted.size()); + assertEquals("alias_1", sorted.get(0).getAliasName()); + assertEquals(testName, sorted.get(0).getCollectionName()); + assertEquals("alias_2", sorted.get(1).getAliasName()); + assertEquals(testName, sorted.get(1).getCollectionName()); + } + + @Test + public void listCollectionAliases() throws ExecutionException, InterruptedException { + CreateCollection createCollection = getCreateCollection(testName); + client.createCollectionAsync(createCollection).get(); + client.createAliasAsync("alias_1", testName).get(); + client.createAliasAsync("alias_2", testName).get(); + + List aliases = client.listCollectionAliasesAsync(testName).get(); + + List sorted = aliases.stream() + .sorted() + .collect(Collectors.toList()); + + assertEquals(2, sorted.size()); + assertEquals("alias_1", sorted.get(0)); + assertEquals("alias_2", sorted.get(1)); + } + + @Test + public void renameAlias() throws ExecutionException, InterruptedException { + CreateCollection createCollection = getCreateCollection(testName); + client.createCollectionAsync(createCollection).get(); + client.createAliasAsync("alias_1", testName).get(); + client.renameAliasAsync("alias_1", "alias_2").get(); + + List aliases = client.listCollectionAliasesAsync(testName).get(); + + List sorted = aliases.stream() + .sorted() + .collect(Collectors.toList()); + + assertEquals(1, sorted.size()); + assertEquals("alias_2", sorted.get(0)); + } + + @Test + public void deleteAlias() throws ExecutionException, InterruptedException { + CreateCollection createCollection = getCreateCollection(testName); + client.createCollectionAsync(createCollection).get(); + client.createAliasAsync("alias_1", testName).get(); + client.deleteAliasAsync("alias_1").get(); + + List aliases = client.listCollectionAliasesAsync(testName).get(); + assertTrue(aliases.isEmpty()); + } +} diff --git a/src/test/java/io/qdrant/client/HealthTest.java b/src/test/java/io/qdrant/client/HealthTest.java new file mode 100644 index 00000000..39afa29e --- /dev/null +++ b/src/test/java/io/qdrant/client/HealthTest.java @@ -0,0 +1,48 @@ +package io.qdrant.client; + +import io.grpc.Grpc; +import io.grpc.InsecureChannelCredentials; +import io.grpc.ManagedChannel; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import io.qdrant.client.container.QdrantContainer; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; + +import static io.qdrant.client.grpc.QdrantOuterClass.HealthCheckReply; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +@Testcontainers +class HealthTest { + @Container + private static final QdrantContainer QDRANT_CONTAINER = new QdrantContainer(); + private QdrantClient client; + private ManagedChannel channel; + + @BeforeEach + public void setup() { + channel = Grpc.newChannelBuilder( + QDRANT_CONTAINER.getGrpcHostAddress(), + InsecureChannelCredentials.create()) + .build(); + QdrantGrpcClient grpcClient = QdrantGrpcClient.newBuilder(channel).build(); + client = new QdrantClient(grpcClient); + } + + @AfterEach + public void teardown() throws Exception { + client.close(); + channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS); + } + + @Test + public void healthCheck() throws ExecutionException, InterruptedException { + HealthCheckReply healthCheckReply = client.healthCheckAsync().get(); + assertNotNull(healthCheckReply.getTitle()); + assertNotNull(healthCheckReply.getVersion()); + } +} diff --git a/src/test/java/io/qdrant/client/PointsTest.java b/src/test/java/io/qdrant/client/PointsTest.java new file mode 100644 index 00000000..82c30c5d --- /dev/null +++ b/src/test/java/io/qdrant/client/PointsTest.java @@ -0,0 +1,532 @@ +package io.qdrant.client; + +import io.grpc.Grpc; +import io.grpc.InsecureChannelCredentials; +import io.grpc.ManagedChannel; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.shaded.com.google.common.collect.ImmutableList; +import org.testcontainers.shaded.com.google.common.collect.ImmutableMap; +import org.testcontainers.shaded.com.google.common.collect.ImmutableSet; +import io.qdrant.client.container.QdrantContainer; + +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; + +import static io.qdrant.client.grpc.Collections.CollectionInfo; +import static io.qdrant.client.grpc.Collections.CreateCollection; +import static io.qdrant.client.grpc.Collections.Distance; +import static io.qdrant.client.grpc.Collections.PayloadSchemaType; +import static io.qdrant.client.grpc.Collections.VectorParams; +import static io.qdrant.client.grpc.Collections.VectorsConfig; +import static io.qdrant.client.grpc.Points.BatchResult; +import static io.qdrant.client.grpc.Points.Filter; +import static io.qdrant.client.grpc.Points.PointGroup; +import static io.qdrant.client.grpc.Points.PointStruct; +import static io.qdrant.client.grpc.Points.RecommendPointGroups; +import static io.qdrant.client.grpc.Points.RecommendPoints; +import static io.qdrant.client.grpc.Points.RetrievedPoint; +import static io.qdrant.client.grpc.Points.ScoredPoint; +import static io.qdrant.client.grpc.Points.ScrollPoints; +import static io.qdrant.client.grpc.Points.ScrollResponse; +import static io.qdrant.client.grpc.Points.SearchPointGroups; +import static io.qdrant.client.grpc.Points.SearchPoints; +import static io.qdrant.client.grpc.Points.UpdateResult; +import static io.qdrant.client.grpc.Points.UpdateStatus; +import static io.qdrant.client.grpc.Points.Vectors; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static io.qdrant.client.ConditionFactory.hasId; +import static io.qdrant.client.ConditionFactory.matchKeyword; +import static io.qdrant.client.PointIdFactory.id; +import static io.qdrant.client.ValueFactory.value; +import static io.qdrant.client.VectorsFactory.vector; + +@Testcontainers +class PointsTest { + @Container + private static final QdrantContainer QDRANT_CONTAINER = new QdrantContainer(); + private QdrantClient client; + private ManagedChannel channel; + private String testName; + + @BeforeEach + public void setup(TestInfo testInfo) { + testName = testInfo.getDisplayName().replace("()", ""); + channel = Grpc.newChannelBuilder( + QDRANT_CONTAINER.getGrpcHostAddress(), + InsecureChannelCredentials.create()) + .build(); + QdrantGrpcClient grpcClient = QdrantGrpcClient.newBuilder(channel).build(); + client = new QdrantClient(grpcClient); + } + + @AfterEach + public void teardown() throws Exception { + List collectionNames = client.listCollectionsAsync().get(); + for (String collectionName : collectionNames) { + client.deleteCollectionAsync(collectionName).get(); + } + client.close(); + channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS); + } + + @Test + public void retrieve() throws ExecutionException, InterruptedException { + createAndSeedCollection(testName); + + List points = client.retrieveAsync( + testName, + id(9), + null).get(); + + assertEquals(1, points.size()); + RetrievedPoint point = points.get(0); + assertEquals(id(9), point.getId()); + assertEquals(ImmutableSet.of("foo", "bar"), point.getPayloadMap().keySet()); + assertEquals(value("goodbye"), point.getPayloadMap().get("foo")); + assertEquals(value(2), point.getPayloadMap().get("bar")); + assertEquals(Vectors.getDefaultInstance(), point.getVectors()); + } + + @Test + public void retrieve_with_vector_without_payload() throws ExecutionException, InterruptedException { + createAndSeedCollection(testName); + + List points = client.retrieveAsync( + testName, + id(8), + false, + true, + null).get(); + + assertEquals(1, points.size()); + RetrievedPoint point = points.get(0); + assertEquals(id(8), point.getId()); + assertTrue(point.getPayloadMap().isEmpty()); + assertEquals(Vectors.VectorsOptionsCase.VECTOR, point.getVectors().getVectorsOptionsCase()); + } + + @Test + public void setPayload() throws ExecutionException, InterruptedException { + createAndSeedCollection(testName); + + client.setPayloadAsync( + testName, + ImmutableMap.of("bar", value("some bar")), + id(9), + null, + null, + null).get(); + + List points = + client.retrieveAsync(testName, id(9), null).get(); + + assertEquals(1, points.size()); + RetrievedPoint point = points.get(0); + assertEquals(id(9), point.getId()); + assertEquals(ImmutableSet.of("foo", "bar"), point.getPayloadMap().keySet()); + assertEquals(value("some bar"), point.getPayloadMap().get("bar")); + assertEquals(value("goodbye"), point.getPayloadMap().get("foo")); + } + + @Test + public void overwritePayload() throws ExecutionException, InterruptedException { + createAndSeedCollection(testName); + + client.overwritePayloadAsync( + testName, + ImmutableMap.of("bar", value("some bar")), + id(9), + null, + null, + null).get(); + + List points = + client.retrieveAsync(testName, id(9), null).get(); + + assertEquals(1, points.size()); + RetrievedPoint point = points.get(0); + assertEquals(id(9), point.getId()); + assertEquals(ImmutableSet.of("bar"), point.getPayloadMap().keySet()); + assertEquals(value("some bar"), point.getPayloadMap().get("bar")); + } + + @Test + public void deletePayload() throws ExecutionException, InterruptedException { + createAndSeedCollection(testName); + + client.setPayloadAsync( + testName, + ImmutableMap.of("bar", value("some bar")), + id(9), + null, + null, + null).get(); + + client.deletePayloadAsync(testName, ImmutableList.of("foo"), id(9), null, null, null).get(); + + List points = + client.retrieveAsync(testName, id(9), null).get(); + + assertEquals(1, points.size()); + RetrievedPoint point = points.get(0); + assertEquals(id(9), point.getId()); + assertEquals(ImmutableSet.of("bar"), point.getPayloadMap().keySet()); + assertEquals(value("some bar"), point.getPayloadMap().get("bar")); + } + + @Test + public void clearPayload() throws ExecutionException, InterruptedException { + createAndSeedCollection(testName); + + client.clearPayloadAsync(testName, id(9), true, null, null).get(); + + List points = + client.retrieveAsync(testName, id(9), null).get(); + + assertEquals(1, points.size()); + RetrievedPoint point = points.get(0); + assertEquals(id(9), point.getId()); + assertTrue(point.getPayloadMap().isEmpty()); + } + + @Test + public void createFieldIndex() throws ExecutionException, InterruptedException { + createAndSeedCollection(testName); + + UpdateResult result = client.createPayloadIndexAsync( + testName, + "foo", + PayloadSchemaType.Keyword, + null, + null, + null, + null).get(); + + assertEquals(UpdateStatus.Completed, result.getStatus()); + + result = client.createPayloadIndexAsync( + testName, + "bar", + PayloadSchemaType.Integer, + null, + null, + null, + null).get(); + + assertEquals(UpdateStatus.Completed, result.getStatus()); + + CollectionInfo collectionInfo = client.getCollectionInfoAsync(testName).get(); + assertEquals(ImmutableSet.of("foo", "bar"), collectionInfo.getPayloadSchemaMap().keySet()); + assertEquals(PayloadSchemaType.Keyword, collectionInfo.getPayloadSchemaMap().get("foo").getDataType()); + assertEquals(PayloadSchemaType.Integer, collectionInfo.getPayloadSchemaMap().get("bar").getDataType()); + } + + @Test + public void deleteFieldIndex() throws ExecutionException, InterruptedException { + createAndSeedCollection(testName); + + UpdateResult result = client.createPayloadIndexAsync( + testName, + "foo", + PayloadSchemaType.Keyword, + null, + null, + null, + null).get(); + assertEquals(UpdateStatus.Completed, result.getStatus()); + + result = client.deletePayloadIndexAsync( + testName, + "foo", + null, + null, + null).get(); + assertEquals(UpdateStatus.Completed, result.getStatus()); + } + + @Test + public void search() throws ExecutionException, InterruptedException { + createAndSeedCollection(testName); + + List points = client.searchAsync( + SearchPoints.newBuilder() + .setCollectionName(testName) + .setWithPayload(WithPayloadSelectorFactory.enable(true)) + .addAllVector(ImmutableList.of(10.4f, 11.4f)) + .setLimit(1) + .build()).get(); + + assertEquals(1, points.size()); + ScoredPoint point = points.get(0); + assertEquals(id(9), point.getId()); + assertEquals(ImmutableSet.of("foo", "bar"), point.getPayloadMap().keySet()); + assertEquals(value("goodbye"), point.getPayloadMap().get("foo")); + assertEquals(value(2), point.getPayloadMap().get("bar")); + assertFalse(point.getVectors().hasVector()); + } + + @Test + public void searchBatch() throws ExecutionException, InterruptedException { + createAndSeedCollection(testName); + + List batchResults = client.searchBatchAsync(testName, + ImmutableList.of( + SearchPoints.newBuilder() + .addAllVector(ImmutableList.of(10.4f, 11.4f)) + .setLimit(1) + .build(), + SearchPoints.newBuilder() + .addAllVector(ImmutableList.of(3.4f, 4.4f)) + .setLimit(1) + .build() + ), null).get(); + + assertEquals(2, batchResults.size()); + BatchResult result = batchResults.get(0); + assertEquals(1, result.getResultCount()); + assertEquals(id(9), result.getResult(0).getId()); + result = batchResults.get(1); + assertEquals(1, result.getResultCount()); + assertEquals(id(8), result.getResult(0).getId()); + } + + @Test + public void searchGroups() throws ExecutionException, InterruptedException { + createAndSeedCollection(testName); + + client.upsertAsync( + testName, + ImmutableList.of( + PointStruct.newBuilder() + .setId(id(10)) + .setVectors(VectorsFactory.vector(30f, 31f)) + .putAllPayload(ImmutableMap.of("foo", value("hello"))) + .build() + ) + ).get(); + + List groups = client.searchGroupsAsync(SearchPointGroups.newBuilder() + .setCollectionName(testName) + .addAllVector(ImmutableList.of(10.4f, 11.4f)) + .setGroupBy("foo") + .setGroupSize(2) + .setLimit(10) + .build() + ).get(); + + assertEquals(2, groups.size()); + assertEquals(1, groups.stream().filter(g -> g.getHitsCount() == 2).count()); + assertEquals(1, groups.stream().filter(g -> g.getHitsCount() == 1).count()); + } + + @Test + public void scroll() throws ExecutionException, InterruptedException { + createAndSeedCollection(testName); + + ScrollResponse scrollResponse = client.scrollAsync(ScrollPoints.newBuilder() + .setCollectionName(testName) + .setLimit(1) + .build() + ).get(); + + assertEquals(1, scrollResponse.getResultCount()); + assertTrue(scrollResponse.hasNextPageOffset()); + + scrollResponse = client.scrollAsync(ScrollPoints.newBuilder() + .setCollectionName(testName) + .setLimit(1) + .setOffset(scrollResponse.getNextPageOffset()) + .build() + ).get(); + + assertEquals(1, scrollResponse.getResultCount()); + assertFalse(scrollResponse.hasNextPageOffset()); + } + + @Test + public void recommend() throws ExecutionException, InterruptedException { + createAndSeedCollection(testName); + + List points = client.recommendAsync(RecommendPoints.newBuilder() + .setCollectionName(testName) + .addPositive(id(8)) + .setLimit(1) + .build() + ).get(); + + assertEquals(1, points.size()); + assertEquals(id(9), points.get(0).getId()); + } + + @Test + public void recommendBatch() throws ExecutionException, InterruptedException { + createAndSeedCollection(testName); + + List batchResults = client.recommendBatchAsync(testName, + ImmutableList.of( + RecommendPoints.newBuilder() + .setCollectionName(testName) + .addPositive(id(8)) + .setLimit(1) + .build(), + RecommendPoints.newBuilder() + .setCollectionName(testName) + .addPositive(id(9)) + .setLimit(1) + .build() + ), + null + ).get(); + + assertEquals(2, batchResults.size()); + BatchResult result = batchResults.get(0); + assertEquals(1, result.getResultCount()); + assertEquals(id(9), result.getResult(0).getId()); + result = batchResults.get(1); + assertEquals(1, result.getResultCount()); + assertEquals(id(8), result.getResult(0).getId()); + } + + @Test + public void recommendGroups() throws ExecutionException, InterruptedException { + createAndSeedCollection(testName); + + client.upsertAsync( + testName, + ImmutableList.of( + PointStruct.newBuilder() + .setId(id(10)) + .setVectors(VectorsFactory.vector(30f, 31f)) + .putAllPayload(ImmutableMap.of("foo", value("hello"))) + .build() + ) + ).get(); + + List groups = client.recommendGroupsAsync(RecommendPointGroups.newBuilder() + .setCollectionName(testName) + .setGroupBy("foo") + .addPositive(id(9)) + .setGroupSize(2) + .setLimit(10) + .build() + ).get(); + + assertEquals(1, groups.size()); + assertEquals(2, groups.get(0).getHitsCount()); + } + + @Test + public void count() throws ExecutionException, InterruptedException { + createAndSeedCollection(testName); + Long count = client.countAsync(testName).get(); + assertEquals(2, count); + } + + @Test + public void count_with_filter() throws ExecutionException, InterruptedException { + createAndSeedCollection(testName); + Long count = client.countAsync( + testName, + Filter.newBuilder() + .addMust(hasId(id(9))) + .addMust(matchKeyword("foo", "goodbye")) + .build(), + null + ).get(); + assertEquals(1, count); + } + + @Test + public void delete_by_id() throws ExecutionException, InterruptedException { + createAndSeedCollection(testName); + + List points = client.retrieveAsync( + testName, + id(8), + false, + false, + null).get(); + + assertEquals(1, points.size()); + + client.deleteAsync(testName, ImmutableList.of(id(8))).get(); + + points = client.retrieveAsync( + testName, + id(8), + false, + false, + null).get(); + + assertEquals(0, points.size()); + } + + @Test + public void delete_by_filter() throws ExecutionException, InterruptedException { + createAndSeedCollection(testName); + + List points = client.retrieveAsync( + testName, + id(8), + false, + false, + null).get(); + + assertEquals(1, points.size()); + + client.deleteAsync( + testName, + Filter.newBuilder().addMust(matchKeyword("foo", "hello")).build()).get(); + + points = client.retrieveAsync( + testName, + id(8), + false, + false, + null).get(); + + assertEquals(0, points.size()); + } + + private void createAndSeedCollection(String collectionName) throws ExecutionException, InterruptedException { + CreateCollection request = CreateCollection.newBuilder() + .setCollectionName(collectionName) + .setVectorsConfig(VectorsConfig.newBuilder() + .setParams(VectorParams.newBuilder() + .setDistance(Distance.Cosine) + .setSize(2) + .build()) + .build()) + .build(); + + client.createCollectionAsync(request).get(); + + UpdateResult result = client.upsertAsync(collectionName, ImmutableList.of( + PointStruct.newBuilder() + .setId(id(8)) + .setVectors(VectorsFactory.vector(ImmutableList.of(3.5f, 4.5f))) + .putAllPayload(ImmutableMap.of( + "foo", value("hello"), + "bar", value(1) + )) + .build(), + PointStruct.newBuilder() + .setId(id(9)) + .setVectors(VectorsFactory.vector(ImmutableList.of(10.5f, 11.5f))) + .putAllPayload(ImmutableMap.of( + "foo", value("goodbye"), + "bar", value(2) + )) + .build() + )).get(); + assertEquals(UpdateStatus.Completed, result.getStatus()); + } +} diff --git a/src/test/java/io/qdrant/client/QdrantClientCollectionTest.java b/src/test/java/io/qdrant/client/QdrantClientCollectionTest.java deleted file mode 100644 index af7e9996..00000000 --- a/src/test/java/io/qdrant/client/QdrantClientCollectionTest.java +++ /dev/null @@ -1,158 +0,0 @@ -package io.qdrant.client; - -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import io.grpc.StatusRuntimeException; -import io.qdrant.client.grpc.Collections; -import io.qdrant.client.grpc.Collections.Distance; -import java.util.UUID; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -class QdrantClientCollectionTest { - - private static QdrantClient qdrantClient; - - @BeforeAll - static void setUp() throws Exception { - String qdrantUrl = System.getenv("QDRANT_URL"); - String apiKey = System.getenv("QDRANT_API_KEY"); - - if (qdrantUrl == null) { - qdrantUrl = "http://localhost:6334"; - } - - if (apiKey == null) { - qdrantClient = new QdrantClient(qdrantUrl); - } else { - qdrantClient = new QdrantClient(qdrantUrl, apiKey); - } - } - - @Test - void testAliasOperations() { - String collectionName = UUID.randomUUID().toString(); - String aliasName = UUID.randomUUID().toString(); - - assertThrows( - StatusRuntimeException.class, - () -> { - // This should fail as collection does not exist - qdrantClient.createAlias(collectionName, aliasName); - }); - - qdrantClient.createCollection(collectionName, 6, Distance.Euclid); - assertDoesNotThrow( - () -> { - Collections.CollectionOperationResponse response = - qdrantClient.createAlias(collectionName, aliasName); - assertTrue(response.getResult()); - }); - - Collections.ListAliasesResponse response = qdrantClient.listAliases(); - assertTrue(response.getAliasesCount() == 1); - Collections.AliasDescription alias = response.getAliasesList().get(0); - assertTrue(alias.getCollectionName().equals(collectionName)); - assertTrue(alias.getAliasName().equals(aliasName)); - - String newAliasName = UUID.randomUUID().toString(); - qdrantClient.renameAlias(aliasName, newAliasName); - response = qdrantClient.listAliases(); - assertTrue(response.getAliasesCount() == 1); - - alias = response.getAliasesList().get(0); - assertTrue(alias.getCollectionName().equals(collectionName)); - assertTrue(alias.getAliasName().equals(newAliasName)); - - qdrantClient.deleteAlias(newAliasName); - response = qdrantClient.listAliases(); - } - - @Test - void testListCollections() { - assertDoesNotThrow( - () -> { - Collections.ListCollectionsResponse response = qdrantClient.listCollections(); - assertTrue(response.getCollectionsCount() >= 0); - }); - } - - @Test - void testHasCollection() { - String collectionName = UUID.randomUUID().toString(); - boolean exists = qdrantClient.hasCollection(collectionName); - assertFalse(exists); - - qdrantClient.createCollection(collectionName, 6, Distance.Euclid); - - exists = qdrantClient.hasCollection(collectionName); - assertTrue(exists); - } - - @Test - void testCollectionConfigOperations() { - long vectorSize = 128; - String collectionName = UUID.randomUUID().toString(); - Collections.Distance distance = Collections.Distance.Cosine; - - Collections.VectorParams.Builder params = - Collections.VectorParams.newBuilder().setDistance(distance).setSize(vectorSize); - - Collections.VectorsConfig config = - Collections.VectorsConfig.newBuilder().setParams(params).build(); - - Collections.HnswConfigDiff hnsw = - Collections.HnswConfigDiff.newBuilder().setM(16).setEfConstruct(200).build(); - - Collections.CreateCollection details = - Collections.CreateCollection.newBuilder() - .setVectorsConfig(config) - .setHnswConfig(hnsw) - .setCollectionName(collectionName) - .build(); - - Collections.CollectionOperationResponse response = qdrantClient.createCollection(details); - assertTrue(response.getResult()); - - Collections.GetCollectionInfoResponse info = qdrantClient.getCollectionInfo(collectionName); - assertTrue(info.getResult().getConfig().getHnswConfig().getM() == 16); - - Collections.UpdateCollection updateCollection = - Collections.UpdateCollection.newBuilder() - .setCollectionName(collectionName) - .setHnswConfig(Collections.HnswConfigDiff.newBuilder().setM(32).build()) - .build(); - qdrantClient.updateCollection(updateCollection); - - info = qdrantClient.getCollectionInfo(collectionName); - assertTrue(info.getResult().getConfig().getHnswConfig().getM() == 32); - } - - @Test - void testRecreateCollection() { - String collectionName = UUID.randomUUID().toString(); - - qdrantClient.createCollection(collectionName, 6, Distance.Euclid); - assertDoesNotThrow( - () -> { - qdrantClient.recreateCollection(collectionName, 12, Distance.Dot); - }); - } - - @Test - void testDeleteCollection() { - String collectionName = UUID.randomUUID().toString(); - - Collections.CollectionOperationResponse response = - qdrantClient.deleteCollection(collectionName); - assertFalse(response.getResult()); - - qdrantClient.createCollection(collectionName, 6, Distance.Euclid); - - response = qdrantClient.deleteCollection(collectionName); - assertTrue(response.getResult()); - } -} diff --git a/src/test/java/io/qdrant/client/QdrantClientPointsTest.java b/src/test/java/io/qdrant/client/QdrantClientPointsTest.java deleted file mode 100644 index 410b21db..00000000 --- a/src/test/java/io/qdrant/client/QdrantClientPointsTest.java +++ /dev/null @@ -1,264 +0,0 @@ -package io.qdrant.client; - -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertEquals; - -import io.qdrant.client.grpc.Collections.Distance; -import io.qdrant.client.grpc.JsonWithInt.Value; -import io.qdrant.client.grpc.Points; -import io.qdrant.client.grpc.Points.Filter; -import io.qdrant.client.grpc.Points.PointId; -import io.qdrant.client.grpc.Points.PointStruct; -import io.qdrant.client.grpc.Points.SearchPoints; -import io.qdrant.client.grpc.Points.SearchResponse; -import io.qdrant.client.utils.FilterUtil; -import io.qdrant.client.utils.PayloadUtil; -import io.qdrant.client.utils.PointUtil; -import io.qdrant.client.utils.SelectorUtil; -import io.qdrant.client.utils.VectorUtil; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.UUID; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -class QdrantClientPointsTest { - - private static final int EMBEDDINGS_SIZE = 768; - private static QdrantClient qdrantClient; - - @BeforeAll - static void setUp() throws Exception { - String qdrantUrl = System.getenv("QDRANT_URL"); - String apiKey = System.getenv("QDRANT_API_KEY"); - - if (qdrantUrl == null) { - qdrantUrl = "http://localhost:6334"; - } - - if (apiKey == null) { - qdrantClient = new QdrantClient(qdrantUrl); - } else { - qdrantClient = new QdrantClient(qdrantUrl, apiKey); - } - } - - @Test - void testPointsWithPayloadFilters() { - String collectionName = UUID.randomUUID().toString(); - - UUID pointID = UUID.randomUUID(); - - qdrantClient.createCollection(collectionName, EMBEDDINGS_SIZE, Distance.Cosine); - - PointId[] pointIds = new PointId[] {PointUtil.pointId(pointID)}; - - Points.GetResponse response = - qdrantClient.getPoints( - collectionName, - Arrays.asList(pointIds), - SelectorUtil.withVectors(), - SelectorUtil.withPayload(), - null); - - assertEquals(0, response.getResultCount()); - - Map data = new HashMap<>(); - data.put("name", "Anush"); - data.put("age", 32); - - Map nestedData = new HashMap<>(); - nestedData.put("color", "Blue"); - nestedData.put("movie", "Man Of Steel"); - - data.put("favourites", nestedData); - - PointStruct point = - PointUtil.point( - pointID, VectorUtil.dummyVector(EMBEDDINGS_SIZE), PayloadUtil.toPayload(data)); - - List points = Arrays.asList(point); - qdrantClient.upsertPointsBlocking(collectionName, points, null); - response = - qdrantClient.getPoints( - collectionName, - Arrays.asList(pointIds), - SelectorUtil.withVectors(), - SelectorUtil.withPayload(), - null); - assertEquals(1, response.getResultCount()); - - Filter filter = - FilterUtil.must( - FilterUtil.fieldCondition("age", FilterUtil.match(32)), - FilterUtil.fieldCondition("name", FilterUtil.match("Anush")), - FilterUtil.fieldCondition("favourites.color", FilterUtil.match("Blue")), - FilterUtil.fieldCondition("favourites.movie", FilterUtil.match("Man Of Steel"))); - qdrantClient.deletePointsBlocking(collectionName, SelectorUtil.filterSelector(filter), null); - - response = - qdrantClient.getPoints( - collectionName, - Arrays.asList(pointIds), - SelectorUtil.withVectors(), - SelectorUtil.withPayload(), - null); - - assertEquals(0, response.getResultCount()); - } - - @Test - void testUpsertPoints() { - String collectionName = UUID.randomUUID().toString(); - - UUID pointID = UUID.randomUUID(); - - qdrantClient.createCollection(collectionName, EMBEDDINGS_SIZE, Distance.Cosine); - - PointId[] pointIds = new PointId[] {PointUtil.pointId(pointID)}; - - Points.GetResponse response = - qdrantClient.getPoints( - collectionName, - Arrays.asList(pointIds), - SelectorUtil.withVectors(), - SelectorUtil.withPayload(), - null); - - assertEquals(0, response.getResultCount()); - - PointStruct point = PointUtil.point(pointID, VectorUtil.dummyVector(EMBEDDINGS_SIZE), null); - List points = Arrays.asList(point); - qdrantClient.upsertPointsBlocking(collectionName, points, null); - response = - qdrantClient.getPoints( - collectionName, - Arrays.asList(pointIds), - SelectorUtil.withVectors(), - SelectorUtil.withPayload(), - null); - assertEquals(1, response.getResultCount()); - - qdrantClient.deletePointsBlocking(collectionName, SelectorUtil.idsSelector(pointIds), null); - - response = - qdrantClient.getPoints( - collectionName, - Arrays.asList(pointIds), - SelectorUtil.withVectors(), - SelectorUtil.withPayload(), - null); - - assertEquals(0, response.getResultCount()); - } - - @Test - void testUpsertPointsBatch() { - String collectionName = UUID.randomUUID().toString(); - - qdrantClient.createCollection(collectionName, EMBEDDINGS_SIZE, Distance.Cosine); - - List points = new ArrayList<>(); - - // Upsert 1000 points with batching - for (int i = 0; i < 1000; i++) { - UUID pointID = UUID.randomUUID(); - PointStruct point = PointUtil.point(pointID, VectorUtil.dummyVector(EMBEDDINGS_SIZE), null); - points.add(point); - } - - assertDoesNotThrow( - () -> { - qdrantClient.upsertPointsBatchBlocking(collectionName, points, null, 100); - }); - } - - @Test - void testSearchPoints() { - String collectionName = UUID.randomUUID().toString(); - - qdrantClient.createCollection(collectionName, EMBEDDINGS_SIZE, Distance.Cosine); - - List points = new ArrayList<>(); - - // Upsert 100 points - for (int i = 0; i < 100; i++) { - UUID pointID = UUID.randomUUID(); - PointStruct point = PointUtil.point(pointID, VectorUtil.dummyVector(EMBEDDINGS_SIZE), null); - points.add(point); - } - - assertDoesNotThrow( - () -> { - qdrantClient.upsertPointsBlocking(collectionName, points, null); - }); - - SearchPoints request = - SearchPoints.newBuilder() - .setCollectionName(collectionName) - .addAllVector(VectorUtil.dummyEmbeddings(EMBEDDINGS_SIZE)) - .setWithPayload(SelectorUtil.withPayload()) - .setLimit(100) - .build(); - - SearchResponse result = qdrantClient.searchPoints(request); - - assertEquals(result.getResultList().size(), 100); - } - - @Test - void testSetPayloadWithScroll() { - String collectionName = UUID.randomUUID().toString(); - - qdrantClient.createCollection(collectionName, EMBEDDINGS_SIZE, Distance.Cosine); - - List points = new ArrayList<>(); - - // Upsert 100 points - for (int i = 0; i < 100; i++) { - UUID pointID = UUID.randomUUID(); - PointStruct point = PointUtil.point(pointID, VectorUtil.dummyVector(EMBEDDINGS_SIZE), null); - points.add(point); - } - - assertDoesNotThrow( - () -> { - qdrantClient.upsertPointsBlocking(collectionName, points, null); - }); - - Map data = new HashMap<>(); - data.put("name", "Anush"); - data.put("age", 32); - - Map nestedData = new HashMap<>(); - nestedData.put("color", "Blue"); - nestedData.put("movie", "Man of Steel"); - - data.put("favourites", nestedData); - - Map payload = PayloadUtil.toPayload(data); - - qdrantClient.setPayloadBlocking( - collectionName, SelectorUtil.filterSelector(FilterUtil.must()), payload, null); - - Points.ScrollPoints request = - Points.ScrollPoints.newBuilder() - .setCollectionName(collectionName) - .setWithPayload(SelectorUtil.withPayload()) - .setLimit(100) - .build(); - - Points.ScrollResponse response = qdrantClient.scroll(request); - - response - .getResultList() - .forEach( - (point) -> { - assertEquals(PayloadUtil.toMap(point.getPayloadMap()), data); - assertEquals(point.getPayloadMap(), PayloadUtil.toPayload(data)); - }); - } -} diff --git a/src/test/java/io/qdrant/client/QdrantClientServiceTest.java b/src/test/java/io/qdrant/client/QdrantClientServiceTest.java deleted file mode 100644 index 0fa4b037..00000000 --- a/src/test/java/io/qdrant/client/QdrantClientServiceTest.java +++ /dev/null @@ -1,44 +0,0 @@ -package io.qdrant.client; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - -import java.net.MalformedURLException; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -class QdrantClientServiceTest { - - private static QdrantClient qdrantClient; - - @BeforeAll - static void setUp() throws Exception { - String qdrantUrl = System.getenv("QDRANT_URL"); - String apiKey = System.getenv("QDRANT_API_KEY"); - - if (qdrantUrl == null) { - qdrantUrl = "http://localhost:6334"; - } - - if (apiKey == null) { - qdrantClient = new QdrantClient(qdrantUrl); - } else { - qdrantClient = new QdrantClient(qdrantUrl, apiKey); - } - } - - @Test - void testInvalidProtocol() { - assertThrows(IllegalArgumentException.class, () -> new QdrantClient("ftp://localhost:6334")); - } - - @Test - void testMalformedUrl() { - assertThrows(MalformedURLException.class, () -> new QdrantClient("qdrant/qdrant:latest")); - } - - @Test - void testHealthCheck() { - assertEquals(qdrantClient.healthCheck().getTitle(), "qdrant - vector search engine"); - } -} diff --git a/src/test/java/io/qdrant/client/QdrantClientSnapshotsTest.java b/src/test/java/io/qdrant/client/QdrantClientSnapshotsTest.java deleted file mode 100644 index fca84a9e..00000000 --- a/src/test/java/io/qdrant/client/QdrantClientSnapshotsTest.java +++ /dev/null @@ -1,106 +0,0 @@ -package io.qdrant.client; - -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertEquals; - -import io.qdrant.client.grpc.Collections.Distance; -import io.qdrant.client.grpc.SnapshotsService; -import java.nio.file.FileSystems; -import java.nio.file.Path; -import java.util.UUID; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -class QdrantClientSnapshotsTest { - - private static QdrantClient qdrantClient; - - @BeforeAll - static void setUp() throws Exception { - String qdrantUrl = System.getenv("QDRANT_URL"); - String apiKey = System.getenv("QDRANT_API_KEY"); - - if (qdrantUrl == null) { - qdrantUrl = "http://localhost:6334"; - } - - if (apiKey == null) { - qdrantClient = new QdrantClient(qdrantUrl); - } else { - qdrantClient = new QdrantClient(qdrantUrl, apiKey); - } - } - - @Test - void testCollectionSnapshots() { - String collectionName = UUID.randomUUID().toString(); - - qdrantClient.createCollection(collectionName, 768, Distance.Cosine); - - assertEquals( - qdrantClient.listSnapshots(collectionName).getSnapshotDescriptionsList().size(), 0); - - assertDoesNotThrow( - () -> { - SnapshotsService.CreateSnapshotResponse response = - qdrantClient.createSnapshot(collectionName); - String snapshotName = response.getSnapshotDescription().getName(); - - assertEquals( - qdrantClient.listSnapshots(collectionName).getSnapshotDescriptionsList().size(), 1); - - qdrantClient.deleteSnapshot(collectionName, snapshotName); - - assertEquals( - qdrantClient.listSnapshots(collectionName).getSnapshotDescriptionsList().size(), 0); - }); - } - - @Test - void testFullSnapshots() { - - assertEquals(qdrantClient.listFullSnapshots().getSnapshotDescriptionsList().size(), 0); - - assertDoesNotThrow( - () -> { - SnapshotsService.CreateSnapshotResponse response = qdrantClient.createFullSnapshot(); - String snapshotName = response.getSnapshotDescription().getName(); - - assertEquals(qdrantClient.listFullSnapshots().getSnapshotDescriptionsList().size(), 1); - - qdrantClient.deleteFullSnapshot(snapshotName); - - assertEquals(qdrantClient.listFullSnapshots().getSnapshotDescriptionsList().size(), 0); - }); - } - - @Test - void testDownloadSnapshot() { - String collectionName = UUID.randomUUID().toString(); - - qdrantClient.createCollection(collectionName, 768, Distance.Cosine); - - assertEquals( - qdrantClient.listSnapshots(collectionName).getSnapshotDescriptionsList().size(), 0); - - // Test with snapshot name - assertDoesNotThrow( - () -> { - SnapshotsService.CreateSnapshotResponse response = - qdrantClient.createSnapshot(collectionName); - String snapshotName = response.getSnapshotDescription().getName(); - - Path path = FileSystems.getDefault().getPath("./test.snapshot"); - qdrantClient.downloadSnapshot(path, collectionName, snapshotName, null); - }); - - // Test without snapshot name - assertDoesNotThrow( - () -> { - qdrantClient.createSnapshot(collectionName); - - Path path = FileSystems.getDefault().getPath("./test_2.snapshot"); - qdrantClient.downloadSnapshot(path, collectionName, null, null); - }); - } -} diff --git a/src/test/java/io/qdrant/client/QdrantGrpcClientTest.java b/src/test/java/io/qdrant/client/QdrantGrpcClientTest.java new file mode 100644 index 00000000..3eb8b39d --- /dev/null +++ b/src/test/java/io/qdrant/client/QdrantGrpcClientTest.java @@ -0,0 +1,47 @@ +package io.qdrant.client; + +import io.qdrant.client.grpc.QdrantOuterClass; +import io.grpc.Grpc; +import io.grpc.InsecureChannelCredentials; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import io.qdrant.client.container.QdrantContainer; + +import java.util.concurrent.ExecutionException; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +@Testcontainers +class QdrantGrpcClientTest { + + @Container + private static final QdrantContainer QDRANT_CONTAINER = new QdrantContainer(); + private QdrantGrpcClient client; + + @BeforeEach + public void setup() { + client = QdrantGrpcClient.newBuilder( + Grpc.newChannelBuilder( + QDRANT_CONTAINER.getGrpcHostAddress(), + InsecureChannelCredentials.create()) + .build()) + .build(); + } + + @AfterEach + public void teardown() { + client.close(); + } + + @Test + void healthCheck() throws ExecutionException, InterruptedException { + QdrantOuterClass.HealthCheckReply healthCheckReply = + client.qdrant().healthCheck(QdrantOuterClass.HealthCheckRequest.getDefaultInstance()).get(); + + assertNotNull(healthCheckReply.getTitle()); + assertNotNull(healthCheckReply.getVersion()); + } +} \ No newline at end of file diff --git a/src/test/java/io/qdrant/client/SnapshotsTest.java b/src/test/java/io/qdrant/client/SnapshotsTest.java new file mode 100644 index 00000000..d2366736 --- /dev/null +++ b/src/test/java/io/qdrant/client/SnapshotsTest.java @@ -0,0 +1,150 @@ +package io.qdrant.client; + +import io.qdrant.client.grpc.Collections; +import io.grpc.Grpc; +import io.grpc.InsecureChannelCredentials; +import io.grpc.ManagedChannel; +import io.grpc.Status; +import io.grpc.StatusRuntimeException; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import io.qdrant.client.container.QdrantContainer; + +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; + +import static io.qdrant.client.grpc.SnapshotsService.SnapshotDescription; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +@Testcontainers +class SnapshotsTest { + @Container + private static final QdrantContainer QDRANT_CONTAINER = new QdrantContainer(); + private QdrantClient client; + private ManagedChannel channel; + private String testName; + + @BeforeEach + public void setup(TestInfo testInfo) { + testName = testInfo.getDisplayName().replace("()", ""); + channel = Grpc.newChannelBuilder( + QDRANT_CONTAINER.getGrpcHostAddress(), + InsecureChannelCredentials.create()) + .build(); + QdrantGrpcClient grpcClient = QdrantGrpcClient.newBuilder(channel).build(); + client = new QdrantClient(grpcClient); + } + + @AfterEach + public void teardown() throws Exception { + List collectionNames = client.listCollectionsAsync().get(); + for (String collectionName : collectionNames) { + List snapshots = client.listSnapshotAsync(collectionName).get(); + for (SnapshotDescription snapshot : snapshots) { + client.deleteSnapshotAsync(collectionName, snapshot.getName()).get(); + } + client.deleteCollectionAsync(collectionName).get(); + } + + List snapshots = client.listFullSnapshotAsync().get(); + for (SnapshotDescription snapshot : snapshots) { + client.deleteFullSnapshotAsync(snapshot.getName()).get(); + } + + client.close(); + channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS); + } + + @Test + public void createSnapshot() throws ExecutionException, InterruptedException { + createCollection(testName); + client.createSnapshotAsync(testName).get(); + } + + @Test + public void deleteSnapshot() throws ExecutionException, InterruptedException { + createCollection(testName); + SnapshotDescription snapshotDescription = client.createSnapshotAsync(testName).get(); + client.deleteSnapshotAsync(testName, snapshotDescription.getName()).get(); + } + + @Test + public void deleteSnapshot_with_missing_snapshot() { + ExecutionException exception = + assertThrows(ExecutionException.class, () -> client.deleteSnapshotAsync(testName, "snapshot_1").get()); + Throwable cause = exception.getCause(); + assertEquals(StatusRuntimeException.class, cause.getClass()); + StatusRuntimeException underlyingException = (StatusRuntimeException) cause; + assertEquals(Status.Code.NOT_FOUND, underlyingException.getStatus().getCode()); + } + + @Test + public void listSnapshots() throws ExecutionException, InterruptedException { + createCollection(testName); + client.createSnapshotAsync(testName).get(); + // snapshots are timestamped named to second precision. Wait more than 1 second to ensure we get 2 snapshots + Thread.sleep(2000); + client.createSnapshotAsync(testName).get(); + + List snapshotDescriptions = client.listSnapshotAsync(testName).get(); + assertEquals(2, snapshotDescriptions.size()); + } + + @Test + public void createFullSnapshot() throws ExecutionException, InterruptedException { + createCollection(testName); + createCollection(testName + "2"); + client.createFullSnapshotAsync().get(); + } + + @Test + public void deleteFullSnapshot() throws ExecutionException, InterruptedException { + createCollection(testName); + createCollection(testName + "2"); + SnapshotDescription snapshotDescription = client.createFullSnapshotAsync().get(); + client.deleteFullSnapshotAsync(snapshotDescription.getName()).get(); + } + + @Test + public void deleteFullSnapshot_with_missing_snapshot() { + ExecutionException exception = + assertThrows(ExecutionException.class, () -> client.deleteFullSnapshotAsync("snapshot_1").get()); + Throwable cause = exception.getCause(); + assertEquals(StatusRuntimeException.class, cause.getClass()); + StatusRuntimeException underlyingException = (StatusRuntimeException) cause; + assertEquals(Status.Code.NOT_FOUND, underlyingException.getStatus().getCode()); + } + + @Test + public void listFullSnapshots() throws ExecutionException, InterruptedException { + createCollection(testName); + createCollection(testName + 2); + client.createFullSnapshotAsync().get(); + // snapshots are timestamped named to second precision. Wait more than 1 second to ensure we get 2 snapshots + Thread.sleep(2000); + client.createFullSnapshotAsync().get(); + + List snapshotDescriptions = client.listFullSnapshotAsync().get(); + assertEquals(2, snapshotDescriptions.size()); + } + + private void createCollection(String collectionName) throws ExecutionException, InterruptedException { + Collections.CreateCollection request = Collections.CreateCollection.newBuilder() + .setCollectionName(collectionName) + .setVectorsConfig(Collections.VectorsConfig.newBuilder() + .setParams(Collections.VectorParams.newBuilder() + .setDistance(Collections.Distance.Cosine) + .setSize(4) + .build()) + .build()) + .build(); + + client.createCollectionAsync(request).get(); + } +} diff --git a/src/test/java/io/qdrant/client/container/QdrantContainer.java b/src/test/java/io/qdrant/client/container/QdrantContainer.java new file mode 100644 index 00000000..f6792fa1 --- /dev/null +++ b/src/test/java/io/qdrant/client/container/QdrantContainer.java @@ -0,0 +1,41 @@ +package io.qdrant.client.container; + +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy; +import org.testcontainers.utility.DockerImageName; + +public class QdrantContainer extends GenericContainer { + + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("qdrant/qdrant"); + private static final String DEFAULT_TAG = System.getProperty("qdrantVersion"); + private static final int GRPC_PORT = 6334; + private static final int HTTP_PORT = 6333; + + public QdrantContainer() { + this(DEFAULT_IMAGE_NAME.withTag(DEFAULT_TAG)); + } + + public QdrantContainer(String dockerImageName) { + this(DockerImageName.parse(dockerImageName)); + } + + public QdrantContainer(final DockerImageName dockerImageName) { + super(dockerImageName); + + addExposedPorts(GRPC_PORT, HTTP_PORT); + + setWaitStrategy(new LogMessageWaitStrategy().withRegEx(".*Actix runtime found; starting in Actix runtime.*")); + } + + public QdrantContainer withApiKey(String apiKey) { + return withEnv("QDRANT__SERVICE__API_KEY", apiKey); + } + + public String getGrpcHostAddress() { + return getHost() + ":" + getMappedPort(GRPC_PORT); + } + + public String getHttpHostAddress() { + return getHost() + ":" + getMappedPort(HTTP_PORT); + } +} diff --git a/src/test/java/io/qdrant/client/package-info.java b/src/test/java/io/qdrant/client/package-info.java new file mode 100644 index 00000000..c79c961b --- /dev/null +++ b/src/test/java/io/qdrant/client/package-info.java @@ -0,0 +1,4 @@ +@ParametersAreNonnullByDefault +package io.qdrant.client; + +import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/src/test/java/io/qdrant/client/utils/FilterUtilTest.java b/src/test/java/io/qdrant/client/utils/FilterUtilTest.java deleted file mode 100644 index d0ed7e46..00000000 --- a/src/test/java/io/qdrant/client/utils/FilterUtilTest.java +++ /dev/null @@ -1,312 +0,0 @@ -package io.qdrant.client.utils; - -import static org.junit.jupiter.api.Assertions.*; - -import io.qdrant.client.grpc.Points.Condition; -import io.qdrant.client.grpc.Points.Filter; -import io.qdrant.client.grpc.Points.GeoBoundingBox; -import io.qdrant.client.grpc.Points.GeoLineString; -import io.qdrant.client.grpc.Points.GeoPoint; -import io.qdrant.client.grpc.Points.GeoPolygon; -import io.qdrant.client.grpc.Points.GeoRadius; -import io.qdrant.client.grpc.Points.Match; -import io.qdrant.client.grpc.Points.PointId; -import io.qdrant.client.grpc.Points.Range; -import io.qdrant.client.grpc.Points.ValuesCount; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import org.junit.jupiter.api.Test; - -class FilterUtilTest { - - @Test - void testFieldConditionForTextMatching() { - String key = "name"; - Match match = FilterUtil.match("Elon"); - Condition condition = FilterUtil.fieldCondition(key, match); - - assertTrue(condition.hasField()); - assertEquals(key, condition.getField().getKey()); - assertTrue(condition.getField().hasMatch()); - assertEquals(match, condition.getField().getMatch()); - } - - @Test - void testFieldConditionForRangeMatching() { - String key = "age"; - Range range = FilterUtil.range(20, 50, 20, 50); - Condition condition = FilterUtil.fieldCondition(key, range); - - assertTrue(condition.hasField()); - assertEquals(key, condition.getField().getKey()); - assertTrue(condition.getField().hasRange()); - assertEquals(range, condition.getField().getRange()); - } - - @Test - void testFieldConditionForGeoBoundingBoxMatching() { - String key = "location"; - GeoBoundingBox geoBoundingBox = - FilterUtil.geoBoundingBox(FilterUtil.geoPoint(10.0, 20.0), FilterUtil.geoPoint(30.0, 40.0)); - Condition condition = FilterUtil.fieldCondition(key, geoBoundingBox); - - assertTrue(condition.hasField()); - assertEquals(key, condition.getField().getKey()); - assertTrue(condition.getField().hasGeoBoundingBox()); - assertEquals(geoBoundingBox, condition.getField().getGeoBoundingBox()); - } - - @Test - void testFieldConditionForGeoRadiusMatching() { - String key = "location"; - GeoRadius geoRadius = FilterUtil.geoRadius(FilterUtil.geoPoint(10.0, 20.0), 100.0f); - Condition condition = FilterUtil.fieldCondition(key, geoRadius); - - assertTrue(condition.hasField()); - assertEquals(key, condition.getField().getKey()); - assertTrue(condition.getField().hasGeoRadius()); - assertEquals(geoRadius, condition.getField().getGeoRadius()); - } - - @Test - void testFieldConditionForValuesCountMatching() { - String key = "count"; - ValuesCount valuesCount = FilterUtil.valuesCount(0, 10, 0, 10); - Condition condition = FilterUtil.fieldCondition(key, valuesCount); - - assertTrue(condition.hasField()); - assertEquals(key, condition.getField().getKey()); - assertTrue(condition.getField().hasValuesCount()); - assertEquals(valuesCount, condition.getField().getValuesCount()); - } - - @Test - void testFieldConditionForGeoPolygonMatching() { - String key = "area"; - GeoPolygon geoPolygon = - FilterUtil.geoPolygon( - GeoLineString.newBuilder() - .addPoints(FilterUtil.geoPoint(10.0, 20.0)) - .addPoints(FilterUtil.geoPoint(30.0, 40.0)) - .build(), - Collections.singletonList( - GeoLineString.newBuilder() - .addPoints(FilterUtil.geoPoint(15.0, 25.0)) - .addPoints(FilterUtil.geoPoint(35.0, 45.0)) - .build())); - Condition condition = FilterUtil.fieldCondition(key, geoPolygon); - - assertTrue(condition.hasField()); - assertEquals(key, condition.getField().getKey()); - assertTrue(condition.getField().hasGeoPolygon()); - assertEquals(geoPolygon, condition.getField().getGeoPolygon()); - } - - @Test - void testMatchForStringMatching() { - String text = "Elon"; - Match match = FilterUtil.match(text); - - assertTrue(match.hasKeyword()); - assertEquals(text, match.getKeyword()); - assertFalse(match.hasText()); - } - - @Test - void testMatchForStringMatchingWithSpace() { - String text = "Elon Musk"; - Match match = FilterUtil.match(text); - - assertTrue(match.hasText()); - assertEquals(text, match.getText()); - assertFalse(match.hasKeyword()); - } - - @Test - void testMatchForIntegerMatching() { - long value = 42; - Match match = FilterUtil.match(value); - - assertTrue(match.hasInteger()); - assertEquals(value, match.getInteger()); - } - - @Test - void testMatchForBooleanMatching() { - boolean value = true; - Match match = FilterUtil.match(value); - - assertTrue(match.hasBoolean()); - assertEquals(value, match.getBoolean()); - } - - @Test - void testMatchWithKeywords() { - List keywords = Arrays.asList("apple", "banana", "cherry"); - Match match = FilterUtil.matchWithKeywords(keywords); - - assertTrue(match.hasKeywords()); - assertEquals(keywords, match.getKeywords().getStringsList()); - } - - @Test - void testMatchWithIntegers() { - List integers = Arrays.asList(1L, 2L, 3L); - Match match = FilterUtil.matchWithIntegers(integers); - - assertTrue(match.hasIntegers()); - assertEquals(integers, match.getIntegers().getIntegersList()); - } - - @Test - void testGeoBoundingBox() { - GeoPoint topLeft = FilterUtil.geoPoint(10.0, 20.0); - GeoPoint bottomRight = FilterUtil.geoPoint(30.0, 40.0); - GeoBoundingBox geoBoundingBox = FilterUtil.geoBoundingBox(topLeft, bottomRight); - - assertEquals(topLeft, geoBoundingBox.getTopLeft()); - assertEquals(bottomRight, geoBoundingBox.getBottomRight()); - } - - @Test - void testGeoRadius() { - GeoPoint center = FilterUtil.geoPoint(10.0, 20.0); - float radius = 100.0f; - GeoRadius geoRadius = FilterUtil.geoRadius(center, radius); - - assertEquals(center, geoRadius.getCenter()); - assertEquals(radius, geoRadius.getRadius()); - } - - @Test - void testGeoPolygon() { - GeoLineString exterior = - GeoLineString.newBuilder() - .addPoints(FilterUtil.geoPoint(10.0, 20.0)) - .addPoints(FilterUtil.geoPoint(30.0, 40.0)) - .build(); - List interiors = - Collections.singletonList( - GeoLineString.newBuilder() - .addPoints(FilterUtil.geoPoint(15.0, 25.0)) - .addPoints(FilterUtil.geoPoint(35.0, 45.0)) - .build()); - GeoPolygon geoPolygon = FilterUtil.geoPolygon(exterior, interiors); - - assertEquals(exterior, geoPolygon.getExterior()); - assertEquals(interiors, geoPolygon.getInteriorsList()); - } - - @Test - void testRange() { - double lt = 10.0; - double gt = 20.0; - double gte = 15.0; - double lte = 25.0; - Range range = FilterUtil.range(lt, gt, gte, lte); - - assertEquals(lt, range.getLt()); - assertEquals(gt, range.getGt()); - assertEquals(gte, range.getGte()); - assertEquals(lte, range.getLte()); - } - - @Test - void testValuesCount() { - long lt = 0; - long gt = 10; - long gte = 0; - long lte = 5; - ValuesCount valuesCount = FilterUtil.valuesCount(lt, gt, gte, lte); - - assertEquals(lt, valuesCount.getLt()); - assertEquals(gt, valuesCount.getGt()); - assertEquals(gte, valuesCount.getGte()); - assertEquals(lte, valuesCount.getLte()); - } - - @Test - void testFilterCondition() { - Filter filter = Filter.newBuilder().build(); - Condition condition = FilterUtil.filterCondition(filter); - - assertTrue(condition.hasFilter()); - assertEquals(filter, condition.getFilter()); - } - - @Test - void testNestedCondition() { - String key = "nested"; - Filter filter = Filter.newBuilder().build(); - Condition condition = FilterUtil.nestedCondition(key, filter); - - assertTrue(condition.hasNested()); - assertEquals(key, condition.getNested().getKey()); - assertEquals(filter, condition.getNested().getFilter()); - } - - @Test - void testIsEmptyCondition() { - String key = "field"; - Condition condition = FilterUtil.isEmptyCondition(key); - - assertTrue(condition.hasIsEmpty()); - assertEquals(key, condition.getIsEmpty().getKey()); - } - - @Test - void testIsNullCondition() { - String key = "field"; - Condition condition = FilterUtil.isNullCondition(key); - - assertTrue(condition.hasIsNull()); - assertEquals(key, condition.getIsNull().getKey()); - } - - @Test - void testHasIdCondition() { - List pointIds = Arrays.asList(PointUtil.pointId(1), PointUtil.pointId(2)); - Condition condition = FilterUtil.hasIdCondition(pointIds); - - assertTrue(condition.hasHasId()); - assertEquals(pointIds, condition.getHasId().getHasIdList()); - } - - @Test - void testGeoPoint() { - double latitude = 10.0; - double longitude = 20.0; - GeoPoint geoPoint = FilterUtil.geoPoint(latitude, longitude); - - assertEquals(latitude, geoPoint.getLat()); - assertEquals(longitude, geoPoint.getLon()); - } - - @Test - void testMust() { - Condition condition1 = Condition.newBuilder().build(); - Condition condition2 = Condition.newBuilder().build(); - Filter filter = FilterUtil.must(condition1, condition2); - - assertEquals(Arrays.asList(condition1, condition2), filter.getMustList()); - } - - @Test - void testMustNot() { - Condition condition1 = Condition.newBuilder().build(); - Condition condition2 = Condition.newBuilder().build(); - Filter filter = FilterUtil.mustNot(condition1, condition2); - - assertEquals(Arrays.asList(condition1, condition2), filter.getMustNotList()); - } - - @Test - void testShould() { - Condition condition1 = Condition.newBuilder().build(); - Condition condition2 = Condition.newBuilder().build(); - Filter filter = FilterUtil.should(condition1, condition2); - - assertEquals(Arrays.asList(condition1, condition2), filter.getShouldList()); - } -} diff --git a/src/test/java/io/qdrant/client/utils/PayloadUtilTest.java b/src/test/java/io/qdrant/client/utils/PayloadUtilTest.java deleted file mode 100644 index 30353f6a..00000000 --- a/src/test/java/io/qdrant/client/utils/PayloadUtilTest.java +++ /dev/null @@ -1,165 +0,0 @@ -package io.qdrant.client.utils; - -import static org.junit.jupiter.api.Assertions.*; - -import io.qdrant.client.grpc.JsonWithInt.ListValue; -import io.qdrant.client.grpc.JsonWithInt.NullValue; -import io.qdrant.client.grpc.JsonWithInt.Struct; -import io.qdrant.client.grpc.JsonWithInt.Value; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import org.junit.jupiter.api.Test; - -class PayloadUtilTest { - - @Test - void testToPayloadStruct() { - // Test case 1: Empty input map - Map inputMap = new HashMap<>(); - Struct payloadStruct = PayloadUtil.toPayloadStruct(inputMap); - assertTrue(payloadStruct.getFieldsMap().isEmpty()); - - // Test case 2: Input map with different value types - inputMap.put("name", "Elon"); - inputMap.put("age", 52); - inputMap.put("isStudent", true); - payloadStruct = PayloadUtil.toPayloadStruct(inputMap); - assertEquals("Elon", payloadStruct.getFieldsMap().get("name").getStringValue()); - assertEquals(52, payloadStruct.getFieldsMap().get("age").getIntegerValue()); - assertEquals(true, payloadStruct.getFieldsMap().get("isStudent").getBoolValue()); - } - - @Test - void testToPayload() { - // Test case 1: Empty input map - Map inputMap = new HashMap<>(); - Map payload = PayloadUtil.toPayload(inputMap); - assertTrue(payload.isEmpty()); - - // Test case 2: Input map with different value types - inputMap.put("name", "Elon"); - inputMap.put("age", 52); - inputMap.put("isStudent", true); - payload = PayloadUtil.toPayload(inputMap); - assertEquals("Elon", payload.get("name").getStringValue()); - assertEquals(52, payload.get("age").getIntegerValue()); - assertEquals(true, payload.get("isStudent").getBoolValue()); - } - - @Test - void testStructtoMap() { - // Test case 1: Empty struct - Struct.Builder structBuilder = Struct.newBuilder(); - Struct struct = structBuilder.build(); - Map structMap = PayloadUtil.toMap(struct); - assertTrue(structMap.isEmpty()); - - // Test case 2: Struct with different value types - structBuilder.putFields("name", Value.newBuilder().setStringValue("Elon").build()); - structBuilder.putFields("age", Value.newBuilder().setIntegerValue(52).build()); - structBuilder.putFields("isStudent", Value.newBuilder().setBoolValue(true).build()); - struct = structBuilder.build(); - structMap = PayloadUtil.toMap(struct); - assertEquals("Elon", structMap.get("name")); - assertEquals(52, (int) structMap.get("age")); - assertEquals(true, structMap.get("isStudent")); - } - - @Test - void testtoMap() { - // Test case 1: Empty payload - Map payload = new HashMap<>(); - Map hashMap = PayloadUtil.toMap(payload); - assertTrue(hashMap.isEmpty()); - - // Test case 2: Payload with different value types - payload.put("name", Value.newBuilder().setStringValue("Elon").build()); - payload.put("age", Value.newBuilder().setIntegerValue(52).build()); - payload.put("isStudent", Value.newBuilder().setBoolValue(true).build()); - hashMap = PayloadUtil.toMap(payload); - assertEquals("Elon", hashMap.get("name")); - assertEquals(52, hashMap.get("age")); - assertEquals(true, hashMap.get("isStudent")); - } - - @Test - void testSetValue() { - // Test case 1: Set string value - Value.Builder valueBuilder = Value.newBuilder(); - PayloadUtil.setValue(valueBuilder, "Elon"); - assertEquals("Elon", valueBuilder.getStringValue()); - - // Test case 2: Set integer value - valueBuilder = Value.newBuilder(); - PayloadUtil.setValue(valueBuilder, 52); - assertEquals(52, valueBuilder.getIntegerValue()); - - // Test case 3: Set boolean value - valueBuilder = Value.newBuilder(); - PayloadUtil.setValue(valueBuilder, true); - assertEquals(true, valueBuilder.getBoolValue()); - } - - @Test - void testListToListValue() { - // Test case 1: Empty list - List list = new ArrayList<>(); - ListValue.Builder listValueBuilder = PayloadUtil.listToListValue(list); - assertTrue(listValueBuilder.getValuesList().isEmpty()); - - // Test case 2: List with different value types - list.add("Elon"); - list.add(52); - list.add(true); - listValueBuilder = PayloadUtil.listToListValue(list); - assertEquals("Elon", listValueBuilder.getValuesList().get(0).getStringValue()); - assertEquals(52, listValueBuilder.getValuesList().get(1).getIntegerValue()); - assertEquals(true, listValueBuilder.getValuesList().get(2).getBoolValue()); - } - - @Test - void testListValueToList() { - // Test case 1: Empty list value - ListValue.Builder listValueBuilder = ListValue.newBuilder(); - ListValue listValue = listValueBuilder.build(); - Object[] objectList = (Object[]) PayloadUtil.listValueToList(listValue); - assertTrue(objectList.length == 0); - - // Test case 2: List value with different value types - listValueBuilder.addValues(Value.newBuilder().setStringValue("Elon").build()); - listValueBuilder.addValues(Value.newBuilder().setIntegerValue(52).build()); - listValueBuilder.addValues(Value.newBuilder().setBoolValue(true).build()); - listValue = listValueBuilder.build(); - objectList = (Object[]) PayloadUtil.listValueToList(listValue); - assert (objectList instanceof Object[]); - - assertEquals("Elon", objectList[0]); - assertEquals(52, objectList[1]); - assertEquals(true, objectList[2]); - } - - @Test - void testValueToObject() { - // Test case 1: String value - Value value = Value.newBuilder().setStringValue("Elon").build(); - Object object = PayloadUtil.valueToObject(value); - assertEquals("Elon", object); - - // Test case 2: Integer value - value = Value.newBuilder().setIntegerValue(52).build(); - object = PayloadUtil.valueToObject(value); - assertEquals(52, object); - - // Test case 3: Boolean value - value = Value.newBuilder().setBoolValue(true).build(); - object = PayloadUtil.valueToObject(value); - assertEquals(true, object); - - // Test case 4: Null value - value = Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build(); - object = PayloadUtil.valueToObject(value); - assertNull(object); - } -} diff --git a/src/test/java/io/qdrant/client/utils/PointUtilTest.java b/src/test/java/io/qdrant/client/utils/PointUtilTest.java deleted file mode 100644 index 727da2c9..00000000 --- a/src/test/java/io/qdrant/client/utils/PointUtilTest.java +++ /dev/null @@ -1,186 +0,0 @@ -package io.qdrant.client.utils; - -import static org.junit.jupiter.api.Assertions.*; - -import io.qdrant.client.grpc.JsonWithInt.Value; -import io.qdrant.client.grpc.Points.Filter; -import io.qdrant.client.grpc.Points.PointId; -import io.qdrant.client.grpc.Points.PointStruct; -import io.qdrant.client.grpc.Points.PointsIdsList; -import io.qdrant.client.grpc.Points.PointsSelector; -import io.qdrant.client.grpc.Points.ReadConsistency; -import io.qdrant.client.grpc.Points.ReadConsistencyType; -import io.qdrant.client.grpc.Points.Vector; -import io.qdrant.client.grpc.Points.WriteOrdering; -import io.qdrant.client.grpc.Points.WriteOrderingType; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.UUID; -import org.junit.jupiter.api.Test; - -class PointUtilTest { - - @Test - void testCreatePointsSelectorWithLongIds() { - long[] pointIds = {1L, 2L, 3L}; - PointsSelector pointsSelector = PointUtil.createPointsSelector(pointIds); - - PointsIdsList expectedIdsList = - PointsIdsList.newBuilder() - .addAllIds( - Arrays.asList(PointUtil.pointId(1L), PointUtil.pointId(2L), PointUtil.pointId(3L))) - .build(); - - assertEquals(expectedIdsList, pointsSelector.getPoints()); - } - - @Test - void testCreatePointsSelectorWithStringIds() { - String[] pointIds = { - "123e4567-e89b-12d3-a456-426614174000", - "123e4567-e89b-12d3-a456-426614174001", - "123e4567-e89b-12d3-a456-426614174002" - }; - PointsSelector pointsSelector = PointUtil.createPointsSelector(pointIds); - - PointsIdsList expectedIdsList = - PointsIdsList.newBuilder() - .addAllIds( - Arrays.asList( - PointUtil.pointId("123e4567-e89b-12d3-a456-426614174000"), - PointUtil.pointId("123e4567-e89b-12d3-a456-426614174001"), - PointUtil.pointId("123e4567-e89b-12d3-a456-426614174002"))) - .build(); - - assertEquals(expectedIdsList, pointsSelector.getPoints()); - } - - @Test - void testCreatePointsSelectorWithIterable() { - List pointIds = - Arrays.asList(PointUtil.pointId(1L), PointUtil.pointId(2L), PointUtil.pointId(3L)); - PointsSelector pointsSelector = PointUtil.createPointsSelector(pointIds); - - PointsIdsList expectedIdsList = - PointsIdsList.newBuilder() - .addAllIds( - Arrays.asList(PointUtil.pointId(1L), PointUtil.pointId(2L), PointUtil.pointId(3L))) - .build(); - - assertEquals(expectedIdsList, pointsSelector.getPoints()); - } - - @Test - void testPointsSelector() { - Filter filter = Filter.newBuilder().build(); - PointsSelector pointsSelector = PointUtil.pointsSelector(filter); - - assertEquals(filter, pointsSelector.getFilter()); - } - - @Test - void testPointIdWithLong() { - long num = 42L; - PointId pointId = PointUtil.pointId(num); - - assertEquals(num, pointId.getNum()); - } - - @Test - void testPointIdWithString() { - String uuid = "123e4567-e89b-12d3-a456-426614174000"; - PointId pointId = PointUtil.pointId(uuid); - - assertEquals(uuid, pointId.getUuid()); - } - - @Test - void testPointIdWithUUID() { - UUID uuid = UUID.fromString("123e4567-e89b-12d3-a456-426614174000"); - PointId pointId = PointUtil.pointId(uuid); - - assertEquals(uuid.toString(), pointId.getUuid()); - } - - @Test - void testPointWithUUID() { - UUID uuid = UUID.fromString("123e4567-e89b-12d3-a456-426614174000"); - Vector vector = Vector.newBuilder().build(); - Map payload = Collections.emptyMap(); - PointStruct pointStruct = PointUtil.point(uuid, vector, payload); - - assertEquals(uuid.toString(), pointStruct.getId().getUuid()); - assertEquals(vector, pointStruct.getVectors().getVector()); - assertEquals(payload, pointStruct.getPayloadMap()); - } - - @Test - void testPointWithLongId() { - long id = 42L; - Vector vector = Vector.newBuilder().build(); - Map payload = Collections.emptyMap(); - PointStruct pointStruct = PointUtil.point(id, vector, payload); - - assertEquals(id, pointStruct.getId().getNum()); - assertEquals(vector, pointStruct.getVectors().getVector()); - assertEquals(payload, pointStruct.getPayloadMap()); - } - - @Test - void testPointWithPointId() { - PointId id = PointUtil.pointId(42L); - Vector vector = Vector.newBuilder().build(); - Map payload = Collections.emptyMap(); - PointStruct pointStruct = PointUtil.point(id, vector, payload); - - assertEquals(id, pointStruct.getId()); - assertEquals(vector, pointStruct.getVectors().getVector()); - assertEquals(payload, pointStruct.getPayloadMap()); - } - - @Test - void testNamedPointWithLongId() { - Long id = 42L; - String vectorName = "vector"; - float[] vectorData = {1.0f, 2.0f, 3.0f}; - Map payload = Collections.emptyMap(); - PointStruct pointStruct = PointUtil.namedPoint(id, vectorName, vectorData, payload); - - assertEquals(PointUtil.pointId(id), pointStruct.getId()); - assertEquals( - VectorUtil.namedVector(vectorName, vectorData), pointStruct.getVectors().getVectors()); - assertEquals(payload, pointStruct.getPayloadMap()); - } - - @Test - void testNamedPointWithPointId() { - PointId id = PointUtil.pointId(42L); - String vectorName = "vector"; - float[] vectorData = {1.0f, 2.0f, 3.0f}; - Map payload = Collections.emptyMap(); - PointStruct pointStruct = PointUtil.namedPoint(id, vectorName, vectorData, payload); - - assertEquals(id, pointStruct.getId()); - assertEquals( - VectorUtil.namedVector(vectorName, vectorData), pointStruct.getVectors().getVectors()); - assertEquals(payload, pointStruct.getPayloadMap()); - } - - @Test - void testOrdering() { - WriteOrderingType orderingType = WriteOrderingType.Medium; - WriteOrdering ordering = PointUtil.ordering(orderingType); - - assertEquals(orderingType, ordering.getType()); - } - - @Test - void testConsistency() { - ReadConsistencyType consistencyType = ReadConsistencyType.Quorum; - ReadConsistency consistency = PointUtil.consistency(consistencyType); - - assertEquals(consistencyType, consistency.getType()); - } -} diff --git a/src/test/java/io/qdrant/client/utils/SelectorUtilTest.java b/src/test/java/io/qdrant/client/utils/SelectorUtilTest.java deleted file mode 100644 index 0735867d..00000000 --- a/src/test/java/io/qdrant/client/utils/SelectorUtilTest.java +++ /dev/null @@ -1,66 +0,0 @@ -package io.qdrant.client.utils; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import io.qdrant.client.grpc.Points.Filter; -import io.qdrant.client.grpc.Points.PointId; -import io.qdrant.client.grpc.Points.PointsSelector; -import io.qdrant.client.grpc.Points.WithPayloadSelector; -import io.qdrant.client.grpc.Points.WithVectorsSelector; -import java.util.Arrays; -import java.util.List; -import org.junit.jupiter.api.Test; - -class SelectorUtilTest { - - @Test - void testWithPayload() { - WithPayloadSelector selector = SelectorUtil.withPayload(); - assertTrue(selector.getEnable()); - } - - @Test - void testWithVectors() { - WithVectorsSelector selector = SelectorUtil.withVectors(); - assertTrue(selector.getEnable()); - } - - @Test - void testWithPayloadWithFields() { - String[] fields = {"field1", "field2"}; - WithPayloadSelector selector = SelectorUtil.withPayload(fields); - List expectedFields = Arrays.asList(fields); - assertEquals(expectedFields, selector.getInclude().getFieldsList()); - } - - @Test - void testWithVectorsWithNames() { - String[] vectors = {"vector1", "vector2"}; - WithVectorsSelector selector = SelectorUtil.withVectors(vectors); - List expectedVectors = Arrays.asList(vectors); - assertEquals(expectedVectors, selector.getInclude().getNamesList()); - } - - @Test - void testIdsSelectorWithList() { - List ids = Arrays.asList(PointUtil.pointId(1), PointUtil.pointId(2)); - PointsSelector selector = SelectorUtil.idsSelector(ids); - assertEquals(ids, selector.getPoints().getIdsList()); - } - - @Test - void testIdsSelectorWithArray() { - PointId[] ids = {PointUtil.pointId(1), PointUtil.pointId(2)}; - PointsSelector selector = SelectorUtil.idsSelector(ids); - List expectedIds = Arrays.asList(ids); - assertEquals(expectedIds, selector.getPoints().getIdsList()); - } - - @Test - void testFilterSelector() { - Filter filter = Filter.newBuilder().build(); - PointsSelector selector = SelectorUtil.filterSelector(filter); - assertEquals(filter, selector.getFilter()); - } -} diff --git a/src/test/java/io/qdrant/client/utils/VectorUtilTest.java b/src/test/java/io/qdrant/client/utils/VectorUtilTest.java deleted file mode 100644 index b7dd2749..00000000 --- a/src/test/java/io/qdrant/client/utils/VectorUtilTest.java +++ /dev/null @@ -1,79 +0,0 @@ -package io.qdrant.client.utils; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import io.qdrant.client.grpc.Points.NamedVectors; -import io.qdrant.client.grpc.Points.PointVectors; -import io.qdrant.client.grpc.Points.Vector; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import org.junit.jupiter.api.Test; - -class VectorUtilTest { - - @Test - void testVectorFromList() { - List vectorData = Arrays.asList(1.0f, 2.0f, 3.0f); - Vector vector = VectorUtil.toVector(vectorData); - - assertEquals(vectorData, vector.getDataList()); - } - - @Test - void testVectorFromArray() { - float[] vectorData = {1.0f, 2.0f, 3.0f}; - Vector vector = VectorUtil.toVector(vectorData); - - assertEquals(Arrays.asList(1.0f, 2.0f, 3.0f), vector.getDataList()); - } - - @Test - void testNamedVectorFromList() { - String name = "vector1"; - List vectorData = Arrays.asList(1.0f, 2.0f, 3.0f); - NamedVectors namedVector = VectorUtil.namedVector(name, vectorData); - - assertTrue(namedVector.getVectorsMap().containsKey(name)); - assertEquals(vectorData, namedVector.getVectorsMap().get(name).getDataList()); - } - - @Test - void testNamedVectorFromArray() { - String name = "vector1"; - float[] vectorData = {1.0f, 2.0f, 3.0f}; - NamedVectors namedVector = VectorUtil.namedVector(name, vectorData); - - assertTrue(namedVector.getVectorsMap().containsKey(name)); - assertEquals( - Arrays.asList(1.0f, 2.0f, 3.0f), namedVector.getVectorsMap().get(name).getDataList()); - } - - @Test - void testNamedVectorsFromMap() { - String name1 = "vector1"; - String name2 = "vector2"; - Map vectorsMap = new HashMap<>(); - vectorsMap.put(name1, VectorUtil.toVector(1.0f, 2.0f, 3.0f)); - vectorsMap.put(name2, VectorUtil.toVector(4.0f, 5.0f, 6.0f)); - NamedVectors namedVectors = VectorUtil.namedVectors(vectorsMap); - - assertEquals(vectorsMap, namedVectors.getVectorsMap()); - } - - @Test - void testPointVectors() { - String id = "123e4567-e89b-12d3-a456-426614174000"; - String name = "vector1"; - float[] vectorData = {1.0f, 2.0f, 3.0f}; - PointVectors pointVectors = VectorUtil.pointVectors(id, name, vectorData); - - assertEquals(PointUtil.pointId(id), pointVectors.getId()); - assertTrue(pointVectors.getVectors().getVectors().getVectorsMap().containsKey(name)); - assertEquals( - Arrays.asList(1.0f, 2.0f, 3.0f), - pointVectors.getVectors().getVectors().getVectorsMap().get(name).getDataList()); - } -} diff --git a/tools/mvn_test.sh b/tools/mvn_test.sh deleted file mode 100644 index 5548a21b..00000000 --- a/tools/mvn_test.sh +++ /dev/null @@ -1,33 +0,0 @@ -#!/bin/bash - -set -ex - -function stop_docker() -{ - echo "Stopping qdrant_test" - docker stop qdrant_test -} - -# Ensure current path is project root -cd "$(dirname "$0")/../" - -QDRANT_VERSION='v1.6.1' - -QDRANT_HOST='localhost:6333' - -docker run -d --rm \ - -p 6333:6333 \ - -p 6334:6334 \ - --name qdrant_test qdrant/qdrant:${QDRANT_VERSION} - -trap stop_docker SIGINT -trap stop_docker ERR - -until curl --output /dev/null --silent --get --fail http://$QDRANT_HOST/collections; do - printf 'Waiting for the Qdrant server to start...' - sleep 5 -done - -mvn test - -stop_docker \ No newline at end of file