Using additional indexes - adhtalbo/RedisContext GitHub Wiki
By default only the Id is indexed. To use additional indexes you must manage them yourself. There exists an IndexEntity that simplifies this.
Consider the following context:
public class ExampleContext : RedisContext
{
// Can be connection string OR the name of a connection string in the config
public ExampleContext(string connectionString) : base(connectionString)
{
}
public RedisSet<BasicEntity> BasicEntity {get; set;}
public RedisSet<IndexEntity> BasicEntitySomeValueIndex {get; set;}
}
Keeping the index up to date
The additional index is defined like any other entity. However its values must be updated when BasicEntity is updated. This is done manually.
// Check item with index already exists
if(context.BasicEntitySomeValueIndex.Fetch(entity.SomeValue) == null)
{
context.BasicEntity.Insert(entity);
context.BasicEntitySomeValueIndex.Insert(new IndexEntity(entity, entity.SomeValue));
}
else
{
// Index item already exists
}
Remember to update/delete your index values when your entities are updated/deleted!
Fetch by index values
Once you have an index you can fetch values from it easily.
// Get all SomeValue items that start with a number
var index = context.BasicEntitySomeValueIndex.Fetch("0", "9");
var ids = index.GetIds();
var entities = context.BasicEntity.Fetch(ids);