|
1 |
| -======================== |
2 |
| -Perform Full-Text Search |
3 |
| -======================== |
| 1 | +================== |
| 2 | +Search on Keywords |
| 3 | +================== |
4 | 4 |
|
5 | 5 | .. default-domain:: mongodb
|
6 | 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. |
| 7 | +To set up searches based on keywords, create an array field in |
| 8 | +your documents and add the keywords as strings in the array. You |
| 9 | +can then create a :ref:`multi-key index <index-type-multi-key>` on the |
| 10 | +array and create queries that search the array's values. |
19 | 11 |
|
20 | 12 | .. example::
|
21 | 13 |
|
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. |
| 14 | + Suppose you have a collection of library volumes that you want to |
| 15 | + make searchable by topics. For each volume, you add the array |
| 16 | + ``topics``, and you add as many keywords as needed for a given |
| 17 | + volume. |
42 | 18 |
|
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: |
| 19 | + For the ``Moby-Dick`` volume you might have the following document: |
50 | 20 |
|
51 | 21 | .. code-block:: javascript
|
52 | 22 |
|
53 |
| - { |
54 |
| - name: "Apollo" , |
55 |
| - text: "The Apollo program was the first to land |
56 |
| - people on the Moon." , |
57 |
| - tags: [ "moon", "apollo", "spaceflight" ] |
| 23 | + { title : "Moby-Dick" , |
| 24 | + author : "Herman Melville" , |
| 25 | + published : 1851 , |
| 26 | + ISBN : 0451526996 , |
| 27 | + topics : [ "whaling" , "allegory" , "revenge" , "American" , |
| 28 | + "novel" , "nautical" , "voyage" , "Cape Cod" ] |
58 | 29 | }
|
59 | 30 |
|
60 |
| - Create a multi-key index on the ``tags`` array: |
| 31 | + You then create a multi-key index on the ``topics`` array: |
61 | 32 |
|
62 | 33 | .. code-block:: javascript
|
63 | 34 |
|
64 |
| - db.articles.ensureIndex( { tags: 1 } ) |
| 35 | + db.volumes.ensureIndex( { topics: 1 } ) |
65 | 36 |
|
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: |
| 37 | + The multi-key index creates separate index entries for each keyword in |
| 38 | + the ``topics`` array. For example the index contains one entry for |
| 39 | + ``whaling`` and another for ``allegory``. |
70 | 40 |
|
71 |
| - .. code-block:: javascript |
| 41 | + You then queries based on the keywords. For example: |
72 | 42 |
|
73 |
| - print(db.articles.findOne( { tags : "apollo" } ).name) |
| 43 | + .. code-block:: javascript |
74 | 44 |
|
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. |
| 45 | + print(db.volumes.findOne( { topics : "voyage" } ).title) |
79 | 46 |
|
80 |
| -Comparison to Full-Text Search Engines |
81 |
| --------------------------------------- |
| 47 | +.. note:: An array with many elements (hundreds or thousands) will incur |
| 48 | + greater insert costs. |
82 | 49 |
|
83 |
| -MongoDB makes certain search full-text searches easy, though differs from |
84 |
| -dedicated full-text search engines: |
| 50 | +Limitations of Keyword Indexing |
| 51 | +------------------------------- |
85 | 52 |
|
86 |
| -Dedicated full-text engines provide the following: |
| 53 | +MongoDB makes keyword search easy, though differs from dedicated |
| 54 | +full-text search engines. Dedicated full-text engines provide the |
| 55 | +following: |
87 | 56 |
|
88 |
| -- Built-in text stemming |
| 57 | +- Built-in text stemming. |
89 | 58 |
|
90 |
| -- Ranking of queries matching various numbers of terms. This can be done with |
91 |
| - MongoDB but requires user supplied code to do so. |
| 59 | +- Ranking of queries matching various numbers of terms. This can be done |
| 60 | + with MongoDB but requires user supplied code to do so. |
92 | 61 |
|
93 | 62 | - 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. |
| 63 | + but has the downside of not being in real time. MongoDB is |
| 64 | + particularly well suited for problems where the search should be done |
| 65 | + in real time. |
0 commit comments