search_queries_match_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 TestMatchQuery(t *testing.T) {
  10. q := NewMatchQuery("message", "this is a test")
  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 := `{"match":{"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 TestMatchPhraseQuery(t *testing.T) {
  22. q := NewMatchPhraseQuery("message", "this is a test")
  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 := `{"match":{"message":{"query":"this is a test","type":"phrase"}}}`
  29. if got != expected {
  30. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  31. }
  32. }
  33. func TestMatchPhrasePrefixQuery(t *testing.T) {
  34. q := NewMatchPhrasePrefixQuery("message", "this is a test")
  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 := `{"match":{"message":{"query":"this is a test","type":"phrase_prefix"}}}`
  41. if got != expected {
  42. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  43. }
  44. }
  45. func TestMatchQueryWithOptions(t *testing.T) {
  46. q := NewMatchQuery("message", "this is a test").Operator("or").Boost(2.5)
  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 := `{"match":{"message":{"boost":2.5,"operator":"or","query":"this is a test"}}}`
  53. if got != expected {
  54. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  55. }
  56. }