Skip to main content

Creating an Index with Redis OM

To unlock some of the nicest functionality of Redis OM, e.g., running searches, matches, aggregations, reductions, mappings, etc... You will need to tell Redis how you want data to be stored and how you want it indexed. One of the features the Redis OM library provides is creating indices that map directly to your objects by declaring the indices as attributes on your class.

Let's start with an example class.

[Document]
public partial class Person
{
[RedisIdField]
public string Id { get; set; }

[Searchable(Sortable = true)]
public string Name { get; set; }

[Indexed(Aggregatable = true)]
public GeoLoc? Home { get; set; }

[Indexed(Aggregatable = true)]
public GeoLoc? Work { get; set; }

[Indexed(Sortable = true)]
public int? Age { get; set; }

[Indexed(Sortable = true)]
public int? DepartmentNumber { get; set; }

[Indexed(Sortable = true)]
public double? Sales { get; set; }

[Indexed(Sortable = true)]
public double? SalesAdjustment { get; set; }

[Indexed(Sortable = true)]
public long? LastTimeOnline { get; set; }

[Indexed(Aggregatable = true)]
public string Email { get; set; }
}

As shown above, you can declare a class as being indexed with the Document Attribute. In the Document attribute, you can set a few fields to help build the index:

Property NameDescriptionDefaultOptional
StorageTypeDefines the underlying data structure used to store the object in Redis, options are HASH and JSON, Note JSON is only useable with Redis StackHASHtrue
IndexNameThe name of the index$"{SimpleClassName.ToLower()}-idx}true
PrefixesThe key prefixes for redis to build an index off ofnew string[]{$"{FullyQualifiedClassName}:"}true
LanguageLanguage to use for full-text search indexingnulltrue
LanguageFieldThe name of the field in which the document stores its Languagenulltrue
FilterThe filter to use to determine whether a particular item is indexed, e.g. @Age>=18nulltrue
IdGenerationStrategyThe strategy used to generate Ids for documents, if left blank it will use a ULID generation strategyUlidGenerationStrategytrue

Field Level Declarations

Id Fields

Every class indexed by Redis must contain an Id Field marked with the RedisIdField.

Indexed Fields

In addition to declaring an Id Field, you can also declare indexed fields, which will let you search for values within those fields afterward. There are two types of Field level attributes.

  1. Indexed - This type of index is valid for fields that are of the type string, a Numeric type (double/int/float etc. . .), or can be decorated for fields that are of the type GeoLoc, the exact way that the indexed field is interpreted depends on the indexed type
  2. Searchable - This type is only valid for string fields, but this enables full-text search on the decorated fields.

IndexedAttribute Properties

There are properties inside the IndexedAttribute that let you further customize how things are stored & queried.

PropertyNametypeDescriptionDefaultOptional
PropertyNamestringThe name of the property to be indexedThe name of the property being indexedtrue
SortableboolWhether to index the item so it can be sorted on in queries, enables use of OrderBy & OrderByDescending -> collection.OrderBy(x=>x.Email)falsetrue
NormalizeboolOnly applicable for string type fields Determines whether the text in a field is normalized (sent to lower case) for purposes of sortingtruetrue
SeparatorcharOnly applicable for string type fields Character to use for separating tag field, allows the application of multiple tags fo the same item e.g. article.Category = technology,parenting is delineated by a , means that collection.Where(x=>x.Category == "technology") and collection.Where(x=>x.Category == "parenting") will both match the record``true
CaseSensitiveboolOnly applicable for string type fields - Determines whether case is considered when performing matches on tagsfalsetrue

SearchableAttribute Properties

There are properties for the SearchableAttribute that let you further customize how the full-text search determines matches

PropertyNametypeDescriptionDefaultOptional
PropertyNamestringThe name of the property to be indexedThe name of the indexed propertytrue
SortableboolWhether to index the item so it can be sorted on in queries, enables use of OrderBy & OrderByDescending -> collection.OrderBy(x=>x.Email)falsetrue
NoStemboolDetermines whether to use stemming, in other words adding the stem of the word to the index, setting to true will stop the Redis from indexing the stems of wordsfalsetrue
PhoneticMatcherstringThe phonetic matcher to use if you'd like the index to use (PhoneticMatching)[https://oss.redis.com/redisearch/Phonetic_Matching/] with the indexnulltrue
Weightdoubledetermines the importance of the field for checking result accuracy1.0true

Creating The Index

After declaring the index, the creation of the index is pretty straightforward. All you have to do is call CreateIndex for the decorated type. The library will take care of serializing the provided type into a searchable index. The library does not try to be particularly clever, so if the index already exists it will the creation request will be rejected, and you will have to drop and re-add the index (migrations is a feature that may be added in the future)

var connection = provider.Connection;
connection.CreateIndex(typeof(Person));