Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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);
        }
        public void ASearchCanOrderByMultipleFields()
        {
            SearchableContextProvider <TestDbContext> searchProvider = new SearchableContextProvider <TestDbContext>();
            LuceneIndexerOptions options = new LuceneIndexerOptions(null, null, true);

            User jc = new User()
            {
                FirstName = "John",
                Surname   = "Chapman",
                JobTitle  = "Test Engineer",
                IndexId   = Guid.NewGuid(),
                Email     = "*****@*****.**"
            };

            searchProvider.Initialize(options, context);

            context.Users.Add(jc);
            context.SaveChanges();

            searchProvider.CreateIndex();

            SearchOptions search = new SearchOptions("John", "FirstName", 1000, null, null, "Surname,JobTitle");

            var results = searchProvider.ScoredSearch <User>(search);

            var topResult    = results.Results[0];
            var secondResult = results.Results[1];

            Assert.AreEqual("Sales Associate", topResult.Entity.JobTitle);
            Assert.AreEqual("Test Engineer", secondResult.Entity.JobTitle);

            searchProvider.DeleteIndex();
        }
Ejemplo n.º 4
0
        public void AScoredSearchWillOrderByRelevence()
        {
            InitializeContext();
            SearchableContextProvider <TestDbContext> searchProvider = new SearchableContextProvider <TestDbContext>();
            LuceneIndexerOptions options = new LuceneIndexerOptions {
                UseRamDirectory = true
            };

            searchProvider.Initialize(options, context);

            searchProvider.CreateIndex();

            SearchOptions searchOptions = new SearchOptions("Jeremy Burns", "FirstName,Surname");

            var results = searchProvider.ScoredSearch <User>(searchOptions);

            var first   = results.Results.First().Entity;
            var highest = results.Results.First().Score;
            var lowest  = results.Results.Last().Score;

            Assert.True(highest > lowest);
            Assert.Equal("Jeremy", first.FirstName);
            Assert.Equal("Burns", first.Surname);

            searchProvider.DeleteIndex();
        }
Ejemplo n.º 5
0
        public void SkipAndTakeWorkWhenSearchingUsingAContextProvider()
        {
            InitializeContext();
            SearchableContextProvider <TestDbContext> searchProvider = new SearchableContextProvider <TestDbContext>();
            LuceneIndexerOptions options = new LuceneIndexerOptions {
                UseRamDirectory = true
            };

            searchProvider.Initialize(options, context);

            searchProvider.CreateIndex();

            SearchOptions searchOptions = new SearchOptions("John", "FirstName");

            // test
            var initialResults = searchProvider.ScoredSearch <User>(searchOptions);
            int lastId         = initialResults.Results[4].Entity.Id;

            Assert.Equal(5, initialResults.TotalHits);
            Assert.Equal(5, initialResults.Results.Count());

            searchOptions.Skip = 4;
            searchOptions.Take = 1;
            var subResults = searchProvider.ScoredSearch <User>(searchOptions);

            Assert.Equal(5, subResults.TotalHits);
            Assert.Equal(1, subResults.Results.Count());
            Assert.Equal(lastId, subResults.Results.First().Entity.Id);

            // cleanup
            searchProvider.DeleteIndex();
        }
 public HomeController(AppDbContext appDbContext, LuceneIndexerOptions luceneIndexerOptions,
                       SearchableContextProvider <AppDbContext> searchProvider)
 {
     _appDbContext         = appDbContext;
     _luceneIndexerOptions = luceneIndexerOptions;
     _searchProvider       = searchProvider;
 }
        public void NonValidEntitiesAreIgnored()
        {
            SearchableContextProvider <MockNonIndexableContext> searchProvider = new SearchableContextProvider <MockNonIndexableContext>();

            LuceneIndexerOptions indexerOptions = new LuceneIndexerOptions(null, null, true);

            searchProvider.Initialize(indexerOptions);

            searchProvider.CreateIndex();

            Assert.AreEqual(0, searchProvider.IndexCount);
        }
        public void ASearchCanIncludeAnOrderBy()
        {
            SearchableContextProvider <TestDbContext> searchProvider = new SearchableContextProvider <TestDbContext>();
            LuceneIndexerOptions options = new LuceneIndexerOptions(null, null, true);

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

            SearchOptions search = new SearchOptions("Jeremy", "FirstName", 5, null, null, "Surname");

            var results = searchProvider.ScoredSearch <User>(search);
        }
        public void AContextProviderCanIndexADatabase()
        {
            SearchableContextProvider <TestDbContext> searchProvider = new SearchableContextProvider <TestDbContext>();
            LuceneIndexerOptions options = new LuceneIndexerOptions(null, null, true);

            searchProvider.Initialize(options, context);

            searchProvider.CreateIndex();

            Assert.AreEqual(2000, searchProvider.IndexCount);

            // cleanup
            searchProvider.DeleteIndex();
        }
        public void ASearchCanReturnMultipleEntityTypes()
        {
            SearchableContextProvider <TestDbContext> searchProvider = new SearchableContextProvider <TestDbContext>();
            LuceneIndexerOptions options = new LuceneIndexerOptions(null, null, true);

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

            SearchOptions query = new SearchOptions("John China", "FirstName,Country");

            var resultSet = searchProvider.ScoredSearch(query);

            Assert.AreNotEqual(resultSet.Results.First().Entity.Type, resultSet.Results.Last().Entity.Type);
        }
Ejemplo n.º 11
0
        public void NonValidEntitiesAreIgnored()
        {
            InitializeContext();
            SearchableContextProvider <MockNonIndexableContext> searchProvider = new SearchableContextProvider <MockNonIndexableContext>();

            LuceneIndexerOptions indexerOptions = new LuceneIndexerOptions {
                UseRamDirectory = true
            };

            searchProvider.Initialize(indexerOptions);

            searchProvider.CreateIndex();

            Assert.Equal(0, searchProvider.IndexCount);
        }
        public void ASearchCanBeOrderedAcrossTypes()
        {
            SearchableContextProvider <TestDbContext> searchProvider = new SearchableContextProvider <TestDbContext>();
            LuceneIndexerOptions options = new LuceneIndexerOptions(null, null, true);

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

            SearchOptions query = new SearchOptions("Moore China", "Surname,Country");

            query.OrderBy.Add("Name");

            var resultSet = searchProvider.ScoredSearch(query);

            Assert.AreNotEqual(resultSet.Results.First().Entity.Type, resultSet.Results.Last().Entity.Type);
        }
Ejemplo n.º 13
0
        public void AContextProviderCanIndexADatabase()
        {
            InitializeContext();
            SearchableContextProvider <TestDbContext> searchProvider = new SearchableContextProvider <TestDbContext>();
            LuceneIndexerOptions options = new LuceneIndexerOptions {
                UseRamDirectory = true
            };

            searchProvider.Initialize(options, context);

            searchProvider.CreateIndex();

            Assert.Equal(2000, searchProvider.IndexCount);

            // cleanup
            searchProvider.DeleteIndex();
        }
        public void AContextCanBeSearchedUsingAContextProvider()
        {
            SearchableContextProvider <TestDbContext> searchProvider = new SearchableContextProvider <TestDbContext>();
            LuceneIndexerOptions options = new LuceneIndexerOptions(null, null, true);

            searchProvider.Initialize(options, context);

            searchProvider.CreateIndex();

            SearchOptions searchOptions = new SearchOptions("John", "FirstName");

            // test
            var results = searchProvider.ScoredSearch <User>(searchOptions);

            Assert.AreEqual(5, results.TotalHits);

            // cleanup
            searchProvider.DeleteIndex();
        }
        public void ASearchWillReturnTheSameResultsAsAScoredSearch()
        {
            SearchableContextProvider <TestDbContext> searchProvider = new SearchableContextProvider <TestDbContext>();
            LuceneIndexerOptions options = new LuceneIndexerOptions(null, null, true);

            searchProvider.Initialize(options, context);

            searchProvider.CreateIndex();

            SearchOptions searchOptions = new SearchOptions("Joh*", "FirstName");

            // test
            var results = searchProvider.Search <User>(searchOptions);

            Assert.AreEqual(10, results.TotalHits);

            // cleanup
            searchProvider.DeleteIndex();
        }
        public void ASearchWillStillOrderByRelevence()
        {
            SearchableContextProvider <TestDbContext> searchProvider = new SearchableContextProvider <TestDbContext>();
            LuceneIndexerOptions options = new LuceneIndexerOptions(null, null, true);

            searchProvider.Initialize(options, context);

            searchProvider.CreateIndex();

            SearchOptions searchOptions = new SearchOptions("Jeremy Burns", "FirstName,Surname");

            var results = searchProvider.Search <User>(searchOptions);

            var first = results.Results.First();

            Assert.AreEqual("Jeremy", first.FirstName);
            Assert.AreEqual("Burns", first.Surname);

            searchProvider.DeleteIndex();
        }
Ejemplo n.º 17
0
        public void AContextCanBeSearchedUsingAWildCard()
        {
            InitializeContext();
            SearchableContextProvider <TestDbContext> searchProvider = new SearchableContextProvider <TestDbContext>();
            LuceneIndexerOptions options = new LuceneIndexerOptions {
                UseRamDirectory = true
            };

            searchProvider.Initialize(options, context);

            searchProvider.CreateIndex();

            SearchOptions searchOptions = new SearchOptions("Joh*", "FirstName");

            // test
            var results = searchProvider.ScoredSearch <User>(searchOptions);

            PrintResult(results);

            Assert.Equal(10, results.TotalHits);

            // cleanup
            searchProvider.DeleteIndex();
        }