suggest_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. "testing"
  8. )
  9. func TestSuggestBuildURL(t *testing.T) {
  10. client := setupTestClient(t)
  11. tests := []struct {
  12. Indices []string
  13. Expected string
  14. }{
  15. {
  16. []string{},
  17. "/_suggest",
  18. },
  19. {
  20. []string{"index1"},
  21. "/index1/_suggest",
  22. },
  23. {
  24. []string{"index1", "index2"},
  25. "/index1%2Cindex2/_suggest",
  26. },
  27. }
  28. for i, test := range tests {
  29. path, _, err := client.Suggest().Index(test.Indices...).buildURL()
  30. if err != nil {
  31. t.Errorf("case #%d: %v", i+1, err)
  32. continue
  33. }
  34. if path != test.Expected {
  35. t.Errorf("case #%d: expected %q; got: %q", i+1, test.Expected, path)
  36. }
  37. }
  38. }
  39. func TestSuggestService(t *testing.T) {
  40. client := setupTestClientAndCreateIndex(t)
  41. // client := setupTestClientAndCreateIndex(t, SetTraceLog(log.New(os.Stdout, "", 0)))
  42. tweet1 := tweet{
  43. User: "olivere",
  44. Message: "Welcome to Golang and Elasticsearch.",
  45. Tags: []string{"golang", "elasticsearch"},
  46. Location: "48.1333,11.5667", // lat,lon
  47. Suggest: NewSuggestField().
  48. Input("Welcome to Golang and Elasticsearch.", "Golang and Elasticsearch").
  49. Weight(0),
  50. }
  51. tweet2 := tweet{
  52. User: "olivere",
  53. Message: "Another unrelated topic.",
  54. Tags: []string{"golang"},
  55. Location: "48.1189,11.4289", // lat,lon
  56. Suggest: NewSuggestField().
  57. Input("Another unrelated topic.", "Golang topic.").
  58. Weight(1),
  59. }
  60. tweet3 := tweet{
  61. User: "sandrae",
  62. Message: "Cycling is fun.",
  63. Tags: []string{"sports", "cycling"},
  64. Location: "47.7167,11.7167", // lat,lon
  65. Suggest: NewSuggestField().
  66. Input("Cycling is fun."),
  67. }
  68. // Add all documents
  69. _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. _, err = client.Flush().Index(testIndexName).Do(context.TODO())
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. // Test _suggest endpoint
  86. termSuggesterName := "my-term-suggester"
  87. termSuggester := NewTermSuggester(termSuggesterName).Text("Goolang").Field("message")
  88. phraseSuggesterName := "my-phrase-suggester"
  89. phraseSuggester := NewPhraseSuggester(phraseSuggesterName).Text("Goolang").Field("message")
  90. completionSuggesterName := "my-completion-suggester"
  91. completionSuggester := NewCompletionSuggester(completionSuggesterName).Text("Go").Field("suggest_field")
  92. result, err := client.Suggest().
  93. Index(testIndexName).
  94. Suggester(termSuggester).
  95. Suggester(phraseSuggester).
  96. Suggester(completionSuggester).
  97. Do(context.TODO())
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. if result == nil {
  102. t.Errorf("expected result != nil; got nil")
  103. }
  104. if len(result) != 3 {
  105. t.Errorf("expected 3 suggester results; got %d", len(result))
  106. }
  107. termSuggestions, found := result[termSuggesterName]
  108. if !found {
  109. t.Errorf("expected to find Suggest[%s]; got false", termSuggesterName)
  110. }
  111. if termSuggestions == nil {
  112. t.Errorf("expected Suggest[%s] != nil; got nil", termSuggesterName)
  113. }
  114. if len(termSuggestions) != 1 {
  115. t.Errorf("expected 1 suggestion; got %d", len(termSuggestions))
  116. }
  117. phraseSuggestions, found := result[phraseSuggesterName]
  118. if !found {
  119. t.Errorf("expected to find Suggest[%s]; got false", phraseSuggesterName)
  120. }
  121. if phraseSuggestions == nil {
  122. t.Errorf("expected Suggest[%s] != nil; got nil", phraseSuggesterName)
  123. }
  124. if len(phraseSuggestions) != 1 {
  125. t.Errorf("expected 1 suggestion; got %d", len(phraseSuggestions))
  126. }
  127. completionSuggestions, found := result[completionSuggesterName]
  128. if !found {
  129. t.Errorf("expected to find Suggest[%s]; got false", completionSuggesterName)
  130. }
  131. if completionSuggestions == nil {
  132. t.Errorf("expected Suggest[%s] != nil; got nil", completionSuggesterName)
  133. }
  134. if len(completionSuggestions) != 1 {
  135. t.Errorf("expected 1 suggestion; got %d", len(completionSuggestions))
  136. }
  137. if len(completionSuggestions[0].Options) != 2 {
  138. t.Errorf("expected 2 suggestion options; got %d", len(completionSuggestions[0].Options))
  139. }
  140. if have, want := completionSuggestions[0].Options[0].Text, "Golang topic."; have != want {
  141. t.Errorf("expected Suggest[%s][0].Options[0].Text == %q; got %q", completionSuggesterName, want, have)
  142. }
  143. if have, want := completionSuggestions[0].Options[1].Text, "Golang and Elasticsearch"; have != want {
  144. t.Errorf("expected Suggest[%s][0].Options[1].Text == %q; got %q", completionSuggesterName, want, have)
  145. }
  146. }