See Exploring the Postgres Gin index for more information on the Gin index strategy within Postgresqsl.
To optimize a wider range of adhoc queries against the document JSONB, you can apply a Gin index to the JSON field in the database:
public class IndexExamples: MartenRegistry
{
public IndexExamples()
{
// Add a gin index to the User document type
For<User>().GinIndexJsonData();
// Adds a basic btree index to the duplicated
// field for this property that also overrides
// the Postgresql database type for the column
For<User>().Duplicate(x => x.FirstName, pgType: "varchar(50)");
// Defining a duplicate column with not null constraint
For<User>().Duplicate(x => x.Department, pgType: "varchar(50)", notNull: true);
// Customize the index on the duplicated field
// for FirstName
For<User>().Duplicate(x => x.FirstName, configure: idx =>
{
idx.IndexName = "idx_special";
idx.Method = IndexMethod.hash;
});
// Customize the index on the duplicated field
// for UserName to be unique
For<User>().Duplicate(x => x.UserName, configure: idx =>
{
idx.IsUnique = true;
});
// Customize the index on the duplicated field
// for LastName to be in descending order
For<User>().Duplicate(x => x.LastName, configure: idx =>
{
idx.SortOrder = SortOrder.Desc;
});
}
}
Marten may be changed to make the Gin index on the data field be automatic in the future.