/test/SlowTests/Tests/Querying/UsingDynamicQueryWithLocalServer.cs

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