suggester_context_category_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright 2012-present Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import (
  6. "encoding/json"
  7. "testing"
  8. )
  9. func TestSuggesterCategoryMapping(t *testing.T) {
  10. q := NewSuggesterCategoryMapping("color").DefaultValues("red")
  11. src, err := q.Source()
  12. if err != nil {
  13. t.Fatal(err)
  14. }
  15. data, err := json.Marshal(src)
  16. if err != nil {
  17. t.Fatalf("marshaling to JSON failed: %v", err)
  18. }
  19. got := string(data)
  20. expected := `{"color":{"default":"red","type":"category"}}`
  21. if got != expected {
  22. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  23. }
  24. }
  25. func TestSuggesterCategoryMappingWithTwoDefaultValues(t *testing.T) {
  26. q := NewSuggesterCategoryMapping("color").DefaultValues("red", "orange")
  27. src, err := q.Source()
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. data, err := json.Marshal(src)
  32. if err != nil {
  33. t.Fatalf("marshaling to JSON failed: %v", err)
  34. }
  35. got := string(data)
  36. expected := `{"color":{"default":["red","orange"],"type":"category"}}`
  37. if got != expected {
  38. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  39. }
  40. }
  41. func TestSuggesterCategoryMappingWithFieldName(t *testing.T) {
  42. q := NewSuggesterCategoryMapping("color").
  43. DefaultValues("red", "orange").
  44. FieldName("color_field")
  45. src, err := q.Source()
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. data, err := json.Marshal(src)
  50. if err != nil {
  51. t.Fatalf("marshaling to JSON failed: %v", err)
  52. }
  53. got := string(data)
  54. expected := `{"color":{"default":["red","orange"],"path":"color_field","type":"category"}}`
  55. if got != expected {
  56. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  57. }
  58. }
  59. func TestSuggesterCategoryQuery(t *testing.T) {
  60. q := NewSuggesterCategoryQuery("color", "red")
  61. src, err := q.Source()
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. data, err := json.Marshal(src)
  66. if err != nil {
  67. t.Fatalf("marshaling to JSON failed: %v", err)
  68. }
  69. got := string(data)
  70. expected := `{"color":[{"context":"red"}]}`
  71. if got != expected {
  72. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  73. }
  74. }
  75. func TestSuggesterCategoryQueryWithTwoValues(t *testing.T) {
  76. q := NewSuggesterCategoryQuery("color", "red", "yellow")
  77. src, err := q.Source()
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. data, err := json.Marshal(src)
  82. if err != nil {
  83. t.Fatalf("marshaling to JSON failed: %v", err)
  84. }
  85. got := string(data)
  86. expectedOutcomes := []string{
  87. `{"color":[{"context":"red"},{"context":"yellow"}]}`,
  88. `{"color":[{"context":"yellow"},{"context":"red"}]}`,
  89. }
  90. var match bool
  91. for _, expected := range expectedOutcomes {
  92. if got == expected {
  93. match = true
  94. break
  95. }
  96. }
  97. if !match {
  98. t.Errorf("expected any of %v\n,got:\n%s", expectedOutcomes, got)
  99. }
  100. }
  101. func TestSuggesterCategoryQueryWithBoost(t *testing.T) {
  102. q := NewSuggesterCategoryQuery("color", "red")
  103. q.ValueWithBoost("yellow", 4)
  104. src, err := q.Source()
  105. if err != nil {
  106. t.Fatal(err)
  107. }
  108. data, err := json.Marshal(src)
  109. if err != nil {
  110. t.Fatalf("marshaling to JSON failed: %v", err)
  111. }
  112. got := string(data)
  113. expectedOutcomes := []string{
  114. `{"color":[{"context":"red"},{"boost":4,"context":"yellow"}]}`,
  115. `{"color":[{"boost":4,"context":"yellow"},{"context":"red"}]}`,
  116. }
  117. var match bool
  118. for _, expected := range expectedOutcomes {
  119. if got == expected {
  120. match = true
  121. break
  122. }
  123. }
  124. if !match {
  125. t.Errorf("expected any of %v\n,got:\n%v", expectedOutcomes, got)
  126. }
  127. }
  128. func TestSuggesterCategoryQueryWithoutBoost(t *testing.T) {
  129. q := NewSuggesterCategoryQuery("color", "red")
  130. q.Value("yellow")
  131. src, err := q.Source()
  132. if err != nil {
  133. t.Fatal(err)
  134. }
  135. data, err := json.Marshal(src)
  136. if err != nil {
  137. t.Fatalf("marshaling to JSON failed: %v", err)
  138. }
  139. got := string(data)
  140. expectedOutcomes := []string{
  141. `{"color":[{"context":"red"},{"context":"yellow"}]}`,
  142. `{"color":[{"context":"yellow"},{"context":"red"}]}`,
  143. }
  144. var match bool
  145. for _, expected := range expectedOutcomes {
  146. if got == expected {
  147. match = true
  148. break
  149. }
  150. }
  151. if !match {
  152. t.Errorf("expected any of %v\n,got:\n%s", expectedOutcomes, got)
  153. }
  154. }