Skip to content

Commit 261431a

Browse files
author
Bob Grabar
committed
DOCS-797 full-text search
1 parent 53933b1 commit 261431a

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
========================
2+
Perform Full-Text Search
3+
========================
4+
5+
.. default-domain:: mongodb
6+
7+
Mongo uses :ref:`multi-key indexes <index-type-multi-key>` to provide
8+
for searches on text and tags. A multi-key index can index an array of
9+
values.
10+
11+
Text Search
12+
-----------
13+
14+
To implement a text search:
15+
16+
1. Create an array of search words.
17+
18+
2. Create a :ref:`multi-key index <index-type-multi-key>` on the array.
19+
20+
.. example::
21+
22+
Create an array with all the keywords to search on. Here is a simple example:
23+
24+
.. code-block:: javascript
25+
26+
{ title : "breakfast drinks" ,
27+
keywords : [ "tea" , "coffee" , "juice", "Tang" ]
28+
}
29+
30+
Create a multi-key index on the ``keywords`` array:
31+
32+
.. code-block:: javascript
33+
34+
db.menu.ensureIndex( { keywords: 1 } )
35+
36+
Tagging
37+
-------
38+
39+
To implement a tag search:
40+
41+
1. Create an array of tags.
42+
43+
2. Create a :ref:`multi-key index <index-type-multi-key>` on the array.
44+
45+
.. example::
46+
47+
Suppose you have documents in the ``articles`` database and the
48+
documents are tagged with category names, such as the following
49+
document:
50+
51+
.. code-block:: javascript
52+
53+
{
54+
name: "Apollo" ,
55+
text: "The Apollo program was the first to land
56+
people on the Moon." ,
57+
tags: [ "moon", "apollo", "spaceflight" ]
58+
}
59+
60+
Create a multi-key index on the ``tags`` array:
61+
62+
.. code-block:: javascript
63+
64+
db.articles.ensureIndex( { tags: 1 } )
65+
66+
The above command indexes all the strings in the ``tags`` array. For
67+
the document shown in this example, the command creates index entries
68+
for the tags ``moon``, ``apollo`` and ``spaceflight``. Users can query
69+
on those tags. For example:
70+
71+
.. code-block:: javascript
72+
73+
print(db.articles.findOne( { tags : "apollo" } ).name)
74+
75+
The database creates an index entry for each item in the array. An
76+
array with many elements (hundreds or thousands) might make inserts very
77+
expensive. Although, for the example above, alternate implementations
78+
are equally expensive.
79+
80+
Comparison to Full-Text Search Engines
81+
--------------------------------------
82+
83+
MongoDB makes certain search full-text searches easy, though differs from
84+
dedicated full-text search engines:
85+
86+
Dedicated full-text engines provide the following:
87+
88+
- Built-in text stemming
89+
90+
- Ranking of queries matching various numbers of terms. This can be done with
91+
MongoDB but requires user supplied code to do so.
92+
93+
- Bulk index building. Bulk index building makes building indexes fast
94+
but has the downside of not being in real time. MongoDB is particularly
95+
well suited for problems where the search should be done in real time.

0 commit comments

Comments
 (0)