termvectors_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. "time"
  9. )
  10. func TestTermVectorsBuildURL(t *testing.T) {
  11. client := setupTestClientAndCreateIndex(t)
  12. tests := []struct {
  13. Index string
  14. Type string
  15. Id string
  16. Expected string
  17. }{
  18. {
  19. "twitter",
  20. "tweet",
  21. "",
  22. "/twitter/tweet/_termvectors",
  23. },
  24. {
  25. "twitter",
  26. "tweet",
  27. "1",
  28. "/twitter/tweet/1/_termvectors",
  29. },
  30. }
  31. for _, test := range tests {
  32. builder := client.TermVectors(test.Index, test.Type)
  33. if test.Id != "" {
  34. builder = builder.Id(test.Id)
  35. }
  36. path, _, err := builder.buildURL()
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. if path != test.Expected {
  41. t.Errorf("expected %q; got: %q", test.Expected, path)
  42. }
  43. }
  44. }
  45. func TestTermVectorsWithId(t *testing.T) {
  46. client := setupTestClientAndCreateIndex(t)
  47. tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
  48. // Add a document
  49. indexResult, err := client.Index().
  50. Index(testIndexName).
  51. Type("tweet").
  52. Id("1").
  53. BodyJson(&tweet1).
  54. Refresh("true").
  55. Do(context.TODO())
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. if indexResult == nil {
  60. t.Errorf("expected result to be != nil; got: %v", indexResult)
  61. }
  62. // TermVectors by specifying ID
  63. field := "Message"
  64. result, err := client.TermVectors(testIndexName, "tweet").
  65. Id("1").
  66. Fields(field).
  67. FieldStatistics(true).
  68. TermStatistics(true).
  69. Do(context.TODO())
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. if result == nil {
  74. t.Fatal("expected to return information and statistics")
  75. }
  76. if !result.Found {
  77. t.Errorf("expected found to be %v; got: %v", true, result.Found)
  78. }
  79. }
  80. func TestTermVectorsWithDoc(t *testing.T) {
  81. client := setupTestClientAndCreateIndex(t)
  82. // Travis lags sometimes
  83. if isTravis() {
  84. time.Sleep(2 * time.Second)
  85. }
  86. // TermVectors by specifying Doc
  87. var doc = map[string]interface{}{
  88. "fullname": "John Doe",
  89. "text": "twitter test test test",
  90. }
  91. var perFieldAnalyzer = map[string]string{
  92. "fullname": "keyword",
  93. }
  94. result, err := client.TermVectors(testIndexName, "tweet").
  95. Doc(doc).
  96. PerFieldAnalyzer(perFieldAnalyzer).
  97. FieldStatistics(true).
  98. TermStatistics(true).
  99. Do(context.TODO())
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. if result == nil {
  104. t.Fatal("expected to return information and statistics")
  105. }
  106. if !result.Found {
  107. t.Errorf("expected found to be %v; got: %v", true, result.Found)
  108. }
  109. }
  110. func TestTermVectorsWithFilter(t *testing.T) {
  111. client := setupTestClientAndCreateIndex(t)
  112. // Travis lags sometimes
  113. if isTravis() {
  114. time.Sleep(2 * time.Second)
  115. }
  116. // TermVectors by specifying Doc
  117. var doc = map[string]interface{}{
  118. "fullname": "John Doe",
  119. "text": "twitter test test test",
  120. }
  121. var perFieldAnalyzer = map[string]string{
  122. "fullname": "keyword",
  123. }
  124. result, err := client.TermVectors(testIndexName, "tweet").
  125. Doc(doc).
  126. PerFieldAnalyzer(perFieldAnalyzer).
  127. FieldStatistics(true).
  128. TermStatistics(true).
  129. Filter(NewTermvectorsFilterSettings().MinTermFreq(1)).
  130. Do(context.TODO())
  131. if err != nil {
  132. t.Fatal(err)
  133. }
  134. if result == nil {
  135. t.Fatal("expected to return information and statistics")
  136. }
  137. if !result.Found {
  138. t.Errorf("expected found to be %v; got: %v", true, result.Found)
  139. }
  140. }