Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.gu.mediaservice.lib

object VectorUtils {
def dotProduct(vectorOne: List[Double], vectorTwo: List[Double]): Double = {
vectorOne.zip(vectorTwo).map(component => component._1 * component._2).sum
}

def magnitude(vector: List[Double]): Double = {
math.sqrt(vector.map(math.pow(_, 2)).sum)
}

def cosineSimilarity(vectorOne: List[Double], vectorTwo: List[Double]): Double = {
val magnitudeProduct = magnitude(vectorOne) * magnitude(vectorTwo)
if (magnitudeProduct == 0.0) 0.0
else dotProduct(vectorOne, vectorTwo) / magnitudeProduct
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.gu.mediaservice.lib.elasticsearch

import com.gu.mediaservice.lib.logging._
import com.sksamuel.elastic4s.ElasticDsl.RichRequest
import com.sksamuel.elastic4s._

import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.gu.mediaservice.lib

import org.scalatest.funspec.AnyFunSpec
import org.scalatest.matchers.should.Matchers


class VectorUtilsTest extends AnyFunSpec with Matchers {
it ("should compute the dot product of two vectors") {
VectorUtils.dotProduct(List(1.0, 2.0, 3.0), List(4.0, 5.0, 6.0)) shouldBe 32.0
}

it ("should compute a dot product of zero for orthogonal vectors") {
VectorUtils.dotProduct(List(1.0, 0.0), List(0.0, 1.0)) shouldBe 0.0
}

it ("should return zero for the dot product of empty vectors") {
VectorUtils.dotProduct(List.empty, List.empty) shouldBe 0.0
}

it ("should compute the magnitude of a vector") {
VectorUtils.magnitude(List(3.0, 4.0)) shouldBe 5.0
}

it ("should return a magnitude of zero for a zero vector") {
VectorUtils.magnitude(List(0.0, 0.0, 0.0)) shouldBe 0.0
}

it ("should return a cosine similarity of 1 for identical vectors") {
VectorUtils.cosineSimilarity(List(1.0, 2.0, 3.0), List(1.0, 2.0, 3.0)) shouldBe 1.0 +- 1e-9
}

it ("should return a cosine similarity of 1 for parallel vectors") {
VectorUtils.cosineSimilarity(List(1.0, 2.0, 3.0), List(2.0, 4.0, 6.0)) shouldBe 1.0 +- 1e-9
}

it ("should return a cosine similarity of 0 for orthogonal vectors") {
VectorUtils.cosineSimilarity(List(1.0, 0.0), List(0.0, 1.0)) shouldBe 0.0 +- 1e-9
}

it ("should return a cosine similarity of -1 for opposite vectors") {
VectorUtils.cosineSimilarity(List(1.0, 2.0, 3.0), List(-1.0, -2.0, -3.0)) shouldBe -1.0 +- 1e-9
}

it ("should return a cosine similarity of 0 when the first vector has zero magnitude") {
VectorUtils.cosineSimilarity(List(0.0, 0.0, 0.0), List(1.0, 2.0, 3.0)) shouldBe 0.0 +- 1e-9
}

it ("should return a cosine similarity of 0 when the second vector has zero magnitude") {
VectorUtils.cosineSimilarity(List(1.0, 2.0, 3.0), List(0.0, 0.0, 0.0)) shouldBe 0.0 +- 1e-9
}

it ("should return a cosine similarity of 0 when both vectors have zero magnitude") {
VectorUtils.cosineSimilarity(List(0.0, 0.0, 0.0), List(0.0, 0.0, 0.0)) shouldBe 0.0 +- 1e-9
}
}
1 change: 1 addition & 0 deletions kahuna/public/js/search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ search.config(['$stateProvider', '$urlMatcherFactoryProvider',
'orderBy',
'useAISearch',
'vecWeight',
'fillScores',
'dateField',
'takenSince',
'takenUntil',
Expand Down
11 changes: 11 additions & 0 deletions kahuna/public/js/search/query.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@
Use AI search
</label>
</span>
<span id="fill-scores-select" class="search__modifier-item"
ng-if="searchQuery.shouldDisplayAISearchOption && searchQuery.useAISearch">
<label>
Fill scores:
<select ng-model="searchQuery.fillScoresMode">
<option value="off">Off (default hybrid)</option>
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
</select>
</label>
</span>
<span class="search-query">
<gr-icon class="search-query__magnifier search-query__icon">search</gr-icon>
<gr-structured-query class="search-query__query"
Expand Down
24 changes: 20 additions & 4 deletions kahuna/public/js/search/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ query.controller('SearchQueryCtrl', [
if (!ctrl.shouldDisplayAISearchOption) {
ctrl.useAISearch = false;
ctrl.vecWeight = undefined;
ctrl.fillScoresMode = 'off';
} else {
ctrl.useAISearch = ($stateParams.useAISearch === 'true' || $stateParams.useAISearch === true) ? true : false;
ctrl.vecWeight = $stateParams.vecWeight;
ctrl.fillScoresMode = $stateParams.fillScores || 'off';
}

//--react - angular interop events--
Expand Down Expand Up @@ -389,8 +391,8 @@ query.controller('SearchQueryCtrl', [
Object.keys($stateParams).
// Exclude date-related filters, managed separately in dateFilter
filter(key => dateFilterParams.indexOf(key) === -1).
// Exclude useAISearch, managed separately by its own dedicated watcher
filter(key => key !== 'useAISearch').
// Exclude useAISearch and fillScores, managed separately by their own dedicated watchers
filter(key => key !== 'useAISearch' && key !== 'fillScores').
forEach(setAndWatchParam);

// URL parameters are not decoded when taken out of the params.
Expand Down Expand Up @@ -458,10 +460,24 @@ query.controller('SearchQueryCtrl', [
$state.go('search.results', {
...ctrl.filter,
useAISearch: true,
vecWeight: ctrl.vecWeight
vecWeight: ctrl.vecWeight,
fillScores: (ctrl.fillScoresMode && ctrl.fillScoresMode !== 'off') ? ctrl.fillScoresMode : null
});
} else {
$state.go('search.results', {...ctrl.filter, useAISearch: null});
// fillScores is only meaningful with AI search, so clear it when AI search is disabled
ctrl.fillScoresMode = 'off';
$state.go('search.results', {...ctrl.filter, useAISearch: null, fillScores: null});
}
});
$scope.$watch(() => ctrl.fillScoresMode, () => {
// fillScores only applies when AI search is enabled
if (ctrl.useAISearch) {
$state.go('search.results', {
...ctrl.filter,
useAISearch: true,
vecWeight: ctrl.vecWeight,
fillScores: (ctrl.fillScoresMode && ctrl.fillScoresMode !== 'off') ? ctrl.fillScoresMode : null
});
}
});

Expand Down
1 change: 1 addition & 0 deletions kahuna/public/js/search/results.js
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ results.controller('SearchResultsCtrl', [
orderBy: orderBy,
useAISearch: $stateParams.useAISearch,
vecWeight: $stateParams.vecWeight,
fillScores: $stateParams.fillScores,
hasRightsAcquired: $stateParams.hasRightsAcquired,
hasCrops: $stateParams.hasCrops,
syndicationStatus: $stateParams.syndicationStatus,
Expand Down
5 changes: 3 additions & 2 deletions kahuna/public/js/services/api/media-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ mediaApi.factory('mediaApi',
payType, uploadedBy, offset, length, orderBy,
takenSince, takenUntil,
modifiedSince, modifiedUntil, hasRightsAcquired, hasCrops,
syndicationStatus, countAll, persisted, useAISearch, vecWeight} = {}) {
syndicationStatus, countAll, persisted, useAISearch, vecWeight, fillScores} = {}) {
return root.follow('search', {
q: query,
since: since,
Expand All @@ -66,7 +66,8 @@ mediaApi.factory('mediaApi',
countAll,
persisted,
useAISearch: maybeStringToBoolean(useAISearch),
vecWeight: vecWeight
vecWeight: vecWeight,
fillScores: fillScores
}).get();
}

Expand Down
5 changes: 4 additions & 1 deletion media-api/app/controllers/MediaApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ class MediaApi(
"countAll",
"persisted",
"useAISearch",
"vecWeight"
"vecWeight",
"fillScores"
).mkString(",")

private val searchLinkHref = s"${config.rootUri}/images{?$searchParamList}"
Expand Down Expand Up @@ -651,6 +652,7 @@ class MediaApi(
}

val weight = params.vecWeight.getOrElse(1.0)
val fillScoresMode = params.fillScoresMode

// cache.get(key) is atomic: if two requests race on the same key, only one
// load fires and both callers receive the same Future.
Expand All @@ -667,6 +669,7 @@ class MediaApi(
k = k,
numCandidates = Math.max(k * 2, 100),
vecWeight = weight,
fillScoresMode = fillScoresMode,
filterOpt = filterOpt
)
} yield searchResults
Expand Down
Loading
Loading