Skip to content

Commit

Permalink
Fix leak in collapsing search results (elastic#110927)
Browse files Browse the repository at this point in the history
Fixing this case for now by enforcing unpooled to plug the leak, this
needs a little more work to function well pooled.
  • Loading branch information
original-brownbear authored and lkts committed Jul 16, 2024
1 parent eac7604 commit e317430
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/changelog/110927.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 110927
summary: Fix leak in collapsing search results
area: Search
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,27 @@ public void testCollapseWithDocValueFields() {
}
);
}

public void testCollapseWithFields() {
final String indexName = "test_collapse";
createIndex(indexName);
final String collapseField = "collapse_field";
final String otherField = "other_field";
assertAcked(indicesAdmin().preparePutMapping(indexName).setSource(collapseField, "type=keyword", otherField, "type=keyword"));
index(indexName, "id_1_0", Map.of(collapseField, "value1", otherField, "other_value1"));
index(indexName, "id_1_1", Map.of(collapseField, "value1", otherField, "other_value2"));
index(indexName, "id_2_0", Map.of(collapseField, "value2", otherField, "other_value3"));
refresh(indexName);

assertNoFailuresAndResponse(
prepareSearch(indexName).setQuery(new MatchAllQueryBuilder())
.setFetchSource(false)
.addFetchField(otherField)
.setCollapse(new CollapseBuilder(collapseField).setInnerHits(new InnerHitBuilder("ih").setSize(2))),
searchResponse -> {
assertEquals(collapseField, searchResponse.getHits().getCollapseField());
assertEquals(Set.of(new BytesRef("value1"), new BytesRef("value2")), Set.of(searchResponse.getHits().getCollapseValues()));
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ private void doRun() {
if (hit.getInnerHits() == null) {
hit.setInnerHits(Maps.newMapWithExpectedSize(innerHitBuilders.size()));
}
if (hit.isPooled() == false) {
// TODO: make this work pooled by forcing the hit itself to become pooled as needed here
innerHits = innerHits.asUnpooled();
}
hit.getInnerHits().put(innerHitBuilder.getName(), innerHits);
assert innerHits.isPooled() == false || hit.isPooled() : "pooled inner hits can only be added to a pooled hit";
innerHits.mustIncRef();
Expand Down

0 comments on commit e317430

Please sign in to comment.