Table of Contents

Index creation

Use the Index<T>() method to define indexes as shown below. Specify index keys by chaining calls to the .Key() method. Compound indexes are created by calling the .Key() method multiple times.

First parameter of the method is a lambda pointing to the property on your entity. Second parameter specifies the type of key. Finally chain in a call to .CreateAsync() to finish defining the index.

Tip

You should define your indexes at the startup of your application so they only run once at launch. Alternatively you can define indexes in the static constructor of your entity classes. If an index exists for the specified config, your commands will just be ignored by the server.

Text indexes

await db.Index<Author>()
        .Key(a => a.Name, KeyType.Text)
        .Key(a => a.Surname, KeyType.Text)
        .CreateAsync();

If the field you want to index is nested within arrays or lists, specify an expression with a [0] index position like so:

.Key(a => a.Books[0].Reviews[0].Content, KeyType.Text)

In order to index all text properties of an entity, you can create a wildcard text index as follows:

.Key(a => a, KeyType.Text)

You can do full text searches after defining a text index as described above with the following:

await db.Find<Book>()
        .Match(Search.Full, "search term")
        .ExecuteAsync();

You can also start a fluent aggregation pipeline with a $text stage as follows:

db.FluentTextSearch<Book>(Search.Full, "search term");
Tip

click here to see more info on how to do text searches for phrases, negations, any words, etc.

In order to run a fuzzy text match, simply change the first parameter to Search.Fuzzy as shown here:

await db.Find<Book>()
        .Match(Search.Fuzzy, "search term")
        .ExecuteAsync();
Note

Fuzzy text searching requires a bit of special handling, please see here for detailed information.

Other index types

Use the same Index<T>() method as above but with the type parameters of the keys set to one of the following enum values:

  • Ascending
  • Descending
  • Geo2D
  • Geo2DSphere
  • Hashed
  • Wildcard

Indexing options

To specify options for index creation, specify an action using the .Option() method before calling the .CreateAsync() method.

await db.Index<Book>()
        .Key(x => x.Title, KeyType.Descending)
        .Option(o =>
        {
            o.Background = false;
            o.Unique = true;
        })
        .CreateAsync();

Retrieve the name of created index

The .CreateAsync() method returns the name of the index that was created.

var name = await db.Index<Book>()
                   .Key(x => x.Title, KeyType.Ascending)
                   .Key(x=> x.Price, KeyType.Descending)
                   .CreateAsync();              

Delete an index by name

await db.Index<Book>().DropAsync(name);

Delete all indexes for an entity type

await db.Index<Book>().DropAllAsync();