1// Copyright 2012-present 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 5package elastic 6 7import ( 8 "encoding/json" 9 "testing" 10) 11 12func TestTopHitsAggregation(t *testing.T) { 13 fsc := NewFetchSourceContext(true).Include("title") 14 agg := NewTopHitsAggregation(). 15 Sort("last_activity_date", false). 16 FetchSourceContext(fsc). 17 Size(1) 18 src, err := agg.Source() 19 if err != nil { 20 t.Fatal(err) 21 } 22 data, err := json.Marshal(src) 23 if err != nil { 24 t.Fatalf("marshaling to JSON failed: %v", err) 25 } 26 got := string(data) 27 expected := `{"top_hits":{"_source":{"includes":["title"]},"size":1,"sort":[{"last_activity_date":{"order":"desc"}}]}}` 28 if got != expected { 29 t.Errorf("expected\n%s\n,got:\n%s", expected, got) 30 } 31} 32