Skip to content

Commit bccd977

Browse files
[Scala] Add additional SearchOperator helper methods for the rest of the Atlas Search operators (#121)
* helper methods * content * ref link * wip * wip * Update whats-new.txt * WIP --------- Co-authored-by: Rea Rustagi <85902999+rustagir@users.noreply.github.com> Co-authored-by: rustagir <rea.rustagi@mongodb.com>
1 parent 897cdf8 commit bccd977

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

source/aggregation.txt

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ plan:
195195
...}
196196

197197
Run an Atlas Full-Text Search
198-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
198+
-----------------------------
199199

200200
.. tip:: Only Available on Atlas for MongoDB v4.2 and later
201201

@@ -244,6 +244,34 @@ This example creates pipeline stages to perform the following actions:
244244
placeholder with the name of the index. To learn more about Atlas Search indexes, see
245245
the :ref:`scala-atlas-search-index` guide.
246246

247+
.. _scala-atlas-search-helper-methods:
248+
249+
Search Operator Helper Methods
250+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
251+
252+
.. sharedinclude:: dbx/jvm/atlas-search-operator-helpers.rst
253+
254+
.. replacement:: as-idx-link
255+
256+
the :ref:`scala-atlas-search-index` guide
257+
258+
.. replacement:: atlas-query-operators-example
259+
260+
.. io-code-block::
261+
262+
.. input:: /includes/aggregation.scala
263+
:language: scala
264+
:start-after: // start-atlas-helper-methods
265+
:end-before: // end-atlas-helper-methods
266+
:dedent:
267+
268+
.. output::
269+
:language: console
270+
:visible: false
271+
272+
{"_id": ..., "genres": ["Comedy", "Romance"], "title": "Love at First Bite", "year": 1979}
273+
{"_id": ..., "genres": ["Comedy", "Drama"], "title": "Love Affair", "year": 1994}
274+
247275
Additional Information
248276
----------------------
249277

@@ -282,4 +310,3 @@ following API documentation:
282310
- `aggregate() <{+driver-api+}/MongoCollection.html#aggregate[C](pipeline:Seq[org.mongodb.scala.bson.conversions.Bson])(implicite:org.mongodb.scala.bson.DefaultHelper.DefaultsTo[C,TResult],implicitct:scala.reflect.ClassTag[C]):org.mongodb.scala.AggregateObservable[C]>`__
283311
- `Aggregates <{+driver-api+}/model/Aggregates$.html>`__
284312
- `explain() <{+driver-api+}/AggregateObservable.html#explain[ExplainResult](verbosity:com.mongodb.ExplainVerbosity)(implicite:org.mongodb.scala.bson.DefaultHelper.DefaultsTo[ExplainResult,org.mongodb.scala.Document],implicitct:scala.reflect.ClassTag[ExplainResult]):org.mongodb.scala.SingleObservable[ExplainResult]>`__
285-

source/includes/aggregation.scala

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import com.mongodb.ExplainVerbosity
55
import org.mongodb.scala.model.Projections
66
import org.mongodb.scala.model.search._
77
import org.mongodb.scala.model.search.SearchOptions.searchOptions
8+
import org.mongodb.scala.model.search.SearchPath.fieldPath
9+
import scala.jdk.CollectionConverters._
810

911
object Aggregation {
1012

@@ -50,6 +52,29 @@ object Aggregation {
5052
(e: Throwable) => println(s"There was an error: $e"))
5153
// end-atlas-search
5254

55+
// start-atlas-helper-methods
56+
val searchStage = Aggregates.search(
57+
SearchOperator.compound()
58+
.must(
59+
Iterable(
60+
SearchOperator.in(fieldPath("genres"), List("Comedy")),
61+
SearchOperator.phrase(fieldPath("fullplot"), "new york"),
62+
SearchOperator.numberRange(fieldPath("year")).gtLt(1950, 2000),
63+
SearchOperator.wildcard("Love *", fieldPath("title")),
64+
).asJava
65+
)
66+
)
67+
68+
val projectStage = Aggregates.project(
69+
Projections.include("title", "year", "genres"))
70+
71+
val aggregatePipelineStages = Seq(searchStage, projectStage)
72+
73+
collection.aggregate(aggregatePipelineStages)
74+
.subscribe((doc: Document) => println(doc.toJson()),
75+
(e: Throwable) => println(s"There was an error: $e"))
76+
// end-atlas-helper-methods
77+
5378
Thread.sleep(1000)
5479
mongoClient.close()
5580
}

source/whats-new.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ and features:
6262

6363
.. replacement:: atlas-query-operators
6464

65-
the `SearchOperator <{+core-api+}/client/model/search/SearchOperator.html>`__
66-
interface API documentation
65+
the :ref:`scala-atlas-search-helper-methods` section of the
66+
Aggregation guide
6767

6868
.. _scala-version-5.3:
6969

0 commit comments

Comments
 (0)