|
| 1 | +.. _scala-text-search: |
| 2 | + |
| 3 | +=========== |
| 4 | +Text Search |
| 5 | +=========== |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: code example, full text search, phrases, match score |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +MongoDB supports query operations that perform a text search on |
| 21 | +string content in documents. To perform a text search, MongoDB uses a text index |
| 22 | +and the ``$text`` query operator. To learn more about text searches, see |
| 23 | +:manual:`Text Search </text-search/>` in the Server manual. |
| 24 | + |
| 25 | +The driver provides the ``Filters.text()`` helper method to facilitate |
| 26 | +the creation of text search query filters. |
| 27 | + |
| 28 | +Prerequisites |
| 29 | +------------- |
| 30 | + |
| 31 | +.. include:: /includes/prereq-restaurants.rst |
| 32 | + |
| 33 | +.. code-block:: scala |
| 34 | + |
| 35 | + import org.mongodb.scala._ |
| 36 | + import org.mongodb.scala.model._ |
| 37 | + |
| 38 | +.. include:: /includes/obs-note.rst |
| 39 | + |
| 40 | +Connect to a MongoDB Deployment |
| 41 | +------------------------------- |
| 42 | + |
| 43 | +.. include:: /includes/connect-section.rst |
| 44 | + |
| 45 | +Create the Text Index |
| 46 | +--------------------- |
| 47 | + |
| 48 | +To create a text index, use the ``Indexes.text()`` static helper to |
| 49 | +create a specification for a text index and pass the specification to the |
| 50 | +``MongoCollection.createIndex()`` method to create the index. |
| 51 | + |
| 52 | +The following example creates a text index on the ``name`` field in |
| 53 | +documents in the collection: |
| 54 | + |
| 55 | +.. code-block:: scala |
| 56 | + |
| 57 | + collection.createIndex(Indexes.text("name")).printResults() |
| 58 | + |
| 59 | +Perform Text Search |
| 60 | +------------------- |
| 61 | + |
| 62 | +To perform text search, use the ``Filters.text()`` helper method to specify |
| 63 | +the text search query filter. |
| 64 | + |
| 65 | +For example, the following code performs a text search on the ``name`` |
| 66 | +field to match the strings ``"bakery"`` or ``"coffee"``: |
| 67 | + |
| 68 | +.. io-code-block:: |
| 69 | + :copyable: true |
| 70 | + |
| 71 | + .. input:: |
| 72 | + :language: scala |
| 73 | + |
| 74 | + collection.countDocuments(Filters.text("bakery coffee")).printResults("Text search matches: ") |
| 75 | + |
| 76 | + .. output:: |
| 77 | + :language: none |
| 78 | + :visible: false |
| 79 | + |
| 80 | + Text search matches: [2] |
| 81 | + |
| 82 | +Text Score |
| 83 | +~~~~~~~~~~ |
| 84 | + |
| 85 | +For each matching document, text search assigns a score that represents |
| 86 | +the relevance of a document to the specified text search query filter. |
| 87 | +To return and sort by score, use the ``$meta`` operator in the |
| 88 | +projection document and the sort expression: |
| 89 | + |
| 90 | +.. code-block:: scala |
| 91 | + |
| 92 | + collection.find(Filters.text("bakery cafe")) |
| 93 | + .projection(Projections.metaTextScore("score")) |
| 94 | + .sort(Sorts.metaTextScore("score")) |
| 95 | + .printResults() |
| 96 | + |
| 97 | +Specify a Text Search Option |
| 98 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 99 | + |
| 100 | +The ``Filters.text()`` helper can accept various text search |
| 101 | +options. The driver provides the ``TextSearchOptions`` class to |
| 102 | +specify these options. |
| 103 | + |
| 104 | +For example, the following text search specifies the text search |
| 105 | +language option when performing a text search for the word ``"cafe"``: |
| 106 | + |
| 107 | + |
| 108 | +.. io-code-block:: |
| 109 | + :copyable: true |
| 110 | + |
| 111 | + .. input:: |
| 112 | + :language: scala |
| 113 | + |
| 114 | + collection.countDocuments(Filters.text("cafe", TextSearchOptions().language("english"))) |
| 115 | + .printResults("Text search matches (english): ") |
| 116 | + |
| 117 | + .. output:: |
| 118 | + :language: none |
| 119 | + :visible: false |
| 120 | + |
| 121 | + Text search matches (english): [1] |
| 122 | + |
| 123 | +To learn more about text search, see the following sections in |
| 124 | +the MongoDB Server manual: |
| 125 | + |
| 126 | +- :manual:`$text </reference/operator/query/text/#mongodb-query-op.-text>` |
| 127 | +- :manual:`Text Indexes </core/indexes/index-types/index-text/>` |
| 128 | +- :manual:`Specify the Default Language for a Text Index </core/indexes/index-types/index-text/specify-text-index-language/>` |
0 commit comments