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

Address FT.PROFILE change #3867

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
29 changes: 25 additions & 4 deletions src/main/java/redis/clients/jedis/CommandObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -4440,7 +4440,12 @@ public void setDefaultSearchDialect(int dialect) {

private class SearchProfileResponseBuilder<T> extends Builder<Map.Entry<T, Map<String, Object>>> {

private static final String PROFILE_STR = "profile";
private static final String PROFILE_1_STR = "profile";

private static final String RESULTS_2_STR = "Results";
private static final String PROFILE_2_STR = "Profile";

private static final String SHARDS_STR = "Shards";

private final Builder<T> replyBuilder;

Expand All @@ -4454,14 +4459,30 @@ public Map.Entry<T, Map<String, Object>> build(Object data) {
if (list == null || list.isEmpty()) return null;

if (list.get(0) instanceof KeyValue) {
Object results = null, profile = null;
for (KeyValue keyValue : (List<KeyValue>) data) {
if (PROFILE_STR.equals(BuilderFactory.STRING.build(keyValue.getKey()))) {
return KeyValue.of(replyBuilder.build(data),
BuilderFactory.AGGRESSIVE_ENCODED_OBJECT_MAP.build(keyValue.getValue()));
String keyString = BuilderFactory.STRING.build(keyValue.getKey());
Object valueRaw = keyValue.getValue();
if (PROFILE_1_STR.equals(keyString)) {
profile = valueRaw;
results = data;
break;
} else if (RESULTS_2_STR.equals(keyString)) {
results = valueRaw;
} else if (PROFILE_2_STR.equals(keyString)) {
profile = valueRaw;
}
}
if (results != null) {
return KeyValue.of(replyBuilder.build(results),
BuilderFactory.AGGRESSIVE_ENCODED_OBJECT_MAP.build(profile));
}
}

// if (SHARDS_STR.equals(BuilderFactory.STRING.build(((List) list.get(1)).get(0)))) {
// return KeyValue.of(replyBuilder.build(list.get(0)),
// SearchBuilderFactory.SEARCH_PROFILE_PROFILE.build(((List) ((List) list.get(1)).get(1)).get(0)));
// }
return KeyValue.of(replyBuilder.build(list.get(0)),
SearchBuilderFactory.SEARCH_PROFILE_PROFILE.build(list.get(1)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,30 @@ public void testAggregations2() {
assertEquals("10", rows.get(1).get("sum"));
}

private Map<String, Object> getIteratorsProfile(Map profile) {
if (protocol != RedisProtocol.RESP3) {
return (Map) profile.get("Iterators profile");
} else {
if (!profile.containsKey("Shards")) {
return (Map) ((List) profile.get("Iterators profile")).get(0);
} else {
return (Map) ((Map) ((List) profile.get("Shards")).get(0)).get("Iterators profile");
}
}
}

private List<Map<String, Object>> getResultProcessorsProfile(Map profile) {
if (protocol != RedisProtocol.RESP3) {
return (List) profile.get("Result processors profile");
} else {
if (!profile.containsKey("Shards")) {
return (List) profile.get("Result processors profile");
} else {
return (List) ((Map) ((List) profile.get("Shards")).get(0)).get("Result processors profile");
}
}
}

@Test
public void testAggregations2Profile() {
Schema sc = new Schema();
Expand Down Expand Up @@ -168,16 +192,10 @@ public void testAggregations2Profile() {
Map<String, Object> profile = reply.getValue();

assertEquals(Arrays.asList("Index", "Grouper", "Sorter"),
((List<Map<String, Object>>) profile.get("Result processors profile")).stream()
getResultProcessorsProfile(profile).stream()
.map(map -> map.get("Type")).collect(Collectors.toList()));

if (protocol != RedisProtocol.RESP3) {
assertEquals("WILDCARD", ((Map<String, Object>) profile.get("Iterators profile")).get("Type"));
} else {
assertEquals(Arrays.asList("WILDCARD"),
((List<Map<String, Object>>) profile.get("Iterators profile")).stream()
.map(map -> map.get("Type")).collect(Collectors.toList()));
}
assertEquals("WILDCARD", getIteratorsProfile(profile).get("Type"));
}

@Test
Expand Down
51 changes: 28 additions & 23 deletions src/test/java/redis/clients/jedis/modules/search/SearchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,30 @@ public void testDialectsWithFTExplain() throws Exception {
assertTrue("Should contain '{K=10 nearest vector'", client.ftExplain(index, query).contains("{K=10 nearest vector"));
}

private Map<String, Object> getIteratorsProfile(Map profile) {
if (protocol != RedisProtocol.RESP3) {
return (Map) profile.get("Iterators profile");
} else {
if (!profile.containsKey("Shards")) {
return (Map) ((List) profile.get("Iterators profile")).get(0);
} else {
return (Map) ((Map) ((List) profile.get("Shards")).get(0)).get("Iterators profile");
}
}
}

private List<Map<String, Object>> getResultProcessorsProfile(Map profile) {
if (protocol != RedisProtocol.RESP3) {
return (List) profile.get("Result processors profile");
} else {
if (!profile.containsKey("Shards")) {
return (List) profile.get("Result processors profile");
} else {
return (List) ((Map) ((List) profile.get("Shards")).get(0)).get("Result processors profile");
}
}
}

@Test
public void searchProfile() {
Schema sc = new Schema().addTextField("t1", 1.0).addTextField("t2", 1.0);
Expand All @@ -1158,22 +1182,15 @@ public void searchProfile() {
assertEquals(Collections.singletonList("doc1"), result.getDocuments().stream().map(Document::getId).collect(Collectors.toList()));

Map<String, Object> profile = reply.getValue();
Map<String, Object> iteratorsProfile;
if (protocol != RedisProtocol.RESP3) {
iteratorsProfile = (Map<String, Object>) profile.get("Iterators profile");
} else {
List iteratorsProfileList = (List) profile.get("Iterators profile");
assertEquals(1, iteratorsProfileList.size());
iteratorsProfile = (Map<String, Object>) iteratorsProfileList.get(0);
}
Map<String, Object> iteratorsProfile = getIteratorsProfile(profile);
assertEquals("TEXT", iteratorsProfile.get("Type"));
assertEquals("foo", iteratorsProfile.get("Term"));
assertEquals(1L, iteratorsProfile.get("Counter"));
assertEquals(1L, iteratorsProfile.get("Size"));
assertSame(Double.class, iteratorsProfile.get("Time").getClass());

assertEquals(Arrays.asList("Index", "Scorer", "Sorter", "Loader"),
((List<Map<String, Object>>) profile.get("Result processors profile")).stream()
getResultProcessorsProfile(profile).stream()
.map(map -> map.get("Type")).collect(Collectors.toList()));
}

Expand Down Expand Up @@ -1206,13 +1223,7 @@ public void testHNSWVVectorSimilarity() {
doc1 = reply.getKey().getDocuments().get(0);
assertEquals("a", doc1.getId());
assertEquals("0", doc1.get("__v_score"));
if (protocol != RedisProtocol.RESP3) {
assertEquals("VECTOR", ((Map<String, Object>) reply.getValue().get("Iterators profile")).get("Type"));
} else {
assertEquals(Arrays.asList("VECTOR"),
((List<Map<String, Object>>) reply.getValue().get("Iterators profile")).stream()
.map(map -> map.get("Type")).collect(Collectors.toList()));
}
assertEquals("VECTOR", getIteratorsProfile(reply.getValue()).get("Type"));
}

@Test
Expand Down Expand Up @@ -1244,13 +1255,7 @@ public void testFlatVectorSimilarity() {
doc1 = reply.getKey().getDocuments().get(0);
assertEquals("a", doc1.getId());
assertEquals("0", doc1.get("__v_score"));
if (protocol != RedisProtocol.RESP3) {
assertEquals("VECTOR", ((Map<String, Object>) reply.getValue().get("Iterators profile")).get("Type"));
} else {
assertEquals(Arrays.asList("VECTOR"),
((List<Map<String, Object>>) reply.getValue().get("Iterators profile")).stream()
.map(map -> map.get("Type")).collect(Collectors.toList()));
}
assertEquals("VECTOR", getIteratorsProfile(reply.getValue()).get("Type"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,30 @@ public void testFlatVectorSimilarity() {
assertEquals("0", doc1.get("__v_score"));
}

private Map<String, Object> getIteratorsProfile(Map profile) {
if (protocol != RedisProtocol.RESP3) {
return (Map) profile.get("Iterators profile");
} else {
if (!profile.containsKey("Shards")) {
return (Map) ((List) profile.get("Iterators profile")).get(0);
} else {
return (Map) ((Map) ((List) profile.get("Shards")).get(0)).get("Iterators profile");
}
}
}

private List<Map<String, Object>> getResultProcessorsProfile(Map profile) {
if (protocol != RedisProtocol.RESP3) {
return (List) profile.get("Result processors profile");
} else {
if (!profile.containsKey("Shards")) {
return (List) profile.get("Result processors profile");
} else {
return (List) ((Map) ((List) profile.get("Shards")).get(0)).get("Result processors profile");
}
}
}

@Test
public void searchProfile() {
assertOK(client.ftCreate(index, TextField.of("t1"), TextField.of("t2")));
Expand All @@ -1070,25 +1094,19 @@ public void searchProfile() {

SearchResult result = reply.getKey();
assertEquals(1, result.getTotalResults());
assertEquals(Collections.singletonList("doc1"), result.getDocuments().stream().map(Document::getId).collect(Collectors.toList()));
assertEquals(Collections.singletonList("doc1"),
result.getDocuments().stream().map(Document::getId).collect(Collectors.toList()));

Map<String, Object> profile = reply.getValue();
Map<String, Object> iteratorsProfile;
if (protocol != RedisProtocol.RESP3) {
iteratorsProfile = (Map<String, Object>) profile.get("Iterators profile");
} else {
List iteratorsProfileList = (List) profile.get("Iterators profile");
assertEquals(1, iteratorsProfileList.size());
iteratorsProfile = (Map<String, Object>) iteratorsProfileList.get(0);
}
Map<String, Object> iteratorsProfile = getIteratorsProfile(profile);
assertEquals("TEXT", iteratorsProfile.get("Type"));
assertEquals("foo", iteratorsProfile.get("Term"));
assertEquals(1L, iteratorsProfile.get("Counter"));
assertEquals(1L, iteratorsProfile.get("Size"));
assertSame(Double.class, iteratorsProfile.get("Time").getClass());

assertEquals(Arrays.asList("Index", "Scorer", "Sorter", "Loader"),
((List<Map<String, Object>>) profile.get("Result processors profile")).stream()
getResultProcessorsProfile(profile).stream()
.map(map -> map.get("Type")).collect(Collectors.toList()));
}

Expand Down Expand Up @@ -1116,16 +1134,9 @@ public void vectorSearchProfile() {

Map<String, Object> profile = reply.getValue();

if (protocol != RedisProtocol.RESP3) {
assertEquals("VECTOR", ((Map<String, Object>) profile.get("Iterators profile")).get("Type"));
} else {
assertEquals(Arrays.asList("VECTOR"),
((List<Map<String, Object>>) profile.get("Iterators profile")).stream()
.map(map -> map.get("Type")).collect(Collectors.toList()));
}
assertEquals("VECTOR", getIteratorsProfile(profile).get("Type"));

List<Map<String, Object>> resultProcessorsProfile
= (List<Map<String, Object>>) reply.getValue().get("Result processors profile");
List<Map<String, Object>> resultProcessorsProfile = getResultProcessorsProfile(profile);
assertEquals(3, resultProcessorsProfile.size());
assertEquals("Index", resultProcessorsProfile.get(0).get("Type"));
assertEquals("Sorter", resultProcessorsProfile.get(2).get("Type"));
Expand All @@ -1146,13 +1157,8 @@ public void maxPrefixExpansionSearchProfile() {
Map.Entry<SearchResult, Map<String, Object>> reply = client.ftProfileSearch(index,
FTProfileParams.profileParams(), "foo*", FTSearchParams.searchParams().limit(0, 0));
// Warning=Max prefix expansion reached
if (protocol != RedisProtocol.RESP3) {
assertEquals("Max prefix expansion reached",
((Map) reply.getValue().get("Iterators profile")).get("Warning"));
} else {
assertEquals("Max prefix expansion reached",
((Map) ((List) reply.getValue().get("Iterators profile")).get(0)).get("Warning"));
}
assertEquals("Max prefix expansion reached",
getIteratorsProfile(reply.getValue()).get("Warning"));
} finally {
client.ftConfigSet(configParam, configValue);
}
Expand All @@ -1164,12 +1170,10 @@ public void noContentSearchProfile() {
client.hset("1", "t", "foo");
client.hset("2", "t", "bar");

Map.Entry<SearchResult, Map<String, Object>> profile = client.ftProfileSearch(index,
Map.Entry<SearchResult, Map<String, Object>> reply = client.ftProfileSearch(index,
FTProfileParams.profileParams(), "foo -@t:baz", FTSearchParams.searchParams().noContent());

Map<String, Object> depth0 = protocol != RedisProtocol.RESP3
? (Map<String, Object>) profile.getValue().get("Iterators profile")
: ((List<Map<String, Object>>) profile.getValue().get("Iterators profile")).get(0);
Map<String, Object> depth0 = getIteratorsProfile(reply.getValue());

assertEquals("INTERSECT", depth0.get("Type"));
List<Map<String, Object>> depth0_children = (List<Map<String, Object>>) depth0.get("Child iterators");
Expand All @@ -1191,13 +1195,11 @@ public void deepReplySearchProfile() {
client.hset("1", "t", "hello");
client.hset("2", "t", "world");

Map.Entry<SearchResult, Map<String, Object>> profile
Map.Entry<SearchResult, Map<String, Object>> reply
= client.ftProfileSearch(index, FTProfileParams.profileParams(),
"hello(hello(hello(hello(hello(hello)))))", FTSearchParams.searchParams().noContent());

Map<String, Object> depth0 = protocol != RedisProtocol.RESP3
? (Map<String, Object>) profile.getValue().get("Iterators profile")
: ((List<Map<String, Object>>) profile.getValue().get("Iterators profile")).get(0);
Map<String, Object> depth0 = getIteratorsProfile(reply.getValue());

AtomicInteger intersectLevelCount = new AtomicInteger();
AtomicInteger textLevelCount = new AtomicInteger();
Expand Down Expand Up @@ -1234,12 +1236,10 @@ public void limitedSearchProfile() {
client.hset("3", "t", "help");
client.hset("4", "t", "helowa");

Map.Entry<SearchResult, Map<String, Object>> profile = client.ftProfileSearch(index,
Map.Entry<SearchResult, Map<String, Object>> reply = client.ftProfileSearch(index,
FTProfileParams.profileParams().limited(), "%hell% hel*", FTSearchParams.searchParams().noContent());

Map<String, Object> depth0 = protocol != RedisProtocol.RESP3
? (Map<String, Object>) profile.getValue().get("Iterators profile")
: ((List<Map<String, Object>>) profile.getValue().get("Iterators profile")).get(0);
Map<String, Object> depth0 = getIteratorsProfile(reply.getValue());

assertEquals("INTERSECT", depth0.get("Type"));
assertEquals(3L, depth0.get("Counter"));
Expand Down
Loading