LuceneIndexSearcherTests.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using Lucene.Net.Analysis;
  2. using Lucene.Net.Analysis.Standard;
  3. using Lucene.Net.Documents;
  4. using Lucene.Net.Store;
  5. using Masuit.LuceneEFCore.SearchEngine.Test.Helpers;
  6. using Masuit.LuceneEFCore.SearchEngine.Test.Models;
  7. using Microsoft.Extensions.Caching.Memory;
  8. using System.Linq;
  9. using Xunit;
  10. namespace Masuit.LuceneEFCore.SearchEngine.Test
  11. {
  12. public class LuceneIndexSearcherTests : IClassFixture<TestDataGenerator>
  13. {
  14. static readonly Directory Directory = new RAMDirectory();
  15. static readonly Analyzer Analyzer = new StandardAnalyzer(Lucene.Net.Util.LuceneVersion.LUCENE_48);
  16. static readonly LuceneIndexer Indexer = new LuceneIndexer(Directory, Analyzer);
  17. static readonly LuceneIndexSearcher Searcher = new LuceneIndexSearcher(Directory, Analyzer, new MemoryCache(new MemoryCacheOptions()));
  18. public LuceneIndexSearcherTests(TestDataGenerator tdg)
  19. {
  20. Indexer.CreateIndex(tdg.AllData);
  21. }
  22. [Fact]
  23. public void AnIndexCanBeSearched()
  24. {
  25. SearchOptions options = new SearchOptions("John", "FirstName");
  26. var results = Searcher.ScoredSearch(options);
  27. Assert.Equal(5, results.TotalHits);
  28. }
  29. [Fact]
  30. public void TopNNumberOfResultsCanBeReturned()
  31. {
  32. SearchOptions options = new SearchOptions("China", "Country", 1000, null, typeof(City));
  33. var allResults = Searcher.ScoredSearch(options);
  34. options.Take = 10;
  35. var subSet = Searcher.ScoredSearch(options);
  36. for (var index = 0; index < 10; index++)
  37. {
  38. Assert.Equal(allResults.Results[index].Document.Get("IndexId"), subSet.Results[index].Document.Get("IndexId"));
  39. }
  40. Assert.Equal(10, subSet.Results.Count);
  41. Assert.Equal(allResults.TotalHits, subSet.TotalHits);
  42. }
  43. [Fact]
  44. public void ResultsetCanBeSkippedAndTaken()
  45. {
  46. SearchOptions options = new SearchOptions("China", "Country", 1000, null, typeof(City));
  47. var allResults = Searcher.ScoredSearch(options);
  48. options.Take = 10;
  49. options.Skip = 10;
  50. var subSet = Searcher.ScoredSearch(options);
  51. for (var index = 0; index < 10; index++)
  52. {
  53. Assert.Equal(allResults.Results[index + 10].Document.Get("IndexId"), subSet.Results[index].Document.Get("IndexId"));
  54. }
  55. Assert.Equal(10, subSet.Results.Count);
  56. Assert.Equal(allResults.TotalHits, subSet.TotalHits);
  57. }
  58. [Fact]
  59. public void ResultsetCanBeOrdered()
  60. {
  61. SearchOptions options = new SearchOptions("John", "FirstName", 1000, null, typeof(User));
  62. var unordered = Searcher.ScoredSearch(options);
  63. options.OrderBy.Add("Surname");
  64. var ordered = Searcher.ScoredSearch(options);
  65. Assert.Equal(ordered.TotalHits, unordered.TotalHits);
  66. Assert.NotEqual(ordered.Results.First().Document.Get("Id"), unordered.Results.First().Document.Get("Id"));
  67. }
  68. [Fact]
  69. public void ASingleDocumentIsReturnedFromScoredSearchSingle()
  70. {
  71. SearchOptions options = new SearchOptions("[email protected]", "Email");
  72. var result = Searcher.ScoredSearchSingle(options);
  73. Assert.NotNull(result);
  74. Assert.IsType<Document>(result);
  75. Assert.IsAssignableFrom<Document>(result);
  76. Assert.Equal("[email protected]", result.Get("Email"));
  77. }
  78. [Fact]
  79. public void MultipleResultsIsNotAProblemFromScoredSearchSingle()
  80. {
  81. SearchOptions options = new SearchOptions("John", "FirstName");
  82. var result = Searcher.ScoredSearchSingle(options);
  83. Assert.NotNull(result);
  84. Assert.IsType<Document>(result);
  85. Assert.Equal("John", result.Get("FirstName"));
  86. }
  87. }
  88. }