-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathlink-text-indexes.txt
112 lines (75 loc) · 3.25 KB
/
link-text-indexes.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
.. _perform-text-search-onprem:
.. _text-search-on-premises:
================================================
Perform a Text Search (Self-Managed Deployments)
================================================
.. default-domain:: mongodb
.. meta::
:keywords: on-prem
:description: Explore text search capabilities for self-managed MongoDB deployments using text indexes to query string content.
.. include:: /includes/extracts/fact-text-search-legacy-atlas.rst
.. include:: /includes/fact-text-index.rst
See the :ref:`<index-type-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" },
{ _id: 6, name: "NYC_Coffee Shop", description: "local NYC coffee" }
]
)
Create a Text Index
~~~~~~~~~~~~~~~~~~~
.. include:: /includes/fact-create-text-index.rst
Search for an Exact String
~~~~~~~~~~~~~~~~~~~~~~~~~~
You can search for exact multi-word strings by wrapping them in double-quotes.
Text search only matches documents that include the whole string.
For example, the following query finds all documents that contain the string
"coffee shop":
.. code-block:: javascript
db.stores.find( { $text: { $search: "\"coffee shop\"" } } )
This query returns the following documents:
.. code-block:: javascript
[
{ _id: 3, name: 'Coffee Shop', description: 'Just coffee' },
{ _id: 6, name: 'NYC_Coffee Shop', description: 'local NYC coffee' }
]
Unless specified, exact string search is not case sensitive or diacritic
sensitive. For example, the following query returns the same
results as the previous query:
.. code-block:: javascript
db.stores.find( { $text: { $search: "\"COFFEé SHOP\"" } } )
Exact string search does not handle stemming or stop words.
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 returns its results in unsorted order by default. However,
``$text`` queries 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`` is also available in the aggregation pipeline.