highlight_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. "context"
  7. "encoding/json"
  8. "testing"
  9. )
  10. func TestHighlighterField(t *testing.T) {
  11. field := NewHighlighterField("grade")
  12. src, err := field.Source()
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. data, err := json.Marshal(src)
  17. if err != nil {
  18. t.Fatalf("marshaling to JSON failed: %v", err)
  19. }
  20. got := string(data)
  21. expected := `{}`
  22. if got != expected {
  23. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  24. }
  25. }
  26. func TestHighlighterFieldWithOptions(t *testing.T) {
  27. field := NewHighlighterField("grade").FragmentSize(2).NumOfFragments(1)
  28. src, err := field.Source()
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. data, err := json.Marshal(src)
  33. if err != nil {
  34. t.Fatalf("marshaling to JSON failed: %v", err)
  35. }
  36. got := string(data)
  37. expected := `{"fragment_size":2,"number_of_fragments":1}`
  38. if got != expected {
  39. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  40. }
  41. }
  42. func TestHighlightWithStringField(t *testing.T) {
  43. builder := NewHighlight().Field("grade")
  44. src, err := builder.Source()
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. data, err := json.Marshal(src)
  49. if err != nil {
  50. t.Fatalf("marshaling to JSON failed: %v", err)
  51. }
  52. got := string(data)
  53. expected := `{"fields":{"grade":{}}}`
  54. if got != expected {
  55. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  56. }
  57. }
  58. func TestHighlightWithFields(t *testing.T) {
  59. gradeField := NewHighlighterField("grade")
  60. builder := NewHighlight().Fields(gradeField)
  61. src, err := builder.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 := `{"fields":{"grade":{}}}`
  71. if got != expected {
  72. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  73. }
  74. }
  75. func TestHighlightWithMultipleFields(t *testing.T) {
  76. gradeField := NewHighlighterField("grade")
  77. colorField := NewHighlighterField("color")
  78. builder := NewHighlight().Fields(gradeField, colorField)
  79. src, err := builder.Source()
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. data, err := json.Marshal(src)
  84. if err != nil {
  85. t.Fatalf("marshaling to JSON failed: %v", err)
  86. }
  87. got := string(data)
  88. expected := `{"fields":{"color":{},"grade":{}}}`
  89. if got != expected {
  90. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  91. }
  92. }
  93. func TestHighlighterWithExplicitFieldOrder(t *testing.T) {
  94. gradeField := NewHighlighterField("grade").FragmentSize(2)
  95. colorField := NewHighlighterField("color").FragmentSize(2).NumOfFragments(1)
  96. builder := NewHighlight().Fields(gradeField, colorField).UseExplicitFieldOrder(true)
  97. src, err := builder.Source()
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. data, err := json.Marshal(src)
  102. if err != nil {
  103. t.Fatalf("marshaling to JSON failed: %v", err)
  104. }
  105. got := string(data)
  106. expected := `{"fields":[{"grade":{"fragment_size":2}},{"color":{"fragment_size":2,"number_of_fragments":1}}]}`
  107. if got != expected {
  108. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  109. }
  110. }
  111. func TestHighlightWithBoundarySettings(t *testing.T) {
  112. builder := NewHighlight().
  113. BoundaryChars(" \t\r").
  114. BoundaryScannerType("word")
  115. src, err := builder.Source()
  116. if err != nil {
  117. t.Fatal(err)
  118. }
  119. data, err := json.Marshal(src)
  120. if err != nil {
  121. t.Fatalf("marshaling to JSON failed: %v", err)
  122. }
  123. got := string(data)
  124. expected := `{"boundary_chars":" \t\r","boundary_scanner":"word"}`
  125. if got != expected {
  126. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  127. }
  128. }
  129. func TestHighlightWithTermQuery(t *testing.T) {
  130. client := setupTestClientAndCreateIndex(t)
  131. tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
  132. tweet2 := tweet{User: "olivere", Message: "Another unrelated topic."}
  133. tweet3 := tweet{User: "sandrae", Message: "Cycling is fun to do."}
  134. // Add all documents
  135. _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
  136. if err != nil {
  137. t.Fatal(err)
  138. }
  139. _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
  140. if err != nil {
  141. t.Fatal(err)
  142. }
  143. _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
  144. if err != nil {
  145. t.Fatal(err)
  146. }
  147. _, err = client.Flush().Index(testIndexName).Do(context.TODO())
  148. if err != nil {
  149. t.Fatal(err)
  150. }
  151. // Specify highlighter
  152. hl := NewHighlight()
  153. hl = hl.Fields(NewHighlighterField("message"))
  154. hl = hl.PreTags("<em>").PostTags("</em>")
  155. // Match all should return all documents
  156. query := NewPrefixQuery("message", "golang")
  157. searchResult, err := client.Search().
  158. Index(testIndexName).
  159. Highlight(hl).
  160. Query(query).
  161. Do(context.TODO())
  162. if err != nil {
  163. t.Fatal(err)
  164. }
  165. if searchResult.Hits == nil {
  166. t.Fatalf("expected SearchResult.Hits != nil; got nil")
  167. }
  168. if searchResult.Hits.TotalHits != 1 {
  169. t.Fatalf("expected SearchResult.Hits.TotalHits = %d; got %d", 1, searchResult.Hits.TotalHits)
  170. }
  171. if len(searchResult.Hits.Hits) != 1 {
  172. t.Fatalf("expected len(SearchResult.Hits.Hits) = %d; got %d", 1, len(searchResult.Hits.Hits))
  173. }
  174. hit := searchResult.Hits.Hits[0]
  175. var tw tweet
  176. if err := json.Unmarshal(*hit.Source, &tw); err != nil {
  177. t.Fatal(err)
  178. }
  179. if hit.Highlight == nil || len(hit.Highlight) == 0 {
  180. t.Fatal("expected hit to have a highlight; got nil")
  181. }
  182. if hl, found := hit.Highlight["message"]; found {
  183. if len(hl) != 1 {
  184. t.Fatalf("expected to have one highlight for field \"message\"; got %d", len(hl))
  185. }
  186. expected := "Welcome to <em>Golang</em> and Elasticsearch."
  187. if hl[0] != expected {
  188. t.Errorf("expected to have highlight \"%s\"; got \"%s\"", expected, hl[0])
  189. }
  190. } else {
  191. t.Fatal("expected to have a highlight on field \"message\"; got none")
  192. }
  193. }