@@ -50,28 +50,34 @@ Text Indexes
50
50
51
51
.. note::
52
52
53
- The ``text`` index type is currently an experimental feature and
54
- you must enable it at run time. Interfaces and on-disk format may
55
- change in future releases.
53
+ The ``text`` index type is currently an experimental feature.
54
+ Interfaces and on-disk format may change in future releases. To use
55
+ ``text`` index, you need to enable it at run time. Do **not** enable
56
+ or use ``text`` indexes on production systems.
56
57
57
58
Background
58
59
``````````
59
60
60
- MongoDB2.3.2 includes a new ``text`` index type. ``text`` indexes
61
- support boolean text search queries. Any set of fields containing
62
- string data may be text indexed. You may only maintain a single
63
- ``text`` index per collection. ``text`` indexes are fully consistent
64
- and updated in real-time as applications insert, update, or delete
65
- documents from the database. The ``text`` index and query system
66
- supports language specific stemming and stop-words. Additionally:
61
+ MongoDB 2.3.2 includes a new ``text`` index type. ``text`` indexes
62
+ support boolean text search queries:
67
63
68
- - indexes and queries drop stop words (i.e. "the," "an," "a," "and,"
69
- etc.)
64
+ - Any set of fields containing string data may be text indexed.
70
65
71
- - MongoDB stores words stemmed during insertion in the index, using
72
- simple suffix stemming, including support for a number of
73
- languages. MongoDB automatically stems :dbcommand:`text` queries at
74
- before beginning the query.
66
+ - You may only maintain a **single** ``text`` index per collection.
67
+
68
+ - ``text`` indexes are fully consistent and updated in real-time as
69
+ applications insert, update, or delete documents from the database.
70
+
71
+ - The ``text`` index and query system supports language specific
72
+ stemming and stop words. Additionally:
73
+
74
+ - Indexes and queries drop stop words (i.e. "the," "an," "a," "and,"
75
+ etc.)
76
+
77
+ - MongoDB stores words stemmed during insertion in the index, using
78
+ simple suffix stemming, including support for a number of
79
+ languages. MongoDB automatically stems :dbcommand:`text` queries at
80
+ before beginning the query.
75
81
76
82
However, ``text`` indexes have large storage requirements and incur
77
83
**significant** performance costs:
@@ -81,11 +87,11 @@ However, ``text`` indexes have large storage requirements and incur
81
87
82
88
- Building a ``text`` index is very similar to building a large
83
89
multi-key index, and therefore may take longer than building a
84
- simple ordered (scalar)index.
90
+ simple ordered (scalar) index.
85
91
86
92
- ``text`` indexes will impede insertion throughput, because MongoDB
87
- must add an index entry for each unique word in each indexed field
88
- of each new source document.
93
+ must add an index entry for each unique word in each indexed field of
94
+ each new source document.
89
95
90
96
- some :dbcommand:`text` searches may affect performance on your
91
97
:program:`mongod`, particularly for negation queries and phrase
@@ -103,11 +109,11 @@ indexes have the following limitations and behaviors:
103
109
- MongoDB does not stem phrases or negations in :dbcommand:`text`
104
110
queries.
105
111
106
- - the index is case insensitive.
112
+ - the index is case- insensitive.
107
113
108
114
- a collection may only have a single ``text`` index at a time.
109
115
110
- .. important:: Do not enable or use ``text`` indexes on production
116
+ .. important:: Do ** not** enable or use ``text`` indexes on production
111
117
systems.
112
118
113
119
.. May be worth including this:
@@ -120,21 +126,25 @@ indexes have the following limitations and behaviors:
120
126
Test ``text`` Indexes
121
127
`````````````````````
122
128
123
- .. important:: The ``text`` index type is an experimental feature and
124
- you must enable the feature before creating or accessing a text
125
- index. To enable text indexes, issue the following command at the
126
- :program:`mongo` shell:
129
+ The ``text`` index type is an experimental feature and you need to
130
+ enable the feature before creating or accessing a text index.
131
+
132
+ To enable text indexes, issue the following command in the
133
+ :program:`mongo` shell:
127
134
128
- .. code-block:: javascript
135
+ .. important:: Do **not** enable or use ``text`` indexes on production
136
+ systems.
129
137
130
- db.adminCommand( { setParameter: 1, textSearchEnabled: true } )
138
+ .. code-block:: javascript
131
139
132
- You can also start the :program:`mongod` with the following
133
- invocation:
140
+ db.adminCommand( { setParameter: 1, textSearchEnabled: true } )
134
141
135
- .. code-block:: sh
142
+ You can also start the :program:`mongod` with the following
143
+ invocation:
136
144
137
- mongod --setParameter textSearchEnabled=true
145
+ .. code-block:: sh
146
+
147
+ mongod --setParameter textSearchEnabled=true
138
148
139
149
Create Text Indexes
140
150
^^^^^^^^^^^^^^^^^^^
@@ -157,7 +167,7 @@ and from fields in sub-documents, as in the following:
157
167
"users.profiles": "text" } )
158
168
159
169
The default name for the index consists of the ``<field name>``
160
- concatenated with ``_text``, as in the following:
170
+ concatenated with ``_text`` for the indexed fields , as in the following:
161
171
162
172
.. code-block:: javascript
163
173
@@ -193,19 +203,22 @@ sub-documents. Furthermore, the ``content`` field has a weight of 1 and
193
203
the ``users.profiles`` field has a weight of 2.
194
204
195
205
You can add a conventional ascending or descending index field(s) as a
196
- prefix or suffix of the index so that queries can limit the number of
197
- index entries the query must review to perform the query. You cannot
198
- include :ref:`multi-key <index-type-multi-key>` index field nor
199
- :ref:`geospatial <index-feature-geospatial>` index field.
206
+ prefix or suffix of the index. You cannot include :ref:`multi-key
207
+ <index-type-multi-key>` index field nor :ref:`geospatial
208
+ <index-feature-geospatial>` index field.
200
209
201
210
If you create an ascending or descending index as a prefix of a
202
211
``text`` index:
203
212
204
213
- MongoDB will only index documents that have the prefix field
205
214
(i.e. ``username``) and
206
215
207
- - All :dbcommand:`text` queries using this index must specify the
208
- prefix field in the ``filter`` query.
216
+ - The :dbcommand:`text` query can limit the number of index entries to
217
+ review in order to perform the query.
218
+
219
+ - All :dbcommand:`text` queries using this index must include the
220
+ ``filter`` option that specifies an equality condition for the prefix
221
+ field or fields.
209
222
210
223
Create this index with the following operation:
211
224
@@ -295,8 +308,15 @@ cursor.
295
308
:param string search:
296
309
297
310
A text string that MongoDB stems and uses to query the ``text``
298
- index. When specifying phrase matches, you must escape quote
299
- characters as ``\"``.
311
+ index. In the :program:`mongo` shell, to specify a phrase to
312
+ match, you can either:
313
+
314
+ - enclose the phrase in escaped double quote characters
315
+ (``\"<phrase>\"``) within the ``search`` string, as in
316
+ ``"\"coffee table\""``, or
317
+
318
+ - enclose the phrase in single quote characters, as in ``"'coffee
319
+ table'"``
300
320
301
321
:param document filter:
302
322
@@ -318,19 +338,20 @@ cursor.
318
338
:param number limit:
319
339
320
340
Optional. Specify the maximum number of documents to include in
321
- the response.
341
+ the response. The default limit is 100.
322
342
323
343
:param string language:
324
344
325
345
Optional. Specify the language that determines the tokenization,
326
- stemming, and the stop words for the search.
346
+ stemming, and the stop words for the search. The default language
347
+ is english.
327
348
328
349
:return:
329
350
330
- :dbcommand:`text` returns results in the form of a
331
- document. Results must fit within the :limit:`BSON Document
332
- Size`. Use a projection setting to limit the size of the result
333
- set.
351
+ :dbcommand:`text` returns results in the form of a document.
352
+ Results must fit within the :limit:`BSON Document Size`. Use the
353
+ ``limit`` and the `` projection`` parameters to limit the size of
354
+ the result set.
334
355
335
356
The implicit connector between the terms of a multi-term search is a
336
357
disjunction (``OR``). Search for ``"first second"`` searches
@@ -367,20 +388,20 @@ cursor.
367
388
368
389
db.collection.runCommand( "text", { search: "search" } )
369
390
370
- This query returns documents that contain the word
371
- ``search``, case-insensitive, in the ``content`` field.
372
-
391
+ This query returns documents that contain the word ``search``,
392
+ case-insensitive, in the ``content`` field.
393
+
373
394
#. Search for multiple words, ``create`` or ``search`` or ``fields``:
374
-
395
+
375
396
.. code-block:: javascript
376
-
397
+
377
398
db.collection.runCommand( "text", { search: "create search fields" } )
378
-
399
+
379
400
This query returns documents that contain the either ``create``
380
401
**or** ``search`` **or** ``field`` in the ``content`` field.
381
-
402
+
382
403
#. Search for the exact phrase ``create search fields``:
383
-
404
+
384
405
.. code-block:: javascript
385
406
386
407
db.collection.runCommand( "text", { search: "\"create search fields\"" } )
@@ -397,7 +418,7 @@ cursor.
397
418
398
419
Use the ``-`` as a prefix to terms to specify negation in the
399
420
search string. The query returns documents that contain the
400
- either ``creat `` **or** ``search``, but **not** ``field``, all
421
+ either ``create `` **or** ``search``, but **not** ``field``, all
401
422
case-insensitive, in the ``content`` field. Prefixing a word
402
423
with a hyphen (``-``) negates a word:
403
424
@@ -407,8 +428,8 @@ cursor.
407
428
- A ``<search string>`` that only contains negative words returns no match.
408
429
409
430
- A hyphenated word, such as ``case-insensitive``, is not a
410
- negation. The :dbcommand:`text` command treats the hyphen and
411
- as a delimiter.
431
+ negation. The :dbcommand:`text` command treats the hyphen as a
432
+ delimiter.
412
433
413
434
#. Search for a single word ``search`` with an additional ``filter`` on
414
435
the ``about`` field, but **limit** the results to 2 documents with the
@@ -424,16 +445,17 @@ cursor.
424
445
projection: { comments: 1, _id: 0 }
425
446
}
426
447
)
427
-
448
+
428
449
- The ``filter`` :ref:`query document <mongodb-query-document>`
429
- is uses a :operator:`regular expression <$regex>`. See the
430
- :ref :`query operators <operator>` page for available query
450
+ uses a :operator:`regular expression <$regex>`. See the
451
+ :doc :`query operators </reference/ operator>` page for available query
431
452
operators.
432
-
433
- - The ``projection`` must explicitly exclude (``0``) the ``_id``
434
- field. Within the ``projection`` document, you cannot mix
435
- inclusions (i.e. ``<fieldA>: 1``) and exclusions (i.e. ``<fieldB>:
436
- 0``), except for the ``_id`` field.
453
+
454
+ - Because the ``_id`` field is implicitly included, in order to
455
+ return **only** the ``comments`` field, you must explicitly
456
+ exclude (``0``) the ``_id`` field. Within the ``projection``
457
+ document, you cannot mix inclusions (i.e. ``<fieldA>: 1``) and
458
+ exclusions (i.e. ``<fieldB>: 0``), except for the ``_id`` field.
437
459
438
460
Additional Authentication Features
439
461
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0 commit comments