search_aggs_tophits_test.go 788 B

123456789101112131415161718192021222324252627
  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 TestTopHitsAggregation(t *testing.T) {
  10. fsc := NewFetchSourceContext(true).Include("title")
  11. agg := NewTopHitsAggregation().
  12. Sort("last_activity_date", false).
  13. FetchSourceContext(fsc).
  14. Size(1)
  15. data, err := json.Marshal(agg.Source())
  16. if err != nil {
  17. t.Fatalf("marshaling to JSON failed: %v", err)
  18. }
  19. got := string(data)
  20. expected := `{"top_hits":{"_source":{"excludes":[],"includes":["title"]},"size":1,"sort":[{"last_activity_date":{"order":"desc"}}]}}`
  21. if got != expected {
  22. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  23. }
  24. }