|
| 1 | +:orphan: |
| 2 | + |
| 3 | +============== |
| 4 | +``text`` Index |
| 5 | +============== |
| 6 | + |
| 7 | +.. default-domain:: mongodb |
| 8 | + |
| 9 | +This document provides details on some of the options available when |
| 10 | +creating ``text`` indexes. |
| 11 | + |
| 12 | +Specify a Name for the ``text`` Index |
| 13 | +------------------------------------- |
| 14 | + |
| 15 | +The default name for the index consists of each index field name |
| 16 | +concatenated with ``_text``. Consider the ``text`` index on the fields |
| 17 | +``content``, ``users.comments``, and ``users.profiles``. |
| 18 | + |
| 19 | +.. code-block:: javascript |
| 20 | + |
| 21 | + db.collection.ensureIndex( |
| 22 | + { |
| 23 | + content: "text", |
| 24 | + "users.comments": "text", |
| 25 | + "users.profiles": "text" |
| 26 | + } |
| 27 | + ) |
| 28 | + |
| 29 | +The default name for the index is: |
| 30 | + |
| 31 | +.. code-block:: javascript |
| 32 | + |
| 33 | + "content_text_users.comments_text_users.profiles_text" |
| 34 | + |
| 35 | +To avoid creating an index with a name that exceeds the :limit:`index |
| 36 | +name length limit <Index Name Length>`, you can pass the ``name`` |
| 37 | +option to the :method:`db.collection.ensureIndex()` method: |
| 38 | + |
| 39 | +.. code-block:: javascript |
| 40 | + |
| 41 | + db.collection.ensureIndex( |
| 42 | + { |
| 43 | + content: "text", |
| 44 | + "users.comments": "text", |
| 45 | + "users.profiles": "text" |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "MyTextIndex" |
| 49 | + } |
| 50 | + ) |
| 51 | + |
| 52 | +.. note:: |
| 53 | + |
| 54 | + To drop the ``text`` index, use the index name. To get the name of |
| 55 | + an index, use :method:`db.collection.getIndexes()`. |
| 56 | + |
| 57 | +Index All Fields |
| 58 | +---------------- |
| 59 | + |
| 60 | +To allow for text search on all fields with string content, use the |
| 61 | +wildcard specifier (``$**``) to index all fields that contain string |
| 62 | +content. |
| 63 | + |
| 64 | +The following example indexes any string value in the data of every |
| 65 | +field of every document in a collection and names it ``TextIndex``: |
| 66 | + |
| 67 | +.. code-block:: javascript |
| 68 | + |
| 69 | + db.collection.ensureIndex( |
| 70 | + { "$**": "text" }, |
| 71 | + { name: "TextIndex" } |
| 72 | + ) |
| 73 | + |
| 74 | +.. _text-index-default-language: |
| 75 | + |
| 76 | +Specify Languages for Text Index |
| 77 | +-------------------------------- |
| 78 | + |
| 79 | +The default language associated with the indexed data determines the |
| 80 | +list of stop words and the rules for the stemmer and tokenizer. The |
| 81 | +default language for the indexed data is ``english``. |
| 82 | + |
| 83 | +To specify a different language, use the ``default_language`` option |
| 84 | +when creating the ``text`` index. See :ref:`text-search-languages` for |
| 85 | +the languages available for ``default_language``. |
| 86 | + |
| 87 | +The following example creates a ``text`` index on the |
| 88 | +``content`` field and sets the ``default_language`` to |
| 89 | +``spanish``: |
| 90 | + |
| 91 | +.. code-block:: javascript |
| 92 | + |
| 93 | + db.collection.ensureIndex( |
| 94 | + { content : "text" }, |
| 95 | + { default_language: "spanish" } |
| 96 | + ) |
| 97 | + |
| 98 | +.. seealso:: |
| 99 | + |
| 100 | + :doc:`/tutorial/create-text-index-on-multi-language-collection` |
| 101 | + |
| 102 | +.. _text-index-internals-weights: |
| 103 | + |
| 104 | +Control Results of Text Search with Weights |
| 105 | +------------------------------------------- |
| 106 | + |
| 107 | +By default, the :dbcommand:`text` command returns matching documents |
| 108 | +based on scores, from highest to lowest. For a ``text`` index, the |
| 109 | +*weight* of an indexed field denote the significance of the field |
| 110 | +relative to the other indexed fields in terms of the score. The score |
| 111 | +calculation for a given word in a document includes the weighted sum of |
| 112 | +the frequency for each of the indexed fields in that document. |
| 113 | + |
| 114 | +The default weight is 1 for the indexed fields. To adjust the weights |
| 115 | +for the indexed fields, include the ``weights`` option in the |
| 116 | +:method:`db.collection.ensureIndex()` method. |
| 117 | + |
| 118 | +.. warning:: |
| 119 | + |
| 120 | + Choose the weights carefully in order to prevent the need to reindex. |
| 121 | + |
| 122 | +A collection ``blog`` has the following documents: |
| 123 | + |
| 124 | +.. code-block:: javascript |
| 125 | + |
| 126 | + { _id: 1, |
| 127 | + content: "This morning I had a cup of coffee.", |
| 128 | + about: "beverage", |
| 129 | + keywords: [ "coffee" ] |
| 130 | + } |
| 131 | + |
| 132 | + { _id: 2, |
| 133 | + content: "Who doesn't like cake?", |
| 134 | + about: "food", |
| 135 | + keywords: [ "cake", "food", "dessert" ] |
| 136 | + } |
| 137 | + |
| 138 | +To create a ``text`` index with different field weights for the |
| 139 | +``content`` field and the ``keywords`` field, include the ``weights`` |
| 140 | +option to the :method:`~db.collection.ensureIndex()` method. |
| 141 | + |
| 142 | +.. code-block:: javascript |
| 143 | + |
| 144 | + db.blog.ensureIndex( |
| 145 | + { |
| 146 | + content: "text", |
| 147 | + keywords: "text", |
| 148 | + about: "text" |
| 149 | + }, |
| 150 | + { |
| 151 | + weights: { |
| 152 | + content: 10, |
| 153 | + keywords: 5, |
| 154 | + }, |
| 155 | + name: "TextIndex" |
| 156 | + } |
| 157 | + ) |
| 158 | + |
| 159 | +The ``text`` index has the following fields and weights: |
| 160 | + |
| 161 | +- ``content`` has a weight of 10, |
| 162 | + |
| 163 | +- ``keywords`` has a weight of 5, and |
| 164 | + |
| 165 | +- ``about`` has the default weight of 1. |
| 166 | + |
| 167 | +These weights denote the relative significance of the indexed fields to |
| 168 | +each other. For instance, a term match in the ``content`` field has: |
| 169 | + |
| 170 | +- ``2`` times (i.e. ``10:5``) the impact as a term match in the |
| 171 | + ``keywords`` field and |
| 172 | + |
| 173 | +- ``10`` times (i.e. ``10:1``) the impact as a term match in the |
| 174 | + ``about`` field. |
| 175 | + |
| 176 | +Tutorials |
| 177 | +--------- |
| 178 | + |
| 179 | +The following tutorials offer additional ``text`` index creation |
| 180 | +patterns: |
| 181 | + |
| 182 | +- :doc:`/tutorial/create-text-index-on-multi-language-collection` |
| 183 | + |
| 184 | +- :doc:`/tutorial/limit-number-of-items-scanned-for-text-search` |
| 185 | + |
| 186 | +- :doc:`/tutorial/return-text-queries-using-only-text-index` |
0 commit comments