Skip to content

Commit

Permalink
Add ability to filter by double values within Mongo queries (#20728)
Browse files Browse the repository at this point in the history
  • Loading branch information
danotorrey authored Oct 18, 2024
1 parent 83511bf commit 9a26861
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ public class SearchQueryField {
public enum Type {
STRING(value -> value),
DATE(value -> dateParser.parseDate(value)),
DOUBLE(value -> Double.parseDouble(value)),
INT(value -> Integer.parseInt(value)),
LONG(value -> Long.parseLong(value)),
OBJECT_ID(value -> new ObjectId(value)),
BOOLEAN(value -> Boolean.parseBoolean(value));

public static final Collection<Type> NUMERIC_TYPES = List.of(DATE, LONG, INT);
public static final Collection<Type> NUMERIC_TYPES = List.of(DATE, LONG, INT, DOUBLE);

private final Function<String, Object> mongoValueConverter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,20 @@ void parsesFilterExpressionCorrectlyForDateType() {
));
}

@Test
void parsesFilterExpressionCorrectlyForDoubleType() {

assertEquals(Filters.eq("num", 22.5),
toTest.parseSingleExpression("num:22.5",
List.of(EntityAttribute.builder()
.id("num")
.title("Num")
.type(SearchQueryField.Type.DOUBLE)
.filterable(true)
.build())
));
}

@Test
void parsesFilterExpressionCorrectlyForIntType() {

Expand Down Expand Up @@ -251,6 +265,26 @@ void parsesFilterExpressionCorrectlyForOpenDateRanges() {
));
}

@Test
void parsesFilterExpressionCorrectlyForDoubleRanges() {
final List<EntityAttribute> entityAttributes = List.of(EntityAttribute.builder()
.id("number")
.title("Number")
.type(SearchQueryField.Type.DOUBLE)
.filterable(true)
.build());

assertEquals(
Filters.and(
Filters.gte("number", 22.5),
Filters.lte("number", 44.5)
),

toTest.parseSingleExpression("number:22.5" + RANGE_VALUES_SEPARATOR + "44.5",
entityAttributes
));
}

@Test
void parsesFilterExpressionCorrectlyForIntRanges() {
final List<EntityAttribute> entityAttributes = List.of(EntityAttribute.builder()
Expand Down

0 comments on commit 9a26861

Please sign in to comment.