Skip to content

Commit a7115e2

Browse files
author
Joseph Smith
committed
De-duplicate before doing any extra work
1 parent c4097e4 commit a7115e2

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

media-api/app/lib/elasticsearch/ElasticSearch.scala

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -296,14 +296,11 @@ class ElasticSearch(
296296
results: List[HybridResult],
297297
vecWeight: Double,
298298
k: Int
299-
)(implicit logMarker: LogMarker): List[HybridResult] = {
300-
val dedupedResults = results.distinctBy(_.id)
301-
logger.info(logMarker, s"${dedupedResults.length} deduped results")
302-
299+
): List[HybridResult] = {
303300
// Account for the rare case in which KNN doesn't return the true closest vector,
304301
// and that true closest vector happens to be among the lexical-only results.
305-
val maxLexicalScore = dedupedResults.maxBy(_.lexicalScore).lexicalScore
306-
val maxSemanticScore = dedupedResults.maxBy(_.semanticScore).semanticScore
302+
val maxLexicalScore = results.maxBy(_.lexicalScore).lexicalScore
303+
val maxSemanticScore = results.maxBy(_.semanticScore).semanticScore
307304

308305
def combinedScore(result: HybridResult): Double = {
309306
val normedLexicalScore = result.lexicalScore / maxLexicalScore
@@ -314,7 +311,7 @@ class ElasticSearch(
314311
(vecWeight * normedSemanticScore) + ((1 - vecWeight) * normedLexicalScore)
315312
}
316313

317-
dedupedResults.sortBy(combinedScore)(Ordering[Double].reverse).take(k)
314+
results.sortBy(combinedScore)(Ordering[Double].reverse).take(k)
318315
}
319316
}
320317

@@ -361,9 +358,15 @@ class ElasticSearch(
361358
lexical <- lexicalSearchResponse
362359
semantic <- semanticSearchResponse
363360
} yield {
364-
logger.info(logMarker, s"${lexical.result.hits.total} lexical hits, ${semantic.result.hits.total} semantic hits")
365-
val allHits = lexical.result.hits.hits.toList ::: semantic.result.hits.hits.toList
366-
val resultsWithSemanticScoresFilledIn = allHits.flatMap { hit =>
361+
val lexicalHits = lexical.result.hits.hits.toList
362+
val semanticHits = semantic.result.hits.hits.toList
363+
val allHits = lexicalHits ::: semanticHits
364+
logger.info(logMarker, s"${allHits.length} total hits: ${lexicalHits.length} lexical, ${semanticHits.length} semantic")
365+
366+
val distinctHits = allHits.distinctBy(_.id)
367+
logger.info(logMarker, s"${distinctHits.length} distinct hits")
368+
369+
val resultsWithSemanticScoresFilledIn = distinctHits.flatMap { hit =>
367370
resolveHitAndFillInSemanticScore(hit, queryEmbedding)
368371
}
369372
val topK = combineScoresAndGetTopK(resultsWithSemanticScoresFilledIn, vecWeight, k)

0 commit comments

Comments
 (0)