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: 6 additions & 5 deletions src/it/java/io/weaviate/ConcurrentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.Random;
import java.util.UUID;
import java.util.stream.IntStream;

import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Rule;
Expand Down Expand Up @@ -56,9 +55,11 @@ protected static String randomUUID() {
* @param bound Value range upper bound.
* @return
*/
protected static Float[] randomVector(int length, float origin, float bound) {
return IntStream.range(0, length)
.<Float>mapToObj(f -> rand.nextFloat(origin, bound))
.toArray(Float[]::new);
protected static float[] randomVector(int length, float origin, float bound) {
var vector = new float[length];
for (var i = 0; i < length; i++) {
vector[i] = rand.nextFloat(origin, bound);
}
return vector;
}
}
8 changes: 4 additions & 4 deletions src/it/java/io/weaviate/integration/DataITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static void beforeAll() throws IOException {
public void testCreateGetDelete() throws IOException {
var artists = client.collections.use(COLLECTION);
var id = randomUUID();
Float[] vector = { 1f, 2f, 3f };
float[] vector = { 1, 2, 3 };

artists.data.insert(Map.of("name", "john doe"),
metadata -> metadata
Expand All @@ -56,8 +56,8 @@ public void testCreateGetDelete() throws IOException {
Assertions.assertThat(obj.metadata().uuid())
.as("object id").isEqualTo(id);

Assertions.assertThat(obj.metadata().vectors()).extracting(v -> v.getSingle(VECTOR_INDEX))
.asInstanceOf(InstanceOfAssertFactories.array(Float[].class)).containsExactly(vector);
Assertions.assertThat(obj.metadata().vectors().getSingle(VECTOR_INDEX))
.containsExactly(vector);

Assertions.assertThat(obj.properties())
.as("has expected properties")
Expand Down Expand Up @@ -227,7 +227,7 @@ public void testUpdate() throws IOException {
var authors = client.collections.use(nsAuthors);
var walter = authors.data.insert(Map.of("name", "walter scott"));

var vector = new Float[] { 1f, 2f, 3f };
var vector = new float[] { 1, 2, 3 };

var books = client.collections.use(nsBooks);

Expand Down
8 changes: 4 additions & 4 deletions src/it/java/io/weaviate/integration/SearchITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class SearchITest extends ConcurrentTest {
/**
* One of the inserted vectors which will be used as target vector for search.
*/
private static Float[] searchVector;
private static float[] searchVector;

@BeforeClass
public static void beforeAll() throws IOException {
Expand Down Expand Up @@ -102,10 +102,10 @@ public void testNearVector_groupBy() {
/**
* Insert 10 objects with random vectors.
*
* @returns IDs of inserted objects and their corresponding vectors.
* @return IDs of inserted objects and their corresponding vectors.
*/
private static Map<String, Float[]> populateTest(int n) throws IOException {
var created = new HashMap<String, Float[]>();
private static Map<String, float[]> populateTest(int n) throws IOException {
var created = new HashMap<String, float[]>();

var things = client.collections.use(COLLECTION);
for (int i = 0; i < n; i++) {
Expand Down
90 changes: 68 additions & 22 deletions src/main/java/io/weaviate/client6/v1/api/collections/Vectors.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,22 @@
*/
@ToString
public class Vectors {
/** Elements of this map must only be {@code float[]} or {@code float[][]}. */
private final Map<String, Object> namedVectors;

public static Vectors of(Float[] vector) {
return new Vectors(VectorIndex.DEFAULT_VECTOR_NAME, vector);
public static Vectors of(float[] vector) {
return of(VectorIndex.DEFAULT_VECTOR_NAME, vector);
}

public static Vectors of(String name, Float[] vector) {
public static Vectors of(String name, float[] vector) {
return new Vectors(name, vector);
}

public static Vectors of(Float[][] vector) {
return new Vectors(VectorIndex.DEFAULT_VECTOR_NAME, vector);
public static Vectors of(float[][] vector) {
return of(VectorIndex.DEFAULT_VECTOR_NAME, vector);
}

public static Vectors of(String name, Float[][] vector) {
public static Vectors of(String name, float[][] vector) {
return new Vectors(name, vector);
}

Expand All @@ -51,33 +52,43 @@ public Vectors(Builder builder) {
this.namedVectors = builder.namedVectors;
}

/*
/**
* Create a single named vector.
* Intended to be used by factory methods, which can statically restrict
* vector's type to {@code Float[]} and {@code Float[][]}.
*
* @param name Vector name.
*
* @param vector {@code Float[]} or {@code Float[][]} vector.
* <p>
* Callers must ensure that vectors are either
* {@code float[]} or {@code float[][]}.
*
* @param name Vector name.
* @param vector {@code float[]} or {@code float[][]} vector.
*/
private Vectors(String name, Object vector) {
this.namedVectors = Collections.singletonMap(name, vector);
}

/**
* Create a Vectors from a map.
*
* <p>
* Callers must ensure that vectors are either
* {@code float[]} or {@code float[][]}.
*
* @param name Vector name.
* @param vector Map of named vectors.
*/
private Vectors(Map<String, Object> namedVectors) {
this.namedVectors = namedVectors;
}

public static class Builder implements ObjectBuilder<Vectors> {
private final Map<String, Object> namedVectors = new HashMap<>();

public Builder vector(String name, Float[] vector) {
public Builder vector(String name, float[] vector) {
this.namedVectors.put(name, vector);
return this;
}

public Builder vector(String name, Float[][] vector) {
public Builder vector(String name, float[][] vector) {
this.namedVectors.put(name, vector);
return this;
}
Expand All @@ -88,22 +99,55 @@ public Vectors build() {
}
}

public Float[] getSingle(String name) {
return (Float[]) namedVectors.get(name);
/**
* Get 1-dimensional vector by name.
*
* @return Vector as {@code float[]} or {@code null}.
* @throws ClassCastException The underlying vector is not a {@code float[]}.
*/
public float[] getSingle(String name) {
return (float[]) namedVectors.get(name);
}

public Float[] getDefaultSingle() {
/**
* Get default 1-dimensional vector.
*
* @return Vector as {@code float[]} or {@code null}.
* @throws ClassCastException if the underlying object is not a {@code float[]}.
*/
public float[] getDefaultSingle() {
return getSingle(VectorIndex.DEFAULT_VECTOR_NAME);
}

public Float[][] getMulti(String name) {
return (Float[][]) namedVectors.get(name);
/**
* Get 2-dimensional vector by name.
*
* @return Vector as {@code float[][]} or {@code null}.
* @throws ClassCastException if the underlying object is not a
* {@code float[][]}.
*/
public float[][] getMulti(String name) {
return (float[][]) namedVectors.get(name);
}

public Float[][] getDefaultMulti() {
/**
* Get default 2-dimensional vector.
*
* @return Vector as {@code float[][]} or {@code null}.
* @throws ClassCastException if the underlying object is not a
* {@code float[][]}.
*/
public float[][] getDefaultMulti() {
return getMulti(VectorIndex.DEFAULT_VECTOR_NAME);
}

/**
* Get all vectors.
* Each element is either a {@code float[]} or a {@code float[][]}.
*
*
* @return Map of name-vector pairs. The returned map is immutable.
*/
public Map<String, Object> asMap() {
return Map.copyOf(namedVectors);
}
Expand All @@ -119,8 +163,8 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
}
final var mapAdapter = gson.getDelegateAdapter(this, new TypeToken<Map<String, Object>>() {
});
final var float_1d = gson.getDelegateAdapter(this, TypeToken.get(Float[].class));
final var float_2d = gson.getDelegateAdapter(this, TypeToken.get(Float[][].class));
final var float_1d = gson.getDelegateAdapter(this, TypeToken.get(float[].class));
final var float_2d = gson.getDelegateAdapter(this, TypeToken.get(float[][].class));
return (TypeAdapter<T>) new TypeAdapter<Vectors>() {

@Override
Expand All @@ -144,6 +188,8 @@ public Vectors read(JsonReader in) throws IOException {
} else {
vector = float_1d.fromJsonTree(array);
}

assert (vector instanceof float[]) || (vector instanceof float[][]) : "invalid vector type";
namedVectors.put(vectorName, vector);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ public GroupedResponseT hybrid(Hybrid filter, Function<Aggregation.Builder, Obje

// NearVector ---------------------------------------------------------------

public ResponseT nearVector(Float[] vector, Function<Aggregation.Builder, ObjectBuilder<Aggregation>> fn) {
public ResponseT nearVector(float[] vector, Function<Aggregation.Builder, ObjectBuilder<Aggregation>> fn) {
return nearVector(NearVector.of(vector), fn);
}

public ResponseT nearVector(Float[] vector, Function<NearVector.Builder, ObjectBuilder<NearVector>> nv,
public ResponseT nearVector(float[] vector, Function<NearVector.Builder, ObjectBuilder<NearVector>> nv,
Function<Aggregation.Builder, ObjectBuilder<Aggregation>> fn) {
return nearVector(NearVector.of(vector, nv), fn);
}
Expand All @@ -85,12 +85,12 @@ public ResponseT nearVector(NearVector filter, Function<Aggregation.Builder, Obj
return performRequest(Aggregation.of(filter, fn));
}

public GroupedResponseT nearVector(Float[] vector, Function<Aggregation.Builder, ObjectBuilder<Aggregation>> fn,
public GroupedResponseT nearVector(float[] vector, Function<Aggregation.Builder, ObjectBuilder<Aggregation>> fn,
GroupBy groupBy) {
return nearVector(NearVector.of(vector), fn, groupBy);
}

public GroupedResponseT nearVector(Float[] vector, Function<NearVector.Builder, ObjectBuilder<NearVector>> nv,
public GroupedResponseT nearVector(float[] vector, Function<NearVector.Builder, ObjectBuilder<NearVector>> nv,
Function<Aggregation.Builder, ObjectBuilder<Aggregation>> fn, GroupBy groupBy) {
return nearVector(NearVector.of(vector, nv), fn, groupBy);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public String text() {
}

public boolean isInteger() {
return value instanceof String;
return value instanceof Long;
}

public Integer integer() {
checkPropertyType(this::isInteger, "Integer");
return (Integer) value;
public Long integer() {
checkPropertyType(this::isInteger, "Long");
return (Long) value;
}

private void checkPropertyType(Supplier<Boolean> check, String expected) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ public static <T> void buildObject(WeaviateProtoBatch.BatchObject.Builder object
var vector = WeaviateProtoBase.Vectors.newBuilder()
.setName(entry.getKey());

if (value instanceof Float[] single) {
if (value instanceof float[] single) {
vector.setType(VectorType.VECTOR_TYPE_SINGLE_FP32);
vector.setVectorBytes(ByteStringUtil.encodeVectorSingle(single));
} else if (value instanceof Float[][] multi) {
} else if (value instanceof float[][] multi) {
vector.setVectorBytes(ByteStringUtil.encodeVectorMulti(multi));
vector.setType(VectorType.VECTOR_TYPE_MULTI_FP32);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,23 @@ public GroupedResponseT hybrid(Hybrid query, GroupBy groupBy) {

// NearVector queries -------------------------------------------------------

public ResponseT nearVector(Float[] vector) {
public ResponseT nearVector(float[] vector) {
return nearVector(NearVector.of(vector));
}

public ResponseT nearVector(Float[] vector, Function<NearVector.Builder, ObjectBuilder<NearVector>> fn) {
public ResponseT nearVector(float[] vector, Function<NearVector.Builder, ObjectBuilder<NearVector>> fn) {
return nearVector(NearVector.of(vector, fn));
}

public ResponseT nearVector(NearVector query) {
return performRequest(query);
}

public GroupedResponseT nearVector(Float[] vector, GroupBy groupBy) {
public GroupedResponseT nearVector(float[] vector, GroupBy groupBy) {
return nearVector(NearVector.of(vector), groupBy);
}

public GroupedResponseT nearVector(Float[] vector, Function<NearVector.Builder, ObjectBuilder<NearVector>> fn,
public GroupedResponseT nearVector(float[] vector, Function<NearVector.Builder, ObjectBuilder<NearVector>> fn,
GroupBy groupBy) {
return nearVector(NearVector.of(vector, fn), groupBy);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ 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<>();

public final SELF limit(Integer limit) {
public final SELF limit(int limit) {
this.limit = limit;
return (SELF) this;
}

public final SELF offset(Integer offset) {
public final SELF offset(int offset) {
this.offset = offset;
return (SELF) this;
}

public final SELF autocut(Integer autocut) {
public final SELF autocut(int autocut) {
this.autocut = autocut;
return (SELF) this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
import io.weaviate.client6.v1.internal.grpc.protocol.WeaviateProtoBaseSearch;
import io.weaviate.client6.v1.internal.grpc.protocol.WeaviateProtoSearchGet;

public record NearVector(Float[] vector, Float distance, Float certainty, BaseQueryOptions common)
public record NearVector(float[] vector, Float distance, Float certainty, BaseQueryOptions common)
implements QueryOperator, AggregateObjectFilter {

public static final NearVector of(Float[] vector) {
public static final NearVector of(float[] vector) {
return of(vector, ObjectBuilder.identity());
}

public static final NearVector of(Float[] vector, Function<Builder, ObjectBuilder<NearVector>> fn) {
public static final NearVector of(float[] vector, Function<Builder, ObjectBuilder<NearVector>> fn) {
return fn.apply(new Builder(vector)).build();
}

Expand All @@ -27,9 +27,9 @@ public NearVector(Builder builder) {

public static class Builder extends BaseVectorSearchBuilder<Builder, NearVector> {
// Required query parameters.
private final Float[] vector;
private final float[] vector;

public Builder(Float[] vector) {
public Builder(float[] vector) {
this.vector = vector;
}

Expand Down
Loading