Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indexes not being automatically generated. #368

Open
wlewis22 opened this issue Sep 28, 2023 · 1 comment
Open

Indexes not being automatically generated. #368

wlewis22 opened this issue Sep 28, 2023 · 1 comment

Comments

@wlewis22
Copy link

wlewis22 commented Sep 28, 2023

Hi James (and any others who might be able to assist).

I've been using MongoFramework for sometime now on my current project and its brilliant. However I have run into a problem.

I am going to be honest, I am not sure if this is an issue or me doing something dumb.

I recently needed to add some text indexes to properties on an entity, in order to use the SearchText method. I tried adding the indexes using the Attribute decorator, as well as via the MongoDbContext in the OnConfiguringMapping method. Neither instance seemed to result in the indexes being created in my DB when the project was run.

I tried calling context.SaveChangesAsync() just in case that was required but to no avail. If I go to the DB and create the indexes manually, the SearchText method works fine. If I just allow MongoFramework to do its thing, it throws the below error.

MongoDB.Driver.MongoCommandException: Command aggregate failed: text index required for $text query.

Hopefully this is just me missing something in the doco and not an actual issue, as it seems pretty fundamental. Any assistance would be much appreciated.

Below is my DB Context, in case that helps.

public class DataContext : MongoDbContext
{
	public MongoDbSet<Creature> Creatures { get; set; }
	...other DbSets etc...

	public DataContext (IMongoDbConnection connection) : base (connection) 
	{
    }

	protected override void OnConfigureMapping (MappingBuilder mappingBuilder) {
        mappingBuilder.Entity<Creature>()
			.HasIndex(e => e.Name, b => b.HasType(IndexType.Text));

        mappingBuilder.Entity<Creature>()
            .HasIndex(e => e.Description, b => b.HasType(IndexType.Text));

        mappingBuilder.Entity<Creature>()
            .HasIndex(e => e.Source, b => b.HasType(IndexType.Text));
    }

	public override void SaveChanges () {
		OnBeforeSaving ();
		base.SaveChanges ();
	}

	public override async Task SaveChangesAsync (CancellationToken cancellationToken = default) {
		OnBeforeSaving ();
		await base.SaveChangesAsync (cancellationToken);
	}

	private void OnBeforeSaving () {
		ChangeTracker.DetectChanges ();

		foreach (var entity in ChangeTracker.Entries ()) {
			switch (entity.State) {
				case EntityEntryState.Added:
					if (entity.Entity is IEntityBase addedEntity) {
						addedEntity.CreatedOn = DateTime.UtcNow;
						addedEntity.ModifiedOn = DateTime.UtcNow;
					}
					break;

				case EntityEntryState.Updated:
					if (entity.Entity is IEntityBase modifiedEntity) {
						modifiedEntity.ModifiedOn = DateTime.UtcNow;
					}
					break;
			}
		}
	}
}
@Turnerj
Copy link
Member

Turnerj commented Sep 30, 2023

Thanks for raising the issue @wlewis22 - it is a strange one. What version of MongoDB are you using?

I tried calling context.SaveChangesAsync() just in case that was required but to no avail.

Yeah, a call to SaveChanges / SaveChangesAsync is required as we're doing an IO operation and we don't know what types you want to apply indexing to until the last minute.

There is a test that does confirm the index creation behaviour for text indexes (EntityIndexWriterTests.WriteIndexAsync) and running that locally still passes so I don't know what you're hitting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants