@@ -188,111 +188,63 @@ for more information.
188188
189189## Example: Indexing and querying JSON documents
190190
191- This example shows how to convert Redis search results to JSON format using ` NRedisStack ` .
191+ This example shows how to create a
192+ [ search index] ({{< relref "/develop/interact/search-and-query/indexing" >}})
193+ for [ JSON] ({{< relref "/develop/data-types/json" >}}) data and
194+ run queries against the index.
192195
193196Make sure that you have Redis Stack and ` NRedisStack ` installed.
194197
195- Import dependencies and connect to the Redis server :
198+ Start by importing dependencies :
196199
197- ``` csharp
198- using NRedisStack ;
199- using NRedisStack .RedisStackCommands ;
200- using NRedisStack .Search ;
201- using NRedisStack .Search .Aggregation ;
202- using NRedisStack .Search .Literals .Enums ;
203- using StackExchange .Redis ;
200+ {{< clients-example cs_home_json import >}}
201+ {{< /clients-example >}}
204202
205- // ...
203+ Connect to the database:
206204
207- ConnectionMultiplexer redis = ConnectionMultiplexer . Connect ( " localhost " );
208- ```
205+ {{< clients-example cs_home_json connect >}}
206+ {{< /clients-example >}}
209207
210- Get a reference to the database and for search and JSON commands.
208+ Create some test data to add to the database:
211209
212- ``` csharp
213- var db = redis .GetDatabase ();
214- var ft = db .FT ();
215- var json = db .JSON ();
216- ```
210+ {{< clients-example cs_home_json create_data >}}
211+ {{< /clients-example >}}
217212
218- Let's create some test data to add to your database .
213+ 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/" >}}) .
219214
220- ``` csharp
221- var user1 = new {
222- name = " Paul John" ,
223- 224- age = 42 ,
225- city = " London"
226- };
215+ {{< clients-example cs_home_json make_index >}}
216+ {{< /clients-example >}}
227217
228- var user2 = new {
229- name = " Eden Zamir" ,
230- 231- age = 29 ,
232- city = " Tel Aviv"
233- };
218+ Add the three sets of user data to the database as
219+ [ JSON] ({{< relref "/develop/data-types/json" >}}) objects.
220+ If you use keys with the ` user: ` prefix then Redis will index the
221+ objects automatically as you add them:
234222
235- var user3 = new {
236- name = " Paul Zamir" ,
237- 238- age = 35 ,
239- city = " Tel Aviv"
240- };
241- ```
223+ {{< clients-example cs_home_json add_data >}}
224+ {{< /clients-example >}}
242225
243- Create an index. In this example, all JSON documents with the key prefix ` user: ` are indexed. For more information, see [ Query syntax] ({{< relref "/develop/interact/search-and-query/query/" >}}).
226+ You can now use the index to search the JSON objects. The
227+ [ query] ({{< relref "/develop/interact/search-and-query/query" >}})
228+ below searches for objects that have the text "Paul" in any field
229+ and have an ` age ` value in the range 30 to 40:
244230
245- ``` csharp
246- var schema = new Schema ()
247- .AddTextField (new FieldName (" $.name" , " name" ))
248- .AddTagField (new FieldName (" $.city" , " city" ))
249- .AddNumericField (new FieldName (" $.age" , " age" ));
250-
251- ft .Create (
252- " idx:users" ,
253- new FTCreateParams ().On (IndexDataType .JSON ).Prefix (" user:" ),
254- schema );
255- ```
231+ {{< clients-example cs_home_json query1 >}}
232+ {{< /clients-example >}}
256233
257- Use [ ` JSON.SET ` ] ({{< baseurl >}}/commands/json.set/) to set each user value at the specified path.
234+ Specify query options to return only the ` city ` field:
258235
259- ``` csharp
260- json .Set (" user:1" , " $" , user1 );
261- json .Set (" user:2" , " $" , user2 );
262- json .Set (" user:3" , " $" , user3 );
263- ```
264-
265- Let's find user ` Paul ` and filter the results by age.
266-
267- ``` csharp
268- var res = ft .Search (" idx:users" , new Query (" Paul @age:[30 40]" )).Documents .Select (x => x [" json" ]);
269- Console .WriteLine (string .Join (" \n " , res ));
270- // Prints: {"name":"Paul Zamir","email":"[email protected] ","age":35,"city":"Tel Aviv"}271- ```
272-
273- Return only the ` city ` field.
236+ {{< clients-example cs_home_json query2 >}}
237+ {{< /clients-example >}}
274238
275- ``` csharp
276- var res_cities = ft .Search (" idx:users" , new Query (" Paul" ).ReturnFields (new FieldName (" $.city" , " city" ))).Documents .Select (x => x [" city" ]);
277- Console .WriteLine (string .Join (" , " , res_cities ));
278- // Prints: London, Tel Aviv
279- ```
239+ Use an
240+ [ aggregation query] ({{< relref "/develop/interact/search-and-query/query/aggregation" >}})
241+ to count all users in each city.
280242
281- Count all users in the same city.
282-
283- ``` csharp
284- var request = new AggregationRequest (" *" ).GroupBy (" @city" , Reducers .Count ().As (" count" ));
285- var result = ft .Aggregate (" idx:users" , request );
243+ {{< clients-example cs_home_json query3 >}}
244+ {{< /clients-example >}}
286245
287- for (var i = 0 ; i < result .TotalResults ; i ++ )
288- {
289- var row = result .GetRow (i );
290- Console .WriteLine ($" {row [" city" ]} - {row [" count" ]}" );
291- }
292- // Prints:
293- // London - 1
294- // Tel Aviv - 2
295- ```
246+ See the [ Redis query engine] ({{< relref "/develop/interact/search-and-query" >}}) docs
247+ for a full description of all query features with examples.
296248
297249## Learn more
298250
0 commit comments