Beispiel #1
0
        public void SaveChangesDeletesRemovedEntitiesFromTheIndex()
        {
            InitializeContext();
            SearchableContextProvider <TestDbContext> searchProvider = new SearchableContextProvider <TestDbContext>();
            LuceneIndexerOptions options = new LuceneIndexerOptions {
                UseRamDirectory = true
            };

            searchProvider.Initialize(options, context);
            searchProvider.CreateIndex();

            var search = new SearchOptions("John", "FirstName");

            int initialIndexCount = searchProvider.IndexCount;

            var initialResults = searchProvider.Search <User>(search);
            int resultsCount   = initialResults.TotalHits;

            // delete entities
            //
            // NOTE: Because the search result entities are attached to the context
            // we can easily do linq operations on them
            //
            searchProvider.Context.Users.RemoveRange(initialResults.Results);
            searchProvider.SaveChanges(true);

            var updatedSearch   = searchProvider.Search <User>(search);
            int finalIndexCount = searchProvider.IndexCount;

            Assert.Equal(0, updatedSearch.TotalHits);
            Assert.Equal(0, updatedSearch.Results.Count);
            Assert.Equal((initialIndexCount - resultsCount), finalIndexCount);
        }
Beispiel #2
0
        public void SaveChangesUpdatesEntitiesAddedToTheIndex()
        {
            InitializeContext();
            SearchableContextProvider <TestDbContext> searchProvider = new SearchableContextProvider <TestDbContext>();
            LuceneIndexerOptions options = new LuceneIndexerOptions {
                UseRamDirectory = true
            };

            searchProvider.Initialize(options, context);
            searchProvider.CreateIndex();

            var newUser = new User()
            {
                FirstName = "Duke",
                Surname   = "Nukem",
                Email     = "*****@*****.**",
                IndexId   = Guid.NewGuid(),
                JobTitle  = "Shooty Man"
            };

            var search = new SearchOptions("Nukem", "Surname");

            var initialResults = searchProvider.Search <User>(search);

            searchProvider.Context.Users.Add(newUser);
            searchProvider.SaveChanges(true);

            var newResults = searchProvider.Search <User>(search);

            Assert.Equal(0, initialResults.TotalHits);
            Assert.Equal(1, newResults.TotalHits);

            Assert.Equal(newUser.Id, newResults.Results[0].Id);
        }
 public IActionResult AddUser(User user)
 {
     if (!ModelState.IsValid)
     {
         return(View());
     }
     _searchProvider.Initialize(_luceneIndexerOptions, _appDbContext);
     _searchProvider.Context.Users.Add(user);
     _searchProvider.SaveChanges();
     return(View());
 }