Skip to content

Commit 18cbb4a

Browse files
committed
feat: add filters for create/update time
1 parent 28ff475 commit 18cbb4a

5 files changed

Lines changed: 80 additions & 7 deletions

File tree

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.weaviate.integration;
22

33
import java.io.IOException;
4+
import java.time.OffsetDateTime;
45
import java.util.Collections;
56
import java.util.Comparator;
67
import java.util.HashMap;
@@ -700,4 +701,26 @@ public void test_filterIsNull() throws IOException {
700701
.hasSize(1).first().actual();
701702
Assertions.assertThat(isNull_1).isNotEqualTo(isNotNull_1);
702703
}
704+
705+
@Test
706+
public void test_filterCreateUpdateTime() throws IOException {
707+
// Arrange
708+
var now = OffsetDateTime.now().minusHours(1);
709+
var nsCounter = ns("Counter");
710+
711+
var counter = client.collections.create(nsCounter,
712+
c -> c
713+
.invertedIndex(idx -> idx.indexTimestamps(true))
714+
.properties(Property.integer("count")));
715+
716+
counter.data.insert(Map.of("count", 0));
717+
718+
// Act
719+
var beforeNow = counter.query.fetchObjects(q -> q.filters(Filter.createdAt().lt(now)));
720+
var afterNow = counter.query.fetchObjects(q -> q.filters(Filter.createdAt().gt(now)));
721+
722+
// Assert
723+
Assertions.assertThat(beforeNow.objects()).isEmpty();
724+
Assertions.assertThat(afterNow.objects()).hasSize(1);
725+
}
703726
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public record BaseQueryOptions(
2626
List<Metadata> returnMetadata,
2727
List<String> includeVectors) {
2828

29+
static final String ID_PROPERTY = "_id";
30+
static final String CREATION_TIME_PROPERTY = "_creationTimeUnix";
31+
static final String LAST_UPDATE_TIME_PROPERTY = "_lastUpdateTimeUnix";
32+
2933
private <T extends Object> BaseQueryOptions(Builder<? extends Builder<?, T>, T> builder) {
3034
this(
3135
builder.limit,

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ public record FetchObjectById(
1919
List<Metadata> returnMetadata,
2020
List<String> includeVectors) implements QueryOperator {
2121

22-
static final String ID_PROPERTY = "_id";
23-
2422
public static FetchObjectById of(String uuid) {
2523
return of(uuid, ObjectBuilder.identity());
2624
}

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

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ private enum Operator {
1818
// Comparison operators
1919
EQUAL("Equal", WeaviateProtoBase.Filters.Operator.OPERATOR_EQUAL),
2020
NOT_EQUAL("NotEqual", WeaviateProtoBase.Filters.Operator.OPERATOR_NOT_EQUAL),
21-
LESS_THAN("LessThen", WeaviateProtoBase.Filters.Operator.OPERATOR_LESS_THAN),
21+
LESS_THAN("LessThan", WeaviateProtoBase.Filters.Operator.OPERATOR_LESS_THAN),
2222
LESS_THAN_EQUAL("LessThenEqual", WeaviateProtoBase.Filters.Operator.OPERATOR_LESS_THAN_EQUAL),
23-
GREATER_THAN("GreaterThen", WeaviateProtoBase.Filters.Operator.OPERATOR_GREATER_THAN),
24-
GREATER_THAN_EQUAL("GreaterThenEqual", WeaviateProtoBase.Filters.Operator.OPERATOR_GREATER_THAN_EQUAL),
23+
GREATER_THAN("GreaterThan", WeaviateProtoBase.Filters.Operator.OPERATOR_GREATER_THAN),
24+
GREATER_THAN_EQUAL("GreaterThanEqual", WeaviateProtoBase.Filters.Operator.OPERATOR_GREATER_THAN_EQUAL),
2525
IS_NULL("IsNull", WeaviateProtoBase.Filters.Operator.OPERATOR_IS_NULL),
2626
LIKE("Like", WeaviateProtoBase.Filters.Operator.OPERATOR_LIKE),
2727
CONTAINS_ANY("ContainsAny", WeaviateProtoBase.Filters.Operator.OPERATOR_CONTAINS_ANY),
@@ -111,7 +111,17 @@ public Filter not() {
111111

112112
/** Filter by object UUID. */
113113
public static FilterBuilder uuid() {
114-
return property(FetchObjectById.ID_PROPERTY);
114+
return property(BaseQueryOptions.ID_PROPERTY);
115+
}
116+
117+
/** Filter by object creation time. */
118+
public static DateProperty createdAt() {
119+
return new DateProperty(BaseQueryOptions.CREATION_TIME_PROPERTY);
120+
}
121+
122+
/** Filter by object last update time. */
123+
public static DateProperty lastUpdatedAt() {
124+
return new DateProperty(BaseQueryOptions.LAST_UPDATE_TIME_PROPERTY);
115125
}
116126

117127
/** Filter by object property. */
@@ -635,6 +645,44 @@ public String toString() {
635645
}
636646
}
637647

648+
public static class DateProperty extends PathOperand {
649+
private DateProperty(String propertyName) {
650+
super(propertyName);
651+
}
652+
653+
public Filter eq(OffsetDateTime value) {
654+
return new Filter(Operator.EQUAL, this, new DateOperand(value));
655+
}
656+
657+
public Filter ne(OffsetDateTime value) {
658+
return new Filter(Operator.NOT_EQUAL, this, new DateOperand(value));
659+
}
660+
661+
public Filter gt(OffsetDateTime value) {
662+
return new Filter(Operator.GREATER_THAN, this, new DateOperand(value));
663+
}
664+
665+
public Filter gte(OffsetDateTime value) {
666+
return new Filter(Operator.GREATER_THAN_EQUAL, this, new DateOperand(value));
667+
}
668+
669+
public Filter lt(OffsetDateTime value) {
670+
return new Filter(Operator.LESS_THAN, this, new DateOperand(value));
671+
}
672+
673+
public Filter lte(OffsetDateTime value) {
674+
return new Filter(Operator.LESS_THAN_EQUAL, this, new DateOperand(value));
675+
}
676+
677+
public Filter containsAny(OffsetDateTime... values) {
678+
return new Filter(Operator.CONTAINS_ANY, this, new DateArrayOperand(values));
679+
}
680+
681+
public Filter containsNone(OffsetDateTime... values) {
682+
return new Filter(Operator.CONTAINS_NONE, this, new DateArrayOperand(values));
683+
}
684+
}
685+
638686
private static class TextOperand implements FilterOperand {
639687
private final String value;
640688

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static SortBy property(String property) {
2626
* @see #desc() to sort in descending order.
2727
*/
2828
public static SortBy uuid() {
29-
return property(FetchObjectById.ID_PROPERTY);
29+
return property(BaseQueryOptions.ID_PROPERTY);
3030
}
3131

3232
/**

0 commit comments

Comments
 (0)