public void Search_TakeAndSkip(int resultsToTake, int resultsToSkip, int expectedResults, int expectedTotalResults) { string searchQuery = "foo bar"; mockCommunityGroupRepo.SetupGet(repoEntities, repoEntities.Count); EntitySearchResults <CommunityGroup> results = uut.Search(searchQuery, resultsToTake, resultsToSkip); Assert.AreEqual(expectedResults, results.MatchedEntities.Count); Assert.AreEqual(expectedTotalResults, results.TotalMatchedEntities); }
public void Search_QueryMatching(string searchQuery, int expectedResults, int expectedTotalResults, params int[] expectedIndexes) { int resultsToTake = repoEntities.Count; int resultsToSkip = 0; mockCommunityGroupRepo.SetupGet(repoEntities, repoEntities.Count); EntitySearchResults <CommunityGroup> results = uut.Search(searchQuery, resultsToTake, resultsToSkip); Assert.AreEqual(expectedResults, results.MatchedEntities.Count); Assert.AreEqual(expectedTotalResults, results.TotalMatchedEntities); for (int i = 0; i < expectedIndexes.Length; ++i) { Assert.IsTrue(results.MatchedEntities.Contains(repoEntities[expectedIndexes[i]])); } }
private EntitySearchResults Search(string q, int pageSize, int page, QueryParser queryParser) { using (var searcher = new IndexSearcher(IndexDir, true)) { Query query; Highlighter highlighter = null; if (q == null) { query = new MatchAllDocsQuery(); } else { query = queryParser.Parse(q); if (q.Any() && !StringExtension.EndsWithWhitespace(q) && !new[] { '"', '*', '~', '?' }.Contains(q.Last())) { query = new BooleanQuery { { queryParser.Parse(q + "*"), Occur.SHOULD }, { query, Occur.SHOULD } }; } highlighter = new Highlighter(new SimpleHTMLFormatter("<span class=\"is-highlighted\">", "</span>"), new QueryScorer(query)); } var take = page * pageSize; var sort = new Sort(new SortField(FIELD_NAME, SortField.STRING)); var docs = searcher.Search(query, null, take, sort); var results = new EntitySearchResults { TotalHits = docs.TotalHits, Hits = docs.ScoreDocs.Skip(take - pageSize).AsParallel().AsOrdered().Select(x => ScoreDocToSearchHit(searcher, highlighter, x)).ToArray() }; return(results); } }