Skip to content

Commit 540fd42

Browse files
committed
Rebase
1 parent c790a29 commit 540fd42

File tree

1 file changed

+81
-54
lines changed

1 file changed

+81
-54
lines changed

src/Weaviate.Client/gRPC/Search.Builders.cs

Lines changed: 81 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -375,64 +375,76 @@ private static (
375375

376376
vector ??= new Models.Vectors();
377377

378-
if (targetVector is null && vector.Count > 0)
378+
targetVector ??= vector.Keys.Where(tv => string.IsNullOrEmpty(tv) is false).ToArray();
379+
380+
targets = targetVector;
381+
382+
if (targetVector.Count() == 1 && vector.Count == 1)
379383
{
380-
targetVector = vector.Keys.Where(tv => string.IsNullOrEmpty(tv) is false).ToArray();
384+
// If only one target vector is specified, use Vectors
385+
// This also covers the case where no target vector is specified and only one vector is provided
386+
// In this case, we assume the single provided vector is the target
387+
vectors = vector
388+
.Select(v => new V1.Vectors
389+
{
390+
Name = v.Key,
391+
Type = v.Value.IsMultiVector
392+
? V1.Vectors.Types.VectorType.MultiFp32
393+
: V1.Vectors.Types.VectorType.SingleFp32,
394+
VectorBytes = v.Value.ToByteString(),
395+
})
396+
.ToList();
397+
return (targets, vectorForTarget, vectors);
381398
}
382399

383-
if (targetVector is not null && targetVector.Count() > 0)
400+
if (
401+
targetVector.Count() > 1
402+
&& vector.Count == targetVector.Count()
403+
&& targetVector.All(tv => vector.ContainsKey(tv)) // TODO Throw an exception if the TargetVector does not match the provided vectors?
404+
)
384405
{
385-
targets = targetVector;
386-
387-
if (
388-
targetVector.Count() > 1
389-
&& vector.Count == targetVector.Count()
390-
&& targetVector.All(tv => vector.ContainsKey(tv)) // TODO Throw an exception if the TargetVector does not match the provided vectors?
391-
)
392-
{
393-
// If multiple target vectors are specified, use VectorForTargets
394-
vectorForTarget = targetVector
395-
.Select(
396-
(v, idx) =>
397-
new
398-
{
399-
Name = v,
400-
Index = idx,
401-
Vector = vector.ContainsKey(v)
402-
? vector[v]
403-
: vector.Values.ElementAt(idx),
404-
}
405-
)
406-
.Select(v => new VectorForTarget()
406+
// If multiple target vectors are specified, use VectorForTargets
407+
vectorForTarget = targetVector
408+
.Select(
409+
(v, idx) =>
410+
new
411+
{
412+
Name = v,
413+
Index = idx,
414+
Vector = vector.ContainsKey(v)
415+
? vector[v]
416+
: vector.Values.ElementAt(idx),
417+
}
418+
)
419+
.Select(v => new VectorForTarget()
420+
{
421+
Name = v.Name,
422+
Vectors =
407423
{
408-
Name = v.Name,
409-
Vectors =
424+
new V1.Vectors
410425
{
411-
new V1.Vectors
412-
{
413-
Name = v.Name,
414-
Type = v.Vector.IsMultiVector
415-
? V1.Vectors.Types.VectorType.MultiFp32
416-
: V1.Vectors.Types.VectorType.SingleFp32,
417-
VectorBytes = v.Vector.ToByteString(),
418-
},
426+
Name = v.Name,
427+
Type = v.Vector.IsMultiVector
428+
? V1.Vectors.Types.VectorType.MultiFp32
429+
: V1.Vectors.Types.VectorType.SingleFp32,
430+
VectorBytes = v.Vector.ToByteString(),
419431
},
420-
})
421-
.ToList();
422-
}
423-
else
424-
{
425-
vectors = vector
426-
.Select(v => new V1.Vectors
427-
{
428-
Name = v.Key,
429-
Type = v.Value.IsMultiVector
430-
? V1.Vectors.Types.VectorType.MultiFp32
431-
: V1.Vectors.Types.VectorType.SingleFp32,
432-
VectorBytes = v.Value.ToByteString(),
433-
})
434-
.ToList();
435-
}
432+
},
433+
})
434+
.ToList();
435+
}
436+
else
437+
{
438+
vectors = vector
439+
.Select(v => new V1.Vectors
440+
{
441+
Name = v.Key,
442+
Type = v.Value.IsMultiVector
443+
? V1.Vectors.Types.VectorType.MultiFp32
444+
: V1.Vectors.Types.VectorType.SingleFp32,
445+
VectorBytes = v.Value.ToByteString(),
446+
})
447+
.ToList();
436448
}
437449

438450
return (targets, vectorForTarget, vectors);
@@ -566,9 +578,23 @@ private static void BuildHybrid(
566578

567579
if (vector is not null && nearText is null && nearVector is null)
568580
{
569-
var (targets, _, vectors) = BuildTargetVector(targetVector, vector);
570-
request.HybridSearch.Vectors.AddRange(vectors);
571-
request.HybridSearch.Targets = targets;
581+
var (targets, vfts, vectors) = BuildTargetVector(targetVector, vector);
582+
583+
if (vfts is not null)
584+
{
585+
nearVector = new HybridNearVector(
586+
vector,
587+
Certainty: null,
588+
Distance: null,
589+
targetVector: targetVector
590+
);
591+
vector = null; // Clear vector to avoid duplication
592+
}
593+
else if (vectors is not null)
594+
{
595+
request.HybridSearch.Vectors.Add(vectors);
596+
request.HybridSearch.Targets = targets;
597+
}
572598
}
573599

574600
if (vector is null && nearText is not null && nearVector is null)
@@ -581,6 +607,7 @@ private static void BuildHybrid(
581607
nearText.MoveAway,
582608
targetVector
583609
);
610+
584611
request.HybridSearch.Targets = request.HybridSearch.NearText.Targets;
585612
}
586613

0 commit comments

Comments
 (0)