/Raven.Tests/MailingList/ItemsBySetIdIndexTests.cs

https://github.com/jalchr/ravendb · C# · 193 lines · 174 code · 16 blank · 3 comment · 0 complexity · eeec903ea799c06cc60c29ef40bec93f MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using Raven.Abstractions.Indexing;
  6. using Raven.Client.Indexes;
  7. using Raven.Tests.Helpers;
  8. using Xunit;
  9. namespace Raven.Tests.MailingList
  10. {
  11. public class ItemsBySetIdIndexTests:RavenTestBase
  12. {
  13. [Fact]
  14. public void CanQuery()
  15. {
  16. using (var store = NewDocumentStore())
  17. {
  18. new ItemsBySetIdIndex().Execute(store);
  19. using (var session = store.OpenSession())
  20. {
  21. session.Store(new Station {Id="stations/radiofm"});
  22. session.Store(new Song { Id = "songs/adrianstern", Title = "AMERIKA", Interpret = "ADRIAN STERN" });
  23. session.Store(new Song { Id = "songs/falco", Title = "EGOIST", Interpret = "FALCO" });
  24. session.Store(new SongConfig { Id = "stations/radiofm/songs/adrianstern", Attributes = new[] { new Attribute { Name = "SoundCode", Value = "POP" } } });
  25. session.Store(new SongConfig { Id = "stations/radiofm/songs/falco", Attributes = new[] { new Attribute { Name = "SoundCode", Value = "ROCK" } } });
  26. session.Store(new Item //TEST-DATA
  27. {
  28. Id ="stations/radiofm/tests/001/songs/adrianstern",
  29. Set = "stations/radiofm/tests/001", //ref to the dataset
  30. StationId = "stations/radiofm", //ref to the station
  31. RelatedTo = "stations/radiofm/songs/adrianstern", //ref to the global.config
  32. SongId = "songs/adrianstern", //ref to the song
  33. Attributes = new[] { new Attribute { Name = "TixN", Value = 1.5m} }
  34. });
  35. session.Store(new Item //TEST-DATA
  36. {
  37. Id = "stations/radiofm/tests/001/songs/falco",
  38. Set = "stations/radiofm/tests/001",
  39. StationId = "stations/radiofm",
  40. RelatedTo = "stations/radiofm/songs/falco",
  41. SongId = "songs/falco",
  42. Attributes = new[] { new Attribute { Name = "TixN", Value = 3.5m } }
  43. });
  44. session.SaveChanges();
  45. }
  46. WaitForIndexing(store);
  47. using (var session = store.OpenSession())
  48. {
  49. var query =session.Advanced.LuceneQuery<ItemsBySetIdIndex.Result, ItemsBySetIdIndex>()
  50. .WhereEquals("SetId", "stations/radiofm/tests/001")
  51. .AndAlso()
  52. .WhereEquals("SoundCode", "ROCK")
  53. .SelectFields<DataView>("SongId", "Title", "Interpret", "Year", "Attributes", "SID", "SetId", "NumberOfTests", "LastTestDate", "LastTestId","Date");
  54. var total = query.QueryResult.TotalResults;
  55. Assert.Equal(1, total);
  56. Assert.Equal("EGOIST", query.First().Title);
  57. }
  58. }
  59. }
  60. public class Station
  61. {
  62. public string Id { get; set; }
  63. }
  64. public class Item
  65. {
  66. public string SongId { get; set; }
  67. public string RelatedTo { get; set; }
  68. public IList<Attribute> Attributes { get; set; }
  69. public string Set { get; set; }
  70. public string StationId { get; set; }
  71. public DateTime Date { get; set; }
  72. public bool IsRotation { get; set; }
  73. public string Id { get; set; }
  74. }
  75. public class Attribute
  76. {
  77. public string Name { get; set; }
  78. public object Value { get; set; }
  79. }
  80. public class Song
  81. {
  82. public string Id { get; set; }
  83. public string Interpret { get; set; }
  84. public string Title { get; set; }
  85. public int Year { get; set; }
  86. }
  87. public class SongConfig
  88. {
  89. public IList<Attribute> Attributes { get; set; }
  90. public int NumberOfTests { get; set; }
  91. public string LastTestId { get; set; }
  92. public DateTime LastTestDate { get; set; }
  93. public string Id { get; set; }
  94. }
  95. public class DataView
  96. {
  97. public string SongId { get; set; }
  98. public string Title { get; set; }
  99. public string Interpret { get; set; }
  100. public int Year { get; set; }
  101. public string SID { get; set; }
  102. public string SetId { get; set; }
  103. public string StationId { get; set; }
  104. public DateTime? Date { get; set; }
  105. public List<Attribute> Attributes { get; set; }
  106. public int? NumberOfTests { get; set; }
  107. public DateTime? LastTestDate { get; set; }
  108. public string LastTestId { get; set; }
  109. }
  110. public class ItemsBySetIdIndex : AbstractIndexCreationTask<Item, ItemsBySetIdIndex.Result>
  111. {
  112. public class Result
  113. {
  114. public string SetId { get; set; }
  115. public string SongId { get; set; }
  116. public string Title { get; set; }
  117. public string Interpret { get; set; }
  118. public int Year { get; set; }
  119. public string StationId { get; set; }
  120. public Attribute[] Attributes { get; set; }
  121. public DateTime Date { get; set; }
  122. public bool IsRotation { get; set; }
  123. public string Title_Sort { get; set; }
  124. public string Interpret_Sort { get; set; }
  125. public string LastTestId { get; set; }
  126. public DateTime? LastTestDate { get; set; }
  127. public int NumberOfTests { get; set; }
  128. }
  129. public ItemsBySetIdIndex()
  130. {
  131. Map = items => from item in items
  132. let song = LoadDocument<Song>(item.SongId)
  133. let config = LoadDocument<SongConfig>(item.RelatedTo)
  134. let globalAttributes = config.Attributes
  135. let attributes = item.Attributes.Union(globalAttributes)
  136. select new
  137. {
  138. SetId = item.Set,
  139. StationId = item.StationId,
  140. Date = item.Date,
  141. IsRotation = item.IsRotation,
  142. SongId = song.Id,
  143. Interpret = song.Interpret,
  144. Title = song.Title,
  145. Interpret_Sort = song.Interpret,
  146. Title_Sort = song.Title,
  147. Year = song.Year,
  148. Attributes = attributes,
  149. NumberOfTests = config.NumberOfTests,
  150. LastTestId = config.LastTestId,
  151. LastTestDate = config.LastTestDate,
  152. _ = attributes.Select(x => CreateField(x.Name, x.Value)),
  153. //but it will work!:
  154. //_ = item.Attributes.Select(x => CreateField(x.Name, x.Value)),
  155. //__ = globalAttributes.Select(x => CreateField(x.Name, x.Value))
  156. };
  157. Sort(x => x.Date, SortOptions.String);
  158. Index(x => x.Interpret, FieldIndexing.Analyzed);
  159. Index(x => x.Title, FieldIndexing.Analyzed);
  160. Stores = new Dictionary<Expression<Func<Result, object>>, FieldStorage>()
  161. {
  162. { e=>e.SongId, FieldStorage.Yes},
  163. { e=>e.SetId, FieldStorage.Yes},
  164. { e=>e.Date, FieldStorage.Yes},
  165. { e=>e.Title, FieldStorage.Yes},
  166. { e=>e.Interpret, FieldStorage.Yes},
  167. { e=>e.Year, FieldStorage.Yes},
  168. { e=>e.Attributes, FieldStorage.Yes},
  169. { e=>e.StationId, FieldStorage.Yes},
  170. { e=>e.NumberOfTests, FieldStorage.Yes},
  171. { e=>e.LastTestDate, FieldStorage.Yes},
  172. { e=>e.LastTestId, FieldStorage.Yes}
  173. };
  174. }
  175. }
  176. }
  177. }