Skip to content

Commit ae8afc3

Browse files
author
Simon Prickett
committed
Search package README initial version.
1 parent 7dc771f commit ae8afc3

File tree

1 file changed

+119
-1
lines changed

1 file changed

+119
-1
lines changed

packages/search/README.md

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,120 @@
11
# @node-redis/search
2-
The sources and docs for this package are in the main [node-redis](https://github.com/redis/node-redis) repo.
2+
3+
This package provides support for the [RediSearch](https://redisearch.io) module, which adds indexing and querying support for data stored in Redis Hashes or as JSON documents with the RedisJSON module. It extends the [Node Redis client](https://github.com/redis/node-redis) to include functions for each of the RediSearch commands.
4+
5+
To use these extra commands, your Redis server must have the RediSearch module installed. To index and query JSON documents, you'll also need to add the RedisJSON module.
6+
7+
## Usage
8+
9+
For complete examples, see [`search-hashes.js`](https://github.com/redis/node-redis/blob/master/examples/search-hashes.js) and [`search-json.js`](https://github.com/redis/node-redis/blob/master/examples/search-json.js) in the Node Redis examples folder.
10+
11+
### Indexing and Querying Data in Redis Hashes
12+
13+
#### Creating an Index
14+
15+
Before we can perform any searches, we need to tell RediSearch how to index our data, and which Redis keys to find that data in. The [FT.CREATE](https://oss.redis.com/redisearch/Commands/#ftcreate) command creates a RediSearch index. Here's how to use it to create an index we'll call `idx:animals` where we want to index hashes containing `name`, `species` and `age` fields, and whose key names in Redis begin with the prefix `noderedis:animals`:
16+
17+
```javascript
18+
await client.ft.create('idx:animals', {
19+
name: {
20+
type: SchemaFieldTypes.TEXT,
21+
sortable: true
22+
},
23+
species: SchemaFieldTypes.TAG,
24+
age: SchemaFieldTypes.NUMERIC
25+
}, {
26+
ON: 'HASH',
27+
PREFIX: 'noderedis:animals'
28+
}
29+
);
30+
```
31+
32+
See the [`FT.CREATE` documentation](https://oss.redis.com/redisearch/Commands/#ftcreate) for information about the different field types and additional options.
33+
34+
#### Querying the Index
35+
36+
Once we've created an index, and added some data to Redis hashes whose keys begin with the prefix `noderedis:animals`, we can start writing some search queries. RediSearch supports a rich query syntax for full-text search, faceted search, aggregation and more. Check out the [`FT.SEARCH` documentation](https://oss.redis.com/redisearch/Commands/#ftsearch) and the [query syntax reference](https://oss.redis.com/redisearch/Query_Syntax/) for more information.
37+
38+
Let's write a query to find all the animals where the `species` field has the value `dog`:
39+
40+
```javascript
41+
const results = await client.ft.search('idx:animals', '@species:{dog}');
42+
```
43+
44+
`results` looks like this:
45+
46+
```javascript
47+
{
48+
total: 2,
49+
documents: [
50+
{
51+
id: 'noderedis:animals:4',
52+
value: {
53+
name: 'Fido',
54+
species: 'dog',
55+
age: '7'
56+
}
57+
},
58+
{
59+
id: 'noderedis:animals:3',
60+
value: {
61+
name: 'Rover',
62+
species: 'dog',
63+
age: '9'
64+
}
65+
}
66+
]
67+
}
68+
```
69+
70+
### Indexing and Querying Data with RedisJSON
71+
72+
RediSearch can also index and query JSON documents stored in Redis using the RedisJSON module. The approach is similar to that for indexing and searching data in hashes, but we can now use JSON Path like syntax and the data no longer has to be flat name/value pairs - it can contain nested objects and arrays.
73+
74+
#### Creating an Index
75+
76+
As before, we create an index with the `FT.CREATE` command, this time specifying we want to index JSON documents that look like this:
77+
78+
```javascript
79+
{
80+
name: 'Alice',
81+
age: 32,
82+
coins: 100
83+
}
84+
```
85+
86+
Each document represents a user in some system, and users have name, age and coins properties.
87+
88+
One way we might choose to index these documents is as follows:
89+
90+
```javascript
91+
await client.ft.create('idx:users', {
92+
'$.name': {
93+
type: SchemaFieldTypes.TEXT,
94+
SORTABLE: 'UNF'
95+
},
96+
'$.age': {
97+
type: SchemaFieldTypes.NUMERIC,
98+
AS: 'age'
99+
},
100+
'$.coins': {
101+
type: SchemaFieldTypes.NUMERIC,
102+
AS: 'coins'
103+
}
104+
}, {
105+
ON: 'JSON',
106+
PREFIX: 'noderedis:users'
107+
});
108+
```
109+
110+
Note that we're using JSON Path to specify where the fields to index are in our JSON documents, and the `AS` clause to define a name/alias for each field. We'll use these when writing queries.
111+
112+
#### Querying the Index
113+
114+
Now we have an index and some data stored as JSON documents in Redis (see the [JSON package documentation](https://github.com/redis/node-redis/tree/master/packages/json) for examples of how to store JSON), we can write some queries...
115+
116+
We'll use the [RediSearch query language](https://oss.redis.com/redisearch/Query_Syntax/) and [`FT.SEARCH`](https://oss.redis.com/redisearch/Commands/#ftsearch) command. Here's a query to find users under the age of 30:
117+
118+
```javascript
119+
await client.ft.search('idx:users', '@age:[0 30]');
120+
```

0 commit comments

Comments
 (0)