From 7b121c4f35ce5d733f157030974c1ee70ee2b710 Mon Sep 17 00:00:00 2001 From: Igor Malinovskiy Date: Fri, 26 Jul 2024 14:09:25 +0200 Subject: [PATCH] Added support for ADDSCORES argument in FT.AGGREGATE (#3908) --- .../clients/jedis/search/SearchProtocol.java | 2 +- .../jedis/search/aggr/AggregationBuilder.java | 5 +++++ .../jedis/modules/search/AggregationTest.java | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main/java/redis/clients/jedis/search/SearchProtocol.java b/src/main/java/redis/clients/jedis/search/SearchProtocol.java index 64482f4fbb..17f5df1349 100644 --- a/src/main/java/redis/clients/jedis/search/SearchProtocol.java +++ b/src/main/java/redis/clients/jedis/search/SearchProtocol.java @@ -56,7 +56,7 @@ public enum SearchKeyword implements Rawable { LANGUAGE_FIELD, SCORE, SCORE_FIELD, SCORER, PARAMS, AS, DIALECT, SLOP, TIMEOUT, INORDER, EXPANDER, MAXTEXTFIELDS, SKIPINITIALSCAN, WITHSUFFIXTRIE, NOSTEM, NOINDEX, PHONETIC, WEIGHT, CASESENSITIVE, LOAD, APPLY, GROUPBY, MAXIDLE, WITHCURSOR, DISTANCE, TERMS, INCLUDE, EXCLUDE, - SEARCH, AGGREGATE, QUERY, LIMITED, COUNT, REDUCE, INDEXMISSING, INDEXEMPTY; + SEARCH, AGGREGATE, QUERY, LIMITED, COUNT, REDUCE, INDEXMISSING, INDEXEMPTY, ADDSCORES; private final byte[] raw; diff --git a/src/main/java/redis/clients/jedis/search/aggr/AggregationBuilder.java b/src/main/java/redis/clients/jedis/search/aggr/AggregationBuilder.java index 7c8b547339..ec478b3367 100644 --- a/src/main/java/redis/clients/jedis/search/aggr/AggregationBuilder.java +++ b/src/main/java/redis/clients/jedis/search/aggr/AggregationBuilder.java @@ -170,6 +170,11 @@ public AggregationBuilder timeout(long timeout) { return this; } + public AggregationBuilder addScores() { + aggrArgs.add(SearchKeyword.ADDSCORES); + return this; + } + public AggregationBuilder params(Map params) { aggrArgs.add(SearchKeyword.PARAMS); aggrArgs.add(params.size() << 1); diff --git a/src/test/java/redis/clients/jedis/modules/search/AggregationTest.java b/src/test/java/redis/clients/jedis/modules/search/AggregationTest.java index 98e811472e..ad960209e3 100644 --- a/src/test/java/redis/clients/jedis/modules/search/AggregationTest.java +++ b/src/test/java/redis/clients/jedis/modules/search/AggregationTest.java @@ -200,6 +200,23 @@ public void testAggregationBuilderVerbatim() { assertEquals(0, res.getTotalResults()); } + @Test + public void testAggregationBuilderAddScores() { + Schema sc = new Schema(); + sc.addSortableTextField("name", 1.0); + sc.addSortableNumericField("age"); + client.ftCreate(index, IndexOptions.defaultOptions(), sc); + addDocument(new Document("data1").set("name", "Adam").set("age", 33)); + addDocument(new Document("data2").set("name", "Sara").set("age", 44)); + + AggregationBuilder r = new AggregationBuilder("sara").addScores() + .apply("@__score * 100", "normalized_score").dialect(3); + + AggregationResult res = client.ftAggregate(index, r); + assertEquals(2, res.getRow(0).getLong("__score")); + assertEquals(200, res.getRow(0).getLong("normalized_score")); + } + @Test public void testAggregationBuilderTimeout() { Schema sc = new Schema();