Skip to content

DOC-5110 added hash search examples #4151

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

Merged
merged 3 commits into from
Apr 29, 2025
Merged
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
90 changes: 81 additions & 9 deletions src/test/java/io/redis/examples/HomeJsonExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
package io.redis.examples;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
// REMOVE_END
// STEP_START import
import redis.clients.jedis.UnifiedJedis;
Expand All @@ -13,10 +14,10 @@
import redis.clients.jedis.search.aggr.*;
import redis.clients.jedis.search.schemafields.*;
import org.json.JSONObject;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// STEP_END

// HIDE_START
Expand All @@ -32,7 +33,8 @@ public void run() {
//REMOVE_START
// Clear the indexes and keys here before using them in tests.
try {jedis.ftDropIndex("idx:users");} catch (JedisDataException j){}
jedis.del("bike", "bike:1", "crashes", "newbike", "riders", "bikes:inventory");
try {jedis.ftDropIndex("hash-idx:users");} catch (JedisDataException j){}
jedis.del("user:1", "user:2", "user:3", "huser:1", "huser:2", "huser:3");
//REMOVE_END

// STEP_START create_data
Expand Down Expand Up @@ -135,10 +137,8 @@ public void run() {
System.out.println(aggResult.getTotalResults()); // >>> 2

for (Row cityRow: aggResult.getRows()) {
System.out.println(String.format(
"%s - %d",
cityRow.getString("city"), cityRow.getLong("count"))
);
System.out.printf("%s - %d%n",
cityRow.getString("city"), cityRow.getLong("count"));
}
// >>> London - 1
// >>> Tel Aviv - 2
Expand All @@ -151,6 +151,78 @@ public void run() {
.sorted().toArray());
// REMOVE_END

// STEP_START make_hash_index
SchemaField[] hashSchema = {
TextField.of("name"),
TextField.of("city"),
NumericField.of("age")
};

String hashCreateResult = jedis.ftCreate("hash-idx:users",
FTCreateParams.createParams()
.on(IndexDataType.HASH)
.addPrefix("huser:"),
hashSchema
);

System.out.println(hashCreateResult); // >>> OK
// STEP_END
// REMOVE_START
assertEquals("OK", hashCreateResult);
// REMOVE_END

// STEP_START add_hash_data
Map<String, String> user1Info = new HashMap<>();
user1Info.put("name", "Paul John");
user1Info.put("email", "[email protected]");
user1Info.put("age", "42");
user1Info.put("city", "London");
long huser1Set = jedis.hset("huser:1", user1Info);

System.out.println(huser1Set); // >>> 4

Map<String, String> user2Info = new HashMap<>();
user2Info.put("name", "Eden Zamir");
user2Info.put("email", "[email protected]");
user2Info.put("age", "29");
user2Info.put("city", "Tel Aviv");
long huser2Set = jedis.hset("huser:2", user2Info);

System.out.println(huser2Set); // >>> 4

Map<String, String> user3Info = new HashMap<>();
user3Info.put("name", "Paul Zamir");
user3Info.put("email", "[email protected]");
user3Info.put("age", "35");
user3Info.put("city", "Tel Aviv");
long huser3Set = jedis.hset("huser:3", user3Info);

System.out.println(huser3Set); // >>> 4
// STEP_END
// REMOVE_START
assertEquals(4, huser1Set);
assertEquals(4, huser2Set);
assertEquals(4, huser3Set);
// REMOVE_END

// STEP_START query1_hash
SearchResult findPaulHashResult = jedis.ftSearch("hash-idx:users",
"Paul @age:[30 40]"
);

System.out.println(findPaulHashResult.getTotalResults()); // >>> 1

List<Document> paulHashDocs = findPaulHashResult.getDocuments();

for (Document doc: paulHashDocs) {
System.out.println(doc.getId());
}
// >>> user:3
// STEP_END
// REMOVE_START
assertEquals("huser:3", paulHashDocs.get(0).getId());
// REMOVE_END

// HIDE_START
jedis.close();
}
Expand Down
Loading