Skip to content

Commit ac2a4e0

Browse files
authored
[DOC] RediSearch usage with FTCreateParams and FTSearchParams classes (#3934)
* Update redisearch.md * Update redisjson.md * fix spell check
1 parent 53b8ad5 commit ac2a4e0

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

.github/wordlist.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ EVAL
2020
EVALSHA
2121
Failback
2222
Failover
23+
FTCreateParams
24+
FTSearchParams
2325
GSON
2426
GenericObjectPool
2527
GenericObjectPoolConfig

docs/redisearch.md

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# RediSearch Jedis Quick Start
22

3-
To use RediSearch features with Jedis, you'll need to use and implementation of RediSearchCommands.
3+
To use RediSearch features with Jedis, you'll need to use an implementation of RediSearchCommands.
44

55
## Creating the RediSearch client
66

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

2323
## Indexing and querying
2424

25+
### Indexing
26+
2527
Defining a schema for an index and creating it:
2628

2729
```java
@@ -37,6 +39,23 @@ IndexDefinition def = new IndexDefinition()
3739
client.ftCreate("item-index", IndexOptions.defaultOptions().setDefinition(def), sc);
3840
```
3941

42+
Alternatively, we can create the same index using FTCreateParams:
43+
44+
```java
45+
client.ftCreate("item-index",
46+
47+
FTCreateParams.createParams()
48+
.prefix("item:", "product:")
49+
.filter("@price>100"),
50+
51+
TextField.of("title").weight(5.0),
52+
TextField.of("body"),
53+
NumericField.of("price")
54+
);
55+
```
56+
57+
### Inserting
58+
4059
Adding documents to the index:
4160

4261
```java
@@ -49,18 +68,37 @@ fields.put("price", 1337);
4968
client.hset("item:hw", RediSearchUtil.toStringMap(fields));
5069
```
5170

71+
Another way to insert documents:
72+
73+
```java
74+
client.hsetObject("item:hw", fields);
75+
```
76+
77+
### Querying
78+
5279
Searching the index:
5380

5481
```java
55-
// creating a complex query
5682
Query q = new Query("hello world")
5783
.addFilter(new Query.NumericFilter("price", 0, 1000))
5884
.limit(0, 5);
5985

60-
// actual search
6186
SearchResult sr = client.ftSearch("item-index", q);
87+
```
88+
89+
Alternative searching using FTSearchParams:
6290

63-
// aggregation query
91+
```java
92+
SearchResult sr = client.ftSearch("item-index",
93+
"hello world",
94+
FTSearchParams.searchParams()
95+
.filter("price", 0, 1000)
96+
.limit(0, 5));
97+
```
98+
99+
Aggregation query:
100+
101+
```java
64102
AggregationBuilder ab = new AggregationBuilder("hello")
65103
.apply("@price/1000", "k")
66104
.groupBy("@state", Reducers.avg("@k").as("avgprice"))

docs/redisjson.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,37 @@ If we want to be able to query this JSON, we'll need to create an index. Let's c
8181
3. Then we actually create the index, called "student-index", by calling `ftCreate()`.
8282

8383
```java
84-
Schema schema = new Schema().addTextField("$.firstName", 1.0).addTextField("$" + ".lastName", 1.0);
84+
Schema schema = new Schema().addTextField("$.firstName", 1.0).addTextField("$.lastName", 1.0);
8585

8686
IndexDefinition rule = new IndexDefinition(IndexDefinition.Type.JSON)
8787
.setPrefixes(new String[]{"student:"});
8888

8989
client.ftCreate("student-index", IndexOptions.defaultOptions().setDefinition(rule), schema);
9090
```
9191

92+
Alternatively creating the same index using FTCreateParams:
93+
94+
```java
95+
client.ftCreate("student-index",
96+
FTCreateParams.createParams().on(IndexDataType.JSON).prefix("student:"),
97+
TextField.of("$.firstName"), TextField.of("$.lastName"));
98+
```
99+
92100
With an index now defined, we can query our JSON. Let's find all students whose name begins with "maya":
93101

94102
```java
95-
Query q = new Query("@\\$\\" + ".firstName:maya*");
103+
Query q = new Query("@\\$\\.firstName:maya*");
96104
SearchResult mayaSearch = client.ftSearch("student-index", q);
97105
```
98106

107+
Same query can be done using FTSearchParams:
108+
109+
```java
110+
SearchResult mayaSearch = client.ftSearch("student-index",
111+
"@\\$\\.firstName:maya*",
112+
FTSearchParams.searchParams());
113+
```
114+
99115
We can then iterate over our search results:
100116

101117
```java

0 commit comments

Comments
 (0)