Actually, now that you refactored LongHash, I think this can be made much simpler: we can just iterate with `i` from `0` to `bucketOrds.size()` (which is the number of entries in the hash table, instead of `bucketOrds.capacity()` which is the number of slots) and directly use `i` as a bucket ordinal.

```patch
--- a/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/LongTermsAggregator.java
+++ b/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/LongTermsAggregator.java
@@ -108,13 +108,7 @@ public class LongTermsAggregator extends BucketsAggregator {
 
         BucketPriorityQueue ordered = new BucketPriorityQueue(size, order.comparator(this));
         LongTerms.Bucket spare = null;
-        for (long i = 0; i < bucketOrds.capacity(); ++i) {
-            final long ord = bucketOrds.id(i);
-            if (ord < 0) {
-                // slot is not allocated
-                continue;
-            }
-
+        for (long ord = 0; ord < bucketOrds.size(); ++ord) {
             if (spare == null) {
                 spare = new LongTerms.Bucket(0, 0, null);
             }
```

(The same change should apply to other aggs.)