Skip to content

Instantly share code, notes, and snippets.

@slorello89
Created May 3, 2024 12:33
Show Gist options
  • Save slorello89/70799dce320d88f3a1e15bcf82300d6d to your computer and use it in GitHub Desktop.
Save slorello89/70799dce320d88f3a1e15bcf82300d6d to your computer and use it in GitHub Desktop.
using NRedisStack.RedisStackCommands;
using NRedisStack.Search;
using NRedisStack.Search.Aggregation;
using StackExchange.Redis;
var muxer = ConnectionMultiplexer.Connect("localhost");
var db = muxer.GetDatabase();
string[] strings = new string[100];
for (int i = 0; i < 100; i++)
{
if (i % 2 == 0)
{
strings[i] = new string((char)('A' + (i % 26)), (i / 26) + 1);
}
else
{
strings[i] = new string((char)('a' + (i % 26)), (i / 26) + 1);
}
}
// Sort the array in a case-sensitive manner
Array.Sort(strings, StringComparer.Ordinal);
var ft = db.FT();
var indexName = "test-idx";
try
{
ft.DropIndex(indexName, true);
}
catch (Exception ex)
{
//ignore
}
Schema schema = new Schema();
schema.AddTextField("name", sortable: true, unf: true);
// schema.AddTextField("name", sortable: true);
FTCreateParams parameters = new FTCreateParams();
parameters.Prefix("test:");
ft.Create(indexName, parameters, schema);
var insertTasks = new List<Task>();
for (int i = 0; i < 100; i++)
{
insertTasks.Add(db.HashSetAsync($"test:{i}", "name", strings[i]));
}
await Task.WhenAll(insertTasks);
SortedField sortField = new SortedField("@name");
for (var i = 0; i < 10; i++)
{
AggregationRequest request = new AggregationRequest();
// request.LoadAll();
request.SortBy(sortField);
request.Limit(i * 10, 10);
var result = ft.Aggregate(indexName, request);
foreach (var res in result.GetResults())
{
Console.WriteLine(res["name"]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment