Skip to content

Commit 8bfcf13

Browse files
authored
DOCSP-38211: text search (#18)
* DOCSP-38211: text search * add tag
1 parent 6013ed4 commit 8bfcf13

File tree

2 files changed

+131
-3
lines changed

2 files changed

+131
-3
lines changed

source/tutorials.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ Tutorials
1414
/tutorials/write-ops/
1515
/tutorials/aggregation/
1616
/tutorials/change-stream/
17-
18-
..
1917
/tutorials/text-search/
18+
19+
..
2020
/tutorials/geo/
2121
/tutorials/gridfs/
2222
/tutorials/command/
@@ -29,9 +29,9 @@ Tutorials
2929
- :ref:`scala-write-ops`
3030
- :ref:`scala-aggregation`
3131
- :ref:`scala-changestream`
32+
- :ref:`scala-text-search`
3233

3334
..
34-
- :ref:`scala-text-search`
3535
- :ref:`scala-geo`
3636
- :ref:`scala-gridfs`
3737
- :ref:`scala-run-command`

source/tutorials/text-search.txt

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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

Comments
 (0)