Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Object from aggregation Row #3253

Merged
merged 2 commits into from
Dec 22, 2022
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
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package redis.clients.jedis.search.aggr;

import redis.clients.jedis.exceptions.JedisDataException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.util.SafeEncoder;

/**
* Created by mnunberg on 2/22/18.
*/
public class AggregationResult {

/**
Expand All @@ -18,7 +17,7 @@ public class AggregationResult {
@Deprecated
public final long totalResults;

private final List<Map<String, Object>> results = new ArrayList<>();
private final List<Map<String, Object>> results;

private long cursorId = -1;

Expand All @@ -28,21 +27,23 @@ public AggregationResult(Object resp, long cursorId) {
}

public AggregationResult(Object resp) {
List<Object> list = (List<Object>) resp;
List<Object> list = (List<Object>) SafeEncoder.encodeObject(resp);

// the first element is always the number of results
totalResults = (Long) list.get(0);
results = new ArrayList<>(list.size() - 1);

for (int i = 1; i < list.size(); i++) {
List<Object> raw = (List<Object>) list.get(i);
Map<String, Object> cur = new HashMap<>();
for (int j = 0; j < raw.size(); j += 2) {
Object r = raw.get(j);
List<Object> mapList = (List<Object>) list.get(i);
Map<String, Object> map = new HashMap<>(mapList.size() / 2, 1f);
for (int j = 0; j < mapList.size(); j += 2) {
Object r = mapList.get(j);
if (r instanceof JedisDataException) {
throw (JedisDataException) r;
}
cur.put(new String((byte[]) r), raw.get(j + 1));
map.put((String) r, mapList.get(j + 1));
}
results.add(cur);
results.add(map);
}
}

Expand All @@ -54,10 +55,15 @@ public List<Map<String, Object>> getResults() {
return results;
}

/**
* @return results as {@link Row}s.
* @see #getResults()
*/
public List<Row> getRows() {
return results.stream().map(Row::new).collect(Collectors.toList());
}

public Row getRow(int index) {
if (index >= results.size()) {
return null;
}
return new Row(results.get(index));
}

Expand Down
21 changes: 13 additions & 8 deletions src/main/java/redis/clients/jedis/search/aggr/Row.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package redis.clients.jedis.search.aggr;

import java.util.Map;
import redis.clients.jedis.util.DoublePrecision;

/**
* Created by mnunberg on 5/17/18.
*
* Row in aggregation result-set
*/
public class Row {

private final Map<String, Object> fields;
Expand All @@ -19,24 +15,33 @@ public boolean containsKey(String key) {
return fields.containsKey(key);
}

public Object get(String key) {
return fields.get(key);
}

public String getString(String key) {
if (!containsKey(key)) {
return "";
}
return new String((byte[]) fields.get(key));
return (String) fields.get(key);
}

public long getLong(String key) {
if (!containsKey(key)) {
return 0;
}
return Long.parseLong(getString(key));
return Long.parseLong((String) fields.get(key));
}

public double getDouble(String key) {
if (!containsKey(key)) {
return 0;
}
return Double.parseDouble(getString(key));
return DoublePrecision.parseFloatingPointNumber((String) fields.get(key));
}

@Override
public String toString() {
return String.valueOf(fields);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package redis.clients.jedis.modules.search;

import redis.clients.jedis.exceptions.JedisDataException;
import static org.junit.Assert.*;

import org.junit.BeforeClass;
import org.junit.Test;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.*;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.search.Document;
import redis.clients.jedis.search.FieldName;
import redis.clients.jedis.search.IndexOptions;
Expand Down Expand Up @@ -85,6 +86,34 @@ public void testAggregations() {
assertEquals(10, r2.getLong("sum"));
}

@Test
public void testAggregations2() {
Schema sc = new Schema();
sc.addSortableTextField("name", 1.0);
sc.addSortableNumericField("count");
client.ftCreate(index, IndexOptions.defaultOptions(), sc);

addDocument(new Document("data1").set("name", "abc").set("count", 10));
addDocument(new Document("data2").set("name", "def").set("count", 5));
addDocument(new Document("data3").set("name", "def").set("count", 25));

AggregationBuilder r = new AggregationBuilder()
.groupBy("@name", Reducers.sum("@count").as("sum"))
.sortBy(10, SortedField.desc("@sum"));

// actual search
AggregationResult res = client.ftAggregate(index, r);
assertEquals(2, res.getTotalResults());

List<Row> rows = res.getRows();
assertEquals("def", rows.get(0).get("name"));
assertEquals("30", rows.get(0).get("sum"));
assertNull(rows.get(0).get("nosuchcol"));

assertEquals("abc", rows.get(1).get("name"));
assertEquals("10", rows.get(1).get("sum"));
}

@Test
public void testAggregationBuilderVerbatim() {
Schema sc = new Schema();
Expand Down