Skip to content

Commit 0ee1d09

Browse files
authored
Add low level search example (#237)
add low level search example
1 parent 8de13aa commit 0ee1d09

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using Redis.OM.BasicMatchingQueries.Models;
2+
using Redis.OM.Searching;
3+
4+
namespace Redis.OM.LowLevelSearchIndex.Helpers;
5+
6+
public class RedisHelper
7+
{
8+
private readonly IRedisCollection<Customer> _customerCollection;
9+
10+
public RedisHelper(RedisConnectionProvider provider)
11+
{
12+
_customerCollection = provider.RedisCollection<Customer>();
13+
}
14+
15+
public void InitializeCustomers()
16+
{
17+
var count = _customerCollection.Count();
18+
if (count > 0)
19+
{
20+
// not re-add when already initialize
21+
return;
22+
}
23+
24+
Console.WriteLine("Initialize Customer Data...");
25+
26+
_customerCollection.Insert(new Customer()
27+
{
28+
FirstName = "Customer",
29+
LastName = "2",
30+
Age = 20,
31+
IsActive = true,
32+
Email = "[email protected]",
33+
});
34+
35+
_customerCollection.Insert(new Customer()
36+
{
37+
FirstName = "Customer",
38+
LastName = "3",
39+
Age = 25,
40+
IsActive = false,
41+
Email = "[email protected]",
42+
});
43+
44+
_customerCollection.Insert(new Customer()
45+
{
46+
FirstName = "Testable",
47+
LastName = "Customer 2",
48+
Age = 99,
49+
IsActive = true,
50+
Email = "[email protected]",
51+
});
52+
53+
_customerCollection.Insert(new Customer()
54+
{
55+
FirstName = "Sharon",
56+
LastName = "Lim",
57+
Age = 25,
58+
IsActive = true,
59+
Email = "[email protected]",
60+
});
61+
}
62+
63+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Redis.OM.BasicMatchingQueries.Models;
2+
3+
using Redis.OM.Modeling;
4+
5+
[Document(StorageType = StorageType.Json, IndexName = "customer-idx")]
6+
public class Customer
7+
{
8+
[Searchable] public string FirstName { get; set; }
9+
[Searchable] public string LastName { get; set; }
10+
[Searchable] public string Email { get; set; }
11+
[Indexed(Sortable = true)] public int Age { get; set; }
12+
[Indexed] public bool IsActive { get; set; }
13+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Redis.OM;
2+
using Redis.OM.BasicMatchingQueries.Models;
3+
using Redis.OM.LowLevelSearchIndex.Helpers;
4+
using Redis.OM.Searching;
5+
using System.Reflection;
6+
7+
static void ShowCustomers(string type, List<Customer> customers)
8+
{
9+
Console.WriteLine($"Customer {type}: {string.Join(", ", customers.Select(x => $"{x.FirstName} {x.LastName}"))}, total: {customers.Count}.");
10+
}
11+
12+
var provider = new RedisConnectionProvider("redis://localhost:6379");
13+
provider.Connection.CreateIndex(typeof(Customer));
14+
15+
var redisHelper = new RedisHelper(provider);
16+
redisHelper.InitializeCustomers();
17+
18+
var connection = provider.Connection;
19+
20+
var result = connection.Execute("FT.SEARCH", "customer-idx", "@IsActive:{true} @FirstName|LastName:customer");
21+
var response = new SearchResponse<Customer>(result);
22+
23+
ShowCustomers("Active & First or Last Name have \"customer\"", response.Documents.Values.ToList());
24+
25+
result = connection.Execute("FT.SEARCH", "customer-idx", "(@FirstName|LastName:customer) | (@LastName:customer) => { $weight: 5.0; }");
26+
response = new SearchResponse<Customer>(result);
27+
28+
ShowCustomers("First or Last Name have \"customer\" but prioritize lastname", response.Documents.Values.ToList());
29+
30+
result = connection.Execute("FT.SEARCH", "customer-idx", "customer");
31+
response = new SearchResponse<Customer>(result);
32+
33+
ShowCustomers("All customers with fields text that match with \"customer\"", response.Documents.Values.ToList());
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.3.0" />
12+
</ItemGroup>
13+
14+
</Project>

0 commit comments

Comments
 (0)