PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Raven.Tests.MailingList/CanFacetOnList.cs

https://github.com/barryhagan/ravendb
C# | 85 lines | 70 code | 15 blank | 0 comment | 0 complexity | b199d53c2808a89777cfb4771c7bc53a MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, CC-BY-SA-3.0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Raven.Abstractions.Data;
  4. using Raven.Client;
  5. using Raven.Client.Indexes;
  6. using Raven.Tests.Common;
  7. using Raven.Tests.Helpers;
  8. using Xunit;
  9. namespace Raven.Tests.MailingList
  10. {
  11. public class FacetTest : RavenTestBase
  12. {
  13. [Fact]
  14. public void CanFacetOnList()
  15. {
  16. using (var store = NewDocumentStore())
  17. {
  18. new BlogIndex().Execute(store);
  19. var facets = new List<Facet>{
  20. new Facet{Name = "Tags", TermSortMode= FacetTermSortMode.HitsDesc}
  21. };
  22. using (var session = store.OpenSession())
  23. {
  24. session.Store(new FacetSetup() { Facets = facets, Id = "facets/BlogFacets" });
  25. var post1 = new BlogPost
  26. {
  27. Title = "my first blog",
  28. Tags = new List<string>() { "news", "funny" }
  29. };
  30. session.Store(post1);
  31. var post2 = new BlogPost
  32. {
  33. Title = "my second blog",
  34. Tags = new List<string>() { "lame", "news" }
  35. };
  36. session.Store(post2);
  37. session.SaveChanges();
  38. }
  39. WaitForIndexing(store);
  40. using (var session = store.OpenSession())
  41. {
  42. var q = session.Query<BlogPost, BlogIndex>();
  43. var f = q.ToFacets("facets/BlogFacets");
  44. Assert.Equal(1, f.Results.Count);
  45. Assert.Equal(3, f.Results["Tags"].Values.Count);
  46. Assert.Equal("news", f.Results["Tags"].Values[0].Range);
  47. Assert.Equal(2, f.Results["Tags"].Values[0].Hits);
  48. }
  49. }
  50. }
  51. public class BlogPost
  52. {
  53. public string Title { get; set; }
  54. public List<string> Tags { get; set; }
  55. }
  56. public class BlogIndex : AbstractIndexCreationTask<BlogPost>
  57. {
  58. public BlogIndex()
  59. {
  60. Map = blogs => from b in blogs
  61. select new
  62. {
  63. Tags = b.Tags
  64. };
  65. Store("Tags", Raven.Abstractions.Indexing.FieldStorage.Yes);
  66. Index("Tags", Raven.Abstractions.Indexing.FieldIndexing.NotAnalyzed);
  67. }
  68. }
  69. }
  70. }