Skip to content
Merged
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
46 changes: 46 additions & 0 deletions LiteDB.Tests/Query/VectorIndex_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,52 @@ public void TopKNear_UsesVectorIndex()
results.Select(x => x.Id).Should().Equal(new[] { 1, 3 });
}

[Fact]
public void OrderBy_VectorSimilarity_WithCompositeOrdering_UsesVectorIndex()
{
using var db = new LiteDatabase(":memory:");
var collection = db.GetCollection<VectorDocument>("vectors");

collection.Insert(new[]
{
new VectorDocument { Id = 1, Embedding = new[] { 1f, 0f }, Flag = true },
new VectorDocument { Id = 2, Embedding = new[] { 1f, 0f }, Flag = false },
new VectorDocument { Id = 3, Embedding = new[] { 0f, 1f }, Flag = true }
});

collection.EnsureIndex(
"embedding_idx",
BsonExpression.Create("$.Embedding"),
new VectorIndexOptions(2, VectorDistanceMetric.Cosine));

var similarity = BsonExpression.Create("VECTOR_SIM($.Embedding, [1.0, 0.0])");

var query = (LiteQueryable<VectorDocument>)collection.Query()
.OrderBy(similarity, Query.Ascending)
.ThenBy(x => x.Flag);

var queryField = typeof(LiteQueryable<VectorDocument>).GetField("_query", BindingFlags.NonPublic | BindingFlags.Instance);
var definition = (Query)queryField.GetValue(query);

definition.OrderBy.Should().HaveCount(2);
definition.OrderBy[0].Expression.Type.Should().Be(BsonExpressionType.VectorSim);

definition.VectorField = "$.Embedding";
definition.VectorTarget = new[] { 1f, 0f };
definition.VectorMaxDistance = double.MaxValue;

var plan = query.GetPlan();

plan["index"]["mode"].AsString.Should().Be("VECTOR INDEX SEARCH");
plan["index"]["expr"].AsString.Should().Be("$.Embedding");
plan.ContainsKey("orderBy").Should().BeFalse();

var results = query.ToArray();

results.Should().HaveCount(3);
results.Select(x => x.Id).Should().BeEquivalentTo(new[] { 1, 2, 3 });
}

[Fact]
public void WhereNear_DotProductHonorsMinimumSimilarity()
{
Expand Down
14 changes: 9 additions & 5 deletions LiteDB/Engine/Query/QueryOptimization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,16 @@ private bool TrySelectVectorIndex(out VectorIndexQuery index, out BsonExpression
}
}

if (expression == null && _query.OrderBy != null)
if (expression == null && _query.OrderBy.Count > 0)
{
if (this.TryParseVectorExpression(_query.OrderBy, out expression, out target))
foreach (var order in _query.OrderBy)
{
matchedFromOrderBy = true;
maxDistance = double.MaxValue;
if (this.TryParseVectorExpression(order.Expression, out expression, out target))
{
matchedFromOrderBy = true;
maxDistance = double.MaxValue;
break;
}
}
}

Expand All @@ -340,7 +344,7 @@ private bool TrySelectVectorIndex(out VectorIndexQuery index, out BsonExpression
expression = NormalizeVectorField(_query.VectorField);
target = _query.VectorTarget?.ToArray();
maxDistance = _query.VectorMaxDistance;
matchedFromOrderBy = matchedFromOrderBy || (_query.OrderBy != null && _query.OrderBy.Type == BsonExpressionType.VectorSim);
matchedFromOrderBy = matchedFromOrderBy || (_query.OrderBy.Any(order => order.Expression?.Type == BsonExpressionType.VectorSim));
}

if (expression == null || target == null)
Expand Down
Loading