mtermvectors_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 TestMultiTermVectorsValidateAndBuildURL(t *testing.T) {
  10. client := setupTestClientAndCreateIndex(t)
  11. tests := []struct {
  12. Index string
  13. Type string
  14. Expected string
  15. ExpectValidateFailure bool
  16. }{
  17. // #0: No index, no type
  18. {
  19. "",
  20. "",
  21. "/_mtermvectors",
  22. false,
  23. },
  24. // #1: Index only
  25. {
  26. "twitter",
  27. "",
  28. "/twitter/_mtermvectors",
  29. false,
  30. },
  31. // #2: Type without index
  32. {
  33. "",
  34. "tweet",
  35. "",
  36. true,
  37. },
  38. // #3: Both index and type
  39. {
  40. "twitter",
  41. "tweet",
  42. "/twitter/tweet/_mtermvectors",
  43. false,
  44. },
  45. }
  46. for i, test := range tests {
  47. builder := client.MultiTermVectors().Index(test.Index).Type(test.Type)
  48. // Validate
  49. err := builder.Validate()
  50. if err != nil {
  51. if !test.ExpectValidateFailure {
  52. t.Errorf("#%d: expected no error, got: %v", i, err)
  53. continue
  54. }
  55. } else {
  56. if test.ExpectValidateFailure {
  57. t.Errorf("#%d: expected error, got: nil", i)
  58. continue
  59. }
  60. // Build
  61. path, _, err := builder.buildURL()
  62. if err != nil {
  63. t.Errorf("#%d: expected no error, got: %v", i, err)
  64. continue
  65. }
  66. if path != test.Expected {
  67. t.Errorf("#%d: expected %q; got: %q", i, test.Expected, path)
  68. }
  69. }
  70. }
  71. }
  72. func TestMultiTermVectorsWithIds(t *testing.T) {
  73. client := setupTestClientAndCreateIndex(t)
  74. tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
  75. tweet2 := tweet{User: "olivere", Message: "Another unrelated topic."}
  76. tweet3 := tweet{User: "sandrae", Message: "Cycling is fun."}
  77. _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. _, err = client.Flush().Index(testIndexName).Do(context.TODO())
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. // Count documents
  94. count, err := client.Count(testIndexName).Do(context.TODO())
  95. if err != nil {
  96. t.Fatal(err)
  97. }
  98. if count != 3 {
  99. t.Errorf("expected Count = %d; got %d", 3, count)
  100. }
  101. // MultiTermVectors by specifying ID by 1 and 3
  102. field := "Message"
  103. res, err := client.MultiTermVectors().
  104. Index(testIndexName).
  105. Type("tweet").
  106. Add(NewMultiTermvectorItem().Index(testIndexName).Type("tweet").Id("1").Fields(field)).
  107. Add(NewMultiTermvectorItem().Index(testIndexName).Type("tweet").Id("3").Fields(field)).
  108. Do(context.TODO())
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. if res == nil {
  113. t.Fatal("expected to return information and statistics")
  114. }
  115. if res.Docs == nil {
  116. t.Fatal("expected result docs to be != nil; got nil")
  117. }
  118. if len(res.Docs) != 2 {
  119. t.Fatalf("expected to have 2 docs; got %d", len(res.Docs))
  120. }
  121. }