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

Support warning messages in search/aggregation query results #3958

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
26 changes: 22 additions & 4 deletions src/main/java/redis/clients/jedis/search/SearchResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ public class SearchResult {

private final long totalResults;
private final List<Document> documents;
private final List<String> warnings;

private SearchResult(long totalResults, List<Document> documents) {
this(totalResults, documents, (List<String>) null);
}

private SearchResult(long totalResults, List<Document> documents, List<String> warnings) {
this.totalResults = totalResults;
this.documents = documents;
this.warnings = warnings;
}

public long getTotalResults() {
Expand All @@ -35,10 +41,16 @@ public List<Document> getDocuments() {
return Collections.unmodifiableList(documents);
}

public List<String> getWarnings() {
return warnings;
}

@Override
public String toString() {
return getClass().getSimpleName() + "{Total results:" + totalResults
+ ", Documents:" + documents + "}";
+ ", Documents:" + documents
+ (warnings != null ? ", Warnings:" + warnings : "")
+ "}";
}

public static class SearchResultBuilder extends Builder<SearchResult> {
Expand Down Expand Up @@ -104,6 +116,7 @@ public static final class PerFieldDecoderSearchResultBuilder extends Builder<Sea

private static final String TOTAL_RESULTS_STR = "total_results";
private static final String RESULTS_STR = "results";
private static final String WARNINGS_STR = "warning";

private final Builder<Document> documentBuilder;

Expand All @@ -120,20 +133,25 @@ public SearchResult build(Object data) {
List<KeyValue> list = (List<KeyValue>) data;
long totalResults = -1;
List<Document> results = null;
List<String> warnings = null;
for (KeyValue kv : list) {
String key = BuilderFactory.STRING.build(kv.getKey());
Object rawVal = kv.getValue();
switch (key) {
case TOTAL_RESULTS_STR:
totalResults = BuilderFactory.LONG.build(kv.getValue());
totalResults = BuilderFactory.LONG.build(rawVal);
break;
case RESULTS_STR:
results = ((List<Object>) kv.getValue()).stream()
results = ((List<Object>) rawVal).stream()
.map(documentBuilder::build)
.collect(Collectors.toList());
break;
case WARNINGS_STR:
warnings = BuilderFactory.STRING_LIST.build(rawVal);
break;
}
}
return new SearchResult(totalResults, results);
return new SearchResult(totalResults, results, warnings);
}
};
/// <-- RESP3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,18 @@ public class AggregationResult {

private final List<Map<String, Object>> results;

private Long cursorId = -1L;

private AggregationResult(Object resp, long cursorId) {
this(resp);
this.cursorId = cursorId;
}
private final List<String> warnings;

private AggregationResult(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);
private Long cursorId = -1L;

for (int i = 1; i < list.size(); i++) {
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;
}
map.put((String) r, mapList.get(j + 1));
}
results.add(map);
}
private AggregationResult(long totalResults, List<Map<String, Object>> results) {
this(totalResults, results, (List<String>) null);
}

private AggregationResult(long totalResults, List<Map<String, Object>> results) {
public AggregationResult(long totalResults, List<Map<String, Object>> results, List<String> warnings) {
this.totalResults = totalResults;
this.results = results;
this.warnings = warnings;
}

private void setCursorId(Long cursorId) {
Expand Down Expand Up @@ -80,12 +61,17 @@ public Row getRow(int index) {
return new Row(results.get(index));
}

public List<String> getWarnings() {
return warnings;
}

public static final Builder<AggregationResult> SEARCH_AGGREGATION_RESULT = new Builder<AggregationResult>() {

private static final String TOTAL_RESULTS_STR = "total_results";
private static final String RESULTS_STR = "results";
// private static final String FIELDS_STR = "fields";
private static final String FIELDS_STR = "extra_attributes";
private static final String WARNINGS_STR = "warning";

@Override
public AggregationResult build(Object data) {
Expand All @@ -96,14 +82,16 @@ public AggregationResult build(Object data) {
List<KeyValue> kvList = (List<KeyValue>) data;
long totalResults = -1;
List<Map<String, Object>> results = null;
List<String> warnings = null;
for (KeyValue kv : kvList) {
String key = BuilderFactory.STRING.build(kv.getKey());
Object rawVal = kv.getValue();
switch (key) {
case TOTAL_RESULTS_STR:
totalResults = BuilderFactory.LONG.build(kv.getValue());
totalResults = BuilderFactory.LONG.build(rawVal);
break;
case RESULTS_STR:
List<List<KeyValue>> resList = (List<List<KeyValue>>) kv.getValue();
List<List<KeyValue>> resList = (List<List<KeyValue>>) rawVal;
results = new ArrayList<>(resList.size());
for (List<KeyValue> rikv : resList) {
for (KeyValue ikv : rikv) {
Expand All @@ -114,9 +102,12 @@ public AggregationResult build(Object data) {
}
}
break;
case WARNINGS_STR:
warnings = BuilderFactory.STRING_LIST.build(rawVal);
break;
}
}
return new AggregationResult(totalResults, results);
return new AggregationResult(totalResults, results, warnings);
}

list = (List<Object>) SafeEncoder.encodeObject(data);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package redis.clients.jedis.modules.search;

import static org.junit.Assert.*;
import static redis.clients.jedis.util.AssertUtil.assertEqualsByProtocol;
import static redis.clients.jedis.util.AssertUtil.assertOK;

import java.util.*;
Expand All @@ -17,6 +18,7 @@
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.search.*;
import redis.clients.jedis.search.schemafields.NumericField;
import redis.clients.jedis.search.schemafields.TagField;
import redis.clients.jedis.search.schemafields.TextField;
import redis.clients.jedis.modules.RedisModuleCommandsTestBase;
import redis.clients.jedis.search.aggr.AggregationBuilder;
Expand Down Expand Up @@ -57,6 +59,14 @@ private void addDocument(Document doc) {
client.hset(key, map);
}

private static Map<String, String> toMap(String... values) {
Map<String, String> map = new HashMap<>();
for (int i = 0; i < values.length; i += 2) {
map.put(values[i], values[i + 1]);
}
return map;
}

@Test
public void testQueryParams() {
Schema sc = new Schema().addNumericField("numval");
Expand Down Expand Up @@ -199,4 +209,30 @@ public void dialectBoundSpellCheck() {
FTSpellCheckParams.spellCheckParams().dialect(0)));
MatcherAssert.assertThat(error.getMessage(), Matchers.containsString("DIALECT requires a non negative integer"));
}

@org.junit.Ignore
@Test
public void warningMaxPrefixExpansions() {
final String configParam = "MAXPREFIXEXPANSIONS";
String configValue = (String) client.ftConfigGet(configParam).get(configParam);
try {
assertOK(client.ftCreate(INDEX, FTCreateParams.createParams().on(IndexDataType.HASH),
TextField.of("t"), TagField.of("t2")));

client.hset("doc13", toMap("t", "foo", "t2", "foo"));

client.ftConfigSet(configParam, "1");

SearchResult srcResult = client.ftSearch(INDEX, "fo*");
assertEqualsByProtocol(protocol, null, Arrays.asList(), srcResult.getWarnings());

client.hset("doc23", toMap("t", "fooo", "t2", "fooo"));

AggregationResult aggResult = client.ftAggregate(INDEX, new AggregationBuilder("fo*").loadAll());
assertEqualsByProtocol(protocol, null, Arrays.asList("Max prefix expansions limit was reached"), aggResult.getWarnings());
} finally {
client.ftConfigSet(configParam, configValue);
}
}

}
1 change: 0 additions & 1 deletion src/test/java/redis/clients/jedis/util/AssertUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

Expand Down
Loading