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
19 changes: 13 additions & 6 deletions src/Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ static async Task Main()
{
Name = "Cat",
Description = "Lots of Cats of multiple breeds",
Properties = Property.FromType<Cat>(),
Properties = Property.FromCollection<Cat>(),
VectorConfig = VectorConfigs,
};

Expand All @@ -108,12 +108,19 @@ static async Task Main()
Console.WriteLine($"Collection: {c.Name}");
}

foreach (var cat in cats)
{
var vectors = new NamedVectors() { { "default", cat.Vector } };
// // Normal Insertion Demo
// foreach (var cat in cats)
// {
// var vectors = new NamedVectors() { { "default", cat.Vector } };

var inserted = await collection.Data.Insert(cat.Data, vectors: vectors);
}
// var inserted = await collection.Data.Insert(cat.Data, vectors: vectors);
// }

// Batch Insertion Demo
var batchInsertions = await collection.Data.InsertMany(add =>
{
cats.ForEach(c => add(c.Data, vectors: new() { { "default", c.Vector } }));
});

// Get all objects and sum up the counter property
var result = await collection.Query.List(limit: 250);
Expand Down
46 changes: 46 additions & 0 deletions src/Weaviate.Client.Tests/Integration/Batch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Weaviate.Client.Models;

namespace Weaviate.Client.Tests.Integration;

public partial class BasicTests
{
[Theory]
[ClassData(typeof(DatasetBatchInsertMany))]
public async Task InsertMany(string key)
{
(
int expectedObjects,
int expectedErrors,
int expectedReferences,
int expectedReferencedObjects,
Action<DataClient<dynamic>.InsertDelegate>[] batcher
) = DatasetBatchInsertMany.Cases[key];

var client = await CollectionFactory(
description: "Testing Batch InsertMany",
properties:
[
Property.For<string>("Name"),
Property.For<int>("Size"),
Property.For<double>("Price"),
Property.For<bool>("IsAvailable"),
Property.For<DateTime>("AvailableSince"),
]
);

await client.AddReference(Property.Reference("ref", client.Name));
await client.AddReference(Property.Reference("ref2", client.Name));

var result = await client.Data.InsertMany(batcher);

var data = await client.Query.List(references: [new("ref"), new("ref2")]);

Assert.Equal(expectedObjects, data.Count());
Assert.Equal(expectedErrors, result.Count(r => r.Error != null));
Assert.Equal(expectedReferences, data.Count(r => r.References.Any()));
Assert.Equal(
expectedReferencedObjects,
data.Select(d => d.References.Sum(r => r.Value.Count)).Sum()
);
}
}
154 changes: 154 additions & 0 deletions src/Weaviate.Client.Tests/Integration/Datasets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,158 @@ public static Dictionary<
public DatasetTimeFilter()
: base(Cases.Keys) { }
}

public class DatasetBatchInsertMany : TheoryData<string>
{
public static Dictionary<
string,
(
int expectedObjects,
int expectedErrors,
int expectedReferences,
int expectedReferencedObjects,
Action<DataClient<dynamic>.InsertDelegate>[] batcher
)
> Cases =>
new()
{
["2 simple objects, no errors"] = (
2,
0,
0,
0,
[
add =>
{
add(
new { Name = "some name" },
vectors: new() { { "default", 1, 2, 3 } }
);
add(new { Name = "some other name" }, id: _reusableUuids[0]);
},
]
),
["all data types"] = (
1,
0,
0,
0,
[
add =>
{
add(
new
{
Name = "some name",
Size = 3,
Price = 10.5,
IsAvailable = true,
AvailableSince = new DateTime(2023, 1, 1),
}
);
},
]
),
["wrong type for property"] = (
0,
1,
0,
0,
[
add =>
{
add(new { Name = 1 });
},
]
),
["batch with self-reference"] = (
5,
0,
1,
1,
[
add =>
{
add(new { Name = "Name 1" }, id: _reusableUuids[0]);
add(new { Name = "Name 2" }, id: _reusableUuids[1]);
add(new { Name = "Name 3" }, id: _reusableUuids[2]);
add(new { Name = "Name 4" }, id: _reusableUuids[3]);
},
add =>
{
add(
new { Name = "Name 5" },
references: [new ObjectReference("ref", _reusableUuids[1])]
);
},
]
),
["batch with multiple self-references"] = (
5,
0,
1,
2,
[
add =>
{
add(new { Name = "Name 1" }, id: _reusableUuids[0]);
add(new { Name = "Name 2" }, id: _reusableUuids[1]);
add(new { Name = "Name 3" }, id: _reusableUuids[2]);
add(new { Name = "Name 4" }, id: _reusableUuids[3]);
},
add =>
{
add(
new { Name = "Name 5" },
references:
[
new ObjectReference(
"ref",
_reusableUuids[1],
_reusableUuids[2]
),
]
);
},
]
),
["batch with multiple self-reference properties"] = (
7,
0,
3,
4,
[
add =>
{
add(new { Name = "Name 1" }, id: _reusableUuids[0]);
add(new { Name = "Name 2" }, id: _reusableUuids[1]);
add(new { Name = "Name 3" }, id: _reusableUuids[2]);
add(new { Name = "Name 4" }, id: _reusableUuids[3]);
},
add =>
{
add(
new { Name = "Name 5" },
references: [new("ref", _reusableUuids[1])]
);
add(
new { Name = "Name 6" },
references: [new("ref2", _reusableUuids[2])]
);
add(
new { Name = "Name 7" },
references:
[
new("ref", _reusableUuids[1]),
new("ref2", _reusableUuids[2]),
]
);
},
]
),
};

public DatasetBatchInsertMany()
: base(Cases.Keys) { }
}
}
11 changes: 7 additions & 4 deletions src/Weaviate.Client.Tests/Integration/_Integration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ internal class TestDataValue
public partial class BasicTests : IAsyncDisposable
{
const bool _deleteCollectionsAfterTest = true;
List<string> _collections = new();

WeaviateClient _weaviate;
HttpClient _httpClient;
Expand Down Expand Up @@ -50,9 +51,9 @@ public BasicTests()

public async ValueTask DisposeAsync()
{
if (_deleteCollectionsAfterTest && TestContext.Current.TestMethod?.MethodName is not null)
if (_deleteCollectionsAfterTest && _collections.Count > 0)
{
await _weaviate.Collections.Delete(TestContext.Current.TestMethod!.MethodName);
await Task.WhenAll(_collections.Select(c => _weaviate.Collections.Delete(c)));
}

_weaviate.Dispose();
Expand All @@ -73,7 +74,7 @@ async Task<CollectionClient<TData>> CollectionFactory<TData>(

name = $"{TestContext.Current.TestMethod?.MethodName ?? string.Empty}_{name}";

properties ??= Property.FromType<TData>();
properties ??= Property.FromCollection<TData>();

ArgumentException.ThrowIfNullOrEmpty(name);

Expand All @@ -89,7 +90,7 @@ async Task<CollectionClient<TData>> CollectionFactory<TData>(
},
};

references = references ?? [];
references ??= [];

var c = new Collection
{
Expand All @@ -104,6 +105,8 @@ async Task<CollectionClient<TData>> CollectionFactory<TData>(

var collectionClient = await _weaviate.Collections.Create<TData>(c);

_collections.Add(name);

return collectionClient;
}

Expand Down
Loading