SearchEngineTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. using JiebaNet.Segmenter;
  2. using Lucene.Net.Analysis.JieBa;
  3. using Lucene.Net.Store;
  4. using Masuit.LuceneEFCore.SearchEngine.Interfaces;
  5. using Masuit.LuceneEFCore.SearchEngine.Test.Helpers;
  6. using Masuit.LuceneEFCore.SearchEngine.Test.Models;
  7. using Microsoft.EntityFrameworkCore;
  8. using Microsoft.Extensions.Caching.Memory;
  9. using System.Linq;
  10. using Xunit;
  11. using Xunit.Abstractions;
  12. namespace Masuit.LuceneEFCore.SearchEngine.Test
  13. {
  14. public class SearchEngineTests
  15. {
  16. private TestDbContext _context;
  17. private readonly ITestOutputHelper _output;
  18. string _tempName;
  19. public SearchEngineTests(ITestOutputHelper output)
  20. {
  21. _output = output;
  22. }
  23. private void InitializeContext()
  24. {
  25. _tempName = System.IO.Path.GetTempFileName();
  26. var dboptions = new DbContextOptionsBuilder<TestDbContext>().UseInMemoryDatabase(databaseName: _tempName).Options;
  27. _context = new TestDbContext(dboptions);
  28. }
  29. [Fact]
  30. public void AContextProviderCanIndexADatabase()
  31. {
  32. InitializeContext();
  33. LuceneIndexerOptions options = new LuceneIndexerOptions()
  34. {
  35. Path = "lucene"
  36. };
  37. var memoryCache = new MemoryCache(new MemoryCacheOptions());
  38. var directory = FSDirectory.Open(options.Path);
  39. JieBaAnalyzer analyzer = new JieBaAnalyzer(TokenizerMode.Search);
  40. SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(_context, directory, analyzer, memoryCache);
  41. searchProvider.CreateIndex();
  42. Assert.Equal(2000, searchProvider.IndexCount);
  43. searchProvider.DeleteIndex();
  44. }
  45. [Fact]
  46. public void AContextCanBeSearchedUsingAContextProvider()
  47. {
  48. InitializeContext();
  49. LuceneIndexerOptions options = new LuceneIndexerOptions()
  50. {
  51. Path = "lucene"
  52. };
  53. var memoryCache = new MemoryCache(new MemoryCacheOptions());
  54. var directory = FSDirectory.Open(options.Path);
  55. JieBaAnalyzer analyzer = new JieBaAnalyzer(TokenizerMode.Search);
  56. SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(_context, directory, analyzer, memoryCache);
  57. searchProvider.CreateIndex();
  58. SearchOptions searchOptions = new SearchOptions("John", "FirstName");
  59. var results = searchProvider.ScoredSearch<User>(searchOptions);
  60. Assert.Equal(5, results.TotalHits);
  61. searchProvider.DeleteIndex();
  62. }
  63. [Fact]
  64. public void SkipAndTakeWorkWhenSearchingUsingAContextProvider()
  65. {
  66. InitializeContext();
  67. LuceneIndexerOptions options = new LuceneIndexerOptions()
  68. {
  69. Path = "lucene"
  70. };
  71. var memoryCache = new MemoryCache(new MemoryCacheOptions());
  72. var directory = FSDirectory.Open(options.Path);
  73. JieBaAnalyzer analyzer = new JieBaAnalyzer(TokenizerMode.Search);
  74. SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(_context, directory, analyzer, memoryCache);
  75. searchProvider.CreateIndex();
  76. SearchOptions searchOptions = new SearchOptions("John", "FirstName");
  77. var initialResults = searchProvider.ScoredSearch<User>(searchOptions);
  78. var lastId = initialResults.Results[4].Entity.Id;
  79. Assert.Equal(5, initialResults.TotalHits);
  80. Assert.Equal(5, initialResults.Results.Count);
  81. searchOptions.Skip = 4;
  82. searchOptions.Take = 1;
  83. var subResults = searchProvider.ScoredSearch<User>(searchOptions);
  84. Assert.Equal(5, subResults.TotalHits);
  85. Assert.Equal(1, subResults.Results.Count);
  86. Assert.Equal(lastId, subResults.Results.First().Entity.Id);
  87. searchProvider.DeleteIndex();
  88. }
  89. [Fact]
  90. public void AContextCanBeSearchedUsingAWildCard()
  91. {
  92. InitializeContext();
  93. LuceneIndexerOptions options = new LuceneIndexerOptions()
  94. {
  95. Path = "lucene"
  96. };
  97. var memoryCache = new MemoryCache(new MemoryCacheOptions());
  98. var directory = FSDirectory.Open(options.Path);
  99. JieBaAnalyzer analyzer = new JieBaAnalyzer(TokenizerMode.Search);
  100. SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(_context, directory, analyzer, memoryCache);
  101. searchProvider.CreateIndex();
  102. SearchOptions searchOptions = new SearchOptions("Joh*", "FirstName");
  103. // test
  104. var results = searchProvider.ScoredSearch<User>(searchOptions);
  105. PrintResult(results);
  106. Assert.Equal(10, results.TotalHits);
  107. searchProvider.DeleteIndex();
  108. }
  109. [Fact]
  110. public void ASearchWillReturnTheSameResultsAsAScoredSearch()
  111. {
  112. InitializeContext();
  113. LuceneIndexerOptions options = new LuceneIndexerOptions()
  114. {
  115. Path = "lucene"
  116. };
  117. var memoryCache = new MemoryCache(new MemoryCacheOptions());
  118. var directory = FSDirectory.Open(options.Path);
  119. JieBaAnalyzer analyzer = new JieBaAnalyzer(TokenizerMode.Search);
  120. SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(_context, directory, analyzer, memoryCache);
  121. searchProvider.CreateIndex();
  122. SearchOptions searchOptions = new SearchOptions("Joh*", "FirstName");
  123. // test
  124. var results = searchProvider.Search<User>(searchOptions);
  125. Assert.Equal(10, results.TotalHits);
  126. searchProvider.DeleteIndex();
  127. }
  128. [Fact]
  129. public void AScoredSearchWillOrderByRelevence()
  130. {
  131. InitializeContext();
  132. LuceneIndexerOptions options = new LuceneIndexerOptions()
  133. {
  134. Path = "lucene"
  135. };
  136. var memoryCache = new MemoryCache(new MemoryCacheOptions());
  137. var directory = FSDirectory.Open(options.Path);
  138. JieBaAnalyzer analyzer = new JieBaAnalyzer(TokenizerMode.Search);
  139. SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(_context, directory, analyzer, memoryCache);
  140. searchProvider.CreateIndex();
  141. SearchOptions searchOptions = new SearchOptions("Burns", "FirstName,Surname");
  142. var results = searchProvider.ScoredSearch<User>(searchOptions);
  143. var first = results.Results.First().Entity;
  144. var highest = results.Results.First().Score;
  145. var lowest = results.Results.Last().Score;
  146. Assert.True(highest > lowest);
  147. Assert.Equal("Jeremy", first.FirstName);
  148. Assert.Equal("Burns", first.Surname);
  149. searchProvider.DeleteIndex();
  150. }
  151. [Fact]
  152. public void ASearchWillStillOrderByRelevence()
  153. {
  154. InitializeContext();
  155. LuceneIndexerOptions options = new LuceneIndexerOptions()
  156. {
  157. Path = "lucene"
  158. };
  159. var memoryCache = new MemoryCache(new MemoryCacheOptions());
  160. var directory = FSDirectory.Open(options.Path);
  161. JieBaAnalyzer analyzer = new JieBaAnalyzer(TokenizerMode.Search);
  162. SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(_context, directory, analyzer, memoryCache);
  163. searchProvider.CreateIndex();
  164. SearchOptions searchOptions = new SearchOptions("Jeremy Burns", "FirstName,Surname");
  165. var results = searchProvider.Search<User>(searchOptions);
  166. var first = results.Results.First();
  167. Assert.Equal("Jeremy", first.FirstName);
  168. Assert.Equal("Burns", first.Surname);
  169. searchProvider.DeleteIndex();
  170. }
  171. [Fact]
  172. public void ASearchCanOrderByMultipleFields()
  173. {
  174. InitializeContext();
  175. LuceneIndexerOptions options = new LuceneIndexerOptions()
  176. {
  177. Path = "lucene"
  178. };
  179. var memoryCache = new MemoryCache(new MemoryCacheOptions());
  180. var directory = FSDirectory.Open(options.Path);
  181. JieBaAnalyzer analyzer = new JieBaAnalyzer(TokenizerMode.Search);
  182. SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(_context, directory, analyzer, memoryCache);
  183. User jc = new User()
  184. {
  185. FirstName = "John",
  186. Surname = "Chapman",
  187. JobTitle = "Test Engineer",
  188. Email = "[email protected]"
  189. };
  190. _context.Users.Add(jc);
  191. _context.SaveChanges();
  192. searchProvider.CreateIndex();
  193. SearchOptions search = new SearchOptions("John", "FirstName", 1000, null, null, "Surname,JobTitle");
  194. var results = searchProvider.ScoredSearch<User>(search);
  195. var topResult = results.Results[0];
  196. var secondResult = results.Results[1];
  197. Assert.Equal("Sales Associate", topResult.Entity.JobTitle);
  198. Assert.Equal("Test Engineer", secondResult.Entity.JobTitle);
  199. searchProvider.DeleteIndex();
  200. }
  201. [Fact]
  202. public void SaveChangesUpdatesEntitiesAddedToTheIndex()
  203. {
  204. InitializeContext();
  205. LuceneIndexerOptions options = new LuceneIndexerOptions()
  206. {
  207. Path = "lucene"
  208. };
  209. var memoryCache = new MemoryCache(new MemoryCacheOptions());
  210. var directory = FSDirectory.Open(options.Path);
  211. JieBaAnalyzer analyzer = new JieBaAnalyzer(TokenizerMode.Search);
  212. SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(_context, directory, analyzer, memoryCache);
  213. searchProvider.CreateIndex();
  214. var newUser = new User()
  215. {
  216. FirstName = "Duke",
  217. Surname = "Nukem",
  218. Email = "[email protected]",
  219. JobTitle = "Shooty Man"
  220. };
  221. var search = new SearchOptions("Nukem", "Surname");
  222. var initialResults = searchProvider.Search<User>(search);
  223. searchProvider.Context.Users.Add(newUser);
  224. searchProvider.SaveChanges();
  225. var newResults = searchProvider.Search<User>(search);
  226. Assert.Equal(0, initialResults.TotalHits);
  227. Assert.Equal(1, newResults.TotalHits);
  228. Assert.Equal(newUser.Id, newResults.Results[0].Id);
  229. }
  230. [Fact]
  231. public void NonValidEntitiesAreIgnored()
  232. {
  233. InitializeContext();
  234. LuceneIndexerOptions options = new LuceneIndexerOptions()
  235. {
  236. Path = "lucene"
  237. };
  238. var memoryCache = new MemoryCache(new MemoryCacheOptions());
  239. var directory = FSDirectory.Open(options.Path);
  240. JieBaAnalyzer analyzer = new JieBaAnalyzer(TokenizerMode.Search);
  241. SearchEngine<TestDbContext> searchProvider = new SearchEngine<TestDbContext>(_context, directory, analyzer, memoryCache);
  242. searchProvider.CreateIndex();
  243. Assert.True(searchProvider.IndexCount > 0);
  244. }
  245. private void PrintResult(IScoredSearchResultCollection<User> results)
  246. {
  247. _output.WriteLine($"总条数: {results.TotalHits}\t耗时: {results.Elapsed}");
  248. foreach (IScoredSearchResult<User> item in results.Results)
  249. {
  250. _output.WriteLine($"匹配度: {item.Score}\tName:{item.Entity.FirstName}\tSurname: {item.Entity.Surname}\tEmail: {item.Entity.Email}");
  251. }
  252. }
  253. }
  254. }