Socket
Socket
Sign inDemoInstall

@node-redis/search

Package Overview
Dependencies
6
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.0-rc.0 to 1.0.0

12

package.json
{
"name": "@node-redis/search",
"version": "1.0.0-rc.0",
"version": "1.0.0",
"license": "MIT",

@@ -12,3 +12,3 @@ "main": "./dist/index.js",

"peerDependencies": {
"@node-redis/client": "^1.0.0-rc"
"@node-redis/client": "^1.0.0"
},

@@ -18,9 +18,9 @@ "devDependencies": {

"@node-redis/test-utils": "*",
"@types/node": "^16.11.7",
"@types/node": "^16.11.10",
"nyc": "^15.1.0",
"release-it": "^14.11.7",
"source-map-support": "^0.5.20",
"release-it": "^14.11.8",
"source-map-support": "^0.5.21",
"ts-node": "^10.4.0",
"typescript": "^4.4.4"
"typescript": "^4.5.2"
}
}
# @node-redis/search
The sources and docs for this package are in the main [node-redis](https://github.com/redis/node-redis) repo.
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.
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.
## Usage
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.
### Indexing and Querying Data in Redis Hashes
#### Creating an Index
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`:
```javascript
await client.ft.create('idx:animals', {
name: {
type: SchemaFieldTypes.TEXT,
sortable: true
},
species: SchemaFieldTypes.TAG,
age: SchemaFieldTypes.NUMERIC
}, {
ON: 'HASH',
PREFIX: 'noderedis:animals'
}
);
```
See the [`FT.CREATE` documentation](https://oss.redis.com/redisearch/Commands/#ftcreate) for information about the different field types and additional options.
#### Querying the Index
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.
Let's write a query to find all the animals where the `species` field has the value `dog`:
```javascript
const results = await client.ft.search('idx:animals', '@species:{dog}');
```
`results` looks like this:
```javascript
{
total: 2,
documents: [
{
id: 'noderedis:animals:4',
value: {
name: 'Fido',
species: 'dog',
age: '7'
}
},
{
id: 'noderedis:animals:3',
value: {
name: 'Rover',
species: 'dog',
age: '9'
}
}
]
}
```
### Indexing and Querying Data with RedisJSON
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.
#### Creating an Index
As before, we create an index with the `FT.CREATE` command, this time specifying we want to index JSON documents that look like this:
```javascript
{
name: 'Alice',
age: 32,
coins: 100
}
```
Each document represents a user in some system, and users have name, age and coins properties.
One way we might choose to index these documents is as follows:
```javascript
await client.ft.create('idx:users', {
'$.name': {
type: SchemaFieldTypes.TEXT,
SORTABLE: 'UNF'
},
'$.age': {
type: SchemaFieldTypes.NUMERIC,
AS: 'age'
},
'$.coins': {
type: SchemaFieldTypes.NUMERIC,
AS: 'coins'
}
}, {
ON: 'JSON',
PREFIX: 'noderedis:users'
});
```
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.
#### Querying the Index
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...
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:
```javascript
await client.ft.search('idx:users', '@age:[0 30]');
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc