search_suggester_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // Copyright 2012-2015 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. _ "net/http"
  8. "testing"
  9. )
  10. func TestTermSuggester(t *testing.T) {
  11. client := setupTestClientAndCreateIndex(t)
  12. tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
  13. tweet2 := tweet{User: "olivere", Message: "Another unrelated topic."}
  14. tweet3 := tweet{User: "sandrae", Message: "Cycling is fun."}
  15. // Add all documents
  16. _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do()
  17. if err != nil {
  18. t.Fatal(err)
  19. }
  20. _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do()
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do()
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. _, err = client.Flush().Index(testIndexName).Do()
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. // Match all should return all documents
  33. all := NewMatchAllQuery()
  34. tsName := "my-suggestions"
  35. ts := NewTermSuggester(tsName)
  36. ts = ts.Text("Goolang")
  37. ts = ts.Field("message")
  38. searchResult, err := client.Search().
  39. Index(testIndexName).
  40. Query(&all).
  41. Suggester(ts).
  42. Do()
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. if searchResult.Suggest == nil {
  47. t.Errorf("expected SearchResult.Suggest != nil; got nil")
  48. }
  49. mySuggestions, found := searchResult.Suggest[tsName]
  50. if !found {
  51. t.Errorf("expected to find SearchResult.Suggest[%s]; got false", tsName)
  52. }
  53. if mySuggestions == nil {
  54. t.Errorf("expected SearchResult.Suggest[%s] != nil; got nil", tsName)
  55. }
  56. if len(mySuggestions) != 1 {
  57. t.Errorf("expected 1 suggestion; got %d", len(mySuggestions))
  58. }
  59. mySuggestion := mySuggestions[0]
  60. if mySuggestion.Text != "goolang" {
  61. t.Errorf("expected Text = 'goolang'; got %s", mySuggestion.Text)
  62. }
  63. if mySuggestion.Offset != 0 {
  64. t.Errorf("expected Offset = %d; got %d", 0, mySuggestion.Offset)
  65. }
  66. if mySuggestion.Length != 7 {
  67. t.Errorf("expected Length = %d; got %d", 7, mySuggestion.Length)
  68. }
  69. if len(mySuggestion.Options) != 1 {
  70. t.Errorf("expected 1 option; got %d", len(mySuggestion.Options))
  71. }
  72. myOption := mySuggestion.Options[0]
  73. if myOption.Text != "golang" {
  74. t.Errorf("expected Text = 'golang'; got %s", myOption.Text)
  75. }
  76. if myOption.Score == float32(0.0) {
  77. t.Errorf("expected Score != 0.0; got %v", myOption.Score)
  78. }
  79. if myOption.Freq == 0 {
  80. t.Errorf("expected Freq != 0; got %v", myOption.Freq)
  81. }
  82. }
  83. func TestPhraseSuggester(t *testing.T) {
  84. client := setupTestClientAndCreateIndex(t)
  85. tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
  86. tweet2 := tweet{User: "olivere", Message: "Another unrelated topic."}
  87. tweet3 := tweet{User: "sandrae", Message: "Cycling is fun."}
  88. // Add all documents
  89. _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do()
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do()
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do()
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. _, err = client.Flush().Index(testIndexName).Do()
  102. if err != nil {
  103. t.Fatal(err)
  104. }
  105. // Match all should return all documents
  106. all := NewMatchAllQuery()
  107. phraseSuggesterName := "my-suggestions"
  108. ps := NewPhraseSuggester(phraseSuggesterName)
  109. ps = ps.Text("Goolang")
  110. ps = ps.Field("message")
  111. searchResult, err := client.Search().
  112. Index(testIndexName).
  113. Query(&all).
  114. Suggester(ps).
  115. Do()
  116. if err != nil {
  117. t.Fatal(err)
  118. }
  119. if searchResult.Suggest == nil {
  120. t.Errorf("expected SearchResult.Suggest != nil; got nil")
  121. }
  122. mySuggestions, found := searchResult.Suggest[phraseSuggesterName]
  123. if !found {
  124. t.Errorf("expected to find SearchResult.Suggest[%s]; got false", phraseSuggesterName)
  125. }
  126. if mySuggestions == nil {
  127. t.Errorf("expected SearchResult.Suggest[%s] != nil; got nil", phraseSuggesterName)
  128. }
  129. if len(mySuggestions) != 1 {
  130. t.Errorf("expected 1 suggestion; got %d", len(mySuggestions))
  131. }
  132. mySuggestion := mySuggestions[0]
  133. if mySuggestion.Text != "Goolang" {
  134. t.Errorf("expected Text = 'Goolang'; got %s", mySuggestion.Text)
  135. }
  136. if mySuggestion.Offset != 0 {
  137. t.Errorf("expected Offset = %d; got %d", 0, mySuggestion.Offset)
  138. }
  139. if mySuggestion.Length != 7 {
  140. t.Errorf("expected Length = %d; got %d", 7, mySuggestion.Length)
  141. }
  142. /*
  143. if len(mySuggestion.Options) != 1 {
  144. t.Errorf("expected 1 option; got %d", len(mySuggestion.Options))
  145. }
  146. myOption := mySuggestion.Options[0]
  147. if myOption.Text != "golang" {
  148. t.Errorf("expected Text = 'golang'; got %s", myOption.Text)
  149. }
  150. if myOption.Score == float32(0.0) {
  151. t.Errorf("expected Score != 0.0; got %v", myOption.Score)
  152. }
  153. */
  154. }
  155. // TODO(oe): I get a "Completion suggester not supported" exception on 0.90.2?!
  156. /*
  157. func TestCompletionSuggester(t *testing.T) {
  158. client := setupTestClientAndCreateIndex(t)
  159. tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
  160. tweet2 := tweet{User: "olivere", Message: "Another unrelated topic."}
  161. tweet3 := tweet{User: "sandrae", Message: "Cycling is fun."}
  162. // Add all documents
  163. _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do()
  164. if err != nil {
  165. t.Fatal(err)
  166. }
  167. _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do()
  168. if err != nil {
  169. t.Fatal(err)
  170. }
  171. _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do()
  172. if err != nil {
  173. t.Fatal(err)
  174. }
  175. _, err = client.Flush().Index(testIndexName).Do()
  176. if err != nil {
  177. t.Fatal(err)
  178. }
  179. // Match all should return all documents
  180. all := NewMatchAllQuery()
  181. suggesterName := "my-suggestions"
  182. cs := NewCompletionSuggester(suggesterName)
  183. cs = cs.Text("Goolang")
  184. cs = cs.Field("message")
  185. searchResult, err := client.Search().
  186. Index(testIndexName).
  187. Query(&all).
  188. Suggester(cs).
  189. Do()
  190. if err != nil {
  191. t.Fatal(err)
  192. }
  193. if searchResult.Suggest == nil {
  194. t.Errorf("expected SearchResult.Suggest != nil; got nil")
  195. }
  196. mySuggestions, found := searchResult.Suggest[suggesterName]
  197. if !found {
  198. t.Errorf("expected to find SearchResult.Suggest[%s]; got false")
  199. }
  200. if mySuggestions == nil {
  201. t.Errorf("expected SearchResult.Suggest[%s] != nil; got nil", suggesterName)
  202. }
  203. if len(mySuggestions) != 1 {
  204. t.Errorf("expected 1 suggestion; got %d", len(mySuggestions))
  205. }
  206. mySuggestion := mySuggestions[0]
  207. if mySuggestion.Text != "Goolang" {
  208. t.Errorf("expected Text = 'Goolang'; got %s", mySuggestion.Text)
  209. }
  210. if mySuggestion.Offset != 0 {
  211. t.Errorf("expected Offset = %d; got %d", 0, mySuggestion.Offset)
  212. }
  213. if mySuggestion.Length != 7 {
  214. t.Errorf("expected Length = %d; got %d", 7, mySuggestion.Length)
  215. }
  216. if len(mySuggestion.Options) != 1 {
  217. t.Errorf("expected 1 option; got %d", len(mySuggestion.Options))
  218. }
  219. myOption := mySuggestion.Options[0]
  220. if myOption.Text != "golang" {
  221. t.Errorf("expected Text = 'golang'; got %s", myOption.Text)
  222. }
  223. if myOption.Score == float32(0.0) {
  224. t.Errorf("expected Score != 0.0; got %v", myOption.Score)
  225. }
  226. }
  227. //*/