Skip to content

Commit 6debe3c

Browse files
author
Joseph Smith
committed
Rename combine to fuse
1 parent da7bada commit 6debe3c

3 files changed

Lines changed: 18 additions & 18 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ class ElasticSearch(
261261
vecWeight: Double,
262262
filterOpt: Option[Query]
263263
)(implicit ex: ExecutionContext, logMarker: LogMarker): Future[SearchResults] = {
264-
import HybridResult.{resolveHitAndFillInSemanticScore, combineScoresAndGetTopK}
264+
import HybridResult.{resolveHitAndFillInSemanticScore, fuseScoresAndGetTopK}
265265

266266
val lexicalQuery = createMultiMatchQuery(query, operator = Or)
267267
val lexicalSearchRequest = lexicalRequest(lexicalQuery, k, filterOpt)
@@ -302,7 +302,7 @@ class ElasticSearch(
302302
val resultsWithSemanticScoresFilledIn = distinctHits.flatMap { hit =>
303303
resolveHitAndFillInSemanticScore(hit, queryEmbedding, resolveHit)
304304
}
305-
val topK = combineScoresAndGetTopK(resultsWithSemanticScoresFilledIn, vecWeight, k)
305+
val topK = fuseScoresAndGetTopK(resultsWithSemanticScoresFilledIn, vecWeight, k)
306306
topK.foreach { r =>
307307
logger.info(logMarker, s"hybrid result ${r.id}: lexicalScore=${r.lexicalScore} semanticScore=${r.semanticScore} " +
308308
s"originallyFromLexical=${lexicalIds.contains(r.id)} originallyFromSemantic=${semanticIds.contains(r.id)}")

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ object HybridResult {
3838
if (max == theoreticalMin) 0.0
3939
else (score - theoreticalMin) / (max - theoreticalMin)
4040

41-
def combinedScore(
41+
def fuseScores(
4242
result: HybridResult,
4343
maxLexicalScore: Double,
4444
maxSemanticScore: Double,
@@ -51,7 +51,7 @@ object HybridResult {
5151
(vecWeight * normedSemanticScore) + ((1 - vecWeight) * normedLexicalScore)
5252
}
5353

54-
def combineScoresAndGetTopK(
54+
def fuseScoresAndGetTopK(
5555
results: List[HybridResult],
5656
vecWeight: Double,
5757
k: Int
@@ -68,7 +68,7 @@ object HybridResult {
6868
maxSemanticScore <- results.map(_.semanticScore).maxOption
6969
} yield {
7070
results
71-
.sortBy(combinedScore(_, maxLexicalScore, maxSemanticScore, vecWeight))(Ordering[Double].reverse)
71+
.sortBy(fuseScores(_, maxLexicalScore, maxSemanticScore, vecWeight))(Ordering[Double].reverse)
7272
.take(k)
7373
}).getOrElse(List())
7474
}

media-api/test/lib/elasticsearch/HybridResultTest.scala

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import play.api.libs.json.Json
1010

1111
class HybridResultTest extends AnyFunSpec with Matchers with OptionValues with Tolerance with Fixtures {
1212

13-
import HybridResult.{combineScoresAndGetTopK, combinedScore, normalise, resolveHitAndFillInSemanticScore}
13+
import HybridResult.{fuseScoresAndGetTopK, fuseScores, normalise, resolveHitAndFillInSemanticScore}
1414

1515
private val tolerance = 1e-9
1616

@@ -153,7 +153,7 @@ class HybridResultTest extends AnyFunSpec with Matchers with OptionValues with T
153153
it("blends the normalised lexical and semantic scores using vecWeight") {
154154
// normedLexical = 2/4 = 0.5, normedSemantic = (0 + 1)/(1 + 1) = 0.5
155155
// combined = 0.25 * 0.5 + 0.75 * 0.5 = 0.5
156-
val score = combinedScore(
156+
val score = fuseScores(
157157
hybridResult("blend", lexicalScore = 2.0, semanticScore = 0.0),
158158
maxLexicalScore = 4.0,
159159
maxSemanticScore = 1.0,
@@ -163,7 +163,7 @@ class HybridResultTest extends AnyFunSpec with Matchers with OptionValues with T
163163
}
164164

165165
it("uses only the lexical score when vecWeight is 0") {
166-
val score = combinedScore(
166+
val score = fuseScores(
167167
hybridResult("lexical-only", lexicalScore = 2.0, semanticScore = 1.0),
168168
maxLexicalScore = 4.0,
169169
maxSemanticScore = 1.0,
@@ -173,7 +173,7 @@ class HybridResultTest extends AnyFunSpec with Matchers with OptionValues with T
173173
}
174174

175175
it("uses only the semantic score when vecWeight is 1") {
176-
val score = combinedScore(
176+
val score = fuseScores(
177177
hybridResult("semantic-only", lexicalScore = 2.0, semanticScore = 0.0),
178178
maxLexicalScore = 4.0,
179179
maxSemanticScore = 1.0,
@@ -184,7 +184,7 @@ class HybridResultTest extends AnyFunSpec with Matchers with OptionValues with T
184184
}
185185

186186
it("contributes 0 from the lexical side when the max lexical score is 0") {
187-
val score = combinedScore(
187+
val score = fuseScores(
188188
hybridResult("no-lexical", lexicalScore = 0.0, semanticScore = 1.0),
189189
maxLexicalScore = 0.0,
190190
maxSemanticScore = 1.0,
@@ -196,7 +196,7 @@ class HybridResultTest extends AnyFunSpec with Matchers with OptionValues with T
196196
}
197197

198198
it("contributes 0 from the semantic side when the max semantic score is -1") {
199-
val score = combinedScore(
199+
val score = fuseScores(
200200
hybridResult("no-semantic", lexicalScore = 4.0, semanticScore = -1.0),
201201
maxLexicalScore = 4.0,
202202
maxSemanticScore = -1.0,
@@ -210,7 +210,7 @@ class HybridResultTest extends AnyFunSpec with Matchers with OptionValues with T
210210

211211
describe("combineScoresAndGetTopK") {
212212
it("returns an empty list if the input is empty") {
213-
combineScoresAndGetTopK(List(), vecWeight = 0.0, k = 3) should be (List())
213+
fuseScoresAndGetTopK(List(), vecWeight = 0.0, k = 3) should be (List())
214214
}
215215

216216
it("doesn't break when tha max lexical score is 0") {
@@ -228,7 +228,7 @@ class HybridResultTest extends AnyFunSpec with Matchers with OptionValues with T
228228
hybridResult("mid-lexical", lexicalScore = 5.0, semanticScore = 0.0)
229229
)
230230

231-
val ranked = combineScoresAndGetTopK(results, vecWeight = 0.0, k = 3)
231+
val ranked = fuseScoresAndGetTopK(results, vecWeight = 0.0, k = 3)
232232

233233
ranked.map(_.id) should be(List("high-lexical", "mid-lexical", "low-lexical"))
234234
}
@@ -240,7 +240,7 @@ class HybridResultTest extends AnyFunSpec with Matchers with OptionValues with T
240240
hybridResult("mid-lexical", lexicalScore = 5.0, semanticScore = -1.0)
241241
)
242242

243-
val ranked = combineScoresAndGetTopK(results, vecWeight = 0.5, k = 3)
243+
val ranked = fuseScoresAndGetTopK(results, vecWeight = 0.5, k = 3)
244244

245245
ranked.map(_.id) should be(List("high-lexical", "mid-lexical", "low-lexical"))
246246
}
@@ -252,7 +252,7 @@ class HybridResultTest extends AnyFunSpec with Matchers with OptionValues with T
252252
hybridResult("mid-semantic", lexicalScore = 5.0, semanticScore = 0.0)
253253
)
254254

255-
val ranked = combineScoresAndGetTopK(results, vecWeight = 1.0, k = 3)
255+
val ranked = fuseScoresAndGetTopK(results, vecWeight = 1.0, k = 3)
256256

257257
ranked.map(_.id) should be(List("high-semantic", "mid-semantic", "low-semantic"))
258258
}
@@ -264,7 +264,7 @@ class HybridResultTest extends AnyFunSpec with Matchers with OptionValues with T
264264
hybridResult("mid-semantic", lexicalScore = 0.0, semanticScore = 0.0)
265265
)
266266

267-
val ranked = combineScoresAndGetTopK(results, vecWeight = 0.5, k = 3)
267+
val ranked = fuseScoresAndGetTopK(results, vecWeight = 0.5, k = 3)
268268

269269
ranked.map(_.id) should be(List("high-semantic", "mid-semantic", "low-semantic"))
270270
}
@@ -276,7 +276,7 @@ class HybridResultTest extends AnyFunSpec with Matchers with OptionValues with T
276276
hybridResult("c", lexicalScore = 5.0, semanticScore = 1.0)
277277
)
278278

279-
val ranked = combineScoresAndGetTopK(results, vecWeight = 0.0, k = 2)
279+
val ranked = fuseScoresAndGetTopK(results, vecWeight = 0.0, k = 2)
280280

281281
ranked should have size 2
282282
ranked.map(_.id) should be(List("b", "c"))
@@ -292,7 +292,7 @@ class HybridResultTest extends AnyFunSpec with Matchers with OptionValues with T
292292
hybridResult("blend", lexicalScore = 2.0, semanticScore = 0.0)
293293
)
294294

295-
val ranked = combineScoresAndGetTopK(results, vecWeight = 0.25, k = 3)
295+
val ranked = fuseScoresAndGetTopK(results, vecWeight = 0.25, k = 3)
296296

297297
val blend = ranked.find(_.id == "blend").value
298298
// Recompute the expected combined score independently of ordering.

0 commit comments

Comments
 (0)