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 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
208 changes: 190 additions & 18 deletions docs/eloquent-models/schema-builder.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,15 @@ drop various types of indexes on a collection.
Create an Index
~~~~~~~~~~~~~~~

To create indexes, call the ``create()`` 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. Specify the
index creation details on the ``Blueprint`` instance.
To create indexes, perform the following actions:

1. Call the ``create()`` 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.

#. Specify the index creation details on the ``Blueprint`` instance.

The following example migration creates indexes on the following collection
fields:
Expand Down Expand Up @@ -262,11 +267,16 @@ indexes:
- Unique indexes, which prevent inserting documents that contain duplicate
values for the indexed field

To create these index types, 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. Call the
appropriate helper method on the ``Blueprint`` instance and pass the
index creation details.
To create these index types, perform the following actions:

1. 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.

#. Call the appropriate helper method for the index type on the
``Blueprint`` instance and pass the index creation details.

The following migration code shows how to create a sparse and a TTL index
by using the index helpers. Click the :guilabel:`{+code-output-label+}` button to see
Expand Down Expand Up @@ -339,10 +349,16 @@ Create a Geospatial Index
In MongoDB, geospatial indexes let you query geospatial coordinate data for
inclusion, intersection, and proximity.

To create geospatial 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. Specify the
geospatial index creation details on the ``Blueprint`` instance.
To create geospatial indexes, perform the following actions:

1. 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.

#. Specify the geospatial index creation details on the ``Blueprint``
instance.

The following example migration creates a ``2d`` and ``2dsphere`` geospatial
index on the ``spaceports`` collection. Click the :guilabel:`{+code-output-label+}`
Expand Down Expand Up @@ -379,11 +395,16 @@ the {+server-docs-name+}.
Drop an Index
~~~~~~~~~~~~~

To drop indexes from a collection, call the ``table()`` method on the
``Schema`` facade in your migration file. Pass it the table name and a
callback method with a ``MongoDB\Laravel\Schema\Blueprint`` parameter.
Call the ``dropIndex()`` method with the index name on the ``Blueprint``
instance.
To drop indexes from a collection, perform the following actions:

1. Call the ``table()`` method on the ``Schema`` facade in your
migration file.

#. Pass it the table name and a callback method with a
``MongoDB\Laravel\Schema\Blueprint`` parameter.

#. Call the ``dropIndex()`` method with the index name on the
``Blueprint`` instance.

.. note::

Expand All @@ -399,4 +420,155 @@ 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, perform the following actions:

1. 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, perform the following actions:

1. 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 vector 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,
perform the following actions:

1. 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
Loading
Loading