Compute distances between documents within a bucket