Skip to content

DOCSP-17260 Direct Text Search traffic to Atlas Search #5946

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 82 additions & 5 deletions source/core/link-text-indexes.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,89 @@
============
Text Indexes
============
==============================
Perform a Text Search (Legacy)
==============================

.. default-domain:: mongodb

.. include:: /includes/fact-text-index.rst

.. include:: /includes/fact-create-text-index.rst
.. include:: /includes/fact-text-search-legacy-atlas.rst

.. include:: /includes/fact-text-index.rst

See the :doc:`/core/index-text` section for a full reference on text
indexes, including behavior, tokenization, and properties.

.. _text-index-eg:

Examples
--------

This example demonstrates how to build a text index and use it to find
coffee shops, given only text fields.

Create a Collection
~~~~~~~~~~~~~~~~~~~

Create a collection ``stores`` with the following documents:

.. code-block:: javascript

db.stores.insertMany(
[
{ _id: 1, name: "Java Hut", description: "Coffee and cakes" },
{ _id: 2, name: "Burger Buns", description: "Gourmet hamburgers" },
{ _id: 3, name: "Coffee Shop", description: "Just coffee" },
{ _id: 4, name: "Clothes Clothes Clothes", description: "Discount clothing" },
{ _id: 5, name: "Java Shopping", description: "Indonesian goods" }
]
)

Create a Text Index
~~~~~~~~~~~~~~~~~~~

.. include:: /includes/fact-create-text-index.rst

Search for an Exact Phrase
~~~~~~~~~~~~~~~~~~~~~~~~~~

You can also search for exact phrases by wrapping them in double-quotes.
If the ``$search`` string includes a phrase and individual terms, text
search will only match documents that include the phrase.

For example, the following will find all documents containing
"coffee shop":

.. code-block:: javascript

db.stores.find( { $text: { $search: "\"coffee shop\"" } } )

For more information, see :ref:`text-operator-phrases`.

Exclude a Term
~~~~~~~~~~~~~~

To exclude a word, you can prepend a "``-``" character. For example, to
find all stores containing "java" or "shop" but not "coffee", use the
following:

.. code-block:: javascript

db.stores.find( { $text: { $search: "java shop -coffee" } } )

Sort the Results
~~~~~~~~~~~~~~~~

MongoDB will return its results in unsorted order by default. However,
text search queries will compute a relevance score for each document
that specifies how well a document matches the query.

To sort the results in order of relevance score, you must explicitly
project the :expression:`$meta` ``textScore`` field and sort on it:

.. code-block:: javascript

db.stores.find(
{ $text: { $search: "java coffee shop" } },
{ score: { $meta: "textScore" } }
).sort( { score: { $meta: "textScore" } } )

Text search is also available in the aggregation pipeline.
12 changes: 6 additions & 6 deletions source/core/text-search-operators.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
=====================
Text Search Operators
=====================
==============================
Text Search Operators (Legacy)
==============================

.. default-domain:: mongodb

Expand All @@ -10,6 +10,8 @@ Text Search Operators
:depth: 1
:class: singlecol

.. include:: /includes/fact-text-search-legacy-atlas.rst

.. note::

.. include:: /includes/extracts/views-unsupported-text-search.rst
Expand Down Expand Up @@ -39,13 +41,12 @@ operators, including restrictions and behavior, see:

- :expression:`$meta` projection operator


Aggregation Framework
---------------------

When working with the :doc:`/aggregation` framework, use
:pipeline:`$match` with a :query:`$text` expression to execute a text
search query. To sort the results in order of relevance score,use the
search query. To sort the results in order of relevance score, use the
:expression:`$meta` *aggregation operator* in the :pipeline:`$sort`
stage [#meta-aggregation]_.

Expand All @@ -58,4 +59,3 @@ For more information and examples of text search in the
.. [#meta-aggregation]

.. include:: /includes/fact-meta-operator-disambiguation.rst

9 changes: 5 additions & 4 deletions source/includes/fact-atlas-search-languages.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
For data hosted on MongoDB Atlas, :atlas:`Atlas Search </atlas-search>`
provides support for additional languages. To see the complete list of
languages supported by Atlas Search, see the :atlas:`Atlas Search
Language Analyzers </reference/atlas-search/analyzers/language/>`.
:atlas:`Atlas Search </atlas-search>` also offers common
:atlas:`analyzers for parsing text for full-text search
</reference/atlas-search/analyzers/>`, including support for over
:atlas:`40 language-specific analyzers
</reference/atlas-search/analyzers/language/>`.
8 changes: 2 additions & 6 deletions source/includes/fact-create-text-index.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
To perform text search queries, you must have a
``text`` index on your collection. A collection can only have **one**
text search index, but that index can cover multiple fields.

For example you can run the following in :binary:`~bin.mongosh` to
allow text search over the ``name`` and ``description`` fields:
Run the following in :binary:`~bin.mongosh` to allow text search over
the ``name`` and ``description`` fields:

.. code-block:: javascript

Expand Down
9 changes: 6 additions & 3 deletions source/includes/fact-text-index.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
MongoDB provides :ref:`text indexes <index-feature-text>` to support
text search queries on string content. ``text`` indexes can include any
field whose value is a string or an array of string elements.
To run legacy text search queries, you must have a ``text`` index on
your collection. MongoDB provides :ref:`text indexes
<index-feature-text>` to support text search queries on string content.
``text`` indexes can include any field whose value is a string or an
array of string elements. A collection can only have **one** text
search index, but that index can cover multiple fields.
4 changes: 4 additions & 0 deletions source/includes/fact-text-search-legacy-atlas.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MongoDB offers a :atlas:`premium full-text search solution, MongoDB
Atlas Search </atlas-search/>`, for data hosted on :atlas:`MongoDB
Atlas </>`. A legacy text search capability is available for users
self-managing MongoDB deployments.
3 changes: 2 additions & 1 deletion source/includes/fact-use-text-operator.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ punctuation as delimiters, and perform a logical ``OR`` of all such
tokens in the search string.

For example, you could use the following query to find all stores
containing any terms from the list "coffee", "shop", and "java":
containing any terms from the list "coffee", "shop", and "java" in
the ``stores`` :ref:`collection <text-index-eg>`:

.. code-block:: javascript

Expand Down
2 changes: 2 additions & 0 deletions source/reference/text-search-languages.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Text Search Languages
:depth: 1
:class: singlecol

.. include:: /includes/fact-text-search-legacy-atlas.rst

The :ref:`text index <index-feature-text>` and the :query:`$text`
operator may be used with the following languages, and accepts either the
two-letter ISO 639-1 language code or the long form of the
Expand Down
113 changes: 28 additions & 85 deletions source/text-search.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,104 +10,47 @@ Text Search
:depth: 1
:class: singlecol

Overview
--------
.. include:: /includes/fact-text-search-legacy-atlas.rst

MongoDB supports query operations that perform a text search of string
content. To perform text search, MongoDB uses a
:ref:`text index <index-feature-text>` and the :query:`$text` operator.
MongoDB Atlas Search
--------------------

.. note::

.. include:: /includes/extracts/views-unsupported-text-search.rst

Example
-------

This example demonstrates how to build a text index and use it to find
coffee shops, given only text fields.

Create a collection ``stores`` with the following documents:

.. code-block:: javascript

db.stores.insert(
[
{ _id: 1, name: "Java Hut", description: "Coffee and cakes" },
{ _id: 2, name: "Burger Buns", description: "Gourmet hamburgers" },
{ _id: 3, name: "Coffee Shop", description: "Just coffee" },
{ _id: 4, name: "Clothes Clothes Clothes", description: "Discount clothing" },
{ _id: 5, name: "Java Shopping", description: "Indonesian goods" }
]
)

Text Index
~~~~~~~~~~

.. include:: /includes/fact-text-index.rst

.. include:: /includes/fact-create-text-index.rst

``$text`` Operator
~~~~~~~~~~~~~~~~~~

.. include:: /includes/fact-use-text-operator.rst

Exact Phrase
````````````
For MongoDB Atlas users, MongoDB's Atlas Search supports fine-grained
text indexing and a rich query language for fast, relevant search
results. To learn more about full-text search indexes and
:pipeline:`$search` queries, see:

You can also search for exact phrases by wrapping them in double-quotes.
If the ``$search`` string includes a phrase and individual terms, text search
will only match documents that include the phrase.
- :atlas:`Atlas Search Aggregation Pipeline Stages
</reference/atlas-search/query-syntax/>`
- :atlas:`Defining Atlas Search Indexes
</reference/atlas-search/index-definitions/>`
- :atlas:`Running Atlas Search Queries
</reference/atlas-search/searching/>`

For example, the following will find all documents containing
"coffee shop":

.. code-block:: javascript

db.stores.find( { $text: { $search: "\"coffee shop\"" } } )

For more information, see :ref:`text-operator-phrases`.

Term Exclusion
``````````````

To exclude a word, you can prepend a "``-``" character. For example, to
find all stores containing "java" or "shop" but not "coffee", use the
following:

.. code-block:: javascript

db.stores.find( { $text: { $search: "java shop -coffee" } } )

Sorting
```````

MongoDB will return its results in unsorted order by default. However,
text search queries will compute a relevance score for each document
that specifies how well a document matches the query.
.. include:: /includes/fact-atlas-search-languages.rst

To sort the results in order of relevance score, you must explicitly
project the :expression:`$meta` ``textScore`` field and sort on it:
Legacy Text Search
------------------

.. code-block:: javascript
For self-managed deployments, MongoDB's legacy text search capability
supports query operations that perform a text search of string content.
To perform text search, MongoDB uses a :ref:`text index
<index-feature-text>` and the :query:`$text` operator.

db.stores.find(
{ $text: { $search: "java coffee shop" } },
{ score: { $meta: "textScore" } }
).sort( { score: { $meta: "textScore" } } )
.. note::

Text search is also available in the aggregation pipeline.
.. include:: /includes/extracts/views-unsupported-text-search.rst

Language Support
----------------
To learn more about legacy text search for self-managed deployments,
see:

- :doc:`Text Indexes </core/link-text-indexes/>`
- :doc:`Text Search Operators </core-text-search-operators/>`

MongoDB supports text search for various languages. See
MongoDB also supports text search for various languages. See
:doc:`/reference/text-search-languages` for a list of supported
languages.

.. include:: /includes/fact-atlas-search-languages.rst

.. toctree::
:titlesonly:
:hidden:
Expand Down
2 changes: 2 additions & 0 deletions source/tutorial/text-search-in-aggregation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Text Search in the Aggregation Pipeline
:depth: 1
:class: singlecol

.. include:: /includes/fact-text-search-legacy-atlas.rst

.. _text-agg-expression-behavior:

In the aggregation pipeline, text search is available via the use of
Expand Down