Skip to content

Commit

Permalink
chore: rename MVI search hit Distance property to Score (#514)
Browse files Browse the repository at this point in the history
To align with the scoreThreshold option in search, and for
clarity in that the search hits are not ordered by distance (which
is lower or better), but by score (which could be higher or better,
or a blend of multiple things), we rename the distance property to
score.
  • Loading branch information
nand4011 authored Nov 8, 2023
1 parent 0885ab2 commit 714c46b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
23 changes: 12 additions & 11 deletions src/Momento.Sdk/Responses/Vector/SearchHit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Momento.Sdk.Responses.Vector;
using System.Collections.Generic;

/// <summary>
/// A hit from a vector search. Contains the ID of the vector, the distance from the query vector,
/// A hit from a vector search. Contains the ID of the vector, the search score,
/// and any requested metadata.
/// </summary>
public class SearchHit
Expand All @@ -16,9 +16,9 @@ public class SearchHit
public string Id { get; }

/// <summary>
/// The distance from the query vector.
/// The similarity to the query vector.
/// </summary>
public double Distance { get; }
public double Score { get; }

/// <summary>
/// Requested metadata associated with the hit.
Expand All @@ -29,37 +29,38 @@ public class SearchHit
/// Constructs a SearchHit with no metadata.
/// </summary>
/// <param name="id">The ID of the hit.</param>
/// <param name="distance">The distance from the query vector.</param>
public SearchHit(string id, double distance)
/// <param name="score">The similarity to the query vector.</param>
public SearchHit(string id, double score)
{
Id = id;
Distance = distance;
Score = score;
Metadata = new Dictionary<string, MetadataValue>();
}

/// <summary>
/// Constructs a SearchHit.
/// </summary>
/// <param name="id">The ID of the hit.</param>
/// <param name="distance">The distance from the query vector.</param>
/// <param name="score">The similarity to the query vector.</param>
/// <param name="metadata">Requested metadata associated with the hit</param>
public SearchHit(string id, double distance, Dictionary<string, MetadataValue> metadata)
public SearchHit(string id, double score, Dictionary<string, MetadataValue> metadata)
{
Id = id;
Distance = distance;
Score = score;
Metadata = metadata;
}

/// <inheritdoc />
public override bool Equals(object obj)
{
if (ReferenceEquals(this, obj)) return true;
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
if (obj is null || GetType() != obj.GetType()) return false;

var other = (SearchHit)obj;

// ReSharper disable once CompareOfFloatsByEqualityOperator
if (Id != other.Id || Distance != other.Distance) return false;
if (Id != other.Id || Score != other.Score) return false;

// Compare Metadata dictionaries
if (Metadata.Count != other.Metadata.Count) return false;
Expand All @@ -81,7 +82,7 @@ public override int GetHashCode()
var hash = 17;

hash = hash * 23 + Id.GetHashCode();
hash = hash * 23 + Distance.GetHashCode();
hash = hash * 23 + Score.GetHashCode();

foreach (var pair in Metadata)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Momento.Sdk/Responses/Vector/SearchResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Success(List<SearchHit> hits)
/// <inheritdoc />
public override string ToString()
{
var displayedHits = Hits.Take(5).Select(hit => $"{hit.Id} ({hit.Distance})");
var displayedHits = Hits.Take(5).Select(hit => $"{hit.Id} ({hit.Score})");
return $"{base.ToString()}: {string.Join(", ", displayedHits)}...";
}

Expand Down

0 comments on commit 714c46b

Please sign in to comment.