search_queries_multi_match_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. "encoding/json"
  7. "testing"
  8. )
  9. func TestMultiMatchQuery(t *testing.T) {
  10. q := NewMultiMatchQuery("this is a test", "subject", "message")
  11. src, err := q.Source()
  12. if err != nil {
  13. t.Fatal(err)
  14. }
  15. data, err := json.Marshal(src)
  16. if err != nil {
  17. t.Fatalf("marshaling to JSON failed: %v", err)
  18. }
  19. got := string(data)
  20. expected := `{"multi_match":{"fields":["subject","message"],"query":"this is a test"}}`
  21. if got != expected {
  22. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  23. }
  24. }
  25. func TestMultiMatchQueryBestFields(t *testing.T) {
  26. q := NewMultiMatchQuery("this is a test", "subject", "message").Type("best_fields")
  27. src, err := q.Source()
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. data, err := json.Marshal(src)
  32. if err != nil {
  33. t.Fatalf("marshaling to JSON failed: %v", err)
  34. }
  35. got := string(data)
  36. expected := `{"multi_match":{"fields":["subject","message"],"query":"this is a test","tie_breaker":0,"type":"best_fields"}}`
  37. if got != expected {
  38. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  39. }
  40. }
  41. func TestMultiMatchQueryMostFields(t *testing.T) {
  42. q := NewMultiMatchQuery("this is a test", "subject", "message").Type("most_fields")
  43. src, err := q.Source()
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. data, err := json.Marshal(src)
  48. if err != nil {
  49. t.Fatalf("marshaling to JSON failed: %v", err)
  50. }
  51. got := string(data)
  52. expected := `{"multi_match":{"fields":["subject","message"],"query":"this is a test","tie_breaker":1,"type":"most_fields"}}`
  53. if got != expected {
  54. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  55. }
  56. }
  57. func TestMultiMatchQueryCrossFields(t *testing.T) {
  58. q := NewMultiMatchQuery("this is a test", "subject", "message").Type("cross_fields")
  59. src, err := q.Source()
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. data, err := json.Marshal(src)
  64. if err != nil {
  65. t.Fatalf("marshaling to JSON failed: %v", err)
  66. }
  67. got := string(data)
  68. expected := `{"multi_match":{"fields":["subject","message"],"query":"this is a test","tie_breaker":0,"type":"cross_fields"}}`
  69. if got != expected {
  70. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  71. }
  72. }
  73. func TestMultiMatchQueryPhrase(t *testing.T) {
  74. q := NewMultiMatchQuery("this is a test", "subject", "message").Type("phrase")
  75. src, err := q.Source()
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. data, err := json.Marshal(src)
  80. if err != nil {
  81. t.Fatalf("marshaling to JSON failed: %v", err)
  82. }
  83. got := string(data)
  84. expected := `{"multi_match":{"fields":["subject","message"],"query":"this is a test","tie_breaker":0,"type":"phrase"}}`
  85. if got != expected {
  86. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  87. }
  88. }
  89. func TestMultiMatchQueryPhrasePrefix(t *testing.T) {
  90. q := NewMultiMatchQuery("this is a test", "subject", "message").Type("phrase_prefix")
  91. src, err := q.Source()
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. data, err := json.Marshal(src)
  96. if err != nil {
  97. t.Fatalf("marshaling to JSON failed: %v", err)
  98. }
  99. got := string(data)
  100. expected := `{"multi_match":{"fields":["subject","message"],"query":"this is a test","tie_breaker":0,"type":"phrase_prefix"}}`
  101. if got != expected {
  102. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  103. }
  104. }
  105. func TestMultiMatchQueryBestFieldsWithCustomTieBreaker(t *testing.T) {
  106. q := NewMultiMatchQuery("this is a test", "subject", "message").
  107. Type("best_fields").
  108. TieBreaker(0.3)
  109. src, err := q.Source()
  110. if err != nil {
  111. t.Fatal(err)
  112. }
  113. data, err := json.Marshal(src)
  114. if err != nil {
  115. t.Fatalf("marshaling to JSON failed: %v", err)
  116. }
  117. got := string(data)
  118. expected := `{"multi_match":{"fields":["subject","message"],"query":"this is a test","tie_breaker":0.3,"type":"best_fields"}}`
  119. if got != expected {
  120. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  121. }
  122. }