Document bucketing algorithm