Beispiel #1
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();
        }
Beispiel #2
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();
        }
        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();
        }
        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);
        }
Beispiel #5
0
        public IActionResult Index(string q)
        {
            if (string.IsNullOrEmpty(q))
            {
                ModelState.AddModelError("q", "search  string required");
                return(View());
            }
            // q = q + "~";
            _searchProvider.Initialize(_luceneIndexerOptions, _appDbContext);
            SearchOptions searchOptions = new SearchOptions(q, "FirstName,Surname,Email,JobTitle,Name,Country");

            var userResults = _searchProvider.ScoredSearch <User>(searchOptions);
            var cityResults = _searchProvider.ScoredSearch <City>(searchOptions);
            SearchResultViewModel viewModel = new SearchResultViewModel
            {
                UserSearchCollection = userResults,
                CitySearchCollection = cityResults
            };

            ViewBag.Message = $"Search completed: {q}";
            return(View(viewModel));
        }
        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);
        }
        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);
        }
        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();
        }
Beispiel #9
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();
        }