/test/SlowTests/Tests/Querying/DynamicIndexesWithCollections.cs

https://github.com/fitzchak/ravendb · C# · 594 lines · 469 code · 44 blank · 81 comment · 4 complexity · 0f6181ea5a6ac0c72a65e27af08d0195 MD5 · raw file

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