Skip to content

Commit

Permalink
Add Basic Matching Queries Example (#228)
Browse files Browse the repository at this point in the history
* Add basic matching queries example

* rename dir

* append new line
  • Loading branch information
berviantoleo authored Oct 7, 2022
1 parent 6f1476e commit 2e59a04
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 0 deletions.
87 changes: 87 additions & 0 deletions examples/Redis.OM.BasicMatchingQueries/Helpers/RedisHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using Redis.OM.BasicMatchingQueries.Models;
using Redis.OM.Searching;

namespace Redis.OM.BasicMatchingQueries.Helpers;

public class RedisHelper
{
private readonly IRedisCollection<Customer> _customerCollection;

public RedisHelper(RedisConnectionProvider provider)
{
_customerCollection = provider.RedisCollection<Customer>();
}

public void InitializeCustomers()
{
var count = _customerCollection.Count();
if (count > 0)
{
// not re-add when already initialize
return;
}

Console.WriteLine("Initialize Customer Data...");

_customerCollection.Insert(new Customer()
{
FirstName = "Customer",
LastName = "2",
Age = 20,
IsActive = true,
Email = "test@test.com",
Gender = Gender.Male,
Address = new Address()
{
City = "London",
HouseNumber = 99,
}
});

_customerCollection.Insert(new Customer()
{
FirstName = "Customer",
LastName = "3",
Age = 25,
IsActive = false,
Email = "test-3@test.com",
Gender = Gender.Female,
Address = new Address()
{
City = "London",
HouseNumber = 100,
}
});

_customerCollection.Insert(new Customer()
{
FirstName = "Testable",
LastName = "2",
Age = 99,
IsActive = true,
Email = "test-55@test.com",
Gender = Gender.Other,
Address = new Address()
{
City = "Washington",
HouseNumber = 99,
}
});

_customerCollection.Insert(new Customer()
{
FirstName = "Sharon",
LastName = "Lim",
Age = 25,
IsActive = true,
Email = "test-111@test.com",
Gender = Gender.Male,
Address = new Address()
{
City = "London",
HouseNumber = 1000,
}
});
}

}
15 changes: 15 additions & 0 deletions examples/Redis.OM.BasicMatchingQueries/Models/Address.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Redis.OM.BasicMatchingQueries.Models;

using Redis.OM.Modeling;

[Document(IndexName = "address-idx", StorageType = StorageType.Json)]
public partial class Address
{
public string StreetName { get; set; }
public string ZipCode { get; set; }
[Indexed] public string City { get; set; }
[Indexed] public string State { get; set; }
[Indexed(CascadeDepth = 1)] public Address ForwardingAddress { get; set; }
[Indexed] public GeoLoc Location { get; set; }
[Indexed] public int HouseNumber { get; set; }
}
16 changes: 16 additions & 0 deletions examples/Redis.OM.BasicMatchingQueries/Models/Customer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Redis.OM.BasicMatchingQueries.Models;

using Redis.OM.Modeling;

[Document(StorageType = StorageType.Json)]
public class Customer
{
[Indexed] public string FirstName { get; set; }
[Indexed] public string LastName { get; set; }
[Indexed] public string Email { get; set; }
[Indexed(Sortable = true)] public int Age { get; set; }
[Indexed] public bool IsActive { get; set; }
[Indexed] public Gender Gender { get; set; }
[Indexed(CascadeDepth = 2)]
public Address Address {get; set;}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Redis.OM.BasicMatchingQueries.Models;

public enum Gender
{
Male,
Female,
Other
}
55 changes: 55 additions & 0 deletions examples/Redis.OM.BasicMatchingQueries/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Redis.OM;
using Redis.OM.BasicMatchingQueries.Helpers;
using Redis.OM.BasicMatchingQueries.Models;

static void ShowCustomers(string type, List<Customer> customers)
{
Console.WriteLine($"Customer {type}: {string.Join(", ", customers.Select(x => $"{x.FirstName} {x.LastName}"))}, total: {customers.Count}.");
}

var provider = new RedisConnectionProvider("redis://localhost:6379");
provider.Connection.CreateIndex(typeof(Customer));

var redisHelper = new RedisHelper(provider);
redisHelper.InitializeCustomers();

var customerCollection = provider.RedisCollection<Customer>();

// match by string
// Find all customers with FirstName is Customer
var customerWithFirstNameCustomer = customerCollection.Where(x => x.FirstName == "Customer").ToList();
ShowCustomers("FirstName == Customer", customerWithFirstNameCustomer);

// match by numeric
// Find all customers with Age is 20
var customerWithAge20 = customerCollection.Where(x => x.Age == 20).ToList();
ShowCustomers("Age == 20", customerWithFirstNameCustomer);

// Find all customers with Age is more than 20
var customerWithAgeMoreThan20 = customerCollection.Where(x => x.Age > 20).ToList();
ShowCustomers("Age > 20", customerWithAgeMoreThan20);

// match by boolean
// Find all customers with IsActive is true
var activeCustomer = customerCollection.Where(x => x.IsActive).ToList();
ShowCustomers("IsActive == true", customerWithFirstNameCustomer);

// match by enums
// Find all customers with Gender is Male
var maleCustomer = customerCollection.Where(x => x.Gender == Gender.Male).ToList();
ShowCustomers("Gender == Gender.Male", customerWithFirstNameCustomer);

// multiple matches

// Find all customers with FirstName is Customer and Age is 20
var customerWithFirstNameCustomerAndAge20 = customerCollection.Where(x => x.FirstName == "Customer" && x.Age == 20).ToList();
ShowCustomers("FirstName == Customer && Age == 20", customerWithFirstNameCustomerAndAge20);

// match by string within embedded documents
// Find all customers with City is Washington
var customerInLondon = customerCollection.Where(x => x.Address.City == "Washington").ToList();
ShowCustomers("Address.City == Washington", customerInLondon);

// Find all customers with City is London and HouseNumber is 100
var customerInLondonWithHouseNumber100 = customerCollection.Where(x => x.Address.City == "London" && x.Address.HouseNumber == 100).ToList();
ShowCustomers("Address.City == London && Address.HouseNumber == 100", customerInLondonWithHouseNumber100);
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Redis.OM" Version="0.2.3" />
</ItemGroup>

</Project>

0 comments on commit 2e59a04

Please sign in to comment.