Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/it/java/io/weaviate/integration/DataITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void testCreateGetDelete() throws IOException {
var object = artists.query.byId(id, query -> query
.returnProperties("name")
.returnMetadata(
Metadata.UUID, Metadata.VECTOR,
Metadata.VECTOR,
Metadata.CREATION_TIME_UNIX, Metadata.LAST_UPDATE_TIME_UNIX));

Assertions.assertThat(artists.data.exists(id))
Expand Down Expand Up @@ -257,10 +257,8 @@ public void testUpdate() throws IOException {
var updIvanhoe = books.query.byId(
ivanhoe.metadata().uuid(),
query -> query
.returnMetadata(Metadata.VECTOR)
.returnReferences(
QueryReference.single("writtenBy",
writtenBy -> writtenBy.returnMetadata(Metadata.UUID))));
.includeVector()
.returnReferences(QueryReference.single("writtenBy")));

Assertions.assertThat(updIvanhoe).get()
.satisfies(book -> {
Expand Down Expand Up @@ -387,8 +385,7 @@ public void testReferenceAddMany() throws IOException {

var goodburgAirports = cities.query.byId(goodburg.metadata().uuid(),
city -> city.returnReferences(
QueryReference.single("hasAirports",
airport -> airport.returnMetadata(Metadata.UUID))));
QueryReference.single("hasAirports")));

Assertions.assertThat(goodburgAirports).get()
.as("Goodburg has 3 airports")
Expand Down
12 changes: 9 additions & 3 deletions src/it/java/io/weaviate/integration/PaginationITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.weaviate.client6.v1.api.collections.WeaviateMetadata;
import io.weaviate.client6.v1.api.collections.WeaviateObject;
import io.weaviate.client6.v1.api.collections.pagination.PaginationException;
import io.weaviate.client6.v1.api.collections.query.Metadata;
import io.weaviate.containers.Container;

public class PaginationITest extends ConcurrentTest {
Expand Down Expand Up @@ -117,11 +118,16 @@ public void testWithQueryOptions() throws IOException {
}

// Act / Assert
var withSomeProperties = things.paginate(p -> p.returnProperties("fetch_me"));
var withSomeProperties = things.paginate(p -> p
.returnMetadata(Metadata.CREATION_TIME_UNIX)
.returnProperties("fetch_me"));
for (var thing : withSomeProperties) {
Assertions.assertThat(thing.properties())
.as("uuid=" + thing.metadata().uuid())
.as("uuid=" + thing.uuid())
.doesNotContainKey("dont_fetch");

Assertions.assertThat(thing.metadata().creationTimeUnix())
.isNotNull();
}
}

Expand All @@ -140,7 +146,7 @@ public void testAsyncPaginator() throws Exception, InterruptedException, Executi
var inserted = new ArrayList<String>();
for (var i = 0; i < count; i++) {
futures[i] = things.data.insert(Collections.emptyMap())
.thenAccept(object -> inserted.add(object.metadata().uuid()));
.thenAccept(object -> inserted.add(object.uuid()));
}
CompletableFuture.allOf(futures).get();

Expand Down
11 changes: 3 additions & 8 deletions src/it/java/io/weaviate/integration/ReferencesITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import io.weaviate.client6.v1.api.collections.ReferenceProperty;
import io.weaviate.client6.v1.api.collections.WeaviateObject;
import io.weaviate.client6.v1.api.collections.data.Reference;
import io.weaviate.client6.v1.api.collections.query.Metadata;
import io.weaviate.client6.v1.api.collections.query.QueryReference;
import io.weaviate.containers.Container;

Expand Down Expand Up @@ -94,10 +93,8 @@ public void testReferences() throws IOException {

var gotAlex = artists.query.byId(alex.metadata().uuid(),
opt -> opt.returnReferences(
QueryReference.multi("hasAwards", nsOscar,
ref -> ref.returnMetadata(Metadata.UUID)),
QueryReference.multi("hasAwards", nsGrammy,
ref -> ref.returnMetadata(Metadata.UUID))));
QueryReference.multi("hasAwards", nsOscar),
QueryReference.multi("hasAwards", nsGrammy)));

Assertions.assertThat(gotAlex).get()
.as("Artists: fetch by id including hasAwards references")
Expand Down Expand Up @@ -164,9 +161,7 @@ public void testNestedReferences() throws IOException {
ref -> ref
// Name of the CEO of the presenting academy
.returnReferences(
QueryReference.single("presentedBy", r -> r.returnProperties("ceo")))
// Grammy ID
.returnMetadata(Metadata.UUID))));
QueryReference.single("presentedBy", r -> r.returnProperties("ceo"))))));

Assertions.assertThat(gotAlex).get()
.as("Artists: fetch by id including nested references")
Expand Down
47 changes: 46 additions & 1 deletion src/it/java/io/weaviate/integration/SearchITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public void testHybrid() throws IOException {
// Act
var winterSport = hobbies.query.hybrid("winter",
hybrid -> hybrid
.returnMetadata(Metadata.UUID, Metadata.SCORE, Metadata.EXPLAIN_SCORE));
.returnMetadata(Metadata.SCORE, Metadata.EXPLAIN_SCORE));

// Assert
Assertions.assertThat(winterSport.objects())
Expand Down Expand Up @@ -415,4 +415,49 @@ public void testBadRequest_async() throws Throwable {
}
}
}

@Test
public void testMetadataAll() throws IOException {
// Arrange
var nsThings = ns("Things");
client.collections.create(nsThings,
c -> c
.properties(Property.text("name"))
.vectors(Vectorizers.text2vecContextionary(
t2v -> t2v.sourceProperties("name"))));

var things = client.collections.use(nsThings);
var frisbee = things.data.insert(Map.of("name", "orange disc"));

// Act
var gotHybrid = things.query.hybrid("orange", q -> q
.queryProperties("name")
.returnMetadata(Metadata.ALL));

var gotNearText = things.query.nearText("frisbee", q -> q
.returnMetadata(Metadata.ALL));

// Assert
var metadataHybrid = Assertions.assertThat(gotHybrid.objects())
.hasSize(1)
.extracting(WeaviateObject::metadata)
.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.score()).as("score").isNotNull();
Assertions.assertThat(metadataHybrid.explainScore()).as("explainScore").isNotNull().isNotEqualTo("");

var metadataNearText = Assertions.assertThat(gotNearText.objects())
.hasSize(1)
.extracting(WeaviateObject::metadata)
.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.distance()).as("distance").isNotNull();
Assertions.assertThat(metadataNearText.certainty()).as("certainty").isNotNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ public CompletableFuture<AsyncPage<PropertiesT>> fetchNextPage() {
return new AsyncPage<>(null, pageSize, fetch, nextPage);
}
var last = nextPage.get(nextPage.size() - 1);
return new AsyncPage<>(last.metadata().uuid(), pageSize, fetch, nextPage);
var nextCursor = last.uuid();
// The cursor can only be null on the first iteration.
// If it is null after the first iteration it is
// because we haven't requested Metadata.UUID, in which
// case pagination will continue to run unbounded.
assert nextCursor != null : "page cursor is null";
return new AsyncPage<>(nextCursor, pageSize, fetch, nextPage);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ public boolean tryAdvance(Consumer<? super WeaviateObject<PropertiesT, Object, Q
if (nextPage.isEmpty()) {
return false;
}
cursor = nextPage.get(nextPage.size() - 1).metadata().uuid();
cursor = nextPage.get(nextPage.size() - 1).uuid();

// The cursor can only be null on the first iteration.
// If it is null after the first iteration it is
// because we haven't requested Metadata.UUID, in which
// case pagination will continue to run unbounded.
assert cursor != null : "page cursor is null";

currentPage = nextPage.iterator();
return tryAdvance(action);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import org.apache.commons.lang3.StringUtils;

import io.weaviate.client6.v1.api.collections.query.Metadata.MetadataField;
import io.weaviate.client6.v1.internal.ObjectBuilder;
import io.weaviate.client6.v1.internal.grpc.protocol.WeaviateProtoBase;
import io.weaviate.client6.v1.internal.grpc.protocol.WeaviateProtoSearchGet;
Expand Down Expand Up @@ -47,6 +48,10 @@ public static abstract class Builder<SELF extends Builder<SELF, T>, T extends Ob
private List<QueryReference> returnReferences = new ArrayList<>();
private List<Metadata> returnMetadata = new ArrayList<>();

protected Builder() {
returnMetadata(MetadataField.UUID);
}

public final SELF limit(int limit) {
this.limit = limit;
return (SELF) this;
Expand Down Expand Up @@ -112,6 +117,10 @@ public final SELF returnMetadata(List<Metadata> metadata) {
return (SELF) this;
}

public final SELF includeVector() {
return returnMetadata(Metadata.VECTOR);
}

final BaseQueryOptions baseOptions() {
return _build();
}
Expand Down Expand Up @@ -151,11 +160,7 @@ final void appendTo(WeaviateProtoSearchGet.SearchRequest.Builder req) {
}

var metadata = WeaviateProtoSearchGet.MetadataRequest.newBuilder();
if (returnMetadata.isEmpty()) {
Metadata.UUID.appendTo(metadata);
} else {
returnMetadata.forEach(m -> m.appendTo(metadata));
}
returnMetadata.forEach(m -> m.appendTo(metadata));
req.setMetadata(metadata);

if (!returnProperties.isEmpty() || !returnReferences.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;

import io.weaviate.client6.v1.api.collections.query.Metadata.MetadataField;
import io.weaviate.client6.v1.internal.ObjectBuilder;
import io.weaviate.client6.v1.internal.grpc.protocol.WeaviateProtoBase;
import io.weaviate.client6.v1.internal.grpc.protocol.WeaviateProtoSearchGet;
Expand All @@ -27,38 +30,55 @@ public static ById of(String uuid, Function<Builder, ObjectBuilder<ById>> fn) {

public ById(Builder builder) {
this(builder.uuid,
builder.returnProperties,
new ArrayList<>(builder.returnProperties),
builder.returnReferences,
builder.returnMetadata);
new ArrayList<>(builder.returnMetadata));
}

public static class Builder implements ObjectBuilder<ById> {
// Required query parameters.
private final String uuid;

private List<String> returnProperties = new ArrayList<>();
private Set<String> returnProperties = new HashSet<>();
private List<QueryReference> returnReferences = new ArrayList<>();
private List<Metadata> returnMetadata = new ArrayList<>();
private Set<Metadata> returnMetadata = new HashSet<>();

public Builder(String uuid) {
this.uuid = uuid;
returnMetadata(MetadataField.UUID);
}

public final Builder returnProperties(String... properties) {
this.returnProperties = Arrays.asList(properties);
return returnProperties(Arrays.asList(properties));
}

public final Builder returnProperties(List<String> properties) {
this.returnProperties.addAll(properties);
return this;
}

public final Builder returnReferences(QueryReference... references) {
this.returnReferences = Arrays.asList(references);
return returnReferences(Arrays.asList(references));
}

public final Builder returnReferences(List<QueryReference> references) {
this.returnReferences.addAll(references);
return this;
}

public final Builder returnMetadata(Metadata... metadata) {
this.returnMetadata = Arrays.asList(metadata);
return returnMetadata(Arrays.asList(metadata));
}

public final Builder returnMetadata(List<Metadata> metadata) {
this.returnMetadata.addAll(metadata);
return this;
}

public final Builder includeVector() {
return returnMetadata(Metadata.VECTOR);
}

@Override
public ById build() {
return new ById(this);
Expand All @@ -73,9 +93,6 @@ public void appendTo(WeaviateProtoSearchGet.SearchRequest.Builder req) {
req.setFilters(filter);

var metadata = WeaviateProtoSearchGet.MetadataRequest.newBuilder();
if (returnMetadata.isEmpty()) {
returnMetadata.add(Metadata.UUID);
}
returnMetadata.forEach(m -> m.appendTo(metadata));
req.setMetadata(metadata);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
public interface Metadata {
void appendTo(WeaviateProtoSearchGet.MetadataRequest.Builder metadata);

/** Include UUID of the object in the metadata response. */
public static final Metadata UUID = MetadataField.UUID;
/** Include metadata in the metadata response. */
public static final Metadata ALL = MetadataField.ALL;
/** Include associated vector in the metadata response. */
public static final Metadata VECTOR = MetadataField.VECTOR;
/** Include object creation time in the metadata response. */
Expand Down Expand Up @@ -70,6 +70,7 @@ public interface Metadata {
* MetadataField are collection properties that can be requested for any object.
*/
enum MetadataField implements Metadata {
ALL,
UUID,
VECTOR,
CREATION_TIME_UNIX,
Expand All @@ -81,6 +82,13 @@ enum MetadataField implements Metadata {

public void appendTo(WeaviateProtoSearchGet.MetadataRequest.Builder metadata) {
switch (this) {
case ALL:
for (final var f : MetadataField.values()) {
if (f != ALL) {
f.appendTo(metadata);
}
}
break;
case UUID:
metadata.setUuid(true);
break;
Expand Down
Loading