-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathMongoDBCollection-createSearchIndex.txt
124 lines (88 loc) · 3.41 KB
/
MongoDBCollection-createSearchIndex.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
========================================
MongoDB\\Collection::createSearchIndex()
========================================
.. meta::
:description: Create an Atlas Search or Vector Search index for a collection with the MongoDB PHP Library, specifying index type and options.
.. versionadded:: 1.17
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. phpmethod:: MongoDB\Collection::createSearchIndex()
Create an Atlas Search or Vector Search index for the collection.
.. code-block:: php
function createSearchIndex(
array|object $definition,
array $options = []
): string
.. include:: /includes/extracts/note-atlas-search-requirement.rst
Parameters
----------
``$definition`` : array|object
Document describing the index to create. For details on definition syntax, see
:manual:`Search Index Definition Syntax </reference/command/createSearchIndexes/#search-index-definition-syntax>`.
``$options`` : array
An array specifying the desired options.
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Name
- Type
- Description
* - comment
- mixed
- .. include:: /includes/extracts/common-option-comment.rst
* - name
- string
- | Name of the search index to create.
| You cannot create multiple indexes with the same name on a single
collection. If you do not specify a name, the default index
name is ``default``.
* - type
- string
- Type of index to create. Accepted values are ``'search'`` and
``'vectorSearch'``. If you omit this option, the default
value is ``'search'`` and the method creates an Atlas Search index.
Return Values
-------------
The name of the created Atlas Search or Vector Search index as a string.
Errors/Exceptions
-----------------
.. include:: /includes/extracts/error-unsupportedexception.rst
.. include:: /includes/extracts/error-invalidargumentexception.rst
.. include:: /includes/extracts/error-driver-runtimeexception.rst
Behavior
--------
.. include:: /includes/extracts/note-atlas-search-async.rst
Examples
--------
Create an Index with Dynamic Mappings
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The following example creates an Atlas Search index using
`dynamic mappings <https://www.mongodb.com/docs/atlas/atlas-search/define-field-mappings/#dynamic-mappings>`__
to index all document fields containing
`supported data types <https://www.mongodb.com/docs/atlas/atlas-search/define-field-mappings/#std-label-bson-data-chart>`__.
.. code-block:: php
<?php
$collection = (new MongoDB\Client)->getCollection('test', 'articles');
$indexName = $collection->createSearchIndex(
['mappings' => ['dynamic' => true]],
['name' => 'test-search-index']
);
var_dump($indexName);
The output would then resemble:
.. code-block:: none
string(17) "test-search-index"
See Also
--------
- :phpmethod:`MongoDB\Collection::createSearchIndexes()`
- :phpmethod:`MongoDB\Collection::dropSearchIndex()`
- :phpmethod:`MongoDB\Collection::listSearchIndexes()`
- :phpmethod:`MongoDB\Collection::updateSearchIndex()`
- :ref:`php-atlas-search-index` guide
- :manual:`createSearchIndexes </reference/command/createSearchIndexes>` command
reference in the MongoDB manual
- `Atlas Search <https://www.mongodb.com/docs/atlas/atlas-search/>`__ documentation in the MongoDB Manual