Skip to content

Commit 8b08829

Browse files
authored
DOCSP-49085: Port existing Vector Search page (#643)
Created ticket in follow-up epic for Damien's feedback!
1 parent aca2c08 commit 8b08829

File tree

7 files changed

+465
-77
lines changed

7 files changed

+465
-77
lines changed

snooty.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ not-available = "N/A"
4444
analyzer = "MongoDB C# Analyzer"
4545
analyzer-short = "C# Analyzer"
4646
query-api = "MongoDB Query API"
47+
avs = "Atlas Vector Search"

source/atlas-vector-search.txt

Lines changed: 224 additions & 76 deletions
Large diffs are not rendered by default.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using MongoDB.Bson;Add commentMore actions
2+
using MongoDB.Bson.Serialization.Conventions;
3+
using MongoDB.Driver;
4+
5+
public class Program
6+
{
7+
public static void Main(string[] args)
8+
{
9+
// Replace with your connection string
10+
const string uri = "<connection string>";
11+
12+
var mongoClient = new MongoClient(uri);
13+
var database = mongoClient.GetDatabase("db");
14+
var _collection = database.GetCollection<Line>("lines");
15+
16+
var line = new Line
17+
{
18+
X = new Memory<int>(new[] { 1, 2, 3, 4, 5 }),
19+
Y = new ReadOnlyMemory<float>(new[] { 1f, 1.41f, 1.73f, 2f, 2.24f })
20+
};
21+
22+
var filter = Builders<Line>.Filter.Empty;
23+
24+
var result = _collection.Find(filter).FirstOrDefault().ToJson();
25+
Console.WriteLine(result);
26+
}
27+
28+
}
29+
30+
// start-line-class
31+
public class Line
32+
{
33+
public ObjectId Id { get; set; }
34+
public Memory<int> X { get; set; }
35+
public ReadOnlyMemory<float> Y { get; set; }
36+
}
37+
// end-line-class
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// start-bson-arrays
2+
public class BsonArrayVectors
3+
{
4+
public BsonArray BsonArrayVector { get; set; }
5+
6+
public Memory<float> MemoryVector { get; set; }
7+
8+
public ReadOnlyMemory<float> ReadOnlyMemoryVector { get; set; }
9+
10+
public float[] FloatArrayVector { get; set; }
11+
}
12+
// end-bson-arrays
13+
14+
// start-binary-vectors
15+
public class BinaryVectors
16+
{
17+
public BinaryVectorInt8 ValuesInt8 { get; set; }
18+
19+
public BinaryVectorPackedBit ValuesPackedBit { get; set; }
20+
21+
public BinaryVectorFloat32 ValuesFloat { get; set; }
22+
23+
[BinaryVector(BinaryVectorDataType.Int8)]
24+
public Memory<byte> ValuesByte { get; set; }
25+
26+
[BinaryVector(BinaryVectorDataType.Float32)]
27+
public float[] ValuesFloat { get; set; }
28+
29+
}
30+
// end-binary-vectors
31+
32+
// start-binary-int-float-serialize
33+
[BinaryVector(BinaryVectorDataType.Int8)]
34+
public Memory<byte> ValuesByte { get; set; }
35+
36+
[BinaryVector(BinaryVectorDataType.Int8)]
37+
public Memory<sbyte> ValuesSByte { get; set; }
38+
39+
[BinaryVector(BinaryVectorDataType.Float32)]
40+
public float[] ValuesFloat { get; set; }
41+
// end-binary-int-float-serialize
42+
43+
// start-to-query-vector
44+
var binaryVector = new BinaryVectorInt8(new sbyte[] { 0, 1, 2, 3, 4 });
45+
46+
var queryVector = binaryVector.ToQueryVector();
47+
// end-to-query-vector
48+
49+
// start-array-query-vector
50+
QueryVector v = new QueryVector(new ReadOnlyMemory<float>([1.2f, 2.3f]));
51+
// end-array-query-vector

source/includes/fundamentals/code-examples/atlas-vector-search/VectorSearchQueryExample.cs

Lines changed: 86 additions & 0 deletions
Large diffs are not rendered by default.

source/indexes.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ and provide sample code for creating each index type.
8686

8787
.. note::
8888

89-
These example uses the ``sample_mflix.movies`` and ``sample_mflix.theaters`` collections
89+
The examples on this page use the ``sample_mflix.movies`` and ``sample_mflix.theaters`` collections
9090
from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to create a
9191
free MongoDB Atlas cluster and load the sample datasets, see :ref:`csharp-get-started`.
9292

source/serialization.txt

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,71 @@ Implementing the `IBsonArraySerializer
184184
interface enables the driver to access serialization information for individual
185185
items in an array.
186186

187+
.. _csharp-array-serialization:
188+
189+
Improve Array Serialization Performance
190+
---------------------------------------
191+
192+
You can improve your application's performance by representing
193+
arrays of primitives as `Memory<T> <https://learn.microsoft.com/en-us/dotnet/api/system.memory-1?view=net-8.0>`__
194+
and `ReadOnlyMemory<T> <https://learn.microsoft.com/en-us/dotnet/api/system.readonlymemory-1?view=net-8.0>`__
195+
structs instead of by using types such as standard {+language+} arrays or
196+
``BsonArray`` objects. The driver implements fast serialization and
197+
deserialization paths for ``Memory<T>`` and ``ReadOnlyMemory<T>``, which
198+
enhances speed and reduces memory usage.
199+
200+
.. note::
201+
202+
Truncation and overflow checks are not supported for ``Memory<T>`` or
203+
``ReadOnlyMemory<T>``, but these checks are implemented for standard
204+
arrays.
205+
206+
You can effect these performance improvements by storing the following
207+
primitive types in ``Memory<T>`` or ``ReadOnlyMemory<T>`` structs:
208+
209+
- ``bool``
210+
- ``sbyte``
211+
- ``byte``
212+
- ``char``
213+
- ``short``
214+
- ``ushort``
215+
- ``int``
216+
- ``uint``
217+
- ``long``
218+
- ``ulong``
219+
- ``float``
220+
- ``double``
221+
- ``decimal``
222+
223+
The following example defines a ``Line`` POCO that contains array fields
224+
modeled by ``Memory`` and ``ReadOnlyMemory`` structs:
225+
226+
.. literalinclude:: /includes/fundamentals/code-examples/MemorySerialization.cs
227+
:start-after: start-line-class
228+
:end-before: end-line-class
229+
:language: csharp
230+
:dedent:
231+
232+
The following document represents how a sample ``Line`` object is
233+
represented in MongoDB:
234+
235+
.. code-block:: json
236+
237+
{
238+
"_id": ...,
239+
"X": [ 1, 2, 3, 4, 5 ],
240+
"Y": [ 1, 1.409999966621399, 1.7300000190734863, 2, 2.240000009536743 ]
241+
}
242+
243+
.. tip:: Model Vectors
244+
245+
:ref:`csharp-atlas-vector-search` involves creating and querying
246+
large numerical arrays. If your application uses
247+
{+avs+}, you might benefit from the performance
248+
improvements from using ``Memory`` and ``ReadOnlyMemory`` to store
249+
array representations of embeddings and query vectors. To learn more,
250+
see :ref:`csharp-supported-vector-types` in the {+avs+} guide.
251+
187252
Additional Information
188253
----------------------
189254

0 commit comments

Comments
 (0)