Skip to content

Commit

Permalink
Allow collapsing on inner hits
Browse files Browse the repository at this point in the history
As described in
https://www.elastic.co/guide/en/elasticsearch/reference/7.9/collapse-search-results.html#second-level-of-collapsing,
collapsing is possible on the second level as well, on the inner hit.

Close olivere#1394
Close olivere#1401
  • Loading branch information
olivere committed Oct 16, 2020
1 parent 8212930 commit 18750ad
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions inner_hit.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ func (hit *InnerHit) Query(query Query) *InnerHit {
return hit
}

func (hit *InnerHit) Collapse(collapse *CollapseBuilder) *InnerHit {
hit.source.Collapse(collapse)
return hit
}

func (hit *InnerHit) From(from int) *InnerHit {
hit.source.From(from)
return hit
Expand Down
17 changes: 17 additions & 0 deletions inner_hit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,20 @@ func TestInnerHitWithName(t *testing.T) {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}

func TestInnerHitSecondLevelCollapse(t *testing.T) {
hit := NewInnerHit().Name("by_location").Size(3).Collapse(NewCollapseBuilder("user.id"))
src, err := hit.Source()
if err != nil {
t.Fatal(err)
}
data, err := json.Marshal(src)
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"collapse":{"field":"user.id"},"name":"by_location","size":3}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}

0 comments on commit 18750ad

Please sign in to comment.