PageRenderTime 58ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/Raven.Tests/Querying/UsingDynamicQueryWithLocalServer.cs

https://github.com/jalchr/ravendb
C# | 552 lines | 496 code | 51 blank | 5 comment | 10 complexity | 45def5d8ce025d8c6dfa71bd1bc4d8a6 MD5 | raw file
  1. //-----------------------------------------------------------------------
  2. // <copyright file="UsingDynamicQueryWithLocalServer.cs" company="Hibernating Rhinos LTD">
  3. // Copyright (c) Hibernating Rhinos LTD. All rights reserved.
  4. // </copyright>
  5. //-----------------------------------------------------------------------
  6. using System;
  7. using System.Linq;
  8. using Raven.Abstractions.Indexing;
  9. using Raven.Client;
  10. using Xunit;
  11. namespace Raven.Tests.Querying
  12. {
  13. public class UsingDynamicQueryWithLocalServer : RavenTest
  14. {
  15. [Fact]
  16. public void CanPerformDynamicQueryUsingClientLinqQueryWithNestedCollection()
  17. {
  18. var blogOne = new Blog
  19. {
  20. Title = "one",
  21. Category = "Ravens",
  22. Tags = new BlogTag[]{
  23. new BlogTag(){ Name = "Birds" }
  24. }
  25. };
  26. var blogTwo = new Blog
  27. {
  28. Title = "two",
  29. Category = "Rhinos",
  30. Tags = new BlogTag[]{
  31. new BlogTag(){ Name = "Mammals" }
  32. }
  33. };
  34. var blogThree = new Blog
  35. {
  36. Title = "three",
  37. Category = "Rhinos",
  38. Tags = new BlogTag[]{
  39. new BlogTag(){ Name = "Mammals" }
  40. }
  41. };
  42. using(var store = this.NewDocumentStore())
  43. {
  44. using (var s = store.OpenSession())
  45. {
  46. s.Store(blogOne);
  47. s.Store(blogTwo);
  48. s.Store(blogThree);
  49. s.SaveChanges();
  50. }
  51. using (var s = store.OpenSession())
  52. {
  53. var results = s.Query<Blog>()
  54. .Customize(x => x.WaitForNonStaleResultsAsOfNow(TimeSpan.FromSeconds(5)))
  55. .Where(x => x.Tags.Any(y=>y.Name == "Birds"))
  56. .ToArray();
  57. Assert.Equal(1, results.Length);
  58. Assert.Equal("one", results[0].Title);
  59. Assert.Equal("Ravens", results[0].Category);
  60. }
  61. }
  62. }
  63. [Fact]
  64. public void CanPerformDynamicQueryUsingClientLinqQuery()
  65. {
  66. var blogOne = new Blog
  67. {
  68. Title = "one",
  69. Category = "Ravens"
  70. };
  71. var blogTwo = new Blog
  72. {
  73. Title = "two",
  74. Category = "Rhinos"
  75. };
  76. var blogThree = new Blog
  77. {
  78. Title = "three",
  79. Category = "Rhinos"
  80. };
  81. using(var store = this.NewDocumentStore())
  82. {
  83. using (var s = store.OpenSession())
  84. {
  85. s.Store(blogOne);
  86. s.Store(blogTwo);
  87. s.Store(blogThree);
  88. s.SaveChanges();
  89. }
  90. using (var s = store.OpenSession())
  91. {
  92. var results = s.Query<Blog>()
  93. .Customize(x => x.WaitForNonStaleResultsAsOfNow())
  94. .Where(x => x.Category == "Rhinos" && x.Title.Length == 3)
  95. .ToArray();
  96. Assert.Equal(1, results.Length);
  97. Assert.Equal("two", results[0].Title);
  98. Assert.Equal("Rhinos", results[0].Category);
  99. }
  100. }
  101. }
  102. [Fact]
  103. public void QueryForASpecificTypeDoesNotBringBackOtherTypes()
  104. {
  105. using (var store = this.NewDocumentStore())
  106. {
  107. using (var s = store.OpenSession())
  108. {
  109. s.Store(new BlogTag());
  110. s.SaveChanges();
  111. }
  112. using (var s = store.OpenSession())
  113. {
  114. var results = s.Query<Blog>()
  115. .Select(b=> new { b.Category})
  116. .ToArray();
  117. Assert.Equal(0, results.Length);
  118. }
  119. }
  120. }
  121. [Fact]
  122. public void CanPerformDynamicQueryUsingClientLuceneQuery()
  123. {
  124. var blogOne = new Blog
  125. {
  126. Title = "one",
  127. Category = "Ravens"
  128. };
  129. var blogTwo = new Blog
  130. {
  131. Title = "two",
  132. Category = "Rhinos"
  133. };
  134. var blogThree = new Blog
  135. {
  136. Title = "three",
  137. Category = "Rhinos"
  138. };
  139. using (var store = this.NewDocumentStore())
  140. {
  141. using (var s = store.OpenSession())
  142. {
  143. s.Store(blogOne);
  144. s.Store(blogTwo);
  145. s.Store(blogThree);
  146. s.SaveChanges();
  147. }
  148. using (var s = store.OpenSession())
  149. {
  150. var results = s.Advanced.LuceneQuery<Blog>()
  151. .Where("Title.Length:3 AND Category:Rhinos")
  152. .WaitForNonStaleResultsAsOfNow().ToArray();
  153. Assert.Equal(1, results.Length);
  154. Assert.Equal("two", results[0].Title);
  155. Assert.Equal("Rhinos", results[0].Category);
  156. }
  157. }
  158. }
  159. [Fact]
  160. public void CanPerformDynamicQueryWithHighlightingUsingClientLuceneQuery()
  161. {
  162. var blogOne = new Blog
  163. {
  164. Title = "Lorem ipsum dolor sit amet, target word, consectetur adipiscing elit.",
  165. Category = "Ravens"
  166. };
  167. var blogTwo = new Blog
  168. {
  169. Title = "Maecenas mauris leo, feugiat sodales facilisis target word, pellentesque, suscipit aliquet turpis.",
  170. Category = "The Rhinos"
  171. };
  172. var blogThree = new Blog
  173. {
  174. Title = "Target cras vitae felis arcu word.",
  175. Category = "Los Rhinos"
  176. };
  177. using (var store = this.NewDocumentStore())
  178. {
  179. string blogOneId;
  180. string blogTwoId;
  181. using (var s = store.OpenSession())
  182. {
  183. s.Store(blogOne);
  184. s.Store(blogTwo);
  185. s.Store(blogThree);
  186. s.SaveChanges();
  187. blogOneId = s.Advanced.GetDocumentId(blogOne);
  188. blogTwoId = s.Advanced.GetDocumentId(blogTwo);
  189. }
  190. using (var s = store.OpenSession())
  191. {
  192. FieldHighlightings titleHighlightings;
  193. FieldHighlightings categoryHighlightings;
  194. var results = s.Advanced.LuceneQuery<Blog>()
  195. .Highlight("Title", 18, 2, out titleHighlightings)
  196. .Highlight("Category", 18, 2, out categoryHighlightings)
  197. .SetHighlighterTags("*","*")
  198. .Where("Title:(target word) OR Category:rhinos")
  199. .WaitForNonStaleResultsAsOfNow()
  200. .ToArray();
  201. Assert.Equal(3, results.Length);
  202. Assert.NotEmpty(titleHighlightings.GetFragments(blogOneId));
  203. Assert.Empty(categoryHighlightings.GetFragments(blogOneId));
  204. Assert.NotEmpty(titleHighlightings.GetFragments(blogTwoId));
  205. Assert.NotEmpty(categoryHighlightings.GetFragments(blogTwoId));
  206. }
  207. }
  208. }
  209. [Fact]
  210. public void CanPerformDynamicQueryWithHighlighting()
  211. {
  212. var blogOne = new Blog
  213. {
  214. Title = "Lorem ipsum dolor sit amet, target word, consectetur adipiscing elit.",
  215. Category = "Ravens"
  216. };
  217. var blogTwo = new Blog
  218. {
  219. Title = "Maecenas mauris leo, feugiat sodales facilisis target word, pellentesque, suscipit aliquet turpis.",
  220. Category = "The Rhinos"
  221. };
  222. var blogThree = new Blog
  223. {
  224. Title = "Target cras vitae felis arcu word.",
  225. Category = "Los Rhinos"
  226. };
  227. using (var store = this.NewDocumentStore())
  228. {
  229. string blogOneId;
  230. string blogTwoId;
  231. using (var s = store.OpenSession())
  232. {
  233. s.Store(blogOne);
  234. s.Store(blogTwo);
  235. s.Store(blogThree);
  236. s.SaveChanges();
  237. blogOneId = s.Advanced.GetDocumentId(blogOne);
  238. blogTwoId = s.Advanced.GetDocumentId(blogTwo);
  239. }
  240. using (var s = store.OpenSession())
  241. {
  242. FieldHighlightings titleHighlightings = null;
  243. FieldHighlightings categoryHighlightings = null;
  244. var results = s.Query<Blog>()
  245. .Customize(
  246. c =>
  247. c.Highlight("Title", 18, 2, out titleHighlightings)
  248. .Highlight("Category", 18, 2, out categoryHighlightings)
  249. .SetHighlighterTags("*", "*")
  250. .WaitForNonStaleResultsAsOfNow())
  251. .Search(x => x.Category, "rhinos")
  252. .Search(x => x.Title, "target word")
  253. .ToArray();
  254. Assert.Equal(3, results.Length);
  255. Assert.NotEmpty(titleHighlightings.GetFragments(blogOneId));
  256. Assert.Empty(categoryHighlightings.GetFragments(blogOneId));
  257. Assert.NotEmpty(titleHighlightings.GetFragments(blogTwoId));
  258. Assert.NotEmpty(categoryHighlightings.GetFragments(blogTwoId));
  259. }
  260. }
  261. }
  262. [Fact]
  263. public void ExecutesQueryWithHighlightingsAgainstSimpleIndex()
  264. {
  265. using (var store = this.NewDocumentStore())
  266. {
  267. const string indexName = "BlogsForHighlightingTests";
  268. store.DatabaseCommands.PutIndex(indexName,
  269. new IndexDefinition
  270. {
  271. Map = "from blog in docs.Blogs select new { blog.Title, blog.Category }",
  272. Stores =
  273. {
  274. {"Title", FieldStorage.Yes},
  275. {"Category", FieldStorage.Yes}
  276. },
  277. Indexes =
  278. {
  279. {"Title", FieldIndexing.Analyzed},
  280. {"Category", FieldIndexing.Analyzed}
  281. },
  282. TermVectors =
  283. {
  284. {"Title", FieldTermVector.WithPositionsAndOffsets},
  285. {"Category", FieldTermVector.WithPositionsAndOffsets}
  286. }
  287. });
  288. var blogOne = new Blog
  289. {
  290. Title = "Lorem ipsum dolor sit amet, target word, consectetur adipiscing elit.",
  291. Category = "Ravens"
  292. };
  293. var blogTwo = new Blog
  294. {
  295. Title =
  296. "Maecenas mauris leo, feugiat sodales facilisis target word, pellentesque, suscipit aliquet turpis.",
  297. Category = "The Rhinos"
  298. };
  299. var blogThree = new Blog
  300. {
  301. Title = "Target cras vitae felis arcu word.",
  302. Category = "Los Rhinos"
  303. };
  304. string blogOneId;
  305. string blogTwoId;
  306. using (var s = store.OpenSession())
  307. {
  308. s.Store(blogOne);
  309. s.Store(blogTwo);
  310. s.Store(blogThree);
  311. s.SaveChanges();
  312. blogOneId = s.Advanced.GetDocumentId(blogOne);
  313. blogTwoId = s.Advanced.GetDocumentId(blogTwo);
  314. }
  315. using (var s = store.OpenSession())
  316. {
  317. FieldHighlightings titleHighlightings = null;
  318. FieldHighlightings categoryHighlightings = null;
  319. var results = s.Query<Blog>(indexName)
  320. .Customize(
  321. c =>
  322. c.Highlight("Title", 18, 2, out titleHighlightings)
  323. .Highlight("Category", 18, 2, out categoryHighlightings)
  324. .SetHighlighterTags("*", "*")
  325. .WaitForNonStaleResultsAsOfNow())
  326. .Search(x => x.Category, "rhinos")
  327. .Search(x => x.Title, "target word")
  328. .ToArray();
  329. Assert.Equal(3, results.Length);
  330. Assert.NotEmpty(titleHighlightings.GetFragments(blogOneId));
  331. Assert.Empty(categoryHighlightings.GetFragments(blogOneId));
  332. Assert.NotEmpty(titleHighlightings.GetFragments(blogTwoId));
  333. Assert.NotEmpty(categoryHighlightings.GetFragments(blogTwoId));
  334. }
  335. }
  336. }
  337. [Fact]
  338. public void ExecutesQueryWithHighlightingsAgainstMapReduceIndex()
  339. {
  340. using (var store = this.NewDocumentStore())
  341. {
  342. const string indexName = "BlogsForHighlightingMRTests";
  343. store.DatabaseCommands.PutIndex(indexName,
  344. new IndexDefinition
  345. {
  346. Map = "from blog in docs.Blogs select new { blog.Title, blog.Category }",
  347. Reduce = @"from result in results
  348. group result by result.Category into g
  349. select new { Category = g.Key, Title = g.Select(x=>x.Title).Aggregate(string.Concat) }",
  350. Stores =
  351. {
  352. {"Title", FieldStorage.Yes},
  353. {"Category", FieldStorage.Yes}
  354. },
  355. Indexes =
  356. {
  357. {"Title", FieldIndexing.Analyzed},
  358. {"Category", FieldIndexing.Analyzed}
  359. },
  360. TermVectors =
  361. {
  362. {"Title", FieldTermVector.WithPositionsAndOffsets},
  363. {"Category", FieldTermVector.WithPositionsAndOffsets}
  364. }
  365. });
  366. var blogOne = new Blog
  367. {
  368. Title = "Lorem ipsum dolor sit amet, target word, consectetur adipiscing elit.",
  369. Category = "Ravens"
  370. };
  371. var blogTwo = new Blog
  372. {
  373. Title =
  374. "Maecenas mauris leo, feugiat sodales facilisis target word, pellentesque, suscipit aliquet turpis.",
  375. Category = "The Rhinos"
  376. };
  377. var blogThree = new Blog
  378. {
  379. Title = "Target cras vitae felis arcu word.",
  380. Category = "Los Rhinos"
  381. };
  382. string blogOneId;
  383. string blogTwoId;
  384. using (var s = store.OpenSession())
  385. {
  386. s.Store(blogOne);
  387. s.Store(blogTwo);
  388. s.Store(blogThree);
  389. s.SaveChanges();
  390. blogOneId = s.Advanced.GetDocumentId(blogOne);
  391. blogTwoId = s.Advanced.GetDocumentId(blogTwo);
  392. }
  393. using (var s = store.OpenSession())
  394. {
  395. var results = s.Query<Blog>(indexName)
  396. .Customize(
  397. c => c.WaitForNonStaleResults().Highlight("Title", 18, 2, "TitleFragments"))
  398. .Where(x => x.Title == "lorem" && x.Category == "ravens")
  399. .Select(x => new
  400. {
  401. x.Title,
  402. x.Category,
  403. TitleFragments = default(string[])
  404. })
  405. .ToArray();
  406. Assert.Equal(1, results.Length);
  407. Assert.NotEmpty(results.First().TitleFragments);
  408. }
  409. }
  410. }
  411. [Fact]
  412. public void ExecutesQueryWithHighlightingsAndProjections()
  413. {
  414. using (var store = this.NewDocumentStore())
  415. {
  416. const string indexName = "BlogsForHighlightingTests";
  417. store.DatabaseCommands.PutIndex(indexName,
  418. new IndexDefinition
  419. {
  420. Map = "from blog in docs.Blogs select new { blog.Title, blog.Category }",
  421. Stores =
  422. {
  423. {"Title", FieldStorage.Yes},
  424. {"Category", FieldStorage.Yes}
  425. },
  426. Indexes =
  427. {
  428. {"Title", FieldIndexing.Analyzed},
  429. {"Category", FieldIndexing.Analyzed}
  430. },
  431. TermVectors =
  432. {
  433. {"Title", FieldTermVector.WithPositionsAndOffsets},
  434. {"Category", FieldTermVector.WithPositionsAndOffsets}
  435. }
  436. });
  437. var blogOne = new Blog
  438. {
  439. Title = "Lorem ipsum dolor sit amet, target word, consectetur adipiscing elit.",
  440. Category = "Ravens"
  441. };
  442. var blogTwo = new Blog
  443. {
  444. Title =
  445. "Maecenas mauris leo, feugiat sodales facilisis target word, pellentesque, suscipit aliquet turpis.",
  446. Category = "The Rhinos"
  447. };
  448. var blogThree = new Blog
  449. {
  450. Title = "Target cras vitae felis arcu word.",
  451. Category = "Los Rhinos"
  452. };
  453. using (var s = store.OpenSession())
  454. {
  455. s.Store(blogOne);
  456. s.Store(blogTwo);
  457. s.Store(blogThree);
  458. s.SaveChanges();
  459. }
  460. using (var s = store.OpenSession())
  461. {
  462. var results = s.Query<Blog>(indexName)
  463. .Customize(
  464. c => c.WaitForNonStaleResults().Highlight("Title", 18, 2, "TitleFragments"))
  465. .Where(x => x.Title == "lorem" && x.Category == "ravens")
  466. .Select(x => new
  467. {
  468. x.Title,
  469. x.Category,
  470. TitleFragments = default(string[])
  471. })
  472. .ToArray();
  473. Assert.Equal(1, results.Length);
  474. Assert.NotEmpty(results.First().TitleFragments);
  475. }
  476. }
  477. }
  478. public class Blog
  479. {
  480. public string Title
  481. {
  482. get;
  483. set;
  484. }
  485. public string Category
  486. {
  487. get;
  488. set;
  489. }
  490. public BlogTag[] Tags
  491. {
  492. get;
  493. set;
  494. }
  495. }
  496. public class BlogTag
  497. {
  498. public string Name { get; set; }
  499. }
  500. }
  501. }