From ac2a4e0e88d0757a3597793d4d2097e9515559fe Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Sun, 25 Aug 2024 14:03:45 +0600 Subject: [PATCH] [DOC] RediSearch usage with FTCreateParams and FTSearchParams classes (#3934) * Update redisearch.md * Update redisjson.md * fix spell check --- .github/wordlist.txt | 2 ++ docs/redisearch.md | 46 ++++++++++++++++++++++++++++++++++++++++---- docs/redisjson.md | 20 +++++++++++++++++-- 3 files changed, 62 insertions(+), 6 deletions(-) diff --git a/.github/wordlist.txt b/.github/wordlist.txt index 7d0d85f49d..d60e297814 100644 --- a/.github/wordlist.txt +++ b/.github/wordlist.txt @@ -20,6 +20,8 @@ EVAL EVALSHA Failback Failover +FTCreateParams +FTSearchParams GSON GenericObjectPool GenericObjectPoolConfig diff --git a/docs/redisearch.md b/docs/redisearch.md index 4292f75048..c446de0b41 100644 --- a/docs/redisearch.md +++ b/docs/redisearch.md @@ -1,6 +1,6 @@ # RediSearch Jedis Quick Start -To use RediSearch features with Jedis, you'll need to use and implementation of RediSearchCommands. +To use RediSearch features with Jedis, you'll need to use an implementation of RediSearchCommands. ## Creating the RediSearch client @@ -22,6 +22,8 @@ JedisCluster client = new JedisCluster(nodes); ## Indexing and querying +### Indexing + Defining a schema for an index and creating it: ```java @@ -37,6 +39,23 @@ IndexDefinition def = new IndexDefinition() client.ftCreate("item-index", IndexOptions.defaultOptions().setDefinition(def), sc); ``` +Alternatively, we can create the same index using FTCreateParams: + +```java +client.ftCreate("item-index", + + FTCreateParams.createParams() + .prefix("item:", "product:") + .filter("@price>100"), + + TextField.of("title").weight(5.0), + TextField.of("body"), + NumericField.of("price") +); +``` + +### Inserting + Adding documents to the index: ```java @@ -49,18 +68,37 @@ fields.put("price", 1337); client.hset("item:hw", RediSearchUtil.toStringMap(fields)); ``` +Another way to insert documents: + +```java +client.hsetObject("item:hw", fields); +``` + +### Querying + Searching the index: ```java -// creating a complex query Query q = new Query("hello world") .addFilter(new Query.NumericFilter("price", 0, 1000)) .limit(0, 5); -// actual search SearchResult sr = client.ftSearch("item-index", q); +``` + +Alternative searching using FTSearchParams: -// aggregation query +```java +SearchResult sr = client.ftSearch("item-index", + "hello world", + FTSearchParams.searchParams() + .filter("price", 0, 1000) + .limit(0, 5)); +``` + +Aggregation query: + +```java AggregationBuilder ab = new AggregationBuilder("hello") .apply("@price/1000", "k") .groupBy("@state", Reducers.avg("@k").as("avgprice")) diff --git a/docs/redisjson.md b/docs/redisjson.md index 6409e726f9..6d9db6e20c 100644 --- a/docs/redisjson.md +++ b/docs/redisjson.md @@ -81,7 +81,7 @@ If we want to be able to query this JSON, we'll need to create an index. Let's c 3. Then we actually create the index, called "student-index", by calling `ftCreate()`. ```java -Schema schema = new Schema().addTextField("$.firstName", 1.0).addTextField("$" + ".lastName", 1.0); +Schema schema = new Schema().addTextField("$.firstName", 1.0).addTextField("$.lastName", 1.0); IndexDefinition rule = new IndexDefinition(IndexDefinition.Type.JSON) .setPrefixes(new String[]{"student:"}); @@ -89,13 +89,29 @@ IndexDefinition rule = new IndexDefinition(IndexDefinition.Type.JSON) client.ftCreate("student-index", IndexOptions.defaultOptions().setDefinition(rule), schema); ``` +Alternatively creating the same index using FTCreateParams: + +```java +client.ftCreate("student-index", + FTCreateParams.createParams().on(IndexDataType.JSON).prefix("student:"), + TextField.of("$.firstName"), TextField.of("$.lastName")); +``` + With an index now defined, we can query our JSON. Let's find all students whose name begins with "maya": ```java -Query q = new Query("@\\$\\" + ".firstName:maya*"); +Query q = new Query("@\\$\\.firstName:maya*"); SearchResult mayaSearch = client.ftSearch("student-index", q); ``` +Same query can be done using FTSearchParams: + +```java +SearchResult mayaSearch = client.ftSearch("student-index", + "@\\$\\.firstName:maya*", + FTSearchParams.searchParams()); +``` + We can then iterate over our search results: ```java