1
0

facet_aggregator_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package searcher
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func TestMergeSingleFacet_UniqueTermsCount(t *testing.T) {
  7. ds := &DistributedSearcher{}
  8. // Create initial facet with some terms from shard 1
  9. existing := &Facet{
  10. Field: "path_exact",
  11. Total: 3, // Initial count of unique terms
  12. Missing: 5,
  13. Other: 10,
  14. Terms: []*FacetTerm{
  15. {Term: "/api/users", Count: 100},
  16. {Term: "/api/posts", Count: 80},
  17. {Term: "/home", Count: 60},
  18. },
  19. }
  20. // Create incoming facet from shard 2
  21. // Has some overlapping terms and some new terms
  22. incoming := &Facet{
  23. Field: "path_exact",
  24. Total: 4, // Should NOT be added to existing.Total
  25. Missing: 3,
  26. Other: 8,
  27. Terms: []*FacetTerm{
  28. {Term: "/api/users", Count: 50}, // Overlapping
  29. {Term: "/api/posts", Count: 30}, // Overlapping
  30. {Term: "/login", Count: 40}, // New
  31. {Term: "/dashboard", Count: 35}, // New
  32. },
  33. }
  34. // Merge the facets
  35. ds.mergeSingleFacet(existing, incoming)
  36. // Verify the results
  37. assert.Equal(t, "path_exact", existing.Field)
  38. // Total should be the count of unique terms after merging, NOT the sum
  39. // We have 5 unique terms: /api/users, /api/posts, /home, /login, /dashboard
  40. assert.Equal(t, 5, existing.Total, "Total should be the count of unique terms, not sum of totals")
  41. // Missing and Other should be summed
  42. assert.Equal(t, 8, existing.Missing)
  43. assert.Equal(t, 18, existing.Other)
  44. // Verify term counts are merged correctly
  45. termMap := make(map[string]int)
  46. for _, term := range existing.Terms {
  47. termMap[term.Term] = term.Count
  48. }
  49. assert.Equal(t, 150, termMap["/api/users"], "Count should be 100+50")
  50. assert.Equal(t, 110, termMap["/api/posts"], "Count should be 80+30")
  51. assert.Equal(t, 60, termMap["/home"], "Count should remain 60")
  52. assert.Equal(t, 40, termMap["/login"], "Count should be 40")
  53. assert.Equal(t, 35, termMap["/dashboard"], "Count should be 35")
  54. }
  55. func TestMergeSingleFacet_WithLimitAndOther(t *testing.T) {
  56. // DefaultFacetSize is 10 by default, so we'll use enough terms to exceed it
  57. ds := &DistributedSearcher{}
  58. existing := &Facet{
  59. Field: "path",
  60. Total: 5,
  61. Missing: 0,
  62. Other: 0,
  63. Terms: []*FacetTerm{
  64. {Term: "/page1", Count: 1000},
  65. {Term: "/page2", Count: 900},
  66. {Term: "/page3", Count: 800},
  67. {Term: "/page4", Count: 700},
  68. {Term: "/page5", Count: 600},
  69. },
  70. }
  71. incoming := &Facet{
  72. Field: "path",
  73. Total: 8,
  74. Missing: 0,
  75. Other: 0,
  76. Terms: []*FacetTerm{
  77. {Term: "/page1", Count: 500},
  78. {Term: "/page6", Count: 550},
  79. {Term: "/page7", Count: 450},
  80. {Term: "/page8", Count: 400},
  81. {Term: "/page9", Count: 350},
  82. {Term: "/page10", Count: 300},
  83. {Term: "/page11", Count: 250},
  84. {Term: "/page12", Count: 200},
  85. },
  86. }
  87. ds.mergeSingleFacet(existing, incoming)
  88. // Total should be 12 unique paths (page1-5 from existing, page6-12 from incoming)
  89. assert.Equal(t, 12, existing.Total, "Should have 12 unique paths")
  90. // DefaultFacetSize is 10, so we should keep top 10 terms
  91. assert.Equal(t, 10, len(existing.Terms), "Should keep top 10 terms (DefaultFacetSize)")
  92. // Verify top terms are correct
  93. assert.Equal(t, "/page1", existing.Terms[0].Term)
  94. assert.Equal(t, 1500, existing.Terms[0].Count) // 1000 + 500
  95. assert.Equal(t, "/page2", existing.Terms[1].Term)
  96. assert.Equal(t, 900, existing.Terms[1].Count)
  97. assert.Equal(t, "/page3", existing.Terms[2].Term)
  98. assert.Equal(t, 800, existing.Terms[2].Count)
  99. // Other should contain the sum of excluded terms (page11: 250, page12: 200)
  100. assert.Equal(t, 450, existing.Other, "Other should contain sum of excluded terms")
  101. }