search_aggs_cardinality_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 TestCardinalityAggregation(t *testing.T) {
  10. agg := NewCardinalityAggregation().Field("author.hash")
  11. data, err := json.Marshal(agg.Source())
  12. if err != nil {
  13. t.Fatalf("marshaling to JSON failed: %v", err)
  14. }
  15. got := string(data)
  16. expected := `{"cardinality":{"field":"author.hash"}}`
  17. if got != expected {
  18. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  19. }
  20. }
  21. func TestCardinalityAggregationWithOptions(t *testing.T) {
  22. agg := NewCardinalityAggregation().Field("author.hash").PrecisionThreshold(100).Rehash(true)
  23. data, err := json.Marshal(agg.Source())
  24. if err != nil {
  25. t.Fatalf("marshaling to JSON failed: %v", err)
  26. }
  27. got := string(data)
  28. expected := `{"cardinality":{"field":"author.hash","precision_threshold":100,"rehash":true}}`
  29. if got != expected {
  30. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  31. }
  32. }
  33. func TestCardinalityAggregationWithFormat(t *testing.T) {
  34. agg := NewCardinalityAggregation().Field("author.hash").Format("00000")
  35. data, err := json.Marshal(agg.Source())
  36. if err != nil {
  37. t.Fatalf("marshaling to JSON failed: %v", err)
  38. }
  39. got := string(data)
  40. expected := `{"cardinality":{"field":"author.hash","format":"00000"}}`
  41. if got != expected {
  42. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  43. }
  44. }