Skip to content

Commit e4d07ad

Browse files
committed
feat: implement filtering by property length
1 parent 6aec8ce commit e4d07ad

2 files changed

Lines changed: 35 additions & 8 deletions

File tree

src/it/java/io/weaviate/integration/SearchITest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,4 +723,21 @@ public void test_filterCreateUpdateTime() throws IOException {
723723
Assertions.assertThat(beforeNow.objects()).isEmpty();
724724
Assertions.assertThat(afterNow.objects()).hasSize(1);
725725
}
726+
727+
@Test
728+
public void teset_filterPropertyLength() throws IOException {
729+
// Arrange
730+
var nsStrings = ns("Strings");
731+
732+
var strings = client.collections.create(nsStrings, c -> c
733+
.invertedIndex(idx -> idx.indexPropertyLength(true))
734+
.properties(Property.text("letters")));
735+
strings.data.insertMany(Map.of("letters", "abc"), Map.of("letters", "abcd"), Map.of("letters", "a"));
736+
737+
// Act
738+
var got = strings.query.fetchObjects(q -> q.filters(Filter.propertyLen("letters").gte(3)));
739+
740+
// Assertions
741+
Assertions.assertThat(got.objects()).hasSize(2);
742+
}
726743
}

src/main/java/io/weaviate/client6/v1/api/collections/query/Filter.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,12 @@ public static DateProperty lastUpdatedAt() {
126126

127127
/** Filter by object property. */
128128
public static FilterBuilder property(String property) {
129-
return new FilterBuilder(new PathOperand(property));
129+
return new FilterBuilder(new PathOperand(false, property));
130+
}
131+
132+
/** Filter by object property's length. */
133+
public static FilterBuilder propertyLen(String property) {
134+
return new FilterBuilder(new PathOperand(true, property));
130135
}
131136

132137
public static class FilterBuilder {
@@ -616,22 +621,27 @@ static FilterOperand fromObject(Object value) {
616621

617622
private static class PathOperand implements FilterOperand {
618623
private final List<String> path;
624+
private final boolean length;
619625

620-
private PathOperand(List<String> path) {
626+
private PathOperand(boolean length, List<String> path) {
621627
this.path = path;
628+
this.length = length;
622629
}
623630

624-
private PathOperand(String... path) {
625-
this(Arrays.asList(path));
631+
private PathOperand(boolean length, String... path) {
632+
this(length, Arrays.asList(path));
626633
}
627634

628635
@Override
629636
public void appendTo(WeaviateProtoBase.Filters.Builder filter) {
630637
if (!path.isEmpty()) {
638+
var property = path.get(0);
639+
if (length) {
640+
property = "len(" + property + ")";
641+
}
631642
filter.setTarget(WeaviateProtoBase.FilterTarget.newBuilder()
632-
.setProperty(path.get(0)));
643+
.setProperty(property));
633644
}
634-
635645
// FIXME: no way to reference objects rn?
636646
}
637647

@@ -643,7 +653,7 @@ public String toString() {
643653

644654
public static class UuidProperty extends PathOperand {
645655
private UuidProperty() {
646-
super(BaseQueryOptions.ID_PROPERTY);
656+
super(false, BaseQueryOptions.ID_PROPERTY);
647657
}
648658

649659
public Filter eq(String value) {
@@ -681,7 +691,7 @@ public Filter containsNone(String... values) {
681691

682692
public static class DateProperty extends PathOperand {
683693
private DateProperty(String propertyName) {
684-
super(propertyName);
694+
super(false, propertyName);
685695
}
686696

687697
public Filter eq(OffsetDateTime value) {

0 commit comments

Comments
 (0)