suggest_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. _ "net/http"
  7. "testing"
  8. )
  9. func TestSuggestService(t *testing.T) {
  10. client := setupTestClientAndCreateIndex(t)
  11. tweet1 := tweet{
  12. User: "olivere",
  13. Message: "Welcome to Golang and Elasticsearch.",
  14. Tags: []string{"golang", "elasticsearch"},
  15. Location: "48.1333,11.5667", // lat,lon
  16. Suggest: NewSuggestField().
  17. Input("Welcome to Golang and Elasticsearch.", "Golang and Elasticsearch").
  18. Output("Golang and Elasticsearch: An introduction.").
  19. Weight(0),
  20. }
  21. tweet2 := tweet{
  22. User: "olivere",
  23. Message: "Another unrelated topic.",
  24. Tags: []string{"golang"},
  25. Location: "48.1189,11.4289", // lat,lon
  26. Suggest: NewSuggestField().
  27. Input("Another unrelated topic.", "Golang topic.").
  28. Output("About Golang.").
  29. Weight(1),
  30. }
  31. tweet3 := tweet{
  32. User: "sandrae",
  33. Message: "Cycling is fun.",
  34. Tags: []string{"sports", "cycling"},
  35. Location: "47.7167,11.7167", // lat,lon
  36. Suggest: NewSuggestField().
  37. Input("Cycling is fun.").
  38. Output("Cycling is a fun sport."),
  39. }
  40. // Add all documents
  41. _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do()
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do()
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do()
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. _, err = client.Flush().Index(testIndexName).Do()
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. // Test _suggest endpoint
  58. termSuggesterName := "my-term-suggester"
  59. termSuggester := NewTermSuggester(termSuggesterName).Text("Goolang").Field("message")
  60. phraseSuggesterName := "my-phrase-suggester"
  61. phraseSuggester := NewPhraseSuggester(phraseSuggesterName).Text("Goolang").Field("message")
  62. completionSuggesterName := "my-completion-suggester"
  63. completionSuggester := NewCompletionSuggester(completionSuggesterName).Text("Go").Field("suggest_field")
  64. result, err := client.Suggest().
  65. Index(testIndexName).
  66. Suggester(termSuggester).
  67. Suggester(phraseSuggester).
  68. Suggester(completionSuggester).
  69. Do()
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. if result == nil {
  74. t.Errorf("expected result != nil; got nil")
  75. }
  76. if len(result) != 3 {
  77. t.Errorf("expected 3 suggester results; got %d", len(result))
  78. }
  79. termSuggestions, found := result[termSuggesterName]
  80. if !found {
  81. t.Errorf("expected to find Suggest[%s]; got false", termSuggesterName)
  82. }
  83. if termSuggestions == nil {
  84. t.Errorf("expected Suggest[%s] != nil; got nil", termSuggesterName)
  85. }
  86. if len(termSuggestions) != 1 {
  87. t.Errorf("expected 1 suggestion; got %d", len(termSuggestions))
  88. }
  89. phraseSuggestions, found := result[phraseSuggesterName]
  90. if !found {
  91. t.Errorf("expected to find Suggest[%s]; got false", phraseSuggesterName)
  92. }
  93. if phraseSuggestions == nil {
  94. t.Errorf("expected Suggest[%s] != nil; got nil", phraseSuggesterName)
  95. }
  96. if len(phraseSuggestions) != 1 {
  97. t.Errorf("expected 1 suggestion; got %d", len(phraseSuggestions))
  98. }
  99. completionSuggestions, found := result[completionSuggesterName]
  100. if !found {
  101. t.Errorf("expected to find Suggest[%s]; got false", completionSuggesterName)
  102. }
  103. if completionSuggestions == nil {
  104. t.Errorf("expected Suggest[%s] != nil; got nil", completionSuggesterName)
  105. }
  106. if len(completionSuggestions) != 1 {
  107. t.Errorf("expected 1 suggestion; got %d", len(completionSuggestions))
  108. }
  109. if len(completionSuggestions[0].Options) != 2 {
  110. t.Errorf("expected 2 suggestion options; got %d", len(completionSuggestions[0].Options))
  111. }
  112. if completionSuggestions[0].Options[0].Text != "About Golang." {
  113. t.Errorf("expected Suggest[%s][0].Options[0].Text == %q; got %q", completionSuggesterName, "About Golang.", completionSuggestions[0].Options[0].Text)
  114. }
  115. if completionSuggestions[0].Options[1].Text != "Golang and Elasticsearch: An introduction." {
  116. t.Errorf("expected Suggest[%s][0].Options[1].Text == %q; got %q", completionSuggesterName, "Golang and Elasticsearch: An introduction.", completionSuggestions[0].Options[1].Text)
  117. }
  118. }