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
10 changes: 5 additions & 5 deletions src/it/java/io/weaviate/integration/AggregationITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
import io.weaviate.client6.v1.api.collections.Property;
import io.weaviate.client6.v1.api.collections.Vectorizers;
import io.weaviate.client6.v1.api.collections.Vectors;
import io.weaviate.client6.v1.api.collections.aggregate.Aggregate;
import io.weaviate.client6.v1.api.collections.aggregate.AggregateResponseGroup;
import io.weaviate.client6.v1.api.collections.aggregate.AggregateResponseGrouped;
import io.weaviate.client6.v1.api.collections.aggregate.Aggregation;
import io.weaviate.client6.v1.api.collections.aggregate.GroupBy;
import io.weaviate.client6.v1.api.collections.aggregate.GroupedBy;
import io.weaviate.client6.v1.api.collections.aggregate.IntegerAggregation;
Expand Down Expand Up @@ -57,7 +57,7 @@ public void testOverAll() {
var result = things.aggregate.overAll(
with -> with
.metrics(
Aggregation.integer("price",
Aggregate.integer("price",
calculate -> calculate.median().max().count()))
.includeTotalCount(true));

Expand All @@ -77,7 +77,7 @@ public void testOverAll_groupBy_category() {
var result = things.aggregate.overAll(
with -> with
.metrics(
Aggregation.integer("price",
Aggregate.integer("price",
calculate -> calculate.min().max().count()))
.includeTotalCount(true),
GroupBy.property("category"));
Expand Down Expand Up @@ -115,7 +115,7 @@ public void testNearVector() {
near -> near.limit(5),
with -> with
.metrics(
Aggregation.integer("price",
Aggregate.integer("price",
calculate -> calculate.min().max().count()))
.objectLimit(4)
.includeTotalCount(true));
Expand All @@ -135,7 +135,7 @@ public void testNearVector_groupBy_category() {
near -> near.distance(2f),
with -> with
.metrics(
Aggregation.integer("price",
Aggregate.integer("price",
calculate -> calculate.min().max().median()))
.objectLimit(9)
.includeTotalCount(true),
Expand Down
60 changes: 60 additions & 0 deletions src/it/java/io/weaviate/integration/DataITest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package io.weaviate.integration;

import java.io.IOException;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.assertj.core.api.Assertions;
import org.assertj.core.api.InstanceOfAssertFactories;
Expand Down Expand Up @@ -407,4 +410,61 @@ public void testDuplicateUuid() throws IOException {
// Act
things.data.insert(Map.of(), thing -> thing.uuid(thing_1.uuid()));
}

@Test
public void testDataTypes() throws IOException {
// Arrange
var nsDataTypes = ns("DataTypes");

// BLOB type is omitted because a base64-encoded image
// isn't doing the failure message any favours.
// It's tested in other test cases above.
client.collections.create(
nsDataTypes, c -> c
.properties(
Property.text("prop_text"),
Property.integer("prop_integer"),
Property.number("prop_number"),
Property.bool("prop_bool"),
Property.date("prop_date"),
Property.uuid("prop_uuid"),
Property.integerArray("prop_integer_array"),
Property.numberArray("prop_number_array"),
Property.boolArray("prop_bool_array"),
Property.dateArray("prop_date_array"),
Property.uuidArray("prop_uuid_array"),
Property.textArray("prop_text_array")));

var types = client.collections.use(nsDataTypes);

var now = OffsetDateTime.now();
var uuid = UUID.randomUUID();

Map<String, Object> want = Map.ofEntries(
Map.entry("prop_text", "Hello, World!"),
Map.entry("prop_integer", 1L),
Map.entry("prop_number", 1D),
Map.entry("prop_bool", true),
Map.entry("prop_date", now),
Map.entry("prop_uuid", uuid),
Map.entry("prop_integer_array", List.of(1L, 2L, 3L)),
Map.entry("prop_number_array", List.of(1D, 2D, 3D)),
Map.entry("prop_bool_array", List.of(true, false)),
Map.entry("prop_date_array", List.of(now, now)),
Map.entry("prop_uuid_array", List.of(uuid, uuid)),
Map.entry("prop_text_array", List.of("a", "b", "c")));
var returnProperties = want.keySet().toArray(String[]::new);

// Act
var object = types.data.insert(want);
var got = types.query.byId(object.uuid(),
q -> q.returnProperties(returnProperties));

// Assert
Assertions.assertThat(got).get()
.extracting(WeaviateObject::properties)
.asInstanceOf(InstanceOfAssertFactories.map(String.class, Object.class))
.containsAllEntriesOf(want);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,20 @@

public interface DataType {
public static final String TEXT = "text";
public static final String TEXT_ARRAY = "text[]";
public static final String INT = "int";
public static final String INT_ARRAY = "int[]";
public static final String NUMBER = "number";
public static final String NUMBER_ARRAY = "number[]";
public static final String BOOL = "boolean";
public static final String BOOL_ARRAY = "boolean[]";
public static final String BLOB = "blob";
public static final String DATE = "date";
public static final String DATE_ARRAY = "date[]";
public static final String UUID = "uuid";
public static final String UUID_ARRAY = "uuid[]";

public static final Set<String> KNOWN_TYPES = ImmutableSet.of(TEXT, INT, BLOB);
public static final Set<String> KNOWN_TYPES = ImmutableSet.of(
TEXT, INT, BLOB, BOOL, DATE, UUID, NUMBER,
TEXT_ARRAY, INT_ARRAY, NUMBER_ARRAY, BOOL_ARRAY, DATE_ARRAY, UUID_ARRAY);
}
90 changes: 87 additions & 3 deletions src/main/java/io/weaviate/client6/v1/api/collections/Property.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,107 @@ public static Property text(String name) {
}

public static Property text(String name, Function<Builder, ObjectBuilder<Property>> fn) {
return fn.apply(new Builder(name, DataType.TEXT)).build();
return newProperty(name, DataType.TEXT, fn);
}

public static Property textArray(String name) {
return textArray(name, ObjectBuilder.identity());
}

public static Property textArray(String name, Function<Builder, ObjectBuilder<Property>> fn) {
return newProperty(name, DataType.TEXT_ARRAY, fn);
}

public static Property integer(String name) {
return integer(name, ObjectBuilder.identity());
}

public static Property integer(String name, Function<Builder, ObjectBuilder<Property>> fn) {
return fn.apply(new Builder(name, DataType.INT)).build();
return newProperty(name, DataType.INT, fn);
}

public static Property integerArray(String name) {
return integerArray(name, ObjectBuilder.identity());
}

public static Property integerArray(String name, Function<Builder, ObjectBuilder<Property>> fn) {
return newProperty(name, DataType.INT_ARRAY, fn);
}

public static Property blob(String name) {
return blob(name, ObjectBuilder.identity());
}

public static Property blob(String name, Function<Builder, ObjectBuilder<Property>> fn) {
return fn.apply(new Builder(name, DataType.BLOB)).build();
return newProperty(name, DataType.BLOB, fn);
}

public static Property bool(String name) {
return bool(name, ObjectBuilder.identity());
}

public static Property bool(String name, Function<Builder, ObjectBuilder<Property>> fn) {
return newProperty(name, DataType.BOOL, fn);
}

public static Property boolArray(String name) {
return boolArray(name, ObjectBuilder.identity());
}

public static Property boolArray(String name, Function<Builder, ObjectBuilder<Property>> fn) {
return newProperty(name, DataType.BOOL_ARRAY, fn);
}

public static Property date(String name) {
return date(name, ObjectBuilder.identity());
}

public static Property date(String name, Function<Builder, ObjectBuilder<Property>> fn) {
return newProperty(name, DataType.DATE, fn);
}

public static Property dateArray(String name) {
return dateArray(name, ObjectBuilder.identity());
}

public static Property dateArray(String name, Function<Builder, ObjectBuilder<Property>> fn) {
return newProperty(name, DataType.DATE_ARRAY, fn);
}

public static Property uuid(String name) {
return uuid(name, ObjectBuilder.identity());
}

public static Property uuid(String name, Function<Builder, ObjectBuilder<Property>> fn) {
return newProperty(name, DataType.UUID, fn);
}

public static Property uuidArray(String name) {
return uuidArray(name, ObjectBuilder.identity());
}

public static Property uuidArray(String name, Function<Builder, ObjectBuilder<Property>> fn) {
return newProperty(name, DataType.UUID_ARRAY, fn);
}

public static Property number(String name) {
return number(name, ObjectBuilder.identity());
}

public static Property number(String name, Function<Builder, ObjectBuilder<Property>> fn) {
return newProperty(name, DataType.NUMBER, fn);
}

public static Property numberArray(String name) {
return numberArray(name, ObjectBuilder.identity());
}

public static Property numberArray(String name, Function<Builder, ObjectBuilder<Property>> fn) {
return newProperty(name, DataType.NUMBER_ARRAY, fn);
}

private static Property newProperty(String name, String dataType, Function<Builder, ObjectBuilder<Property>> fn) {
return fn.apply(new Builder(name, dataType)).build();
}

public static ReferenceProperty reference(String name, String... collections) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.weaviate.client6.v1.api.collections.aggregate;

import java.util.function.Function;

import io.weaviate.client6.v1.internal.ObjectBuilder;

public final class Aggregate {
/** Prevent public initialization. */
private Aggregate() {
}

public static final PropertyAggregation text(String property,
Function<TextAggregation.Builder, ObjectBuilder<TextAggregation>> fn) {
return TextAggregation.of(property, fn);
}

public static final PropertyAggregation integer(String property,
Function<IntegerAggregation.Builder, ObjectBuilder<IntegerAggregation>> fn) {
return IntegerAggregation.of(property, fn);
}

public static final PropertyAggregation bool(String property,
Function<BooleanAggregation.Builder, ObjectBuilder<BooleanAggregation>> fn) {
return BooleanAggregation.of(property, fn);
}

public static final PropertyAggregation date(String property,
Function<DateAggregation.Builder, ObjectBuilder<DateAggregation>> fn) {
return DateAggregation.of(property, fn);
}

public static final PropertyAggregation number(String property,
Function<NumberAggregation.Builder, ObjectBuilder<NumberAggregation>> fn) {
return NumberAggregation.of(property, fn);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.HashMap;
import java.util.Map;

import io.weaviate.client6.v1.internal.DateUtil;
import io.weaviate.client6.v1.api.collections.CollectionHandleDefaults;
import io.weaviate.client6.v1.internal.grpc.Rpc;
import io.weaviate.client6.v1.internal.grpc.protocol.WeaviateGrpc.WeaviateBlockingStub;
Expand Down Expand Up @@ -62,9 +63,21 @@ static <T> Rpc<AggregateRequest, WeaviateProtoAggregate.AggregateRequest, Aggreg
var property = groupBy.getPathList().get(0);

if (groupBy.hasInt()) {
groupedBy = new GroupedBy<Long>(property, groupBy.getInt());
groupedBy = new GroupedBy<>(property, groupBy.getInt());
} else if (groupBy.hasText()) {
groupedBy = new GroupedBy<String>(property, groupBy.getText());
groupedBy = new GroupedBy<>(property, groupBy.getText());
} else if (groupBy.hasBoolean()) {
groupedBy = new GroupedBy<>(property, groupBy.getBoolean());
} else if (groupBy.hasNumber()) {
groupedBy = new GroupedBy<>(property, groupBy.getNumber());
} else if (groupBy.hasTexts()) {
groupedBy = new GroupedBy<>(property, groupBy.getTexts().getValuesList().toArray(String[]::new));
} else if (groupBy.hasInts()) {
groupedBy = new GroupedBy<>(property, groupBy.getInts().getValuesList().toArray(Long[]::new));
} else if (groupBy.hasNumbers()) {
groupedBy = new GroupedBy<>(property, groupBy.getNumbers().getValuesList().toArray(Double[]::new));
} else if (groupBy.hasBooleans()) {
groupedBy = new GroupedBy<>(property, groupBy.getBooleans().getValuesList().toArray(Boolean[]::new));
} else {
assert false : "(aggregate) branch not covered";
}
Expand All @@ -77,6 +90,7 @@ static <T> Rpc<AggregateRequest, WeaviateProtoAggregate.AggregateRequest, Aggreg
}
return new AggregateResponseGrouped(groups);
}, () -> rpc.method(), () -> rpc.methodAsync());

}

private static Map<String, Object> unmarshalAggregation(WeaviateProtoAggregate.AggregateReply.Aggregations result) {
Expand Down Expand Up @@ -107,7 +121,32 @@ private static Map<String, Object> unmarshalAggregation(WeaviateProtoAggregate.A
value = new TextAggregation.Values(
metric.hasCount() ? metric.getCount() : null,
topOccurrences);

} else if (aggregation.hasBoolean()) {
var metric = aggregation.getBoolean();
value = new BooleanAggregation.Values(
metric.hasCount() ? metric.getCount() : null,
metric.hasPercentageFalse() ? Float.valueOf((float) metric.getPercentageFalse()) : null,
metric.hasPercentageTrue() ? Float.valueOf((float) metric.getPercentageTrue()) : null,
metric.hasTotalFalse() ? metric.getTotalFalse() : null,
metric.hasTotalTrue() ? metric.getTotalTrue() : null);
} else if (aggregation.hasDate()) {
var metric = aggregation.getDate();
value = new DateAggregation.Values(
metric.hasCount() ? metric.getCount() : null,
metric.hasMinimum() ? DateUtil.fromISO8601(metric.getMinimum()) : null,
metric.hasMaximum() ? DateUtil.fromISO8601(metric.getMaximum()) : null,
metric.hasMedian() ? DateUtil.fromISO8601(metric.getMedian()) : null,
metric.hasMode() ? DateUtil.fromISO8601(metric.getMode()) : null);
} else if (aggregation.hasNumber()) {
var metric = aggregation.getNumber();
value = new NumberAggregation.Values(
metric.hasCount() ? metric.getCount() : null,
metric.hasMinimum() ? metric.getMinimum() : null,
metric.hasMaximum() ? metric.getMaximum() : null,
metric.hasMean() ? metric.getMean() : null,
metric.hasMedian() ? metric.getMedian() : null,
metric.hasMode() ? metric.getMode() : null,
metric.hasSum() ? metric.getSum() : null);
} else {
assert false : "branch not covered";
}
Expand Down
Loading