Skip to content

Commit

Permalink
[DOC] RediSearch usage with FTCreateParams and FTSearchParams classes (
Browse files Browse the repository at this point in the history
…#3934)

* Update redisearch.md

* Update redisjson.md

* fix spell check
  • Loading branch information
sazzad16 authored Aug 25, 2024
1 parent 53b8ad5 commit ac2a4e0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .github/wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ EVAL
EVALSHA
Failback
Failover
FTCreateParams
FTSearchParams
GSON
GenericObjectPool
GenericObjectPoolConfig
Expand Down
46 changes: 42 additions & 4 deletions docs/redisearch.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -22,6 +22,8 @@ JedisCluster client = new JedisCluster(nodes);

## Indexing and querying

### Indexing

Defining a schema for an index and creating it:

```java
Expand All @@ -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
Expand All @@ -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"))
Expand Down
20 changes: 18 additions & 2 deletions docs/redisjson.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,37 @@ 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:"});

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
Expand Down

0 comments on commit ac2a4e0

Please sign in to comment.