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:
Every class indexed by Redis must contain an Id Field marked with the RedisIdField
.
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.
There are properties inside the IndexedAttribute that let you further customize how things are stored & queried.
There are properties for the SearchableAttribute that let you further customize how the full-text search determines matches
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));
Property Name | Description | Default | Optional |
---|---|---|---|
StorageType | Defines the underlying data structure used to store the object in Redis, options are HASH and JSON, Note JSON is only useable with Redis Stack | HASH | true |
IndexName | The name of the index |
| true |
Prefixes | The key prefixes for redis to build an index off of |
| true |
Language | Language to use for full-text search indexing |
| true |
LanguageField | The name of the field in which the document stores its Language |
| true |
Filter | The filter to use to determine whether a particular item is indexed, e.g. |
| true |
IdGenerationStrategy | The strategy used to generate Ids for documents, if left blank it will use a ULID generation strategy | UlidGenerationStrategy | true |
PropertyName | type | Description | Default | Optional | |
---|---|---|---|---|---|
PropertyName |
| The name of the property to be indexed | The name of the property being indexed | true | |
Sortable |
| Whether to index the item so it can be sorted on in queries, enables use of |
| true | |
Normalize |
| Only applicable for |
| true | |
Separator |
| Only applicable for | ` | ` | true |
CaseSensitive |
| Only applicable for |
| true |
PropertyName | type | Description | Default | Optional |
---|---|---|---|---|
PropertyName |
| The name of the property to be indexed | The name of the indexed property | true |
Sortable |
| Whether to index the item so it can be sorted on in queries, enables use of |
| true |
NoStem |
| Determines 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 words |
| true |
PhoneticMatcher |
| The phonetic matcher to use if you'd like the index to use (PhoneticMatching)[https://oss.redis.com/redisearch/Phonetic_Matching/] with the index | null | true |
Weight |
| determines the importance of the field for checking result accuracy | 1.0 | true |