Skip to content

DOCSP-49085: Port existing Vector Search page #643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: docsp-45382-comp-cvg
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 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
1 change: 1 addition & 0 deletions snooty.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ not-available = "N/A"
analyzer = "MongoDB C# Analyzer"
analyzer-short = "C# Analyzer"
query-api = "MongoDB Query API"
avs = "Atlas Vector Search"
300 changes: 224 additions & 76 deletions source/atlas-vector-search.txt

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions source/includes/fundamentals/code-examples/MemorySerialization.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using MongoDB.Bson;Add commentMore actions
using MongoDB.Bson.Serialization.Conventions;
using MongoDB.Driver;

public class Program
{
public static void Main(string[] args)
{
// Replace with your connection string
const string uri = "<connection string>";

var mongoClient = new MongoClient(uri);
var database = mongoClient.GetDatabase("db");
var _collection = database.GetCollection<Line>("lines");

var line = new Line
{
X = new Memory<int>(new[] { 1, 2, 3, 4, 5 }),
Y = new ReadOnlyMemory<float>(new[] { 1f, 1.41f, 1.73f, 2f, 2.24f })
};

var filter = Builders<Line>.Filter.Empty;

var result = _collection.Find(filter).FirstOrDefault().ToJson();
Console.WriteLine(result);
}

}

// start-line-class
public class Line
{
public ObjectId Id { get; set; }
public Memory<int> X { get; set; }
public ReadOnlyMemory<float> Y { get; set; }
}
// end-line-class
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// start-bson-arrays
public class BsonArrayVectors
{
public BsonArray BsonArrayVector { get; set; }

public Memory<float> MemoryVector { get; set; }

public ReadOnlyMemory<float> ReadOnlyMemoryVector { get; set; }

public float[] FloatArrayVector { get; set; }
}
// end-bson-arrays

// start-binary-vectors
public class BinaryVectors
{
public BinaryVectorInt8 ValuesInt8 { get; set; }

public BinaryVectorPackedBit ValuesPackedBit { get; set; }

public BinaryVectorFloat32 ValuesFloat { get; set; }

[BinaryVector(BinaryVectorDataType.Int8)]
public Memory<byte> ValuesByte { get; set; }

[BinaryVector(BinaryVectorDataType.Float32)]
public float[] ValuesFloat { get; set; }

}
// end-binary-vectors

// start-binary-int-float-serialize
[BinaryVector(BinaryVectorDataType.Int8)]
public Memory<byte> ValuesByte { get; set; }

[BinaryVector(BinaryVectorDataType.Int8)]
public Memory<sbyte> ValuesSByte { get; set; }

[BinaryVector(BinaryVectorDataType.Float32)]
public float[] ValuesFloat { get; set; }
// end-binary-int-float-serialize

// start-to-query-vector
var binaryVector = new BinaryVectorInt8(new sbyte[] { 0, 1, 2, 3, 4 });

var queryVector = binaryVector.ToQueryVector();
// end-to-query-vector

// start-array-query-vector
QueryVector v = new QueryVector(new ReadOnlyMemory<float>([1.2f, 2.3f]));
// end-array-query-vector

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion source/indexes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ and provide sample code for creating each index type.

.. note::

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

Expand Down
65 changes: 65 additions & 0 deletions source/serialization.txt
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,71 @@ Implementing the `IBsonArraySerializer
interface enables the driver to access serialization information for individual
items in an array.

.. _csharp-array-serialization:

Improve Array Serialization Performance
---------------------------------------

You can improve your application's performance by representing
arrays of primitives as `Memory<T> <https://learn.microsoft.com/en-us/dotnet/api/system.memory-1?view=net-8.0>`__
and `ReadOnlyMemory<T> <https://learn.microsoft.com/en-us/dotnet/api/system.readonlymemory-1?view=net-8.0>`__
structs instead of by using types such as standard {+language+} arrays or
``BsonArray`` objects. The driver implements fast serialization and
deserialization paths for ``Memory<T>`` and ``ReadOnlyMemory<T>``, which
enhances speed and reduces memory usage.

.. note::

Truncation and overflow checks are not supported for ``Memory<T>`` or
``ReadOnlyMemory<T>``, but these checks are implemented for standard
arrays.

You can effect these performance improvements by storing the following
primitive types in ``Memory<T>`` or ``ReadOnlyMemory<T>`` structs:

- ``bool``
- ``sbyte``
- ``byte``
- ``char``
- ``short``
- ``ushort``
- ``int``
- ``uint``
- ``long``
- ``ulong``
- ``float``
- ``double``
- ``decimal``

The following example defines a ``Line`` POCO that contains array fields
modeled by ``Memory`` and ``ReadOnlyMemory`` structs:

.. literalinclude:: /includes/fundamentals/code-examples/MemorySerialization.cs
:start-after: start-line-class
:end-before: end-line-class
:language: csharp
:dedent:

The following document represents how a sample ``Line`` object is
represented in MongoDB:

.. code-block:: json

{
"_id": ...,
"X": [ 1, 2, 3, 4, 5 ],
"Y": [ 1, 1.409999966621399, 1.7300000190734863, 2, 2.240000009536743 ]
}

.. tip:: Model Vectors

:ref:`csharp-atlas-vector-search` involves creating and querying
large numerical arrays. If your application uses
{+avs+}, you might benefit from the performance
improvements from using ``Memory`` and ``ReadOnlyMemory`` to store
array representations of embeddings and query vectors. To learn more,
see :ref:`csharp-supported-vector-types` in the {+avs+} guide.

Additional Information
----------------------

Expand Down
Loading