|
| 1 | +.. _search_indexes: |
| 2 | + |
| 3 | +Search Indexes |
| 4 | +============== |
| 5 | + |
| 6 | +In addition to standard :ref:`indexes <indexes>`, ODM allows you to define |
| 7 | +search indexes for use with `MongoDB Atlas Search <https://www.mongodb.com/docs/atlas/atlas-search/>`__. |
| 8 | +Search indexes may be queried using the `$search <https://www.mongodb.com/docs/atlas/atlas-search/aggregation-stages/search/>`__ |
| 9 | +and `$searchMeta <https://www.mongodb.com/docs/atlas/atlas-search/aggregation-stages/searchMeta/>`__ |
| 10 | +aggregation pipeline stages. |
| 11 | + |
| 12 | +Search indexes have some notable differences from regular |
| 13 | +:ref:`indexes <indexes>` in ODM. They may only be defined on document classes. |
| 14 | +Definitions will not be incorporated from embedded documents. Additionally, ODM |
| 15 | +will **NOT** translate field names in search index definitions. Database field |
| 16 | +names must be used instead of mapped field names (i.e. PHP property names). |
| 17 | + |
| 18 | +Search Index Options |
| 19 | +-------------------- |
| 20 | + |
| 21 | +Search indexes are defined using a more complex syntax than regular |
| 22 | +:ref:`indexes <indexes>`. |
| 23 | + |
| 24 | +ODM supports the following search index options: |
| 25 | + |
| 26 | +- |
| 27 | + ``name`` - Name of the search index to create, which must be unique to the |
| 28 | + collection. Defaults to ``"default"``. |
| 29 | +- |
| 30 | + ``dynamic`` - Enables or disables dynamic field mapping for this index. |
| 31 | + If ``true``, the index will include all fields with |
| 32 | + `supported data types <https://www.mongodb.com/docs/atlas/atlas-search/define-field-mappings/#std-label-bson-data-chart>`__. |
| 33 | + If ``false``, the ``fields`` attribute must be specified. Defaults to ``false``. |
| 34 | +- |
| 35 | + ``fields`` - Associative array of `field mappings <https://www.mongodb.com/docs/atlas/atlas-search/define-field-mappings/>`__ |
| 36 | + that specify the fields to index (keys). Required only if dynamic mapping is disabled. |
| 37 | +- |
| 38 | + ``analyzer`` - Specifies the `analyzer <https://www.mongodb.com/docs/atlas/atlas-search/analyzers/>`__ |
| 39 | + to apply to string fields when indexing. Defaults to the |
| 40 | + `standard analyzer <https://www.mongodb.com/docs/atlas/atlas-search/analyzers/standard/>`__. |
| 41 | +- |
| 42 | + ``searchAnalyzer`` - Specifies the `analyzer <https://www.mongodb.com/docs/atlas/atlas-search/analyzers/>`__ |
| 43 | + to apply to query text before the text is searched. Defaults to the |
| 44 | + ``analyzer`` attribute, or the `standard analyzer <https://www.mongodb.com/docs/atlas/atlas-search/analyzers/standard/>`__. |
| 45 | + if both are unspecified. |
| 46 | +- |
| 47 | + ``analyzers`` - Array of `custom analyzers <https://www.mongodb.com/docs/atlas/atlas-search/analyzers/custom/>`__ |
| 48 | + to use in this index. |
| 49 | +- |
| 50 | + ``storedSource`` - Specifies document fields to store for queries performed |
| 51 | + using the `returnedStoredSource <https://www.mongodb.com/docs/atlas/atlas-search/return-stored-source/>`__ |
| 52 | + option. Specify ``true`` to store all fields, ``false`` to store no fields, |
| 53 | + or a `document <https://www.mongodb.com/docs/atlas/atlas-search/stored-source-definition/#std-label-fts-stored-source-document>`__ |
| 54 | + to specify individual fields to include or exclude from storage. Defaults to ``false``. |
| 55 | +- |
| 56 | + ``synonyms`` - Array of `synonym mapping definitions <https://www.mongodb.com/docs/atlas/atlas-search/synonyms/>`__ |
| 57 | + to use in this index. |
| 58 | + |
| 59 | +Additional documentation for defining search indexes may be found in |
| 60 | +`search index definition <https://www.mongodb.com/docs/manual/reference/command/createSearchIndexes/#search-index-definition-syntax>`__ |
| 61 | +within the MongoDB manual. |
| 62 | + |
| 63 | +Static Mapping |
| 64 | +-------------- |
| 65 | + |
| 66 | +`Static mapping <https://www.mongodb.com/docs/atlas/atlas-search/define-field-mappings/#static-mappings>`__ |
| 67 | +can be used to configure indexing of specific fields within a document. |
| 68 | + |
| 69 | +The following example demonstrates how to define a search index using static |
| 70 | +mapping. |
| 71 | + |
| 72 | +.. configuration-block:: |
| 73 | + |
| 74 | + .. code-block:: php |
| 75 | +
|
| 76 | + <?php |
| 77 | +
|
| 78 | + /** |
| 79 | + * @Document |
| 80 | + * @SearchIndex( |
| 81 | + * name="usernameAndAddresses", |
| 82 | + * fields={ |
| 83 | + * "username"={ |
| 84 | + * {"type"="string"}, |
| 85 | + * {"type"="autocomplete"}, |
| 86 | + * }, |
| 87 | + * "addresses"={"type"="embeddedDocuments", "dynamic"=true}, |
| 88 | + * }, |
| 89 | + * ) |
| 90 | + */ |
| 91 | + class User |
| 92 | + { |
| 93 | + /** @Id */ |
| 94 | + private $id; |
| 95 | +
|
| 96 | + /** @Field(type="string") */ |
| 97 | + private $username; |
| 98 | +
|
| 99 | + /** @EmbedMany(targetDocument=Address::class) */ |
| 100 | + private $addresses; |
| 101 | +
|
| 102 | + // ... |
| 103 | + } |
| 104 | +
|
| 105 | + .. code-block:: xml |
| 106 | +
|
| 107 | + <doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mongo-mapping" |
| 108 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 109 | + xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mongo-mapping |
| 110 | + http://doctrine-project.org/schemas/orm/doctrine-mongo-mapping.xsd"> |
| 111 | +
|
| 112 | + <document name="Documents\User"> |
| 113 | + <search-indexes> |
| 114 | + <search-index name="usernameAndAddresses"> |
| 115 | + <field name="username" type="string" /> |
| 116 | + <field name="username" type="autocomplete" /> |
| 117 | + <field name="addresses" type="embeddedDocuments" dynamic="true" /> |
| 118 | + </search-index> |
| 119 | + </search-indexes> |
| 120 | +
|
| 121 | + <!-- ... --> |
| 122 | + </document> |
| 123 | + </doctrine-mongo-mapping> |
| 124 | +
|
| 125 | +The ``username`` field will indexed both as a string and for autocompletion. |
| 126 | +Since the ``addresses`` field uses an :ref:`embed-many <embed_many>` |
| 127 | +relationship, it must be indexed using the ``embeddedDocuments`` type; however, |
| 128 | +embedded documents within the array are permitted to use dynamic mapping. |
| 129 | + |
| 130 | +Dynamic Mapping |
| 131 | +--------------- |
| 132 | + |
| 133 | +`Dynamic mapping <https://www.mongodb.com/docs/atlas/atlas-search/define-field-mappings/#dynamic-mappings>`__ |
| 134 | +can be used to automatically index fields with |
| 135 | +`supported data types <https://www.mongodb.com/docs/atlas/atlas-search/define-field-mappings/#std-label-bson-data-chart>`__ |
| 136 | +within a document. Dynamically mapped indexes occupy more disk space than |
| 137 | +statically mapped indexes and may be less performant; however, they may be |
| 138 | +useful if your schema changes or for when experimenting with Atlas Search |
| 139 | + |
| 140 | +.. note:: |
| 141 | + |
| 142 | + Atlas Search does **NOT** dynamically index embedded documents contained |
| 143 | + within arrays (e.g. :ref:`embed-many <embed_many>` relationships). You must |
| 144 | + use static mappings with the `embeddedDocument <https://www.mongodb.com/docs/atlas/atlas-search/field-types/embedded-documents-type/>`__ |
| 145 | + field type. |
| 146 | + |
| 147 | +The following example demonstrates how to define a search index using dynamic |
| 148 | +mapping: |
| 149 | + |
| 150 | +.. configuration-block:: |
| 151 | + |
| 152 | + .. code-block:: php |
| 153 | +
|
| 154 | + <?php |
| 155 | +
|
| 156 | + /** |
| 157 | + * @Document |
| 158 | + * @SearchIndex(dynamic=true) |
| 159 | + */ |
| 160 | + class BlogPost |
| 161 | + { |
| 162 | + /** @Id */ |
| 163 | + private $id; |
| 164 | +
|
| 165 | + /** @Field(type="string") */ |
| 166 | + private $title; |
| 167 | +
|
| 168 | + /** @Field(type="string") */ |
| 169 | + private $body; |
| 170 | +
|
| 171 | + // ... |
| 172 | + } |
| 173 | +
|
| 174 | + .. code-block:: xml |
| 175 | +
|
| 176 | + <doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mongo-mapping" |
| 177 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 178 | + xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mongo-mapping |
| 179 | + http://doctrine-project.org/schemas/orm/doctrine-mongo-mapping.xsd"> |
| 180 | +
|
| 181 | + <document name="Documents\BlogPost"> |
| 182 | + <search-indexes> |
| 183 | + <search-index dynamic="true" /> |
| 184 | + </search-indexes> |
| 185 | +
|
| 186 | + <!-- ... --> |
| 187 | + </document> |
| 188 | + </doctrine-mongo-mapping> |
0 commit comments