Skip to content
44 changes: 35 additions & 9 deletions src/it/java/io/weaviate/integration/DataITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import io.weaviate.client6.v1.api.collections.ReferenceProperty;
import io.weaviate.client6.v1.api.collections.VectorConfig;
import io.weaviate.client6.v1.api.collections.Vectors;
import io.weaviate.client6.v1.api.collections.WeaviateObject;
import io.weaviate.client6.v1.api.collections.data.BatchReference;
import io.weaviate.client6.v1.api.collections.data.DeleteManyResponse;
import io.weaviate.client6.v1.api.collections.data.Reference;
import io.weaviate.client6.v1.api.collections.data.WriteWeaviateObject;
import io.weaviate.client6.v1.api.collections.query.Filter;
import io.weaviate.client6.v1.api.collections.query.Metadata;
import io.weaviate.client6.v1.api.collections.query.Metadata.MetadataField;
Expand Down Expand Up @@ -66,22 +66,48 @@ public void testCreateGetDelete() throws IOException {
Assertions.assertThat(object)
.as("object has correct properties").get()
.satisfies(obj -> {
Assertions.assertThat(obj.metadata().uuid())
Assertions.assertThat(obj.queryMetadata().uuid())
.as("object id").isEqualTo(id);

Assertions.assertThat(obj.metadata().vectors().getSingle(VECTOR_INDEX))
Assertions.assertThat(obj.queryMetadata().vectors().getSingle(VECTOR_INDEX))
.containsExactly(vector);

Assertions.assertThat(obj.properties())
.as("has expected properties")
.containsEntry("name", "john doe");

Assertions.assertThat(obj.metadata().creationTimeUnix())
.as("creationTimeUnix").isNotNull();
Assertions.assertThat(obj.metadata().lastUpdateTimeUnix())
.as("lastUpdateTimeUnix").isNotNull();
Assertions.assertThat(obj.queryMetadata().createdAt())
.as("createdAt").isNotNull();
Assertions.assertThat(obj.queryMetadata().lastUpdatedAt())
.as("lastUpdatedAt").isNotNull();
});

// var write = WriteWeaviateObject.of(null);
// write.tenant(); // can be null, but that's perfectly fine
//
// write.references().get("").getFirst().asWeaviateObject();
//
// // Three key changes:
// var wv = WeaviateObject.write(null); // 1: you can use WeaviateObject, and
// not WriteWeaviateObject
// write.queryMetadata(); // 2: This should be called "queryMetadata" to avoid
// confusion
// wv.references().forEach((key, references) -> {
// references.forEach(ref -> {
// ref.collection();
// ref.uuid();
//
// // get "title" property from a referenced object
// var title = ref.asWeaviateObject().properties().get("title");
//
// ref.asWeaviateObject().references().forEach((__, nestedRefs) -> {
// nestedRefs.forEach(nref -> {
// var n_title = ref.asWeaviateObject().properties().get("title");
// });
// });
// });
// });

var deleted = artists.data.deleteById(id);
Assertions.assertThat(deleted)
.as("object was deleted").isTrue();
Expand Down Expand Up @@ -292,7 +318,7 @@ public void testUpdate() throws IOException {

Assertions.assertThat(book)
.as("has a vector")
.extracting(ReadWeaviateObject::metadata)
.extracting(ReadWeaviateObject::queryMetadata)
.extracting(QueryMetadata::vectors)
.returns(vector, Vectors::getDefaultSingle);
});
Expand Down Expand Up @@ -557,6 +583,6 @@ public void test_multiTenant() throws IOException {
var inserted = emails.data.insert(Map.of("subject", "McDonald's Xmas Bonanza"));

// Assert
Assertions.assertThat(inserted).returns(johndoe, WriteWeaviateObject::tenant);
Assertions.assertThat(inserted).returns(johndoe, WeaviateObject::tenant);
}
}
8 changes: 4 additions & 4 deletions src/it/java/io/weaviate/integration/PaginationITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void testIterateAll() throws IOException {

// Act: stream
var gotStream = allThings.stream()
.map(ReadWeaviateObject::metadata).map(QueryMetadata::uuid).toList();
.map(ReadWeaviateObject::queryMetadata).map(QueryMetadata::uuid).toList();

// Assert
Assertions.assertThat(gotStream)
Expand All @@ -58,7 +58,7 @@ public void testIterateAll() throws IOException {
// Act: for-loop
var gotLoop = new ArrayList<String>();
for (var thing : allThings) {
gotLoop.add(thing.metadata().uuid());
gotLoop.add(thing.queryMetadata().uuid());
}

// Assert
Expand Down Expand Up @@ -89,7 +89,7 @@ public void testResumePagination() throws IOException {

// Iterate over first 5 objects
String lastId = things.paginate(p -> p.pageSize(5)).stream()
.limit(5).map(thing -> thing.metadata().uuid())
.limit(5).map(thing -> thing.queryMetadata().uuid())
.reduce((prev, next) -> next).get();

// Act
Expand Down Expand Up @@ -126,7 +126,7 @@ public void testWithQueryOptions() throws IOException {
.as("uuid=" + thing.uuid())
.doesNotContainKey("dont_fetch");

Assertions.assertThat(thing.metadata().creationTimeUnix())
Assertions.assertThat(thing.queryMetadata().createdAt())
.isNotNull();
}
}
Expand Down
34 changes: 17 additions & 17 deletions src/it/java/io/weaviate/integration/SearchITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import io.weaviate.client6.v1.api.collections.VectorConfig;
import io.weaviate.client6.v1.api.collections.Vectors;
import io.weaviate.client6.v1.api.collections.WeaviateMetadata;
import io.weaviate.client6.v1.api.collections.WeaviateObject;
import io.weaviate.client6.v1.api.collections.data.Reference;
import io.weaviate.client6.v1.api.collections.data.WriteWeaviateObject;
import io.weaviate.client6.v1.api.collections.generate.GenerativeObject;
import io.weaviate.client6.v1.api.collections.generate.TaskOutput;
import io.weaviate.client6.v1.api.collections.generative.DummyGenerative;
Expand Down Expand Up @@ -91,7 +91,7 @@ public void testNearVector() {

Assertions.assertThat(result.objects()).hasSize(3);
float maxDistance = Collections.max(result.objects(),
Comparator.comparing(obj -> obj.metadata().distance())).metadata().distance();
Comparator.comparing(obj -> obj.queryMetadata().distance())).queryMetadata().distance();
Assertions.assertThat(maxDistance).isLessThanOrEqualTo(2f);
}

Expand Down Expand Up @@ -265,7 +265,7 @@ public void testFetchObjectsWithFilters() throws IOException {
Filter.property("size").lt(6)))));

Assertions.assertThat(got.objects())
.extracting(hat -> hat.metadata().uuid())
.extracting(hat -> hat.queryMetadata().uuid())
.containsOnly(
redHat.uuid(),
greenHat.uuid(),
Expand Down Expand Up @@ -331,7 +331,7 @@ public void testBm25() throws IOException, InterruptedException, ExecutionExcept

Assertions.assertThat(dollarWorlds.objects())
.hasSize(1)
.extracting(ReadWeaviateObject::metadata).extracting(QueryMetadata::uuid)
.extracting(ReadWeaviateObject::queryMetadata).extracting(QueryMetadata::uuid)
.containsOnly(want.uuid());
}

Expand Down Expand Up @@ -363,7 +363,7 @@ public void testBm25_async() throws Exception, InterruptedException, ExecutionEx

Assertions.assertThat(dollarWorlds.objects())
.hasSize(1)
.extracting(ReadWeaviateObject::metadata).extracting(QueryMetadata::uuid)
.extracting(ReadWeaviateObject::queryMetadata).extracting(QueryMetadata::uuid)
.containsOnly(want.uuid());
}
}
Expand Down Expand Up @@ -393,7 +393,7 @@ public void testNearObject() throws IOException {
// Assert
Assertions.assertThat(terrestrial.objects())
.hasSize(1)
.extracting(ReadWeaviateObject::metadata).extracting(WeaviateMetadata::uuid)
.extracting(ReadWeaviateObject::queryMetadata).extracting(WeaviateMetadata::uuid)
.containsOnly(lion.uuid());
}

Expand All @@ -420,13 +420,13 @@ public void testHybrid() throws IOException {
// Assert
Assertions.assertThat(winterSport.objects())
.hasSize(1)
.extracting(ReadWeaviateObject::metadata).extracting(WeaviateMetadata::uuid)
.extracting(ReadWeaviateObject::queryMetadata).extracting(WeaviateMetadata::uuid)
.containsOnly(skiing.uuid());

var first = winterSport.objects().get(0);
Assertions.assertThat(first.metadata().score())
Assertions.assertThat(first.queryMetadata().score())
.as("metadata::score").isNotNull();
Assertions.assertThat(first.metadata().explainScore())
Assertions.assertThat(first.queryMetadata().explainScore())
.as("metadata::explainScore").isNotNull();
}

Expand Down Expand Up @@ -519,23 +519,23 @@ public void testMetadataAll() throws IOException {
// Assert
var metadataHybrid = Assertions.assertThat(gotHybrid.objects())
.hasSize(1)
.extracting(ReadWeaviateObject::metadata)
.extracting(ReadWeaviateObject::queryMetadata)
.first().actual();

Assertions.assertThat(metadataHybrid.uuid()).as("uuid").isNotNull().isEqualTo(frisbee.uuid());
Assertions.assertThat(metadataHybrid.creationTimeUnix()).as("creationTimeUnix").isNotNull();
Assertions.assertThat(metadataHybrid.lastUpdateTimeUnix()).as("lastUpdateTimeUnix").isNotNull();
Assertions.assertThat(metadataHybrid.createdAt()).as("createdAt").isNotNull();
Assertions.assertThat(metadataHybrid.lastUpdatedAt()).as("lastUpdateTimeUnix").isNotNull();
Assertions.assertThat(metadataHybrid.score()).as("score").isNotNull();
Assertions.assertThat(metadataHybrid.explainScore()).as("explainScore").isNotNull().isNotEqualTo("");

var metadataNearText = Assertions.assertThat(gotNearText.objects())
.hasSize(1)
.extracting(ReadWeaviateObject::metadata)
.extracting(ReadWeaviateObject::queryMetadata)
.first().actual();

Assertions.assertThat(metadataNearText.uuid()).as("uuid").isNotNull().isEqualTo(frisbee.uuid());
Assertions.assertThat(metadataNearText.creationTimeUnix()).as("creationTimeUnix").isNotNull();
Assertions.assertThat(metadataNearText.lastUpdateTimeUnix()).as("lastUpdateTimeUnix").isNotNull();
Assertions.assertThat(metadataNearText.createdAt()).as("createdAt").isNotNull();
Assertions.assertThat(metadataNearText.lastUpdatedAt()).as("lastUpdateTimeUnix").isNotNull();
Assertions.assertThat(metadataNearText.distance()).as("distance").isNotNull();
Assertions.assertThat(metadataNearText.certainty()).as("certainty").isNotNull();
}
Expand All @@ -558,7 +558,7 @@ public void testNearVector_targetVectors() throws IOException {
Vectors.of("v2d", new float[][] { { 1, 2, 3 }, { 1, 2, 3 } })));

var thing456 = things.data.insertMany(List.of(
WriteWeaviateObject.of(thing -> thing
WeaviateObject.write(thing -> thing
.vectors(
Vectors.of("v1d", new float[] { 4, 5, 6 }),
Vectors.of("v2d", new float[][] { { 4, 5, 6 }, { 4, 5, 6 } })))));
Expand Down Expand Up @@ -760,7 +760,7 @@ public void test_maxGrpcMessageSize() throws Exception {
.vectorConfig(VectorConfig.selfProvided()));

final var vector = randomVector(5000, -.01f, .01f);
final WriteWeaviateObject<Map<String, Object>> hugeObject = WriteWeaviateObject.of(
final WeaviateObject<Map<String, Object>> hugeObject = WeaviateObject.write(
obj -> obj.vectors(Vectors.of(vector)));

Assertions.assertThatThrownBy(() -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.weaviate.client6.v1.api.collections;

import java.util.Map;

public interface IReference {
String uuid();

String collection();

WeaviateObject<Map<String, Object>> asWeaviateObject();
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
package io.weaviate.client6.v1.api.collections;

public interface WeaviateObject {
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import io.weaviate.client6.v1.api.collections.data.WriteWeaviateObject;
import io.weaviate.client6.v1.api.collections.query.QueryMetadata;
import io.weaviate.client6.v1.internal.ObjectBuilder;

public interface WeaviateObject<PropertiesT> {
String uuid();

String collection();

Vectors vectors();

String tenant();

PropertiesT properties();

Map<String, List<IReference>> references();

Long createdAt();

Long lastUpdatedAt();

QueryMetadata queryMetadata();

public static <PropertiesT> WeaviateObject<PropertiesT> write(
Function<WriteWeaviateObject.Builder<PropertiesT>, ObjectBuilder<WriteWeaviateObject<PropertiesT>>> fn) {
return fn.apply(new WriteWeaviateObject.Builder<>()).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.weaviate.client6.v1.api.collections.CollectionHandleDefaults;
import io.weaviate.client6.v1.api.collections.GeoCoordinates;
import io.weaviate.client6.v1.api.collections.PhoneNumber;
import io.weaviate.client6.v1.api.collections.WeaviateObject;
import io.weaviate.client6.v1.internal.MapUtil;
import io.weaviate.client6.v1.internal.grpc.ByteStringUtil;
import io.weaviate.client6.v1.internal.grpc.Rpc;
Expand All @@ -20,24 +21,24 @@
import io.weaviate.client6.v1.internal.grpc.protocol.WeaviateProtoBatch;
import io.weaviate.client6.v1.internal.orm.CollectionDescriptor;

public record InsertManyRequest<PropertiesT>(List<WriteWeaviateObject<PropertiesT>> objects) {
public record InsertManyRequest<PropertiesT>(List<WeaviateObject<PropertiesT>> objects) {

@SafeVarargs
public InsertManyRequest(WriteWeaviateObject<PropertiesT>... objects) {
public InsertManyRequest(WeaviateObject<PropertiesT>... objects) {
this(Arrays.asList(objects));
}

@SuppressWarnings("unchecked")
@SafeVarargs
public static final <PropertiesT> InsertManyRequest<PropertiesT> of(PropertiesT... properties) {
var objects = Arrays.stream(properties)
.map(p -> (WriteWeaviateObject<PropertiesT>) WriteWeaviateObject.of(obj -> obj.properties(p)))
.map(p -> (WeaviateObject<PropertiesT>) WeaviateObject.write(obj -> obj.properties(p)))
.toList();
return new InsertManyRequest<>(objects);
}

public static <PropertiesT> Rpc<InsertManyRequest<PropertiesT>, WeaviateProtoBatch.BatchObjectsRequest, InsertManyResponse, WeaviateProtoBatch.BatchObjectsReply> rpc(
List<WriteWeaviateObject<PropertiesT>> insertObjects,
List<WeaviateObject<PropertiesT>> insertObjects,
CollectionDescriptor<PropertiesT> collection,
CollectionHandleDefaults defaults) {
return Rpc.insert(
Expand All @@ -46,7 +47,7 @@ public static <PropertiesT> Rpc<InsertManyRequest<PropertiesT>, WeaviateProtoBat

var batch = request.objects.stream().map(obj -> {
var batchObject = WeaviateProtoBatch.BatchObject.newBuilder();
buildObject(batchObject, obj, collection, defaults);
buildObject(batchObject, (WriteWeaviateObject) obj, collection, defaults);
return batchObject.build();
}).toList();

Expand Down Expand Up @@ -135,11 +136,13 @@ public static <T> void buildObject(WeaviateProtoBatch.BatchObject.Builder object
// is single- or multi-target?
for (var ref : references) {
if (ref.collection() == null) {
singleRef.add(WeaviateProtoBatch.BatchObject.SingleTargetRefProps.newBuilder().addAllUuids(ref.uuids())
singleRef.add(WeaviateProtoBatch.BatchObject.SingleTargetRefProps.newBuilder()
.addAllUuids(((Reference) ref).uuids())
.setPropName(entry.getKey()).build());
} else {
multiRef.add(WeaviateProtoBatch.BatchObject.MultiTargetRefProps.newBuilder()
.setTargetCollection(ref.collection()).addAllUuids(ref.uuids()).setPropName(entry.getKey()).build());
.setTargetCollection(ref.collection()).addAllUuids(((Reference) ref).uuids())
.setPropName(entry.getKey()).build());
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@
import com.google.gson.reflect.TypeToken;

import io.weaviate.client6.v1.api.collections.CollectionHandleDefaults;
import io.weaviate.client6.v1.api.collections.WeaviateObject;
import io.weaviate.client6.v1.internal.ObjectBuilder;
import io.weaviate.client6.v1.internal.json.JSON;
import io.weaviate.client6.v1.internal.orm.CollectionDescriptor;
import io.weaviate.client6.v1.internal.rest.Endpoint;
import io.weaviate.client6.v1.internal.rest.SimpleEndpoint;

public record InsertObjectRequest<PropertiesT>(WriteWeaviateObject<PropertiesT> object) {
public record InsertObjectRequest<PropertiesT>(WeaviateObject<PropertiesT> object) {

@SuppressWarnings("unchecked")
public static final <PropertiesT> Endpoint<InsertObjectRequest<PropertiesT>, WriteWeaviateObject<PropertiesT>> endpoint(
public static final <PropertiesT> Endpoint<InsertObjectRequest<PropertiesT>, WeaviateObject<PropertiesT>> endpoint(
CollectionDescriptor<PropertiesT> collection,
CollectionHandleDefaults defaults) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,27 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import io.weaviate.client6.v1.api.collections.IReference;
import io.weaviate.client6.v1.api.collections.WeaviateObject;

public record Reference(String collection, List<String> uuids) {
public record Reference(String collection, List<String> uuids) implements IReference {

@Override
public String uuid() {
return uuids.get(0);
}

@Override
public WeaviateObject<Map<String, Object>> asWeaviateObject() {
throw new IllegalStateException("cannot convert to WeaviateObject");
}

public Reference(String collection, String uuid) {
this(collection, List.of(uuid));
Expand Down
Loading