Skip to content

Commit fe83545

Browse files
authored
Create an Index and Store Documents (#232)
1 parent 6fcf8f6 commit fe83545

File tree

8 files changed

+211
-0
lines changed

8 files changed

+211
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.2.32602.215
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Redis.OM.CreateIndexStore", "Redis.OM.CreateIndexStore\Redis.OM.CreateIndexStore.csproj", "{78B8EF87-5067-45D1-A864-37BC2931FF38}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{78B8EF87-5067-45D1-A864-37BC2931FF38}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{78B8EF87-5067-45D1-A864-37BC2931FF38}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{78B8EF87-5067-45D1-A864-37BC2931FF38}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{78B8EF87-5067-45D1-A864-37BC2931FF38}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {41CDF499-8994-409A-8C71-2278EAB55B83}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using Redis.OM.CreateIndexStore.Models;
2+
using Redis.OM.Searching;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Redis.OM.CreateIndexStore
10+
{
11+
public static class SeedDataHelpers
12+
{
13+
public static async Task SeedEmployess(IRedisCollection<Employee> collection)
14+
{
15+
await collection.InsertAsync(new Employee() { Id = "1", Age = 18, EmploymentType = EmploymentType.PartTime, FullName = "Jean Francois" });
16+
await collection.InsertAsync(new Employee() { Id = "2", Age = 21, EmploymentType = EmploymentType.PartTime, FullName = "Bowman Sophie" });
17+
await collection.InsertAsync(new Employee() { Id = "3", Age = 78, EmploymentType = EmploymentType.FullTime, FullName = "Will Quinn" });
18+
}
19+
20+
public static async Task SeedCustomers(IRedisCollection<Customer> collection)
21+
{
22+
await collection.InsertAsync(new Customer()
23+
{
24+
Id = Guid.NewGuid(),
25+
Email = "[email protected]",
26+
FullName = "Albert Einstein",
27+
Publications = new[] { "Conclusions Drawn from the Phenomena of Capillarity", "Foundations of the General Theory of Relativity", "Investigations of Brownian Motion" },
28+
Address = new Address("4891 Island Hwy", "Campbell River")
29+
});
30+
await collection.InsertAsync(new Customer()
31+
{
32+
Id = Guid.NewGuid(),
33+
Email = "[email protected]",
34+
FullName = "Isaac Newton",
35+
Publications = new[] { "Philosophiæ Naturalis Principia Mathematica", "Opticks", "De mundi systemate" },
36+
Address = new Address("2019 90th Avenue", "Delia")
37+
});
38+
await collection.InsertAsync(new Customer()
39+
{
40+
Id = Guid.NewGuid(),
41+
Email = "[email protected]",
42+
FullName = "Galileo Galilei",
43+
Publications = new[] { "Sidereus Nuncius", "The Assayer" }
44+
});
45+
await collection.InsertAsync(new Customer()
46+
{
47+
Id = Guid.NewGuid(),
48+
Email = "[email protected]",
49+
FullName = "Marie Curie",
50+
Publications = new[] { "Recherches sur les substances radioactives", "Traité de Radioactivité", "L'isotopie et les éléments isotopes" },
51+
Address = new Address("1704 rue Ontario Ouest", "Montréal")
52+
});
53+
}
54+
55+
public static async Task SeedStore(IRedisCollection<Store> collection)
56+
{
57+
await collection.InsertAsync(new Store() { Name = "CF Toronto Eaton Centre", FullAddress = "220 Yonge St, Toronto, ON M5B 2H1", Id = 599 });
58+
await collection.InsertAsync(new Store() { Name = "Yorkdale Shopping Centre", FullAddress = "3401 Dufferin St, Toronto, ON M6A 2T9", Id = 600 });
59+
}
60+
}
61+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Redis.OM.CreateIndexStore.Models
8+
{
9+
public class Address
10+
{
11+
public string? AddressLine1 { get; set; }
12+
13+
public string? AddressLine2 { get; set; }
14+
public string? City { get; set; }
15+
16+
public Address(string addressLine1, string? city)
17+
{
18+
AddressLine1 = addressLine1;
19+
City = city;
20+
}
21+
}
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Redis.OM.Modeling;
2+
3+
namespace Redis.OM.CreateIndexStore.Models
4+
{
5+
[Document(StorageType = StorageType.Json)]
6+
public class Customer
7+
{
8+
[RedisIdField][Indexed] public Guid Id { get; set; }
9+
10+
[Indexed] public string FullName { get; set; } = null!;
11+
[Indexed] public string Email { get; set; } = null!;
12+
13+
[Indexed] public string[] Publications { get; set; } = null!;
14+
15+
[Indexed] public Address? Address { get; set; }
16+
}
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Redis.OM.Modeling;
2+
3+
namespace Redis.OM.CreateIndexStore.Models
4+
{
5+
[Document(StorageType = StorageType.Json, Prefixes = new[] { "CANADA.STORE.TORONTO" }, IndexName = "Store1-idx")]
6+
public class Employee
7+
{
8+
[RedisIdField][Indexed] public string Id { get; set; } = null!;
9+
10+
[Indexed] public string FullName { get; set; } = null!;
11+
12+
[Indexed] public int Age { get; set; }
13+
14+
[Indexed] public EmploymentType EmploymentType { get; set; }
15+
}
16+
17+
public enum EmploymentType
18+
{
19+
FullTime,
20+
PartTime
21+
}
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Redis.OM.Modeling;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Redis.OM.CreateIndexStore.Models
9+
{
10+
[Document(StorageType = StorageType.Hash)]
11+
public record Store
12+
{
13+
[RedisIdField][Indexed] public int Id { get; set; }
14+
15+
[Indexed] public string FullAddress { get; set; } = null!;
16+
[Indexed] public string Name { get; set; } = null!;
17+
}
18+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Redis.OM;
2+
using Redis.OM.CreateIndexStore;
3+
using Redis.OM.CreateIndexStore.Models;
4+
5+
var provider = new RedisConnectionProvider("redis://localhost:6379");
6+
7+
provider.Connection.CreateIndex(typeof(Customer));
8+
provider.Connection.CreateIndex(typeof(Employee));
9+
provider.Connection.CreateIndex(typeof(Store));
10+
11+
var customers = provider.RedisCollection<Customer>();
12+
var employee = provider.RedisCollection<Employee>();
13+
var stores = provider.RedisCollection<Store>();
14+
15+
await SeedDataHelpers.SeedEmployess(employee);
16+
await SeedDataHelpers.SeedCustomers(customers);
17+
await SeedDataHelpers.SeedStore(stores);
18+
19+
//retrieve all the contents
20+
var allStores = await stores.ToListAsync();
21+
var allEmployees = await employee.ToListAsync();
22+
var allCustomers = await customers.ToListAsync();
23+
24+
//Filter by diffenrent properties
25+
var store = await stores.Where(x => x.Id == 600).FirstOrDefaultAsync();
26+
var newton = await customers.Where(x => x.FullName == "Albert Einstein").FirstOrDefaultAsync();
27+
var fulltimeEmployees = await employee.Where(x => x.EmploymentType == EmploymentType.FullTime).ToListAsync();
28+
29+
//Drop the index
30+
await provider.Connection.DropIndexAsync(typeof(Employee));
31+
await provider.Connection.DropIndexAsync(typeof(Customer));
32+
await provider.Connection.DropIndexAsync(typeof(Store));
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Redis.OM" Version="0.2.3" />
12+
</ItemGroup>
13+
14+
</Project>

0 commit comments

Comments
 (0)