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 2 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
81 changes: 76 additions & 5 deletions source/core/link-text-indexes.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,83 @@
============
Text Indexes
============
=====================
Perform a Text Search
=====================

.. default-domain:: mongodb

.. include:: /includes/fact-text-index.rst
Overview
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest removing the "Overview" heading. I don't think it adds much here.

--------

.. include:: /includes/fact-create-text-index.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:

Example
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe make this "Examples" since there are several examples in this section.

-------

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.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" }
]
)

.. 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.
4 changes: 1 addition & 3 deletions source/core/text-search-operators.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,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 +57,3 @@ For more information and examples of text search in the
.. [#meta-aggregation]

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

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 perform 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.
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
115 changes: 30 additions & 85 deletions source/text-search.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,104 +10,49 @@ Text Search
:depth: 1
:class: singlecol

Overview
--------
MongoDB offers a premium full-text search solution, MongoDB Atlas
Search, for MongoDB Atlas users and a legacy text search capability for
self-managed deployments.

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 using several kinds of text analyzers and searching using
a rich query language. 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