Skip to content

Indexes

A secondary index lets you find records by a field inside the payload rather than by stream name. You define it with a filter and a list of fields to project; the server maintains it, and you read the result back through the normal stream APIs.

await client.Indexes.Create(new CreateIndexOptions {
Name = "ORDERS_BY_CUSTOMER",
Filter = "schema_name = 'OrderPlaced'",
Fields = [
new IndexField("customer_id", "data.customerId", IndexFieldType.String),
new IndexField("amount", "data.amount", IndexFieldType.Double)
]
});

The short form skips the options type for the common case:

await client.Indexes.Create(
"ORDERS_BY_CUSTOMER",
"schema_name = 'OrderPlaced'",
[
new IndexField("customer_id", "data.customerId", IndexFieldType.String),
new IndexField("amount", "data.amount", IndexFieldType.Double)
]);
Option Type Meaning
Name IndexName Unique
Filter string Decides which records the index contains. Same grammar as a read filter
Fields IList<IndexField> The indexed columns
Start bool? false creates it stopped. null takes the server default

An IndexField is a name, a selector path, and a type.

IndexFieldType Maps to
String string
Double double
Int32 int
Int64 long

Selector paths are dot-separated and resolve against the decoded JSON payload. data.customerId reads a root member; data.shipping.country walks two levels in.

A populated index exposes itself as a stream named $idx-user-{name}. Read it with RecordFilter.ByUserIndex:

var all = new ReadRecordsOptions {
Start = LogPosition.Earliest,
Filter = RecordFilter.ByUserIndex("ORDERS_BY_CUSTOMER")
};

To narrow further on the indexed columns, name the index stream in an expression:

var highValue = new ReadRecordsOptions {
Start = LogPosition.Earliest,
Filter = RecordFilter.ByExpression(
"stream = '$idx-user-ORDERS_BY_CUSTOMER' AND data.amount > 1000")
};
IReadOnlyList<Index> indexes = await client.Indexes.List();
Index orders = await client.Indexes.Get("ORDERS_BY_CUSTOMER");

Returns Index:

Member Type Meaning
Name IndexName The name
Filter string The original filter expression
Fields IReadOnlyList<IndexField> The indexed columns
State IndexState Unspecified, Started, Stopped, Deleted

Stopping pauses ingestion. The index keeps what it has already built, and starting resumes from there.

await client.Indexes.Stop("ORDERS_BY_CUSTOMER");
await client.Indexes.Start("ORDERS_BY_CUSTOMER");

Permanent. The index and everything it built are gone, and rebuilding means re-indexing the log from scratch.

await client.Indexes.Delete("ORDERS_BY_CUSTOMER");