}
protected SearchResponse filterMaxHits(SearchResponse response, int maxHits) {
// We will use internal APIs here for efficiency. The plugin has restricted explicit ES compatibility
// anyway. Alternatively, we could serialize/ filter/ deserialize JSON, but this seems simpler.
SearchHits allHits = response.getHits();
InternalSearchHit [] trimmedHits = new InternalSearchHit[Math.min(maxHits, allHits.hits().length)];
System.arraycopy(allHits.hits(), 0, trimmedHits, 0, trimmedHits.length);
InternalFacets facets = null;
if (response.getFacets() != null) {
facets = new InternalFacets(response.getFacets().facets());
}
InternalAggregations aggregations = null;
if (response.getAggregations() != null) {
aggregations = new InternalAggregations(toInternal(response.getAggregations().asList()));
}
return new SearchResponse(
new InternalSearchResponse(
new InternalSearchHits(trimmedHits, allHits.getTotalHits(), allHits.getMaxScore()),
facets,
aggregations,
response.getSuggest(),
response.isTimedOut()),
response.getScrollId(),