Skip to content

Commit 4b157f9

Browse files
authored
Merge pull request #132 from weaviate/chore/more-target-vectors
Fix hybrid multi vector target search issues
2 parents 5ad8cad + e39da50 commit 4b157f9

File tree

4 files changed

+269
-59
lines changed

4 files changed

+269
-59
lines changed

src/Weaviate.Client.Tests/Integration/TestNamedVectorMultiTarget.cs

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,180 @@ string[] targetVector
190190
Assert.Equal(expected, ids);
191191
}
192192

193+
public static IEnumerable<object[]> HybridMultiInputCombinations =>
194+
new List<object[]>
195+
{
196+
new object[]
197+
{
198+
new Vectors
199+
{
200+
{ "first", new[] { 0f, 1f } },
201+
{
202+
"second",
203+
new float[,]
204+
{
205+
{ 1f, 0f, 0f },
206+
{ 0f, 0f, 1f },
207+
}
208+
},
209+
},
210+
new[] { "first", "second" },
211+
},
212+
new object[]
213+
{
214+
new Vectors
215+
{
216+
{
217+
"first",
218+
new float[,]
219+
{
220+
{ 0f, 1f },
221+
{ 0f, 1f },
222+
}
223+
},
224+
{ "second", new[] { 1f, 0f, 0f } },
225+
},
226+
new[] { "first", "second" },
227+
},
228+
new object[]
229+
{
230+
new Vectors
231+
{
232+
{
233+
"first",
234+
new float[,]
235+
{
236+
{ 0f, 1f },
237+
{ 0f, 1f },
238+
}
239+
},
240+
{
241+
"second",
242+
new float[,]
243+
{
244+
{ 1f, 0f, 0f },
245+
{ 0f, 0f, 1f },
246+
}
247+
},
248+
},
249+
new[] { "first", "second" },
250+
},
251+
// The following are equivalent to above, but mimic the Python HybridVector.near_vector usage
252+
new object[]
253+
{
254+
new HybridNearVector(
255+
new Vectors
256+
{
257+
{ "first", new[] { 0f, 1f } },
258+
{
259+
"second",
260+
new float[,]
261+
{
262+
{ 1f, 0f, 0f },
263+
{ 0f, 0f, 1f },
264+
}
265+
},
266+
},
267+
Certainty: null,
268+
Distance: null
269+
),
270+
new[] { "first", "second" },
271+
},
272+
new object[]
273+
{
274+
new HybridNearVector(
275+
new Vectors
276+
{
277+
{
278+
"first",
279+
new float[,]
280+
{
281+
{ 0f, 1f },
282+
{ 0f, 1f },
283+
}
284+
},
285+
{ "second", new[] { 1f, 0f, 0f } },
286+
}
287+
),
288+
new[] { "first", "second" },
289+
},
290+
new object[]
291+
{
292+
new HybridNearVector(
293+
new Vectors
294+
{
295+
{
296+
"first",
297+
new float[,]
298+
{
299+
{ 0f, 1f },
300+
{ 0f, 1f },
301+
}
302+
},
303+
{
304+
"second",
305+
new float[,]
306+
{
307+
{ 1f, 0f, 0f },
308+
{ 0f, 0f, 1f },
309+
}
310+
},
311+
}
312+
),
313+
new[] { "first", "second" },
314+
},
315+
};
316+
317+
[Theory]
318+
[MemberData(nameof(HybridMultiInputCombinations))]
319+
public async Task Test_SameTargetVector_MultipleInputCombinations_Hybrid(
320+
IHybridVectorInput nearVector,
321+
string[] targetVector
322+
)
323+
{
324+
var dummy = await CollectionFactory();
325+
if (dummy.WeaviateVersion < Version.Parse("1.27.0"))
326+
{
327+
Assert.Skip("Multi vector per target is not supported in versions lower than 1.27.0");
328+
}
329+
330+
var collection = await CollectionFactory(
331+
properties: Array.Empty<Property>(),
332+
vectorConfig: new[]
333+
{
334+
Configure.Vectors.SelfProvided(name: "first"),
335+
Configure.Vectors.SelfProvided(name: "second"),
336+
}
337+
);
338+
339+
var uuid1 = await collection.Data.Insert(
340+
new { },
341+
vectors: new Vectors
342+
{
343+
{ "first", new[] { 1f, 0f } },
344+
{ "second", new[] { 0f, 1f, 0f } },
345+
}
346+
);
347+
var uuid2 = await collection.Data.Insert(
348+
new { },
349+
vectors: new Vectors
350+
{
351+
{ "first", new[] { 0f, 1f } },
352+
{ "second", new[] { 1f, 0f, 0f } },
353+
}
354+
);
355+
356+
var objs = await collection.Query.Hybrid(
357+
query: null,
358+
vectors: nearVector,
359+
targetVector: targetVector,
360+
returnMetadata: MetadataOptions.All
361+
);
362+
var ids = objs.Objects.Select(o => o.ID!.Value).OrderBy(x => x).ToList();
363+
var expected = new[] { uuid2, uuid1 }.OrderBy(x => x).ToList();
364+
Assert.Equal(expected, ids);
365+
}
366+
193367
public static IEnumerable<object[]> MultiTargetVectorsWithDistances =>
194368
new List<object[]>
195369
{

src/Weaviate.Client.Tests/Integration/TestSearchHybrid.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ await collection.Query.NearVector(
227227
query: null,
228228
vectors: new HybridNearVector(
229229
obj.Vectors["default"],
230+
Certainty: null,
230231
Distance: Convert.ToSingle(nearVec.First().Metadata.Distance!.Value + 0.001)
231232
),
232233
returnMetadata: MetadataOptions.All
@@ -286,6 +287,7 @@ await collection.Query.Hybrid(
286287
query: null,
287288
vectors: new HybridNearVector(
288289
obj.Vectors["text"],
290+
Certainty: null,
289291
Distance: Convert.ToSingle(nearVec.First().Metadata.Distance!.Value + 0.001)
290292
),
291293
targetVector: ["text"],
@@ -325,6 +327,8 @@ await collection.Query.Hybrid(
325327
query: null,
326328
vectors: new HybridNearText(
327329
"banana",
330+
Certainty: null,
331+
Distance: null,
328332
MoveTo: new Move(force: 0.1f, concepts: ["pudding"]),
329333
MoveAway: new Move(force: 0.1f, concepts: ["smoothie"])
330334
),
@@ -368,6 +372,8 @@ await collection.Query.Hybrid(
368372
query: null,
369373
vectors: new HybridNearText(
370374
"banana",
375+
Certainty: null,
376+
Distance: null,
371377
MoveTo: new Move(force: 0.1f, concepts: ["pudding"]),
372378
MoveAway: new Move(force: 0.1f, concepts: ["smoothie"])
373379
),
@@ -429,7 +435,7 @@ await collection.Query.Hybrid(
429435
objs = (
430436
await collection.Query.Hybrid(
431437
query: null,
432-
vectors: new HybridNearVector(vector, Distance: 0.1f),
438+
vectors: new HybridNearVector(vector, Certainty: null, Distance: 0.1f),
433439
targetVector: new[] { "first", "second" }
434440
)
435441
).Objects.ToList();

src/Weaviate.Client/Models/Search.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ public interface IHybridVectorInput
88

99
public record HybridNearVector(
1010
Models.Vectors? Vector,
11-
float? Distance = null,
1211
float? Certainty = null,
12+
float? Distance = null,
1313
TargetVectors? targetVector = null
1414
) : IHybridVectorInput { };
1515

1616
public record HybridNearText(
1717
string Query,
18-
float? Distance = null,
1918
float? Certainty = null,
19+
float? Distance = null,
2020
Move? MoveTo = null,
2121
Move? MoveAway = null
2222
) : IHybridVectorInput;

0 commit comments

Comments
 (0)