|
| 1 | +using NRedisStack.Core.DataTypes; |
| 2 | +using NRedisStack.RedisStackCommands; |
| 3 | +using NRedisStack.Search.Literals.Enums; |
| 4 | +using NRedisStack.Search; |
| 5 | +using NRedisStack; |
| 6 | +using StackExchange.Redis; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Threading; |
| 9 | +using System; |
| 10 | +using static NRedisStack.Search.Schema; |
| 11 | + |
| 12 | + |
| 13 | +public class PackageVerification |
| 14 | +{ |
| 15 | + public static void Main() |
| 16 | + { |
| 17 | + ConfigurationOptions configurationOptions = new ConfigurationOptions(); |
| 18 | + configurationOptions.SyncTimeout = 20000; |
| 19 | + configurationOptions.EndPoints.Add("localhost:6379"); |
| 20 | + ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(configurationOptions); |
| 21 | + |
| 22 | + IDatabase db = redis.GetDatabase(); |
| 23 | + db.Execute("FLUSHALL"); |
| 24 | + |
| 25 | + IJsonCommands json = db.JSON(); |
| 26 | + |
| 27 | + json.Set("product:15970", "$", new |
| 28 | + { |
| 29 | + id = 15970, |
| 30 | + description = "Turtle Navy Blue Shirt", |
| 31 | + price = 34.95, |
| 32 | + }); |
| 33 | + |
| 34 | + json.Set("product:59263", "$", new |
| 35 | + { |
| 36 | + id = 59263, |
| 37 | + description = "Titan Silver Watch", |
| 38 | + price = 129.99, |
| 39 | + }); |
| 40 | + |
| 41 | + ISearchCommands ft = db.FT(); |
| 42 | + |
| 43 | + try |
| 44 | + { |
| 45 | + ft.DropIndex("idx1"); |
| 46 | + } |
| 47 | + catch |
| 48 | + { |
| 49 | + } |
| 50 | + |
| 51 | + ft.Create("idx1", new FTCreateParams().On(IndexDataType.JSON) |
| 52 | + .Prefix("product:"), |
| 53 | + new Schema().AddNumericField(new FieldName("$.id", "id")) |
| 54 | + .AddTextField(new FieldName("$.description", "description")) |
| 55 | + .AddNumericField(new FieldName("$.price", "price"))); |
| 56 | + |
| 57 | + // wait for index to be created |
| 58 | + Thread.Sleep(2000); |
| 59 | + |
| 60 | + List<string> results = ft.Search("idx1", new Query("@description:Blue")).ToJson(); |
| 61 | + |
| 62 | + Assert(1 == results.Count); |
| 63 | + |
| 64 | + var expected = "{\"id\":15970,\"description\":\"Turtle Navy Blue Shirt\",\"price\":34.95}"; |
| 65 | + Assert(expected == results[0]); |
| 66 | + |
| 67 | + Console.WriteLine("All good."); |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Poor Man's assert, since we don't want to depend on NUnit. |
| 72 | + /// </summary> |
| 73 | + private static void Assert(bool condition) |
| 74 | + { |
| 75 | + if (!condition) |
| 76 | + { |
| 77 | + throw new System.Exception(); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments