search_aggs_bucket_geohash_grid_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package elastic
  2. import (
  3. "encoding/json"
  4. "testing"
  5. )
  6. func TestGeoHashGridAggregation(t *testing.T) {
  7. agg := NewGeoHashGridAggregation().Field("location").Precision(5)
  8. src, err := agg.Source()
  9. if err != nil {
  10. t.Fatal(err)
  11. }
  12. data, err := json.Marshal(src)
  13. if err != nil {
  14. t.Fatalf("Marshalling to JSON failed: %v", err)
  15. }
  16. got := string(data)
  17. expected := `{"geohash_grid":{"field":"location","precision":5}}`
  18. if got != expected {
  19. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  20. }
  21. }
  22. func TestGeoHashGridAggregationWithMetaData(t *testing.T) {
  23. agg := NewGeoHashGridAggregation().Field("location").Precision(5)
  24. agg = agg.Meta(map[string]interface{}{"name": "Oliver"})
  25. src, err := agg.Source()
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. data, err := json.Marshal(src)
  30. if err != nil {
  31. t.Fatalf("Marshalling to JSON failed: %v", err)
  32. }
  33. got := string(data)
  34. expected := `{"geohash_grid":{"field":"location","precision":5},"meta":{"name":"Oliver"}}`
  35. if got != expected {
  36. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  37. }
  38. }
  39. func TestGeoHashGridAggregationWithSize(t *testing.T) {
  40. agg := NewGeoHashGridAggregation().Field("location").Precision(5).Size(5)
  41. agg = agg.Meta(map[string]interface{}{"name": "Oliver"})
  42. src, err := agg.Source()
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. data, err := json.Marshal(src)
  47. if err != nil {
  48. t.Fatalf("Marshalling to JSON failed: %v", err)
  49. }
  50. got := string(data)
  51. expected := `{"geohash_grid":{"field":"location","precision":5,"size":5},"meta":{"name":"Oliver"}}`
  52. if got != expected {
  53. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  54. }
  55. }
  56. func TestGeoHashGridAggregationWithShardSize(t *testing.T) {
  57. agg := NewGeoHashGridAggregation().Field("location").Precision(5).ShardSize(5)
  58. agg = agg.Meta(map[string]interface{}{"name": "Oliver"})
  59. src, err := agg.Source()
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. data, err := json.Marshal(src)
  64. if err != nil {
  65. t.Fatalf("Marshalling to JSON failed: %v", err)
  66. }
  67. got := string(data)
  68. expected := `{"geohash_grid":{"field":"location","precision":5,"shard_size":5},"meta":{"name":"Oliver"}}`
  69. if got != expected {
  70. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  71. }
  72. }