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

[DOC] RediSearch usage with FTCreateParams and FTSearchParams classes #3934

Merged
merged 3 commits into from
Aug 25, 2024
Merged
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
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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the field-level decoding PR we will need to change it to:

Alternatively, you can use FTSearchParams to access more advanced capabilities, such as field-level decoding control. Here is an example:

Copy link
Collaborator Author

@sazzad16 sazzad16 Aug 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uglide IMO, we need a completely new example. I'm having difficulty making a decent example from title (text), body (text) and price (numeric) fields.


// 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
Loading