/Testing/unittest/Query/FullText/IndexSearchTests.cs

http://dotnetrdf.codeplex.com · C# · 460 lines · 385 code · 51 blank · 24 comment · 48 complexity · 7b511fb0108058eb30bdf11f85d7550b MD5 · raw file

  1. /*
  2. dotNetRDF is free and open source software licensed under the MIT License
  3. -----------------------------------------------------------------------------
  4. Copyright (c) 2009-2012 dotNetRDF Project (dotnetrdf-developer@lists.sf.net)
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is furnished
  10. to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in all
  12. copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  17. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  18. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. */
  20. using System;
  21. using System.Text;
  22. using System.Collections.Generic;
  23. using System.Linq;
  24. using Microsoft.VisualStudio.TestTools.UnitTesting;
  25. using VDS.RDF.Query;
  26. using VDS.RDF.Query.FullText;
  27. using VDS.RDF.Query.FullText.Indexing;
  28. using VDS.RDF.Query.FullText.Indexing.Lucene;
  29. using VDS.RDF.Query.FullText.Search;
  30. using VDS.RDF.Query.FullText.Search.Lucene;
  31. using VDS.RDF.Writing.Formatting;
  32. namespace VDS.RDF.Query.FullText
  33. {
  34. [TestClass]
  35. public class IndexSearchTests
  36. {
  37. private IGraph GetTestData()
  38. {
  39. Graph g = new Graph();
  40. g.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl");
  41. return g;
  42. }
  43. [TestMethod]
  44. public void FullTextIndexSearchLuceneObjects()
  45. {
  46. IFullTextIndexer indexer = null;
  47. IFullTextSearchProvider provider = null;
  48. try
  49. {
  50. indexer = new LuceneObjectsIndexer(LuceneTestHarness.Index, LuceneTestHarness.Analyzer, LuceneTestHarness.Schema);
  51. indexer.Index(this.GetTestData());
  52. }
  53. finally
  54. {
  55. if (indexer != null) indexer.Dispose();
  56. }
  57. try
  58. {
  59. provider = new LuceneSearchProvider(LuceneTestHarness.LuceneVersion, LuceneTestHarness.Index);
  60. NTriplesFormatter formatter = new NTriplesFormatter();
  61. foreach (IFullTextSearchResult result in provider.Match("http"))
  62. {
  63. Console.WriteLine(result.Node.ToString(formatter) + " - Scores " + result.Score);
  64. }
  65. }
  66. finally
  67. {
  68. if (provider != null) provider.Dispose();
  69. }
  70. }
  71. [TestMethod]
  72. public void FullTextIndexSearchLuceneObjectsWithLimit()
  73. {
  74. IFullTextIndexer indexer = null;
  75. IFullTextSearchProvider provider = null;
  76. try
  77. {
  78. indexer = new LuceneObjectsIndexer(LuceneTestHarness.Index, LuceneTestHarness.Analyzer, LuceneTestHarness.Schema);
  79. indexer.Index(this.GetTestData());
  80. }
  81. finally
  82. {
  83. if (indexer != null) indexer.Dispose();
  84. }
  85. try
  86. {
  87. provider = new LuceneSearchProvider(LuceneTestHarness.LuceneVersion, LuceneTestHarness.Index);
  88. NTriplesFormatter formatter = new NTriplesFormatter();
  89. int i = 0;
  90. foreach (IFullTextSearchResult result in provider.Match("http", 5))
  91. {
  92. i++;
  93. Console.WriteLine(result.Node.ToString(formatter) + " - Scores " + result.Score);
  94. }
  95. Assert.IsTrue(i <= 5, "Should be a max of 5 results");
  96. }
  97. finally
  98. {
  99. if (provider != null) provider.Dispose();
  100. }
  101. }
  102. [TestMethod]
  103. public void FullTextIndexSearchLuceneObjectsWithThreshold()
  104. {
  105. IFullTextIndexer indexer = null;
  106. IFullTextSearchProvider provider = null;
  107. try
  108. {
  109. indexer = new LuceneObjectsIndexer(LuceneTestHarness.Index, LuceneTestHarness.Analyzer, LuceneTestHarness.Schema);
  110. indexer.Index(this.GetTestData());
  111. }
  112. finally
  113. {
  114. if (indexer != null) indexer.Dispose();
  115. }
  116. try
  117. {
  118. provider = new LuceneSearchProvider(LuceneTestHarness.LuceneVersion, LuceneTestHarness.Index);
  119. NTriplesFormatter formatter = new NTriplesFormatter();
  120. foreach (IFullTextSearchResult result in provider.Match("http", 0.75d))
  121. {
  122. Console.WriteLine(result.Node.ToString(formatter) + " - Scores " + result.Score);
  123. Assert.IsTrue(result.Score >= 0.75d, "Score should be higher than desired threshold");
  124. }
  125. }
  126. finally
  127. {
  128. if (provider != null) provider.Dispose();
  129. }
  130. }
  131. [TestMethod]
  132. public void FullTextIndexSearchLuceneObjectsWithLimitAndThreshold()
  133. {
  134. IFullTextIndexer indexer = null;
  135. IFullTextSearchProvider provider = null;
  136. try
  137. {
  138. indexer = new LuceneObjectsIndexer(LuceneTestHarness.Index, LuceneTestHarness.Analyzer, LuceneTestHarness.Schema);
  139. indexer.Index(this.GetTestData());
  140. }
  141. finally
  142. {
  143. if (indexer != null) indexer.Dispose();
  144. }
  145. try
  146. {
  147. provider = new LuceneSearchProvider(LuceneTestHarness.LuceneVersion, LuceneTestHarness.Index);
  148. NTriplesFormatter formatter = new NTriplesFormatter();
  149. int i = 0;
  150. foreach (IFullTextSearchResult result in provider.Match("http", 1.0d, 5))
  151. {
  152. i++;
  153. Console.WriteLine(result.Node.ToString(formatter) + " - Scores " + result.Score);
  154. Assert.IsTrue(result.Score >= 1.0d, "Score should be higher than desired threshold");
  155. }
  156. Assert.IsTrue(i <= 5, "Should be a max of 5 results");
  157. }
  158. finally
  159. {
  160. if (provider != null) provider.Dispose();
  161. }
  162. }
  163. [TestMethod]
  164. public void FullTextIndexSearchLuceneSubjects()
  165. {
  166. IFullTextIndexer indexer = null;
  167. IFullTextSearchProvider provider = null;
  168. try
  169. {
  170. indexer = new LuceneSubjectsIndexer(LuceneTestHarness.Index, LuceneTestHarness.Analyzer, LuceneTestHarness.Schema);
  171. indexer.Index(this.GetTestData());
  172. }
  173. finally
  174. {
  175. if (indexer != null) indexer.Dispose();
  176. }
  177. try
  178. {
  179. provider = new LuceneSearchProvider(LuceneTestHarness.LuceneVersion, LuceneTestHarness.Index);
  180. NTriplesFormatter formatter = new NTriplesFormatter();
  181. foreach (IFullTextSearchResult result in provider.Match("http"))
  182. {
  183. Console.WriteLine(result.Node.ToString(formatter) + " - Scores " + result.Score);
  184. }
  185. }
  186. finally
  187. {
  188. if (provider != null) provider.Dispose();
  189. }
  190. }
  191. [TestMethod]
  192. public void FullTextIndexSearchLuceneSubjectsWithLimit()
  193. {
  194. IFullTextIndexer indexer = null;
  195. IFullTextSearchProvider provider = null;
  196. try
  197. {
  198. indexer = new LuceneSubjectsIndexer(LuceneTestHarness.Index, LuceneTestHarness.Analyzer, LuceneTestHarness.Schema);
  199. indexer.Index(this.GetTestData());
  200. }
  201. finally
  202. {
  203. if (indexer != null) indexer.Dispose();
  204. }
  205. try
  206. {
  207. provider = new LuceneSearchProvider(LuceneTestHarness.LuceneVersion, LuceneTestHarness.Index);
  208. NTriplesFormatter formatter = new NTriplesFormatter();
  209. int i = 0;
  210. foreach (IFullTextSearchResult result in provider.Match("http", 5))
  211. {
  212. i++;
  213. Console.WriteLine(result.Node.ToString(formatter) + " - Scores " + result.Score);
  214. }
  215. Assert.IsTrue(i <= 5, "Should be a max of 5 results");
  216. }
  217. finally
  218. {
  219. if (provider != null) provider.Dispose();
  220. }
  221. }
  222. [TestMethod]
  223. public void FullTextIndexSearchLuceneSubjectsWithThreshold()
  224. {
  225. IFullTextIndexer indexer = null;
  226. IFullTextSearchProvider provider = null;
  227. try
  228. {
  229. indexer = new LuceneSubjectsIndexer(LuceneTestHarness.Index, LuceneTestHarness.Analyzer, LuceneTestHarness.Schema);
  230. indexer.Index(this.GetTestData());
  231. }
  232. finally
  233. {
  234. if (indexer != null) indexer.Dispose();
  235. }
  236. try
  237. {
  238. provider = new LuceneSearchProvider(LuceneTestHarness.LuceneVersion, LuceneTestHarness.Index);
  239. NTriplesFormatter formatter = new NTriplesFormatter();
  240. foreach (IFullTextSearchResult result in provider.Match("http", 0.75d))
  241. {
  242. Console.WriteLine(result.Node.ToString(formatter) + " - Scores " + result.Score);
  243. Assert.IsTrue(result.Score >= 0.75d, "Score should be higher than desired threshold");
  244. }
  245. }
  246. finally
  247. {
  248. if (provider != null) provider.Dispose();
  249. }
  250. }
  251. [TestMethod]
  252. public void FullTextIndexSearchLuceneSubjectsWithLimitAndThreshold()
  253. {
  254. IFullTextIndexer indexer = null;
  255. IFullTextSearchProvider provider = null;
  256. try
  257. {
  258. indexer = new LuceneSubjectsIndexer(LuceneTestHarness.Index, LuceneTestHarness.Analyzer, LuceneTestHarness.Schema);
  259. indexer.Index(this.GetTestData());
  260. }
  261. finally
  262. {
  263. if (indexer != null) indexer.Dispose();
  264. }
  265. try
  266. {
  267. provider = new LuceneSearchProvider(LuceneTestHarness.LuceneVersion, LuceneTestHarness.Index);
  268. NTriplesFormatter formatter = new NTriplesFormatter();
  269. int i = 0;
  270. foreach (IFullTextSearchResult result in provider.Match("http", 1.0d, 5))
  271. {
  272. i++;
  273. Console.WriteLine(result.Node.ToString(formatter) + " - Scores " + result.Score);
  274. Assert.IsTrue(result.Score >= 1.0d, "Score should be higher than desired threshold");
  275. }
  276. Assert.IsTrue(i <= 5, "Should be a max of 5 results");
  277. }
  278. finally
  279. {
  280. if (provider != null) provider.Dispose();
  281. }
  282. }
  283. [TestMethod]
  284. public void FullTextIndexSearchLucenePredicates()
  285. {
  286. IFullTextIndexer indexer = null;
  287. IFullTextSearchProvider provider = null;
  288. try
  289. {
  290. indexer = new LucenePredicatesIndexer(LuceneTestHarness.Index, LuceneTestHarness.Analyzer, LuceneTestHarness.Schema);
  291. indexer.Index(this.GetTestData());
  292. }
  293. finally
  294. {
  295. if (indexer != null) indexer.Dispose();
  296. }
  297. try
  298. {
  299. provider = new LuceneSearchProvider(LuceneTestHarness.LuceneVersion, LuceneTestHarness.Index);
  300. NTriplesFormatter formatter = new NTriplesFormatter();
  301. foreach (IFullTextSearchResult result in provider.Match("http"))
  302. {
  303. Console.WriteLine(result.Node.ToString(formatter) + " - Scores " + result.Score);
  304. }
  305. }
  306. finally
  307. {
  308. if (provider != null) provider.Dispose();
  309. }
  310. }
  311. [TestMethod]
  312. public void FullTextIndexSearchLucenePredicatesWithLimit()
  313. {
  314. IFullTextIndexer indexer = null;
  315. IFullTextSearchProvider provider = null;
  316. try
  317. {
  318. indexer = new LucenePredicatesIndexer(LuceneTestHarness.Index, LuceneTestHarness.Analyzer, LuceneTestHarness.Schema);
  319. indexer.Index(this.GetTestData());
  320. }
  321. finally
  322. {
  323. if (indexer != null) indexer.Dispose();
  324. }
  325. try
  326. {
  327. provider = new LuceneSearchProvider(LuceneTestHarness.LuceneVersion, LuceneTestHarness.Index);
  328. NTriplesFormatter formatter = new NTriplesFormatter();
  329. int i = 0;
  330. foreach (IFullTextSearchResult result in provider.Match("http", 5))
  331. {
  332. i++;
  333. Console.WriteLine(result.Node.ToString(formatter) + " - Scores " + result.Score);
  334. }
  335. Assert.IsTrue(i <= 5, "Should be a max of 5 results");
  336. }
  337. finally
  338. {
  339. if (provider != null) provider.Dispose();
  340. }
  341. }
  342. [TestMethod]
  343. public void FullTextIndexSearchLucenePredicatesWithThreshold()
  344. {
  345. IFullTextIndexer indexer = null;
  346. IFullTextSearchProvider provider = null;
  347. try
  348. {
  349. indexer = new LucenePredicatesIndexer(LuceneTestHarness.Index, LuceneTestHarness.Analyzer, LuceneTestHarness.Schema);
  350. indexer.Index(this.GetTestData());
  351. }
  352. finally
  353. {
  354. if (indexer != null) indexer.Dispose();
  355. }
  356. try
  357. {
  358. provider = new LuceneSearchProvider(LuceneTestHarness.LuceneVersion, LuceneTestHarness.Index);
  359. NTriplesFormatter formatter = new NTriplesFormatter();
  360. foreach (IFullTextSearchResult result in provider.Match("http", 0.75d))
  361. {
  362. Console.WriteLine(result.Node.ToString(formatter) + " - Scores " + result.Score);
  363. Assert.IsTrue(result.Score >= 0.75d, "Score should be higher than desired threshold");
  364. }
  365. }
  366. finally
  367. {
  368. if (provider != null) provider.Dispose();
  369. }
  370. }
  371. [TestMethod]
  372. public void FullTextIndexSearchLucenePredicatesWithLimitAndThreshold()
  373. {
  374. IFullTextIndexer indexer = null;
  375. IFullTextSearchProvider provider = null;
  376. try
  377. {
  378. indexer = new LucenePredicatesIndexer(LuceneTestHarness.Index, LuceneTestHarness.Analyzer, LuceneTestHarness.Schema);
  379. indexer.Index(this.GetTestData());
  380. }
  381. finally
  382. {
  383. if (indexer != null) indexer.Dispose();
  384. }
  385. try
  386. {
  387. provider = new LuceneSearchProvider(LuceneTestHarness.LuceneVersion, LuceneTestHarness.Index);
  388. NTriplesFormatter formatter = new NTriplesFormatter();
  389. int i = 0;
  390. foreach (IFullTextSearchResult result in provider.Match("http", 1.0d, 5))
  391. {
  392. i++;
  393. Console.WriteLine(result.Node.ToString(formatter) + " - Scores " + result.Score);
  394. Assert.IsTrue(result.Score >= 1.0d, "Score should be higher than desired threshold");
  395. }
  396. Assert.IsTrue(i <= 5, "Should be a max of 5 results");
  397. }
  398. finally
  399. {
  400. if (provider != null) provider.Dispose();
  401. }
  402. }
  403. }
  404. }