Skip to content

[Kotlin Sync] Add additional SearchOperator helper methods for the rest of the Atlas Search operators #87

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 16 commits into from
Apr 2, 2025
52 changes: 50 additions & 2 deletions source/aggregation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Transform Your Data with Aggregation
:values: reference

.. meta::
:keywords: code example, transform, computed, pipeline
:keywords: code example, transform, computed, pipeline, Atlas Search
:description: Learn how to use the Kotlin Sync driver to perform aggregation operations.

.. contents:: On this page
Expand Down Expand Up @@ -80,6 +80,7 @@ The following limitations apply when using aggregation operations:
</reference/operator/aggregation/graphLookup/>` stage has a strict
memory limit of 100 megabytes and ignores the ``allowDiskUse`` option.


Aggregation Example
-------------------

Expand Down Expand Up @@ -188,6 +189,50 @@ and adds the ``$explain`` stage to output the operation details:
...
}

.. _kotlin-sync-atlas-search-stage:

Atlas Search
------------

You can perform an :atlas:`Atlas Search </atlas-search>` query by creating and running
an aggregation pipeline that contains one of the following pipeline stages:

- ``$search``
- ``$searchMeta``

To learn more about Atlas Search pipeline stages, see :atlas:`Choose the
Aggregation Pipeline Stage </atlas-search/query-syntax/>` in the Atlas
documentation.

Create a Pipeline Search Stage
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can create the search criteria in your Atlas Search pipeline stage
by using Search operators.

.. sharedinclude:: dbx/jvm/atlas-search-operator-helpers.rst

.. replacement:: as-idx-link

the :ref:`kotlin-sync-atlas-search-index` guide

.. replacement:: atlas-query-operators-example

.. io-code-block::

.. input:: /includes/aggregation/aggregation.kt
:language: kotlin
:start-after: // start-atlas-searchoperator-helpers
:end-before: // end-atlas-searchoperator-helpers
:dedent:

.. output::
:language: console
:visible: false

Document{{_id=..., genres=[Comedy, Romance], title=Love at First Bite, year=1979}}
Document{{_id=..., genres=[Comedy, Drama], title=Love Affair, year=1994}}

Additional Information
----------------------

Expand Down Expand Up @@ -217,4 +262,7 @@ For more information about executing aggregation operations with the {+driver-sh
see the following API documentation:

- `aggregate() <{+driver-api+}/-mongo-collection/aggregate.html>`__
- `AggregateIterable <{+driver-api+}/-aggregate-iterable/index.html>`__
- `AggregateIterable <{+driver-api+}/-aggregate-iterable/index.html>`__
- `Aggregates <{+core-api+}/client/model/Aggregates>`__
- `search() <{+core-api+}/client/model/Aggregates#search(com.mongodb.client.model.search.SearchOperator)>`__
- `project() <{+core-api+}/client/model/Aggregates#project(org.bson.conversions.Bson)>`__
25 changes: 25 additions & 0 deletions source/includes/aggregation/aggregation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import com.mongodb.client.model.Accumulators
import com.mongodb.client.model.Aggregates
import com.mongodb.client.model.Filters
import com.mongodb.kotlin.client.MongoClient
import com.mongodb.client.model.Projections
import com.mongodb.client.model.search.SearchOperator
import com.mongodb.client.model.search.SearchPath.fieldPath
import org.bson.Document

// start-data-class
Expand Down Expand Up @@ -42,5 +45,27 @@ fun main() {
// start-aggregation-explain
print(collection.aggregate(pipeline).explain())
// end-aggregation-explain

// start-atlas-searchoperator-helpers
val searchStage = Aggregates.search(
SearchOperator.compound()
.filter(
listOf(
SearchOperator.`in`(fieldPath("genres"), listOf("Comedy")),
SearchOperator.phrase(fieldPath("fullplot"), "new york"),
SearchOperator.numberRange(fieldPath("year")).gtLt(1950, 2000),
SearchOperator.wildcard(fieldPath("title"), "Love *")
)
)
)

val projectStage = Aggregates.project(
Projections.include("title", "year", "genres"))

val pipeline = listOf(searchStage, projectStage)
val results = collection.aggregate(pipeline)

results.forEach { result -> println(result) }
// end-atlas-searchoperator-helpers
}

3 changes: 1 addition & 2 deletions source/whats-new.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ and features:

.. replacement:: atlas-query-operators

the `SearchOperator <{+core-api+}/client/model/search/SearchOperator.html>`__
interface API documentation
the :ref:`Atlas Search <kotlin-sync-atlas-search-stage>` section of the Aggregation guide

.. _kotlin-sync-version-5.3:

Expand Down
Loading