|
| 1 | +# HSET and Search |
| 2 | +## An example of mixing Redis open source command (HSET) with Redis Stack Redis commands (FT.CREATE & FT.SEARCH) |
| 3 | + |
| 4 | +Connect to the Redis server: |
| 5 | +```csharp |
| 6 | +var redis = ConnectionMultiplexer.Connect("localhost"); |
| 7 | +``` |
| 8 | +Get a reference to the database and for search commands: |
| 9 | +```csharp |
| 10 | +var db = redis.GetDatabase(); |
| 11 | +var ft = db.FT(); |
| 12 | +``` |
| 13 | +Use HSET to add a field-value pair to a hash: |
| 14 | +```csharp |
| 15 | +db.HashSet("profesor:5555", new HashEntry[] { new("first", "Albert"), new("last", "Blue"), new("age", "55") }); |
| 16 | +db.HashSet("student:1111", new HashEntry[] { new("first", "Joe"), new("last", "Dod"), new("age", "18") }); |
| 17 | +db.HashSet("pupil:2222", new HashEntry[] { new("first", "Jen"), new("last", "Rod"), new("age", "14") }); |
| 18 | +db.HashSet("student:3333", new HashEntry[] { new("first", "El"), new("last", "Mark"), new("age", "17") }); |
| 19 | +db.HashSet("pupil:4444", new HashEntry[] { new("first", "Pat"), new("last", "Shu"), new("age", "21") }); |
| 20 | +db.HashSet("student:5555", new HashEntry[] { new("first", "Joen"), new("last", "Ko"), new("age", "20") }); |
| 21 | +db.HashSet("teacher:6666", new HashEntry[] { new("first", "Pat"), new("last", "Rod"), new("age", "20") }); |
| 22 | +``` |
| 23 | + |
| 24 | +Create the schema indexing the text fields ```first``` and ```last```, and ```age``` as a numeric field: |
| 25 | +```csharp |
| 26 | +var schema = new Schema().AddTextField("first").AddTextField("last").AddNumericField("age"); |
| 27 | +``` |
| 28 | +Filter the index to only include hashes with an age greater than 16, and prefix of 'student:' or 'pupil:' |
| 29 | +```csharp |
| 30 | +var parameters = FTCreateParams.CreateParams().Filter("@age>16").Prefix("student:", "pupil:"); |
| 31 | +``` |
| 32 | +Create the index: |
| 33 | +```csharp |
| 34 | +ft.Create("example_index", parameters, schema); |
| 35 | +``` |
| 36 | +## Search Examples: |
| 37 | + |
| 38 | +Search all hashes in the index: |
| 39 | +```csharp |
| 40 | +var noFilters = ft.Search("example_index", new Query()); |
| 41 | +``` |
| 42 | +_noFilters_ now contains: _student:1111_, _student:5555_, _pupil:4444_, _student:3333_.<br /><br /> |
| 43 | + |
| 44 | +Search for hashes with a first name starting with Jo |
| 45 | +```csharp |
| 46 | +var startWithJo = ft.Search("example_index", new Query("@first:Jo*")); |
| 47 | +``` |
| 48 | +_startWithJo_ now contains: _student:1111_ (Joe), _student:5555_ (Joen).<br /><br /> |
| 49 | + |
| 50 | +Search for hashes with first name of Pat |
| 51 | +```csharp |
| 52 | +var namedPat = ft.Search("example_index", new Query("@first:Pat")); |
| 53 | +``` |
| 54 | +_namedPat_ now contains _pupil:4444_ (Pat). _teacher:6666_ (Pat) is not included because it does not have a prefix of 'student:' or 'pupil:'<br /><br /> |
| 55 | + |
| 56 | +Search for hashes with last name of Rod |
| 57 | +```csharp |
| 58 | +var lastNameRod = ft.Search("example_index", new Query("@last:Rod")); |
| 59 | +``` |
| 60 | +_lastNameRod_ is empty because there are no hashes with a last name of Rod that match the index definition. |
0 commit comments