search_collapse_builder_test.go 834 B

1234567891011121314151617181920212223242526272829
  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. package elastic
  5. import (
  6. "encoding/json"
  7. "testing"
  8. )
  9. func TestCollapseBuilderSource(t *testing.T) {
  10. b := NewCollapseBuilder("user").
  11. InnerHit(NewInnerHit().Name("last_tweets").Size(5).Sort("date", true)).
  12. MaxConcurrentGroupRequests(4)
  13. src, err := b.Source()
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. data, err := json.Marshal(src)
  18. if err != nil {
  19. t.Fatalf("marshaling to JSON failed: %v", err)
  20. }
  21. got := string(data)
  22. expected := `{"field":"user","inner_hits":{"name":"last_tweets","size":5,"sort":[{"date":{"order":"asc"}}]},"max_concurrent_group_searches":4}`
  23. if got != expected {
  24. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  25. }
  26. }