Skip to content

DOCSP-46230: atlas search index mgmt #3270

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

Merged
merged 12 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from 10 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
138 changes: 138 additions & 0 deletions docs/eloquent-models/schema-builder.txt
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,142 @@ from the ``flights`` collection:
:start-after: begin drop index
:end-before: end drop index

.. _laravel-schema-builder-atlas-idx:

Manage Atlas Search and Vector Search Indexes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In MongoDB, :atlas:`Atlas Search indexes
</atlas-search/manage-indexes/>` support your full-text queries.
:atlas:`Atlas Vector Search indexes
</atlas-vector-search/vector-search-type/>` support similarity
searches that compare query vectors to vector embeddings in your
documents.

View the following guides to learn more about the Atlas Search and
Vector Search features:

- :ref:`laravel-atlas-search` guide
- :ref:`laravel-vector-search` guide

Atlas Search
````````````

To create Atlas Search indexes, call the ``create()`` method on the
``Schema`` facade in your migration file. Pass ``create()`` the
collection name and a callback method with a
``MongoDB\Laravel\Schema\Blueprint`` parameter. Pass the Atlas index
creation details to the ``searchIndex()`` method on the ``Blueprint``
instance.

This example migration creates the following Atlas Search indexes on the
``galaxies`` collection:

- ``dynamic_index``: Creates dynamic mappings
- ``auto_index``: Supports autocomplete queries on the ``name`` field

Click the :guilabel:`{+code-output-label+}` button to see the Search
indexes created by running the migration:

.. io-code-block::

.. input:: /includes/schema-builder/galaxies_migration.php
:language: php
:dedent:
:start-after: begin-create-search-indexes
:end-before: end-create-search-indexes

.. output::
:language: json
:visible: false

{
"id": "...",
"name": "dynamic_index",
"type": "search",
"status": "READY",
"queryable": true,
"latestDefinition": {
"mappings": { "dynamic": true }
},
...
}
{
"id": "...",
"name": "auto_index",
"type": "search",
"status": "READY",
"queryable": true,
"latestDefinition": {
"mappings": {
"fields": { "name": [
{ "type": "string", "analyzer": "lucene.english" },
{ "type": "autocomplete", "analyzer": "lucene.english" },
{ "type": "token" }
] }
}
},
...
}

Vector Search
`````````````

To create Vector Search indexes, call the ``create()`` method on the
``Schema`` facade in your migration file. Pass ``create()`` the
collection name and a callback method with a
``MongoDB\Laravel\Schema\Blueprint`` parameter. Pass the Atlas index
creation details to the ``vectorSearchIndex()`` method on the ``Blueprint``
instance.

The following example migration creates a Vector Search index called
``vs_index`` on the ``galaxies`` collection.

Click the :guilabel:`{+code-output-label+}` button to see the Search
indexes created by running the migration:

.. io-code-block::
.. input:: /includes/schema-builder/galaxies_migration.php
:language: php
:dedent:
:start-after: begin-create-vs-index
:end-before: end-create-vs-index

.. output::
:language: json
:visible: false

{
"id": "...",
"name": "vs_index",
"type": "vectorSearch",
"status": "READY",
"queryable": true,
"latestDefinition": {
"fields": [ {
"type": "vector",
"numDimensions": 4,
"path": "embeddings",
"similarity": "cosine"
} ]
},
...
}

Drop a Search Index
```````````````````

To drop an Atlas Search or Vector Search index from a collection, call
the ``table()`` method on the ``Schema`` facade in your migration file.
Pass it the collection name and a callback method with a
``MongoDB\Laravel\Schema\Blueprint`` parameter. Call the ``dropSearchIndex()``
method with the Search index name on the ``Blueprint`` instance.

The following example migration drops an index called ``auto_index``
from the ``galaxies`` collection:

.. literalinclude:: /includes/schema-builder/galaxies_migration.php
:language: php
:dedent:
:start-after: begin-drop-search-index
:end-before: end-drop-search-index
20 changes: 19 additions & 1 deletion docs/fundamentals/atlas-search.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,25 @@ documentation.
Create an Atlas Search Index
----------------------------

.. TODO in DOCSP-46230
You can create an Atlas Search index in either of the following ways:

- Call the ``create()`` method on the ``Schema`` facade and pass the
``searchIndex()`` helper method with index creation details. To learn
more about this strategy, see the
:ref:`laravel-schema-builder-atlas-idx` section of the Schema Builder guide.

- Access a collection, then call the
:phpmethod:`createSearchIndex() <phpmethod.MongoDB\\Collection::createSearchIndex()>`
method from the {+php-library+}, as shown in the following code:

.. code-block:: php

$collection = DB::connection('mongodb')->getCollection('movies');

$collection->createSearchIndex(
['mappings' => ['dynamic' => true]],
['name' => 'search_index']
);

Perform Queries
---------------
Expand Down
27 changes: 26 additions & 1 deletion docs/fundamentals/vector-search.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,32 @@ documentation.
Create an Atlas Vector Search Index
-----------------------------------

.. TODO in DOCSP-46230
You can create an Atlas Search index in either of the following ways:

- Call the ``create()`` method on the ``Schema`` facade and pass the
``vectorSearchIndex()`` helper method with index creation details. To learn
more about this strategy, see the
:ref:`laravel-schema-builder-atlas-idx` section of the Schema Builder guide.

- Access a collection, then call the
:phpmethod:`createSearchIndex() <phpmethod.MongoDB\\Collection::createSearchIndex()>`
method from the {+php-library+}. You must specify the ``type`` option as
``'vectorSearch'``, as shown in the following code:

.. code-block:: php

$collection = DB::connection('mongodb')->getCollection('movies');

$collection->createSearchIndex([
'fields' => [
[
'type' => 'vector',
'numDimensions' => 4,
'path' => 'embeddings',
'similarity' => 'cosine'
],
],
], ['name' => 'vector_index', 'type' => 'vectorSearch']);

Perform Queries
---------------
Expand Down
60 changes: 60 additions & 0 deletions docs/includes/schema-builder/galaxies_migration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\Schema;
use MongoDB\Laravel\Schema\Blueprint;

return new class extends Migration
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the migration class is not actually displayed in the docs, you can move code snippets in up and down methods into unit tests that would validate them (ensure the index is correctly created) and deleted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

{
protected $connection = 'mongodb';

public function up(): void
{
// begin-create-search-indexes
Schema::create('galaxies', function (Blueprint $collection) {
$collection->searchIndex([
'mappings' => [
'dynamic' => true,
],
], 'dynamic_index');
$collection->searchIndex([
'mappings' => [
'fields' => [
'name' => [
['type' => 'string', 'analyzer' => 'lucene.english'],
['type' => 'autocomplete', 'analyzer' => 'lucene.english'],
['type' => 'token'],
],
],
],
], 'auto_index');
});
// end-create-search-indexes

// begin-create-vs-index
Schema::create('galaxies', function (Blueprint $collection) {
$collection->vectorSearchIndex([
'fields' => [
[
'type' => 'vector',
'numDimensions' => 4,
'path' => 'embeddings',
'similarity' => 'cosine',
],
],
], 'vs_index');
});
// end-create-vs-index
}

public function down(): void
{
// begin-drop-search-index
Schema::table('galaxies', function (Blueprint $collection) {
$collection->dropSearchIndex('auto_index');
});
// end-drop-search-index
}
};
2 changes: 1 addition & 1 deletion docs/query-builder.txt
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ a query:
:end-before: end options

The query builder accepts the same options that you can set for
the :phpmethod:`MongoDB\Collection::find()` method in the
the :phpmethod:`find() <phpmethod.MongoDB\\Collection::find()>` method in the
{+php-library+}. Some of the options to modify query results, such as
``skip``, ``sort``, and ``limit``, are settable directly as query
builder methods and are described in the
Expand Down
Loading