-
Notifications
You must be signed in to change notification settings - Fork 280
DOC-4345 node.js JSON examples #1237
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| --- | ||
| categories: | ||
| - docs | ||
| - develop | ||
| - stack | ||
| - oss | ||
| - rs | ||
| - rc | ||
| - oss | ||
| - kubernetes | ||
| - clients | ||
| description: Learn how to use the Redis Query Engine with JSON | ||
| linkTitle: Index and query JSON | ||
| title: Example - Index and query JSON documents | ||
| weight: 2 | ||
| --- | ||
|
|
||
| This example shows how to create a | ||
| [search index]({{< relref "/develop/interact/search-and-query/indexing" >}}) | ||
| for [JSON]({{< relref "/develop/data-types/json" >}}) data and | ||
| run queries against the index. | ||
|
|
||
| Make sure that you have Redis Stack and `node-redis` installed. | ||
|
|
||
| Start by importing dependencies: | ||
|
|
||
| ```js | ||
| import { | ||
| createClient, | ||
| SchemaFieldTypes, | ||
| AggregateGroupByReducers, | ||
| AggregateSteps, | ||
| } from 'redis'; | ||
| ``` | ||
|
|
||
| Connect to the database: | ||
|
|
||
| ```js | ||
| const client = await createClient(); | ||
| await client.connect(); | ||
| ``` | ||
|
|
||
| Create some test data to add to the database: | ||
|
|
||
| ```js | ||
| const user1 = { | ||
| name: 'Paul John', | ||
| email: '[email protected]', | ||
| age: 42, | ||
| city: 'London' | ||
| }; | ||
|
|
||
| const user2 = { | ||
| name: 'Eden Zamir', | ||
| email: '[email protected]', | ||
| age: 29, | ||
| city: 'Tel Aviv' | ||
| }; | ||
|
|
||
| const user3 = { | ||
| name: 'Paul Zamir', | ||
| email: '[email protected]', | ||
| age: 35, | ||
| city: 'Tel Aviv' | ||
| }; | ||
| ``` | ||
|
|
||
| Create an index. In this example, only JSON documents with the key prefix `user:` are indexed. For more information, see [Query syntax]({{< relref "/develop/interact/search-and-query/query/" >}}). | ||
|
|
||
| ```js | ||
| await client.ft.create('idx:users', { | ||
| '$.name': { | ||
| type: SchemaFieldTypes.TEXT, | ||
| AS: 'name' | ||
| }, | ||
| '$.city': { | ||
| type: SchemaFieldTypes.TEXT, | ||
| AS: 'city' | ||
| }, | ||
| '$.age': { | ||
| type: SchemaFieldTypes.NUMERIC, | ||
| AS: 'age' | ||
| } | ||
| }, { | ||
| ON: 'JSON', | ||
| PREFIX: 'user:' | ||
| }); | ||
| ``` | ||
|
|
||
| Add the three sets of user data to the database as | ||
| [JSON]({{< relref "/develop/data-types/json" >}}) objects. | ||
| If you use keys with the `user:` prefix then Redis will index the | ||
| objects automatically as you add them. Note that placing | ||
| the commands in a `Promise.all()` call is an easy way to create a | ||
| [pipeline]({{< relref "/develop/clients/nodejs/transpipe" >}}), | ||
| which is more efficient than sending the commands individually. | ||
|
|
||
| ```js | ||
| const [user1Reply, user2Reply, user3Reply] = await Promise.all([ | ||
| client.json.set('user:1', '$', user1), | ||
| client.json.set('user:2', '$', user2), | ||
| client.json.set('user:3', '$', user3) | ||
| ]); | ||
| ``` | ||
|
|
||
| You can now use the index to search the JSON objects. The | ||
| [query]({{< relref "/develop/interact/search-and-query/query" >}}) | ||
| below searches for objects that have the text "Paul" in any field | ||
| and have an `age` value in the range 30 to 40: | ||
|
|
||
| ```js | ||
| let findPaulResult = await client.ft.search('idx:users', 'Paul @age:[30 40]'); | ||
|
|
||
| console.log(findPaulResult.total); // >>> 1 | ||
|
|
||
| findPaulResult.documents.forEach(doc => { | ||
| console.log(`ID: ${doc.id}, name: ${doc.value.name}, age: ${doc.value.age}`); | ||
| }); | ||
| ``` | ||
|
|
||
| Specify query options to return only the `city` field: | ||
|
|
||
| ```js | ||
| let citiesResult = await client.ft.search('idx:users', '*',{ | ||
| RETURN: 'city' | ||
| }); | ||
|
|
||
| console.log(citiesResult.total); // >>> 3 | ||
|
|
||
| citiesResult.documents.forEach(cityDoc => { | ||
| console.log(cityDoc.value); | ||
| }); | ||
| ``` | ||
|
|
||
| Use an | ||
| [aggregation query]({{< relref "/develop/interact/search-and-query/query/aggregation" >}}) | ||
| to count all users in each city. | ||
|
|
||
| ```js | ||
| let aggResult = await client.ft.aggregate('idx:users', '*', { | ||
| STEPS: [{ | ||
| type: AggregateSteps.GROUPBY, | ||
| properties: '@city', | ||
| REDUCE: [{ | ||
| type: AggregateGroupByReducers.COUNT, | ||
| AS: 'count' | ||
| }] | ||
| }] | ||
| }); | ||
|
|
||
| console.log(aggResult.total); // >>> 2 | ||
|
|
||
| aggResult.results.forEach(result => { | ||
| console.log(`${result.city} - ${result.count}`); | ||
| }); | ||
| ``` | ||
|
|
||
| Finally, close the connection to Redis. | ||
|
|
||
| ```js | ||
| await client.quit(); | ||
| ``` | ||
|
|
||
|
|
||
| See the [Redis Query Engine]({{< relref "/develop/interact/search-and-query" >}}) docs | ||
| for a full description of all query features with examples. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Andy: Please make a note to change Redis Stack to Redis Community Edition (CE) when CE8 ships.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do.