search_facets_histogram_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 TestHistogramFacetWithField(t *testing.T) {
  10. f := NewHistogramFacet().Field("field_name").Interval(100)
  11. data, err := json.Marshal(f.Source())
  12. if err != nil {
  13. t.Fatalf("marshaling to JSON failed: %v", err)
  14. }
  15. got := string(data)
  16. expected := `{"histogram":{"field":"field_name","interval":100}}`
  17. if got != expected {
  18. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  19. }
  20. }
  21. func TestHistogramFacetWithValueField(t *testing.T) {
  22. f := NewHistogramFacet().
  23. KeyField("timestamp").
  24. ValueField("price").
  25. TimeInterval("1.5d")
  26. data, err := json.Marshal(f.Source())
  27. if err != nil {
  28. t.Fatalf("marshaling to JSON failed: %v", err)
  29. }
  30. got := string(data)
  31. expected := `{"histogram":{"key_field":"timestamp","time_interval":"1.5d","value_field":"price"}}`
  32. if got != expected {
  33. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  34. }
  35. }
  36. func TestHistogramFacetWithGlobals(t *testing.T) {
  37. f := NewHistogramFacet().
  38. KeyField("timestamp").
  39. ValueField("price").
  40. Interval(1000).
  41. Global(true).
  42. FacetFilter(NewTermFilter("user", "kimchy"))
  43. data, err := json.Marshal(f.Source())
  44. if err != nil {
  45. t.Fatalf("marshaling to JSON failed: %v", err)
  46. }
  47. got := string(data)
  48. expected := `{"facet_filter":{"term":{"user":"kimchy"}},"global":true,"histogram":{"interval":1000,"key_field":"timestamp","value_field":"price"}}`
  49. if got != expected {
  50. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  51. }
  52. }