search_queries_range_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 TestRangeQuery(t *testing.T) {
  10. q := NewRangeQuery("postDate").From("2010-03-01").To("2010-04-01").Boost(3).Relation("within")
  11. q = q.QueryName("my_query")
  12. src, err := q.Source()
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. data, err := json.Marshal(src)
  17. if err != nil {
  18. t.Fatalf("marshaling to JSON failed: %v", err)
  19. }
  20. got := string(data)
  21. expected := `{"range":{"_name":"my_query","postDate":{"boost":3,"from":"2010-03-01","include_lower":true,"include_upper":true,"relation":"within","to":"2010-04-01"}}}`
  22. if got != expected {
  23. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  24. }
  25. }
  26. func TestRangeQueryWithTimeZone(t *testing.T) {
  27. q := NewRangeQuery("born").
  28. Gte("2012-01-01").
  29. Lte("now").
  30. TimeZone("+1:00")
  31. src, err := q.Source()
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. data, err := json.Marshal(src)
  36. if err != nil {
  37. t.Fatalf("marshaling to JSON failed: %v", err)
  38. }
  39. got := string(data)
  40. expected := `{"range":{"born":{"from":"2012-01-01","include_lower":true,"include_upper":true,"time_zone":"+1:00","to":"now"}}}`
  41. if got != expected {
  42. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  43. }
  44. }
  45. func TestRangeQueryWithFormat(t *testing.T) {
  46. q := NewRangeQuery("born").
  47. Gte("2012/01/01").
  48. Lte("now").
  49. Format("yyyy/MM/dd")
  50. src, err := q.Source()
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. data, err := json.Marshal(src)
  55. if err != nil {
  56. t.Fatalf("marshaling to JSON failed: %v", err)
  57. }
  58. got := string(data)
  59. expected := `{"range":{"born":{"format":"yyyy/MM/dd","from":"2012/01/01","include_lower":true,"include_upper":true,"to":"now"}}}`
  60. if got != expected {
  61. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  62. }
  63. }