|
| 1 | +--- |
| 2 | +categories: |
| 3 | +- docs |
| 4 | +- develop |
| 5 | +- stack |
| 6 | +- oss |
| 7 | +- rs |
| 8 | +- rc |
| 9 | +- oss |
| 10 | +- kubernetes |
| 11 | +- clients |
| 12 | +description: Learn how to use the Redis Query Engine with JSON |
| 13 | +linkTitle: Index and query JSON |
| 14 | +title: Example - Index and query JSON documents |
| 15 | +weight: 2 |
| 16 | +--- |
| 17 | + |
| 18 | +This example shows how to create a |
| 19 | +[search index]({{< relref "/develop/interact/search-and-query/indexing" >}}) |
| 20 | +for [JSON]({{< relref "/develop/data-types/json" >}}) data and |
| 21 | +run queries against the index. |
| 22 | + |
| 23 | +Make sure that you have Redis Stack and `node-redis` installed. |
| 24 | + |
| 25 | +Start by importing dependencies: |
| 26 | + |
| 27 | +```js |
| 28 | +import { |
| 29 | + createClient, |
| 30 | + SchemaFieldTypes, |
| 31 | + AggregateGroupByReducers, |
| 32 | + AggregateSteps, |
| 33 | +} from 'redis'; |
| 34 | +``` |
| 35 | + |
| 36 | +Connect to the database: |
| 37 | + |
| 38 | +```js |
| 39 | +const client = await createClient(); |
| 40 | +await client.connect(); |
| 41 | +``` |
| 42 | + |
| 43 | +Create some test data to add to the database: |
| 44 | + |
| 45 | +```js |
| 46 | +const user1 = { |
| 47 | + name: 'Paul John', |
| 48 | + email: 'paul.john@example.com', |
| 49 | + age: 42, |
| 50 | + city: 'London' |
| 51 | +}; |
| 52 | + |
| 53 | +const user2 = { |
| 54 | + name: 'Eden Zamir', |
| 55 | + email: 'eden.zamir@example.com', |
| 56 | + age: 29, |
| 57 | + city: 'Tel Aviv' |
| 58 | +}; |
| 59 | + |
| 60 | +const user3 = { |
| 61 | + name: 'Paul Zamir', |
| 62 | + email: 'paul.zamir@example.com', |
| 63 | + age: 35, |
| 64 | + city: 'Tel Aviv' |
| 65 | +}; |
| 66 | +``` |
| 67 | + |
| 68 | +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/" >}}). |
| 69 | + |
| 70 | +```js |
| 71 | +await client.ft.create('idx:users', { |
| 72 | + '$.name': { |
| 73 | + type: SchemaFieldTypes.TEXT, |
| 74 | + AS: 'name' |
| 75 | + }, |
| 76 | + '$.city': { |
| 77 | + type: SchemaFieldTypes.TEXT, |
| 78 | + AS: 'city' |
| 79 | + }, |
| 80 | + '$.age': { |
| 81 | + type: SchemaFieldTypes.NUMERIC, |
| 82 | + AS: 'age' |
| 83 | + } |
| 84 | +}, { |
| 85 | + ON: 'JSON', |
| 86 | + PREFIX: 'user:' |
| 87 | +}); |
| 88 | +``` |
| 89 | + |
| 90 | +Add the three sets of user data to the database as |
| 91 | +[JSON]({{< relref "/develop/data-types/json" >}}) objects. |
| 92 | +If you use keys with the `user:` prefix then Redis will index the |
| 93 | +objects automatically as you add them. Note that placing |
| 94 | +the commands in a `Promise.all()` call is an easy way to create a |
| 95 | +[pipeline]({{< relref "/develop/clients/nodejs/transpipe" >}}), |
| 96 | +which is more efficient than sending the commands individually. |
| 97 | + |
| 98 | +```js |
| 99 | +const [user1Reply, user2Reply, user3Reply] = await Promise.all([ |
| 100 | + client.json.set('user:1', '$', user1), |
| 101 | + client.json.set('user:2', '$', user2), |
| 102 | + client.json.set('user:3', '$', user3) |
| 103 | +]); |
| 104 | +``` |
| 105 | + |
| 106 | +You can now use the index to search the JSON objects. The |
| 107 | +[query]({{< relref "/develop/interact/search-and-query/query" >}}) |
| 108 | +below searches for objects that have the text "Paul" in any field |
| 109 | +and have an `age` value in the range 30 to 40: |
| 110 | + |
| 111 | +```js |
| 112 | +let findPaulResult = await client.ft.search('idx:users', 'Paul @age:[30 40]'); |
| 113 | + |
| 114 | +console.log(findPaulResult.total); // >>> 1 |
| 115 | + |
| 116 | +findPaulResult.documents.forEach(doc => { |
| 117 | + console.log(`ID: ${doc.id}, name: ${doc.value.name}, age: ${doc.value.age}`); |
| 118 | +}); |
| 119 | +``` |
| 120 | + |
| 121 | +Specify query options to return only the `city` field: |
| 122 | + |
| 123 | +```js |
| 124 | +let citiesResult = await client.ft.search('idx:users', '*',{ |
| 125 | + RETURN: 'city' |
| 126 | +}); |
| 127 | + |
| 128 | +console.log(citiesResult.total); // >>> 3 |
| 129 | + |
| 130 | +citiesResult.documents.forEach(cityDoc => { |
| 131 | + console.log(cityDoc.value); |
| 132 | +}); |
| 133 | +``` |
| 134 | + |
| 135 | +Use an |
| 136 | +[aggregation query]({{< relref "/develop/interact/search-and-query/query/aggregation" >}}) |
| 137 | +to count all users in each city. |
| 138 | + |
| 139 | +```js |
| 140 | +let aggResult = await client.ft.aggregate('idx:users', '*', { |
| 141 | + STEPS: [{ |
| 142 | + type: AggregateSteps.GROUPBY, |
| 143 | + properties: '@city', |
| 144 | + REDUCE: [{ |
| 145 | + type: AggregateGroupByReducers.COUNT, |
| 146 | + AS: 'count' |
| 147 | + }] |
| 148 | + }] |
| 149 | +}); |
| 150 | + |
| 151 | +console.log(aggResult.total); // >>> 2 |
| 152 | + |
| 153 | +aggResult.results.forEach(result => { |
| 154 | + console.log(`${result.city} - ${result.count}`); |
| 155 | +}); |
| 156 | +``` |
| 157 | + |
| 158 | +Finally, close the connection to Redis. |
| 159 | + |
| 160 | +```js |
| 161 | +await client.quit(); |
| 162 | +``` |
| 163 | + |
| 164 | + |
| 165 | +See the [Redis Query Engine]({{< relref "/develop/interact/search-and-query" >}}) docs |
| 166 | +for a full description of all query features with examples. |
0 commit comments