Skip to content

Commit eb4ba29

Browse files
DOC-4345 updated .NET and Python with testable examples
1 parent e4616dc commit eb4ba29

File tree

2 files changed

+36
-156
lines changed

2 files changed

+36
-156
lines changed

content/develop/connect/clients/dotnet.md

Lines changed: 19 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -192,107 +192,45 @@ This example shows how to convert Redis search results to JSON format using `NRe
192192

193193
Make sure that you have Redis Stack and `NRedisStack` installed.
194194

195-
Import dependencies and connect to the Redis server:
195+
Import dependencies:
196196

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;
197+
{{< clients-example cs_home_json import >}}
198+
{{< /clients-example >}}
204199

205-
// ...
200+
Connect to the server:
206201

207-
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
208-
```
202+
{{< clients-example cs_home_json connect >}}
203+
{{< /clients-example >}}
209204

210-
Get a reference to the database and for search and JSON commands.
211-
212-
```csharp
213-
var db = redis.GetDatabase();
214-
var ft = db.FT();
215-
var json = db.JSON();
216-
```
217-
218-
Let's create some test data to add to your database.
219-
220-
```csharp
221-
var user1 = new {
222-
name = "Paul John",
223-
email = "[email protected]",
224-
age = 42,
225-
city = "London"
226-
};
227-
228-
var user2 = new {
229-
name = "Eden Zamir",
230-
email = "[email protected]",
231-
age = 29,
232-
city = "Tel Aviv"
233-
};
205+
Create some test data to add to the database:
234206

235-
var user3 = new {
236-
name = "Paul Zamir",
237-
email = "[email protected]",
238-
age = 35,
239-
city = "Tel Aviv"
240-
};
241-
```
207+
{{< clients-example cs_home_json create_data >}}
208+
{{< /clients-example >}}
242209

243210
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/" >}}).
244211

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-
```
212+
{{< clients-example cs_home_json make_index >}}
213+
{{< /clients-example >}}
256214

257215
Use [`JSON.SET`]({{< baseurl >}}/commands/json.set/) to set each user value at the specified path.
258216

259-
```csharp
260-
json.Set("user:1", "$", user1);
261-
json.Set("user:2", "$", user2);
262-
json.Set("user:3", "$", user3);
263-
```
217+
{{< clients-example cs_home_json add_data >}}
218+
{{< /clients-example >}}
264219

265220
Let's find user `Paul` and filter the results by age.
266221

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-
```
222+
{{< clients-example cs_home_json query1 >}}
223+
{{< /clients-example >}}
272224

273225
Return only the `city` field.
274226

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-
```
227+
{{< clients-example cs_home_json query2 >}}
228+
{{< /clients-example >}}
280229

281230
Count all users in the same city.
282231

283-
```csharp
284-
var request = new AggregationRequest("*").GroupBy("@city", Reducers.Count().As("count"));
285-
var result = ft.Aggregate("idx:users", request);
286-
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-
```
232+
{{< clients-example cs_home_json query3 >}}
233+
{{< /clients-example >}}
296234

297235
## Learn more
298236

content/develop/connect/clients/python/redis-py.md

Lines changed: 17 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -270,101 +270,43 @@ pool.close()
270270

271271
Make sure that you have Redis Stack and `redis-py` installed. Import dependencies:
272272

273-
```python
274-
import redis
275-
from redis.commands.json.path import Path
276-
import redis.commands.search.aggregation as aggregations
277-
import redis.commands.search.reducers as reducers
278-
from redis.commands.search.field import TextField, NumericField, TagField
279-
from redis.commands.search.indexDefinition import IndexDefinition, IndexType
280-
from redis.commands.search.query import NumericFilter, Query
281-
```
273+
{{< clients-example py_home_json import >}}
274+
{{< /clients-example >}}
282275

283276
Connect to your Redis database.
284277

285-
```python
286-
r = redis.Redis(host='localhost', port=6379)
287-
```
278+
{{< clients-example py_home_json connect >}}
279+
{{< /clients-example >}}
288280

289281
Let's create some test data to add to your database.
290282

291-
```python
292-
user1 = {
293-
"name": "Paul John",
294-
"email": "[email protected]",
295-
"age": 42,
296-
"city": "London"
297-
}
298-
user2 = {
299-
"name": "Eden Zamir",
300-
"email": "[email protected]",
301-
"age": 29,
302-
"city": "Tel Aviv"
303-
}
304-
user3 = {
305-
"name": "Paul Zamir",
306-
"email": "[email protected]",
307-
"age": 35,
308-
"city": "Tel Aviv"
309-
}
310-
```
311-
312-
Define indexed fields and their data types using `schema`. Use JSON path expressions to map specific JSON elements to the schema fields.
313-
314-
```python
315-
schema = (
316-
TextField("$.name", as_name="name"),
317-
TagField("$.city", as_name="city"),
318-
NumericField("$.age", as_name="age")
319-
)
320-
```
283+
{{< clients-example py_home_json create_data >}}
284+
{{< /clients-example >}}
321285

322-
Create an index. In this example, all JSON documents with the key prefix `user:` will be indexed. For more information, see [Query syntax]({{< relref "/develop/interact/search-and-query/query/" >}}).
286+
Define indexed fields and their data types using `schema`. Use JSON path expressions to map specific JSON elements to the schema fields. Then, use the schema to create an index. In this example, all JSON documents with the key prefix `user:` will be indexed. For more information, see [Query syntax]({{< relref "/develop/interact/search-and-query/query/" >}}).
323287

324-
```python
325-
rs = r.ft("idx:users")
326-
rs.create_index(
327-
schema,
328-
definition=IndexDefinition(
329-
prefix=["user:"], index_type=IndexType.JSON
330-
)
331-
)
332-
# b'OK'
333-
```
288+
{{< clients-example py_home_json make_index >}}
289+
{{< /clients-example >}}
334290

335291
Use [`JSON.SET`]({{< baseurl >}}/commands/json.set/) to set each user value at the specified path.
336292

337-
```python
338-
r.json().set("user:1", Path.root_path(), user1)
339-
r.json().set("user:2", Path.root_path(), user2)
340-
r.json().set("user:3", Path.root_path(), user3)
341-
```
293+
{{< clients-example py_home_json add_data >}}
294+
{{< /clients-example >}}
342295

343296
Let's find user `Paul` and filter the results by age.
344297

345-
```python
346-
res = rs.search(
347-
Query("Paul @age:[30 40]")
348-
)
349-
# Result{1 total, docs: [Document {'id': 'user:3', 'payload': None, 'json': '{"name":"Paul Zamir","email":"[email protected]","age":35,"city":"Tel Aviv"}'}]}
350-
```
298+
{{< clients-example py_home_json query1 >}}
299+
{{< /clients-example >}}
351300

352301
Query using JSON Path expressions.
353302

354-
```python
355-
rs.search(
356-
Query("Paul").return_field("$.city", as_field="city")
357-
).docs
358-
# [Document {'id': 'user:1', 'payload': None, 'city': 'London'}, Document {'id': 'user:3', 'payload': None, 'city': 'Tel Aviv'}]
359-
```
303+
{{< clients-example py_home_json query2 >}}
304+
{{< /clients-example >}}
360305

361306
Aggregate your results using [`FT.AGGREGATE`]({{< baseurl >}}/commands/ft.aggregate/).
362307

363-
```python
364-
req = aggregations.AggregateRequest("*").group_by('@city', reducers.count().alias('count'))
365-
print(rs.aggregate(req).rows)
366-
# [[b'city', b'Tel Aviv', b'count', b'2'], [b'city', b'London', b'count', b'1']]
367-
```
308+
{{< clients-example py_home_json query3 >}}
309+
{{< /clients-example >}}
368310

369311
## Learn more
370312

0 commit comments

Comments
 (0)