| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- using Lucene.Net.Analysis;
- using Lucene.Net.Analysis.Standard;
- using Lucene.Net.Documents;
- using Lucene.Net.Store;
- using Masuit.LuceneEFCore.SearchEngine.Test.Helpers;
- using Masuit.LuceneEFCore.SearchEngine.Test.Models;
- using Microsoft.Extensions.Caching.Memory;
- using System.Linq;
- using Xunit;
- namespace Masuit.LuceneEFCore.SearchEngine.Test
- {
- public class LuceneIndexSearcherTests : IClassFixture<TestDataGenerator>
- {
- static readonly Directory Directory = new RAMDirectory();
- static readonly Analyzer Analyzer = new StandardAnalyzer(Lucene.Net.Util.LuceneVersion.LUCENE_48);
- static readonly LuceneIndexer Indexer = new LuceneIndexer(Directory, Analyzer);
- static readonly LuceneIndexSearcher Searcher = new LuceneIndexSearcher(Directory, Analyzer, new MemoryCache(new MemoryCacheOptions()));
- public LuceneIndexSearcherTests(TestDataGenerator tdg)
- {
- Indexer.CreateIndex(tdg.AllData);
- }
- [Fact]
- public void AnIndexCanBeSearched()
- {
- SearchOptions options = new SearchOptions("John", "FirstName");
- var results = Searcher.ScoredSearch(options);
- Assert.Equal(5, results.TotalHits);
- }
- [Fact]
- public void TopNNumberOfResultsCanBeReturned()
- {
- SearchOptions options = new SearchOptions("China", "Country", 1000, null, typeof(City));
- var allResults = Searcher.ScoredSearch(options);
- options.Take = 10;
- var subSet = Searcher.ScoredSearch(options);
- for (var index = 0; index < 10; index++)
- {
- Assert.Equal(allResults.Results[index].Document.Get("IndexId"), subSet.Results[index].Document.Get("IndexId"));
- }
- Assert.Equal(10, subSet.Results.Count);
- Assert.Equal(allResults.TotalHits, subSet.TotalHits);
- }
- [Fact]
- public void ResultsetCanBeSkippedAndTaken()
- {
- SearchOptions options = new SearchOptions("China", "Country", 1000, null, typeof(City));
- var allResults = Searcher.ScoredSearch(options);
- options.Take = 10;
- options.Skip = 10;
- var subSet = Searcher.ScoredSearch(options);
- for (var index = 0; index < 10; index++)
- {
- Assert.Equal(allResults.Results[index + 10].Document.Get("IndexId"), subSet.Results[index].Document.Get("IndexId"));
- }
- Assert.Equal(10, subSet.Results.Count);
- Assert.Equal(allResults.TotalHits, subSet.TotalHits);
- }
- [Fact]
- public void ResultsetCanBeOrdered()
- {
- SearchOptions options = new SearchOptions("John", "FirstName", 1000, null, typeof(User));
- var unordered = Searcher.ScoredSearch(options);
- options.OrderBy.Add("Surname");
- var ordered = Searcher.ScoredSearch(options);
- Assert.Equal(ordered.TotalHits, unordered.TotalHits);
- Assert.NotEqual(ordered.Results.First().Document.Get("Id"), unordered.Results.First().Document.Get("Id"));
- }
- [Fact]
- public void ASingleDocumentIsReturnedFromScoredSearchSingle()
- {
- SearchOptions options = new SearchOptions("[email protected]", "Email");
- var result = Searcher.ScoredSearchSingle(options);
- Assert.NotNull(result);
- Assert.IsType<Document>(result);
- Assert.IsAssignableFrom<Document>(result);
- Assert.Equal("[email protected]", result.Get("Email"));
- }
- [Fact]
- public void MultipleResultsIsNotAProblemFromScoredSearchSingle()
- {
- SearchOptions options = new SearchOptions("John", "FirstName");
- var result = Searcher.ScoredSearchSingle(options);
- Assert.NotNull(result);
- Assert.IsType<Document>(result);
- Assert.Equal("John", result.Get("FirstName"));
- }
- }
- }
|