Skip to content

Add low level search example #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions examples/Redis.OM.LowLevelSearchIndex/Helpers/RedisHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Redis.OM.BasicMatchingQueries.Models;
using Redis.OM.Searching;

namespace Redis.OM.LowLevelSearchIndex.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 = "[email protected]",
});

_customerCollection.Insert(new Customer()
{
FirstName = "Customer",
LastName = "3",
Age = 25,
IsActive = false,
Email = "[email protected]",
});

_customerCollection.Insert(new Customer()
{
FirstName = "Testable",
LastName = "Customer 2",
Age = 99,
IsActive = true,
Email = "[email protected]",
});

_customerCollection.Insert(new Customer()
{
FirstName = "Sharon",
LastName = "Lim",
Age = 25,
IsActive = true,
Email = "[email protected]",
});
}

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

using Redis.OM.Modeling;

[Document(StorageType = StorageType.Json, IndexName = "customer-idx")]
public class Customer
{
[Searchable] public string FirstName { get; set; }
[Searchable] public string LastName { get; set; }
[Searchable] public string Email { get; set; }
[Indexed(Sortable = true)] public int Age { get; set; }
[Indexed] public bool IsActive { get; set; }
}
33 changes: 33 additions & 0 deletions examples/Redis.OM.LowLevelSearchIndex/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Redis.OM;
using Redis.OM.BasicMatchingQueries.Models;
using Redis.OM.LowLevelSearchIndex.Helpers;
using Redis.OM.Searching;
using System.Reflection;

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 connection = provider.Connection;

var result = connection.Execute("FT.SEARCH", "customer-idx", "@IsActive:{true} @FirstName|LastName:customer");
var response = new SearchResponse<Customer>(result);

ShowCustomers("Active & First or Last Name have \"customer\"", response.Documents.Values.ToList());

result = connection.Execute("FT.SEARCH", "customer-idx", "(@FirstName|LastName:customer) | (@LastName:customer) => { $weight: 5.0; }");
response = new SearchResponse<Customer>(result);

ShowCustomers("First or Last Name have \"customer\" but prioritize lastname", response.Documents.Values.ToList());

result = connection.Execute("FT.SEARCH", "customer-idx", "customer");
response = new SearchResponse<Customer>(result);

ShowCustomers("All customers with fields text that match with \"customer\"", response.Documents.Values.ToList());
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.3.0" />
</ItemGroup>

</Project>