Skip to content

Commit de3caa3

Browse files
committed
atlas operators
1 parent 666e714 commit de3caa3

File tree

1 file changed

+325
-13
lines changed

1 file changed

+325
-13
lines changed

source/atlas-search.txt

Lines changed: 325 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,328 @@ form of an :ref:`aggregation pipeline <java-aggregation>` stage.
3131

3232
The following operators:
3333

34-
- ``phrase``
35-
36-
- ``regex``
37-
38-
- ``queryString``
39-
40-
- ``equals``
41-
42-
- ``moreLikeThis``
43-
44-
- ``in``
45-
46-
- ``wildcard``
34+
- :ref:`phrase <java-as-phrase>`
35+
36+
- :ref:`regex <java-as-regex>`
37+
38+
- :ref:`queryString <java-as-querystring>`
39+
40+
- :ref:`equals <java-as-equals>`
41+
42+
- :ref:`moreLikeThis <java-as-morelikethis>`
43+
44+
- :ref:`in <java-as-in>`
45+
46+
- :ref:`wildcard<java-as-wildcard>`
47+
48+
The following examples use the MongoDB Atlas sample dataset. Specifically, the
49+
``movies`` collection in the ``sample_mflix`` database. You can learn how
50+
to set up your own free-tier Atlas cluster and how to load the sample dataset
51+
in our :ref:`quick start guide <java-get-started>`.
52+
53+
.. _java-as-phrase:
54+
55+
phrase
56+
------
57+
58+
The ``phrase`` operator searches documents for terms in an order similar to the
59+
query. For more information, see the :atlas:`phrase</atlas-search/phrase/>` guide in
60+
the Atlas documentation.
61+
62+
The ``phrase()`` method accepts the following parameters:
63+
64+
.. list-table::
65+
:header-rows: 1
66+
:stub-columns: 1
67+
:widths: 30,20,50
68+
69+
* - Parameter
70+
- Type
71+
- Description
72+
73+
* - path
74+
- ``SearchPath`` of Array of ``SearchPath`` objects
75+
- Indexed field or fields to search.
76+
77+
* - query
78+
- string or Array of strings
79+
- String or strings to search for.
80+
81+
* - slop
82+
*(Optional)*
83+
- integer
84+
- Allowable distance between words in the query phrase.
85+
86+
* - synonym
87+
*(Optional)*
88+
- string
89+
- Name of the :atlas:`synonym mapping definition </atlas-search/synonyms/#std-label-synonyms-ref>` in the index definition.
90+
91+
The following code creates a search stage for an aggregation pipeline that uses
92+
the ``phase()`` method to find movies that include ``"keanu reeves"`` or names
93+
similar to it in the cast. This would include misspellings and difference capitalizations.
94+
95+
.. code-block:: java
96+
97+
Bson search_stage = Aggregates.search(
98+
SearchOperator.phrase(fieldPath("cast"), "keanu reeves")
99+
);
100+
101+
.. _java-as-regex:
102+
103+
regex
104+
-----
105+
106+
The ``regex`` operator interprets the query field as a regular expression. For
107+
more information, see the :atlas:`regex</atlas-search/regex/>` guide in the
108+
Atlas documentation. You can also find a list of :atlas:`supported Lucene
109+
regular expression operators
110+
</atlas-search/regex/#lucene-regular-expression-behavior>` on this page.
111+
112+
The ``regex()`` method accepts the following parameters:
113+
114+
.. list-table::
115+
:header-rows: 1
116+
:stub-columns: 1
117+
:widths: 30,20,50
118+
119+
* - Parameter
120+
- Type
121+
- Description
122+
123+
* - path
124+
- ``SearchPath`` of Array of ``SearchPath`` objects
125+
- Indexed field or fields to search.
126+
127+
* - query
128+
- string or Array of strings
129+
- String or strings, interpreted as regular expressions, to search for.
130+
131+
.. These don't appear to be implemented?
132+
.. * - allowAnalyzedField
133+
.. *(Optional)*
134+
.. - boolean
135+
.. - Must be set to true if the query is run against an analyzed field.
136+
137+
.. * - score
138+
.. *(Optional)*
139+
.. - ``SearchScore``
140+
.. - Score to assign to matching search term results.
141+
142+
143+
The following code creates a search stage for an aggregation pipeline that uses
144+
the ``regex()`` method to find movies with titles that end with the word ``"Seattle"``:
145+
146+
.. code-block:: java
147+
148+
Bson search_stage = Aggregates.search(
149+
SearchOperator.regex(fieldPath("title"), "(.*) Seattle")
150+
);
151+
152+
.. _java-as-querystring:
153+
154+
queryString
155+
-----------
156+
157+
The ``queryString`` operator queries a combination of indexed fields and values.
158+
For more information, see the :atlas:`queryString</atlas-search/queryString/>`
159+
guide in the Atlas documentation.
160+
161+
The ``queryString()`` method accepts the following parameters:
162+
163+
.. list-table::
164+
:header-rows: 1
165+
:stub-columns: 1
166+
:widths: 30,20,50
167+
168+
* - Parameter
169+
- Type
170+
- Description
171+
172+
* - path
173+
- ``SearchPath``
174+
- Indexed field to search.
175+
176+
* - value
177+
- string or Array of strings
178+
- String or strings to search for.
179+
180+
* - score
181+
*(Optional)*
182+
- ``SearchScore``
183+
- Score to assign to matching search term results.
184+
185+
The following code creates a search stage for an aggregation pipeline that uses
186+
the ``queryString()`` method to find movies with the title ``"Rocky IV"`` with
187+
any representation of the number four:
188+
189+
.. code-block:: java
190+
191+
Bson search_stage = Aggregates.search(
192+
SearchOperator.queryString(fieldPath("title"), "Rocky AND (IV OR 4 OR Four)")
193+
);
194+
195+
.. _java-as-equals:
196+
197+
equals
198+
------
199+
200+
The ``equals`` operator queries a combination of indexed fields and values.
201+
For more information, see the :atlas:`equals</atlas-search/equals/>`
202+
guide in the Atlas documentation.
203+
204+
The ``equals()`` method accepts the following parameters:
205+
206+
.. list-table::
207+
:header-rows: 1
208+
:stub-columns: 1
209+
:widths: 30,20,50
210+
211+
* - Parameter
212+
- Type
213+
- Description
214+
215+
* - path
216+
- ``SearchPath``
217+
- Indexed field to search.
218+
219+
* - value
220+
- boolean, objectId, UUID, integer, double, DateTime, string, null
221+
- Value to query for.
222+
223+
The following code creates a search stage for an aggregation pipeline that uses
224+
the ``equals()`` method to find movies with a runtime of ``30`` minutes:
225+
226+
.. code-block:: java
227+
228+
Bson search_stage = Aggregates.search(
229+
SearchOperator.equals(fieldPath("runtime"), 30)
230+
);
231+
232+
.. _java-as-morelikethis:
233+
234+
moreLikeThis
235+
------------
236+
237+
The ``moreLikeThis`` operator returns documents similar to the input documents.
238+
For more information, see the :atlas:`moreLikeThis</atlas-search/moreLikeThis/>`
239+
guide in the Atlas documentation.
240+
241+
The ``moreLikeThis()`` method accepts the following parameters:
242+
243+
.. list-table::
244+
:header-rows: 1
245+
:stub-columns: 1
246+
:widths: 30,20,50
247+
248+
* - Parameter
249+
- Type
250+
- Description
251+
252+
* - like
253+
- BSON document or Array of BSON documents
254+
- A representative example of the type of document to return.
255+
256+
* - score
257+
*(Optional)*
258+
- ``SearchScore``
259+
- Score to assign to matching search term results.
260+
261+
The following code creates a search stage for an aggregation pipeline that uses
262+
the ``moreLikeThis()`` method to find movies with similar descriptions to the examples:
263+
264+
.. code-block:: java
265+
266+
Bson search_stage = Aggregates.search(
267+
SearchOperator.moreLikeThis(asList(
268+
new BsonDocument("genre", new BsonString("Drama"))
269+
.append("cast", new BsonString("keanu reeves")),
270+
new BsonDocument("genre", new BsonString("Sci-Fi"))))
271+
);
272+
273+
274+
.. _java-as-in:
275+
276+
in
277+
--
278+
279+
The ``in`` operator searches for an array of number, date, boolean,
280+
objectId, uuid, or string values at the given path and returns documents where
281+
the value of the field equals any value in the specified array. For more
282+
information, see the :atlas:`in</atlas-search/in/>` guide in the Atlas
283+
documentation.
284+
285+
The ``in()`` method accepts the following parameters:
286+
287+
.. list-table::
288+
:header-rows: 1
289+
:stub-columns: 1
290+
:widths: 30,20,50
291+
292+
* - Parameter
293+
- Type
294+
- Description
295+
296+
* - path
297+
- string
298+
- Indexed field to search.
299+
300+
* - score
301+
*(Optional)*
302+
- ``SearchScore``
303+
- Score to assign to matching search term results.
304+
305+
* - value
306+
- boolean, objectId, integer, double, DateTime, uuid
307+
- Value or array of values to search.
308+
309+
The following code creates a search stage for an aggregation pipeline that uses
310+
the ``in()`` method to find movies that were made between 1995 and 1999, inclusive:
311+
312+
.. code-block:: java
313+
314+
Bson search_stage = Aggregates.search(
315+
SearchOperator.in(fieldPath("year"), [1995, 1996, 1997, 1998, 1999])
316+
);
317+
318+
.. _java-as-wildcard:
319+
320+
wildcard
321+
--------
322+
323+
The ``wildcard`` operator enables queries which use special characters in the search
324+
string. For more information, see the :atlas:`wildcard</atlas-search/in/>` guide in
325+
the Atlas documentation, which includes a list of supported special characters.
326+
327+
The ``wildcard()`` method accepts the following parameters:
328+
329+
.. list-table::
330+
:header-rows: 1
331+
:stub-columns: 1
332+
:widths: 30,20,50
333+
334+
* - Parameter
335+
- Type
336+
- Description
337+
338+
* - query
339+
- string or Array of strings
340+
- String or strings to search for.
341+
342+
* - path
343+
- ``SearchPath`` of Array of ``SearchPath`` objects
344+
- Indexed field or fields to search.
345+
346+
* - score
347+
*(Optional)*
348+
- ``SearchScore``
349+
- Score to assign to matching search term results.
350+
351+
The following code creates a search stage for an aggregation pipeline that uses
352+
the ``wildcard()`` method to find movies with titles beginning with ``"Green"``:
353+
354+
.. code-block:: java
355+
356+
Bson search_stage = Aggregates.search(
357+
SearchOperator.in("Green *", fieldPath("year"))
358+
);

0 commit comments

Comments
 (0)