the-algorithm/timelineranker/server/src/main/scala/com/twitter/timelineranker/common/SearchResultDedupAndSortingTransform.scala
twitter-team ef4c5eb65e Twitter Recommendation Algorithm
Please note we have force-pushed a new initial commit in order to remove some publicly-available Twitter user information. Note that this process may be required in the future.
2023-03-31 17:36:31 -05:00

24 lines
821 B
Scala

package com.twitter.timelineranker.common
import com.twitter.servo.util.FutureArrow
import com.twitter.timelineranker.core.CandidateEnvelope
import com.twitter.timelines.model.TweetId
import com.twitter.util.Future
import scala.collection.mutable
/**
* Remove duplicate search results and order them reverse-chron.
*/
object SearchResultDedupAndSortingTransform
extends FutureArrow[CandidateEnvelope, CandidateEnvelope] {
def apply(envelope: CandidateEnvelope): Future[CandidateEnvelope] = {
val seenTweetIds = mutable.Set.empty[TweetId]
val dedupedResults = envelope.searchResults
.filter(result => seenTweetIds.add(result.id))
.sortBy(_.id)(Ordering[TweetId].reverse)
val transformedEnvelope = envelope.copy(searchResults = dedupedResults)
Future.value(transformedEnvelope)
}
}