Guest User

Untitled

a guest
Oct 9th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. using NUnit.Framework;
  6. using SharpTestsEx;
  7.  
  8. using Lucene.Net.Analysis;
  9. using Lucene.Net.Store;
  10. using Lucene.Net.Index;
  11. using Lucene.Net.Documents;
  12. using Lucene.Net.Search;
  13.  
  14. namespace Learning.Lucene
  15. {
  16. [TestFixture]
  17. public class BasicOperationsTests
  18. {
  19. class Person
  20. {
  21. public string Id { get; set; }
  22. public string Name { get; set; }
  23. public string Bio { get; set; }
  24. public string Email { get; set; }
  25.  
  26. public Document ToDocument()
  27. {
  28. var result = new Document();
  29. result.Add(new Field(
  30. "id", Id,
  31. Field.Store.YES,
  32. Field.Index.NOT_ANALYZED
  33. ));
  34.  
  35. result.Add(new Field(
  36. "name", Name,
  37. Field.Store.YES,
  38. Field.Index.NO
  39. ));
  40.  
  41. result.Add(new Field(
  42. "bio", Bio,
  43. Field.Store.NO,
  44. Field.Index.ANALYZED
  45. ));
  46.  
  47. result.Add(new Field(
  48. "email", Email,
  49. Field.Store.YES,
  50. Field.Index.ANALYZED
  51. ));
  52.  
  53. return result;
  54. }
  55. }
  56.  
  57.  
  58. private IEnumerable<Person> people = new[]
  59. {
  60. new Person
  61. {
  62. Id = "1",
  63. Name = "Elemar Junior",
  64. Bio = "Pai, Programador e Louco",
  65. Email = "elemarjr@gmail.com"
  66. },
  67. new Person
  68. {
  69. Id = "2",
  70. Name = "John Doe",
  71. Bio = "Something like that. Whaever?!",
  72. Email = "johndoe@gmail.com"
  73. }
  74. };
  75.  
  76. private Directory directory;
  77.  
  78. [SetUp]
  79. public void Init()
  80. {
  81. directory = new RAMDirectory();
  82. var writer = GetWriter();
  83. foreach (var person in people)
  84. writer.AddDocument(person.ToDocument());
  85. writer.Close();
  86. }
  87.  
  88. public IndexWriter GetWriter()
  89. {
  90. return new IndexWriter(
  91. directory,
  92. new WhitespaceAnalyzer(),
  93. IndexWriter.MaxFieldLength.UNLIMITED
  94. );
  95. }
  96.  
  97. [Test]
  98. public void TestIndexWriter()
  99. {
  100. var writer = GetWriter();
  101. writer.NumDocs().Should().Be(people.Count());
  102. }
  103.  
  104. [Test]
  105. public void TestIndexReader()
  106. {
  107. var reader = IndexReader.Open(directory);
  108.  
  109. reader.NumDocs().Should().Be(people.Count());
  110. reader.MaxDoc().Should().Be(people.Count());
  111.  
  112. reader.Close();
  113. }
  114.  
  115. [Test]
  116. public void TestDeleteBeforeOptimize()
  117. {
  118. var writer = GetWriter();
  119. writer.NumDocs().Should().Be(people.Count());
  120.  
  121. writer.DeleteDocuments(new Term("id", "1"));
  122. writer.Commit();
  123.  
  124. writer.HasDeletions().Should().Be.True();
  125. writer.MaxDoc().Should().Be(people.Count());
  126. writer.NumDocs().Should().Be(people.Count() - 1);
  127.  
  128. writer.Close();
  129. }
  130.  
  131. [Test]
  132. public void TestDeleteAfterOptimize()
  133. {
  134. var writer = GetWriter();
  135. writer.NumDocs().Should().Be(people.Count());
  136.  
  137. writer.DeleteDocuments(new Term("id", "1"));
  138. writer.Optimize();
  139. writer.Commit();
  140.  
  141. writer.HasDeletions().Should().Be.False();
  142. writer.MaxDoc().Should().Be(people.Count() - 1);
  143. writer.NumDocs().Should().Be(people.Count() - 1);
  144.  
  145. writer.Close();
  146. }
  147.  
  148. public bool EmailExists(string email)
  149. {
  150. var searcher = new IndexSearcher(directory, true);
  151. var term = new Term("email", email);
  152. var query = new TermQuery(term);
  153. var result = searcher.Search(query, 1).TotalHits > 0;
  154. searcher.Close();
  155. return result;
  156. }
  157.  
  158. [Test]
  159. public void TestFind()
  160. {
  161. EmailExists("elemarjr@gmail.com").Should().Be.True();
  162. }
  163.  
  164. [Test]
  165. public void TestUpdate()
  166. {
  167. EmailExists("elemarjr@gmail.com").Should().Be.True();
  168.  
  169. var newPerson = new Person
  170. {
  171. Id = "1",
  172. Name = "Elemar Junior",
  173. Bio = "Pai, Programador e Louco",
  174. Email = "elemar@promob.com"
  175. };
  176.  
  177. var writer = GetWriter();
  178. writer.UpdateDocument(
  179. new Term("id", "1"),
  180. newPerson.ToDocument()
  181. );
  182.  
  183. writer.Close();
  184.  
  185. EmailExists("elemarjr@gmail.com").Should().Be.False();
  186. EmailExists("elemar@promob.com").Should().Be.True();
  187. }
  188. }
  189. }
Add Comment
Please, Sign In to add comment