search_queries_multi_match_test.go 3.4 KB

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