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
12 changes: 10 additions & 2 deletions src/it/java/io/weaviate/integration/DataITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import io.weaviate.ConcurrentTest;
import io.weaviate.client6.v1.api.WeaviateApiException;
import io.weaviate.client6.v1.api.WeaviateClient;
import io.weaviate.client6.v1.api.collections.GeoCoordinates;
import io.weaviate.client6.v1.api.collections.PhoneNumber;
import io.weaviate.client6.v1.api.collections.Property;
import io.weaviate.client6.v1.api.collections.ReferenceProperty;
import io.weaviate.client6.v1.api.collections.VectorConfig;
Expand Down Expand Up @@ -432,6 +434,8 @@ public void testDataTypes() throws IOException {
Property.dateArray("prop_date_array"),
Property.uuidArray("prop_uuid_array"),
Property.textArray("prop_text_array"),
Property.phoneNumber("prop_phone_number"),
Property.geoCoordinates("prop_geo_coordinates"),
Property.object("prop_object",
p -> p.nestedProperties(
Property.text("marco"))),
Expand All @@ -457,6 +461,8 @@ public void testDataTypes() throws IOException {
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")),
Map.entry("prop_phone_number", PhoneNumber.international("+380 95 1433336")),
Map.entry("prop_geo_coordinates", new GeoCoordinates(1f, 2f)),
Map.entry("prop_object", Map.of("marco", "polo")),
Map.entry("prop_object_array", List.of(Map.of("marco", "polo"))));

Expand All @@ -468,8 +474,10 @@ public void testDataTypes() throws IOException {
Assertions.assertThat(got).get()
.extracting(WeaviateObject::properties)
.asInstanceOf(InstanceOfAssertFactories.map(String.class, Object.class))
.containsAllEntriesOf(want);

// Most of PhoneNumber fields are only present on read and are null on write.
.usingRecursiveComparison()
.withComparatorForType(ORMITest::comparePhoneNumbers, PhoneNumber.class)
.isEqualTo(want);
}

record Address(
Expand Down
29 changes: 22 additions & 7 deletions src/it/java/io/weaviate/integration/ORMITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import io.weaviate.ConcurrentTest;
import io.weaviate.client6.v1.api.WeaviateClient;
import io.weaviate.client6.v1.api.collections.CollectionConfig;
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.api.collections.annotations.Collection;
import io.weaviate.client6.v1.api.collections.annotations.Property;
Expand Down Expand Up @@ -79,7 +81,10 @@ static record Thing(
Boolean booleanBoxed,
boolean[] booleanArray,
Boolean[] booleanBoxedArray,
List<Boolean> booleanBoxedList) {
List<Boolean> booleanBoxedList,

PhoneNumber phoneNumber,
GeoCoordinates geoCoordinates) {
}

@BeforeClass
Expand Down Expand Up @@ -150,14 +155,18 @@ public void test_createCollection() throws Exception {
Map.entry("booleanBoxed", "boolean"),
Map.entry("booleanArray", "boolean[]"),
Map.entry("booleanBoxedArray", "boolean[]"),
Map.entry("booleanBoxedList", "boolean[]"));
Map.entry("booleanBoxedList", "boolean[]"),

Map.entry("phoneNumber", "phoneNumber"),
Map.entry("geoCoordinates", "geoCoordinates"));
}

private final RecursiveComparisonConfiguration COMPARISON_CONFIG = RecursiveComparisonConfiguration.builder()
private static final RecursiveComparisonConfiguration COMPARISON_CONFIG = RecursiveComparisonConfiguration.builder()
// Assertj is having a really bad time comparing List<Float>,
// so we'll just always return true here.
.withComparatorForFields((a, b) -> 0, "floatBoxedList")
.withComparatorForType((a, b) -> Double.compare(a.doubleValue(), b.doubleValue()), Number.class)
.withComparatorForType(ORMITest::comparePhoneNumbers, PhoneNumber.class)
.build();

@Test
Expand Down Expand Up @@ -219,7 +228,10 @@ public void test_insertAndQuery() throws Exception {
boolean_,
new boolean[] { boolean_ },
new Boolean[] { boolean_ },
List.of(boolean_));
List.of(boolean_),

PhoneNumber.international("+380 95 1433336"),
new GeoCoordinates(1f, 2f));

var things = client.collections.use(Thing.class);

Expand Down Expand Up @@ -294,7 +306,10 @@ public void test_insertManyAndQuery() throws Exception {
boolean_,
new boolean[] { boolean_ },
new Boolean[] { boolean_ },
List.of(boolean_));
List.of(boolean_),

PhoneNumber.international("+380 95 1433336"),
new GeoCoordinates(1f, 2f));

var things = client.collections.use(Thing.class);

Expand Down Expand Up @@ -351,7 +366,7 @@ public void test_partialScan() throws IOException {
.returns(null, Song::monthlyListeners);
}

@Test
public void test_nestedProperties() throws IOException {
static int comparePhoneNumbers(PhoneNumber phone1, PhoneNumber phone2) {
return phone1.rawInput().compareTo(phone2.rawInput());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public interface DataType {
public static final String UUID_ARRAY = "uuid[]";
public static final String OBJECT = "object";
public static final String OBJECT_ARRAY = "object[]";
public static final String PHONE_NUMBER = "phoneNumber";
public static final String GEO_COORDINATES = "geoCoordinates";

/**
* Scalar/array types which Weaviate and WeaviateClient recognize.
Expand All @@ -34,5 +36,6 @@ public interface DataType {
*/
public static final Set<String> KNOWN_TYPES = ImmutableSet.of(
TEXT, INT, BLOB, BOOL, DATE, UUID, NUMBER, OBJECT,
TEXT_ARRAY, INT_ARRAY, NUMBER_ARRAY, BOOL_ARRAY, DATE_ARRAY, UUID_ARRAY, OBJECT_ARRAY);
TEXT_ARRAY, INT_ARRAY, NUMBER_ARRAY, BOOL_ARRAY, DATE_ARRAY, UUID_ARRAY, OBJECT_ARRAY,
PHONE_NUMBER, GEO_COORDINATES);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.weaviate.client6.v1.api.collections;

import com.google.gson.annotations.SerializedName;

public record GeoCoordinates(
@SerializedName("latitude") float latitude,
@SerializedName("longitude") float longitude) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.weaviate.client6.v1.api.collections;

import com.google.gson.annotations.SerializedName;

public record PhoneNumber(
/** Raw input data provided at creation. */
@SerializedName("input") String rawInput,
/**
* ISO 3166-1 alpha-2 country code. Required only if the raw input does not
* include an explicit country code, e.g. {@code +31}. Only present if provided
* by user.
*/
@SerializedName("defaultCountry") String defaultCountry,
/** Numerical country code. Returned by Weaviate on read. */
@SerializedName("countryCode") Integer countryCode,
/**
* Phone number with numerical country code prepended.
* Returned by Weaviate on read.
*/
@SerializedName("internationalFormatted") String internationalFormatted,
/**
* Numerical representation of the national number.
* Returned by Weaviate on read.
*/
@SerializedName("national") Integer national,
/**
* Formatted national number.
* Returned by Weaviate on read.
*/
@SerializedName("nationalFormatted") String nationalFormatted,
/**
* Whether the server recognized this number as valid.
* Returned by Weaviate on read.
*/
@SerializedName("valid") Boolean valid) {

/**
* Create national phone number (without explicit country code),
* e.g. {@code "020 1234567"}
*
* @param country ISO 3166-1 alpha-2 country code.
* @param phoneNumber Phone number.
* @return PhoneNumber
*/
public static PhoneNumber national(String country, String phoneNumber) {
return new PhoneNumber(phoneNumber, country, null, null, null, null, null);
}

/**
* Create a phone number with explicit country code,
* e.g. {@code "+31 20 1234567"}
*
* @param phoneNumber Phone number.
* @return PhoneNumber
*/
public static PhoneNumber international(String phoneNumber) {
return new PhoneNumber(phoneNumber, null, null, null, null, null, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public static Property objectArray(String name) {
}

/**
* Create a {@code objectArray[]} property with additional configuration.
* Create a {@code object[]} property with additional configuration.
*
* @param name Property name.
* @param fn Lambda expression for optional parameters.
Expand All @@ -308,6 +308,44 @@ public static Property objectArray(String name, Function<Builder, ObjectBuilder<
return newProperty(name, DataType.OBJECT_ARRAY, fn);
}

/**
* Create a {@code phoneNumber} property.
*
* @param name Property name.
*/
public static Property phoneNumber(String name) {
return phoneNumber(name, ObjectBuilder.identity());
}

/**
* Create a {@code phoneNumber} property with additional configuration.
*
* @param name Property name.
* @param fn Lambda expression for optional parameters.
*/
public static Property phoneNumber(String name, Function<Builder, ObjectBuilder<Property>> fn) {
return newProperty(name, DataType.PHONE_NUMBER, fn);
}

/**
* Create a {@code geoCoordinates} property.
*
* @param name Property name.
*/
public static Property geoCoordinates(String name) {
return geoCoordinates(name, ObjectBuilder.identity());
}

/**
* Create a {@code geoCoordinates} property with additional configuration.
*
* @param name Property name.
* @param fn Lambda expression for optional parameters.
*/
public static Property geoCoordinates(String name, Function<Builder, ObjectBuilder<Property>> fn) {
return newProperty(name, DataType.GEO_COORDINATES, fn);
}

private static Property newProperty(String name, String dataType, Function<Builder, ObjectBuilder<Property>> fn) {
return fn.apply(new Builder(name, dataType)).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import java.util.UUID;

import io.weaviate.client6.v1.api.collections.CollectionHandleDefaults;
import io.weaviate.client6.v1.api.collections.GeoCoordinates;
import io.weaviate.client6.v1.api.collections.ObjectMetadata;
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;
Expand Down Expand Up @@ -181,6 +183,23 @@ private static com.google.protobuf.Value marshalValue(Object value) {
protoValue.setBoolValue(v.booleanValue());
} else if (value instanceof Number v) {
protoValue.setNumberValue(v.doubleValue());
} else if (value instanceof PhoneNumber phone) {
var phoneProto = com.google.protobuf.Struct.newBuilder();
if (phone.rawInput() != null) {
var input = com.google.protobuf.Value.newBuilder().setStringValue(phone.rawInput());
phoneProto.putFields("input", input.build());
}
if (phone.defaultCountry() != null) {
var defaultCountry = com.google.protobuf.Value.newBuilder().setStringValue(phone.defaultCountry());
phoneProto.putFields("defaultCountry", defaultCountry.build());
}
protoValue.setStructValue(phoneProto);
} else if (value instanceof GeoCoordinates geo) {
var latitude = com.google.protobuf.Value.newBuilder().setNumberValue(geo.latitude());
var longitude = com.google.protobuf.Value.newBuilder().setNumberValue(geo.longitude());
protoValue.setStructValue(com.google.protobuf.Struct.newBuilder()
.putFields("latitude", latitude.build())
.putFields("longitude", longitude.build()));
} else if (value instanceof List<?> v) {
protoValue.setListValue(
com.google.protobuf.ListValue.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import java.util.UUID;
import java.util.stream.Stream;

import io.weaviate.client6.v1.api.collections.GeoCoordinates;
import io.weaviate.client6.v1.api.collections.ObjectMetadata;
import io.weaviate.client6.v1.api.collections.PhoneNumber;
import io.weaviate.client6.v1.api.collections.Vectors;
import io.weaviate.client6.v1.api.collections.WeaviateObject;
import io.weaviate.client6.v1.internal.DateUtil;
Expand Down Expand Up @@ -159,6 +161,21 @@ static <PropertiesT> void setProperty(String property, WeaviateProtoProperties.V
builder.setOffsetDateTime(property, DateUtil.fromISO8601(value.getDateValue()));
} else if (value.hasUuidValue()) {
builder.setUuid(property, UUID.fromString(value.getUuidValue()));
} else if (value.hasPhoneValue()) {
var phone = value.getPhoneValue();
builder.setPhoneNumber(property, new PhoneNumber(
phone.getInput(),
phone.getDefaultCountry(),
Long.valueOf(phone.getCountryCode()).intValue(),
phone.getInternationalFormatted(),
Long.valueOf(phone.getNational()).intValue(),
phone.getNationalFormatted(),
phone.getValid()));
} else if (value.hasGeoValue()) {
var geo = value.getGeoValue();
builder.setGeoCoordinates(property, new GeoCoordinates(
geo.getLatitude(),
geo.getLongitude()));
} else if (value.hasListValue()) {
var list = value.getListValue();
if (list.hasTextValues()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -826,11 +826,11 @@ public String toString() {
}

private static class GeoRangeOperand implements WhereOperand {
private final Float lat;
private final Float lon;
private final Float distance;
private final float lat;
private final float lon;
private final float distance;

private GeoRangeOperand(Float lat, Float lon, Float distance) {
private GeoRangeOperand(float lat, float lon, float distance) {
this.lat = lat;
this.lon = lon;
this.distance = distance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import java.util.Map;
import java.util.UUID;

import io.weaviate.client6.v1.api.collections.GeoCoordinates;
import io.weaviate.client6.v1.api.collections.PhoneNumber;

public class MapBuilder implements PropertiesBuilder<Map<String, Object>> {
private final Map<String, Object> properties = new HashMap<>();

Expand Down Expand Up @@ -89,8 +92,18 @@ public void setNestedObjectArray(String property, List<? extends Object> value)
properties.put(property, value);
}

@Override
public void setPhoneNumber(String property, PhoneNumber value) {
properties.put(property, value);
}

@Override
public void setGeoCoordinates(String property, GeoCoordinates value) {
properties.put(property, value);
}

@Override
public Map<String, Object> build() {
return properties;
return new HashMap<>(properties);
}
}
13 changes: 13 additions & 0 deletions src/main/java/io/weaviate/client6/v1/internal/orm/PojoBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

import org.apache.commons.lang3.ArrayUtils;

import io.weaviate.client6.v1.api.collections.GeoCoordinates;
import io.weaviate.client6.v1.api.collections.PhoneNumber;

final class PojoBuilder<PropertiesT extends Record> implements PropertiesBuilder<PropertiesT> {
private static final Map<Class<?>, Object> PRIMITIVE_DEFAULTS;

Expand Down Expand Up @@ -237,6 +240,16 @@ public void setNestedObjectArray(String property, List<? extends Object> value)
throw new UnsupportedOperationException("Unimplemented method 'setNestedObjectArray'");
}

@Override
public void setPhoneNumber(String propertyName, PhoneNumber value) {
setValue(propertyName, value);
}

@Override
public void setGeoCoordinates(String propertyName, GeoCoordinates value) {
setValue(propertyName, value);
}

@Override
public PropertiesT build() {
Object[] args = ctorArgs.values().stream().map(Arg::value).toArray();
Expand Down
Loading