search_facets_histogram_script_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 TestHistogramScriptFacetWithKeyScripts(t *testing.T) {
  10. f := NewHistogramScriptFacet().
  11. KeyScript("doc['date'].date.minuteOfHour").
  12. ValueScript("doc['num1'].value")
  13. data, err := json.Marshal(f.Source())
  14. if err != nil {
  15. t.Fatalf("marshaling to JSON failed: %v", err)
  16. }
  17. got := string(data)
  18. expected := `{"histogram":{"key_script":"doc['date'].date.minuteOfHour","value_script":"doc['num1'].value"}}`
  19. if got != expected {
  20. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  21. }
  22. }
  23. func TestHistogramScriptFacetWithParams(t *testing.T) {
  24. f := NewHistogramScriptFacet().
  25. KeyScript("doc['date'].date.minuteOfHour * factor1").
  26. ValueScript("doc['num1'].value * factor2").
  27. Param("factor1", 2).
  28. Param("factor2", 3)
  29. data, err := json.Marshal(f.Source())
  30. if err != nil {
  31. t.Fatalf("marshaling to JSON failed: %v", err)
  32. }
  33. got := string(data)
  34. expected := `{"histogram":{"key_script":"doc['date'].date.minuteOfHour * factor1","params":{"factor1":2,"factor2":3},"value_script":"doc['num1'].value * factor2"}}`
  35. if got != expected {
  36. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  37. }
  38. }
  39. func TestHistogramScriptFacetWithGlobals(t *testing.T) {
  40. f := NewHistogramScriptFacet().
  41. KeyScript("doc['date'].date.minuteOfHour").
  42. ValueScript("doc['num1'].value").
  43. Global(true).
  44. FacetFilter(NewTermFilter("user", "kimchy"))
  45. data, err := json.Marshal(f.Source())
  46. if err != nil {
  47. t.Fatalf("marshaling to JSON failed: %v", err)
  48. }
  49. got := string(data)
  50. expected := `{"facet_filter":{"term":{"user":"kimchy"}},"global":true,"histogram":{"key_script":"doc['date'].date.minuteOfHour","value_script":"doc['num1'].value"}}`
  51. if got != expected {
  52. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  53. }
  54. }