From be7523972280f8781c930de19ef0607ed67923fd Mon Sep 17 00:00:00 2001 From: Rachel Mackintosh <rachel.mackintosh@mongodb.com> Date: Thu, 27 Mar 2025 15:38:03 -0400 Subject: [PATCH 1/7] atlas searchoperator helper methods --- .DS_Store | Bin 0 -> 6148 bytes source/aggregation.txt | 32 ++++++++++++++++++++++++++++++++ source/whats-new.txt | 4 ++-- 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**<q8>++&mCkOWA81W14cNZ<zv;LbK1Poaz?KmsK2CSc!( z0ynLxE!0092;Krf2c+FF_Fe*7ECH>lEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0<F0fCPF1$Cyrb|F7^5{eNG?83~ZUUlGt@xh*qZDeu<Z%US-OSsOPv j)R!Z4KLME7ReXlK;d!wEw5GODWMKRea10D2@KpjYNUI8I literal 0 HcmV?d00001 diff --git a/source/aggregation.txt b/source/aggregation.txt index 3f454875..32f77047 100644 --- a/source/aggregation.txt +++ b/source/aggregation.txt @@ -124,3 +124,35 @@ To ``$explain`` an aggregation pipeline, call the Aggregates.group("$stars", Accumulators.sum("count", 1)))) .explain() .subscribe(new PrintDocumentSubscriber()); + +.. _java-rs-atlas-search-stage: + +Pipelines Stages for Atlas Search +--------------------------------- + +:atlas:`Atlas Search </atlas-search>` queries take the form of an aggregation pipeline stage. Atlas +Search provides ``$search`` and ``$searchMeta`` stages, both of which must be the first +stage in any query pipeline. For more information about Atlas pipeline stages, +see the :atlas:`Choose the Aggregation Pipeline Stage +</atlas-search/query-syntax/>` page in the Atlas +manual. + +.. sharedinclude:: dbx/jvm/atlas-search-operator-helpers.rst + + .. replacement:: atlas-query-operators-example + + .. code-block:: java + + Bson searchStageFilters = Aggregates.search( + SearchOperator.compound() + .filter( + List.of( + SearchOperator.text(fieldPath("genres"), "Drama"), + SearchOperator.phrase(fieldPath("cast"), "sylvester stallone"), + SearchOperator.numberRange(fieldPath("year")).gtLt(1980, 1989), + SearchOperator.wildcard(fieldPath("title"),"Rocky *") + ))); + + .. replacement:: searchoperator-interface-api-docs + + the `SearchOperator Interface API documentation <{+core-api+}/com/mongodb/client/model/search/SearchOperator.html>`__ diff --git a/source/whats-new.txt b/source/whats-new.txt index 0a4a5548..ee04ab7b 100644 --- a/source/whats-new.txt +++ b/source/whats-new.txt @@ -57,8 +57,8 @@ and features: .. replacement:: atlas-query-operators - the `SearchOperator <{+core-api+}/client/model/search/SearchOperator.html>`__ - interface API documentation + the :ref:`Pipelines Stages for Atlas Search <java-rs-atlas-search-stage>` + section of the Aggregation page .. _javars-version-5.3: From fc61efa00dbda6f8f1773950412e82c2512c885a Mon Sep 17 00:00:00 2001 From: Rachel Mackintosh <rachel.mackintosh@mongodb.com> Date: Thu, 27 Mar 2025 16:11:29 -0400 Subject: [PATCH 2/7] fix link --- source/aggregation.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/aggregation.txt b/source/aggregation.txt index 32f77047..98121314 100644 --- a/source/aggregation.txt +++ b/source/aggregation.txt @@ -155,4 +155,4 @@ manual. .. replacement:: searchoperator-interface-api-docs - the `SearchOperator Interface API documentation <{+core-api+}/com/mongodb/client/model/search/SearchOperator.html>`__ + the `SearchOperator Interface API documentation <{+core-api+}/client/model/search/SearchOperator.html>`__ From 4b3de1b09d161272a253db22aeb5d7a44d20d02b Mon Sep 17 00:00:00 2001 From: Rachel Mackintosh <rachel.mackintosh@mongodb.com> Date: Fri, 28 Mar 2025 22:34:53 -0400 Subject: [PATCH 3/7] eg output --- source/aggregation.txt | 26 ++++---- .../aggregation/atlas-search-examples.java | 59 +++++++++++++++++++ 2 files changed, 74 insertions(+), 11 deletions(-) create mode 100644 source/includes/aggregation/atlas-search-examples.java diff --git a/source/aggregation.txt b/source/aggregation.txt index 98121314..05344f16 100644 --- a/source/aggregation.txt +++ b/source/aggregation.txt @@ -141,17 +141,21 @@ manual. .. replacement:: atlas-query-operators-example - .. code-block:: java - - Bson searchStageFilters = Aggregates.search( - SearchOperator.compound() - .filter( - List.of( - SearchOperator.text(fieldPath("genres"), "Drama"), - SearchOperator.phrase(fieldPath("cast"), "sylvester stallone"), - SearchOperator.numberRange(fieldPath("year")).gtLt(1980, 1989), - SearchOperator.wildcard(fieldPath("title"),"Rocky *") - ))); + .. io-code-block:: + + .. input:: source/includes/aggregation/atlas-search-examples.java + :language: java + :start-after: // begin atlasHelperMethods + :end-before: // end atlasHelperMethods + :language: java + :dedent: + + .. output:: + :language: console + :visible: false + + {"_id": {"$oid": "573a1397f29313caabce86db"}, "genres": ["Drama", "Sport"], "cast": ["Sylvester Stallone", "Talia Shire", "Burt Young", "Carl Weathers"], "title": "Rocky III", "year": 1982} + {"_id": {"$oid": "573a1398f29313caabce9af0"}, "genres": ["Drama", "Sport"], "cast": ["Sylvester Stallone", "Talia Shire", "Burt Young", "Carl Weathers"], "title": "Rocky IV", "year": 1985} .. replacement:: searchoperator-interface-api-docs diff --git a/source/includes/aggregation/atlas-search-examples.java b/source/includes/aggregation/atlas-search-examples.java new file mode 100644 index 00000000..e1ffaa37 --- /dev/null +++ b/source/includes/aggregation/atlas-search-examples.java @@ -0,0 +1,59 @@ +package org.example; +import com.mongodb.*; +import com.mongodb.client.model.Projections; +import com.mongodb.reactivestreams.client.*; +import org.bson.Document; + +import reactor.core.publisher.Mono; +import java.util.List; +import org.bson.conversions.Bson; + +import com.mongodb.client.model.Aggregates; +import com.mongodb.client.model.search.SearchOperator; +import static com.mongodb.client.model.search.SearchPath.fieldPath; +import org.reactivestreams.Publisher; + +public class Main { + public static void main(String[] args) { + // Replace the placeholder with your Atlas connection string + String uri = "mongodb+srv://admin:APap8822@cluster0.guj3b.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"; + + // Construct a ServerApi instance using the ServerApi.builder() method + ServerApi serverApi = ServerApi.builder() + .version(ServerApiVersion.V1) + .build(); + + MongoClientSettings settings = MongoClientSettings.builder() + .applyConnectionString(new ConnectionString(uri)) + .serverApi(serverApi) + .build(); + + // Create a new client and connect to the server + try (MongoClient mongoClient = MongoClients.create(settings)) { + MongoDatabase database = mongoClient.getDatabase("sample_mflix"); + MongoCollection<Document> movies = database.getCollection("movies"); + + // start atlasHelperMethods + Bson searchStageFilters = Aggregates.search( + SearchOperator.compound() + .filter( + List.of( + SearchOperator.text(fieldPath("genres"), "Drama"), + SearchOperator.phrase(fieldPath("cast"), "sylvester stallone"), + SearchOperator.numberRange(fieldPath("year")).gtLt(1980, 1989), + SearchOperator.wildcard(fieldPath("title"),"Rocky *") + ))); + + Bson projection = Aggregates.project(Projections.fields( + Projections.include("title", "year", "genres", "cast") + )); + + List<Bson> aggregateStages = List.of(searchStageFilters,projection); + + Publisher<Document> publisher = movies.aggregate(aggregateStages); + publisher.subscribe(new SubscriberHelpers.PrintDocumentSubscriber()); + Mono.from(publisher).block(); + // end atlasHelperMethods + } + } +} From a7c87948d4a762f98024f099faf4c60f55457999 Mon Sep 17 00:00:00 2001 From: Rachel Mackintosh <rachel.mackintosh@mongodb.com> Date: Fri, 28 Mar 2025 22:50:44 -0400 Subject: [PATCH 4/7] format --- source/aggregation.txt | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/source/aggregation.txt b/source/aggregation.txt index 05344f16..c33e7a3d 100644 --- a/source/aggregation.txt +++ b/source/aggregation.txt @@ -127,8 +127,8 @@ To ``$explain`` an aggregation pipeline, call the .. _java-rs-atlas-search-stage: -Pipelines Stages for Atlas Search ---------------------------------- +Pipeline Stages for Atlas Search +-------------------------------- :atlas:`Atlas Search </atlas-search>` queries take the form of an aggregation pipeline stage. Atlas Search provides ``$search`` and ``$searchMeta`` stages, both of which must be the first @@ -147,7 +147,6 @@ manual. :language: java :start-after: // begin atlasHelperMethods :end-before: // end atlasHelperMethods - :language: java :dedent: .. output:: @@ -159,4 +158,4 @@ manual. .. replacement:: searchoperator-interface-api-docs - the `SearchOperator Interface API documentation <{+core-api+}/client/model/search/SearchOperator.html>`__ + the `SearchOperator Interface API documentation <{+core-api+}/client/model/search/SearchOperator.html>`__ From c6150e8dd852127d8ff04e6b0c1f9698536120e7 Mon Sep 17 00:00:00 2001 From: Rachel Mackintosh <rachel.mackintosh@mongodb.com> Date: Fri, 28 Mar 2025 23:13:41 -0400 Subject: [PATCH 5/7] eg filepath --- source/aggregation.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/aggregation.txt b/source/aggregation.txt index c33e7a3d..787756a5 100644 --- a/source/aggregation.txt +++ b/source/aggregation.txt @@ -143,7 +143,7 @@ manual. .. io-code-block:: - .. input:: source/includes/aggregation/atlas-search-examples.java + .. input:: /includes/aggregation/atlas-search-examples.java :language: java :start-after: // begin atlasHelperMethods :end-before: // end atlasHelperMethods From 13ff79d86260a7b496b3ea15b9e2ed82060ce800 Mon Sep 17 00:00:00 2001 From: Rachel Mackintosh <rachel.mackintosh@mongodb.com> Date: Fri, 28 Mar 2025 23:25:55 -0400 Subject: [PATCH 6/7] eg --- source/aggregation.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/aggregation.txt b/source/aggregation.txt index 787756a5..34c25d86 100644 --- a/source/aggregation.txt +++ b/source/aggregation.txt @@ -145,7 +145,7 @@ manual. .. input:: /includes/aggregation/atlas-search-examples.java :language: java - :start-after: // begin atlasHelperMethods + :start-after: // start atlasHelperMethods :end-before: // end atlasHelperMethods :dedent: From 69fda13b31fc0fdb00e72ac59c0f7b17decc9d07 Mon Sep 17 00:00:00 2001 From: rustagir <rea.rustagi@mongodb.com> Date: Tue, 1 Apr 2025 16:44:31 -0400 Subject: [PATCH 7/7] WIP --- source/aggregation.txt | 67 +++++++++++++++---- .../aggregation/atlas-search-examples.java | 33 ++++----- source/indexes.txt | 4 +- source/whats-new.txt | 4 +- 4 files changed, 74 insertions(+), 34 deletions(-) diff --git a/source/aggregation.txt b/source/aggregation.txt index 34c25d86..34d6246a 100644 --- a/source/aggregation.txt +++ b/source/aggregation.txt @@ -127,18 +127,37 @@ To ``$explain`` an aggregation pipeline, call the .. _java-rs-atlas-search-stage: -Pipeline Stages for Atlas Search --------------------------------- +Atlas Search +------------ -:atlas:`Atlas Search </atlas-search>` queries take the form of an aggregation pipeline stage. Atlas -Search provides ``$search`` and ``$searchMeta`` stages, both of which must be the first -stage in any query pipeline. For more information about Atlas pipeline stages, -see the :atlas:`Choose the Aggregation Pipeline Stage -</atlas-search/query-syntax/>` page in the Atlas -manual. +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`` + +The {+driver-short+} provides the `Aggregates.search() +<{+core-api+}/client/model/Aggregates.html#search(com.mongodb.client.model.search.SearchOperator)>`__ +and `Aggregates.searchMeta() +<{+core-api+}/client/model/Aggregates.html#searchMeta(com.mongodb.client.model.search.SearchOperator)>`__ +methods to perform Atlas Search queries. + +To learn more about Atlas Search pipeline stages, see :atlas:`Choose the +Aggregation Pipeline Stage </atlas-search/query-syntax/>` in the Atlas +documentation. + +Create Pipeline Search Stages +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +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:`java-rs-atlas-search-idx-mgmt` section of the Indexes guide + .. replacement:: atlas-query-operators-example .. io-code-block:: @@ -153,9 +172,33 @@ manual. :language: console :visible: false - {"_id": {"$oid": "573a1397f29313caabce86db"}, "genres": ["Drama", "Sport"], "cast": ["Sylvester Stallone", "Talia Shire", "Burt Young", "Carl Weathers"], "title": "Rocky III", "year": 1982} - {"_id": {"$oid": "573a1398f29313caabce9af0"}, "genres": ["Drama", "Sport"], "cast": ["Sylvester Stallone", "Talia Shire", "Burt Young", "Carl Weathers"], "title": "Rocky IV", "year": 1985} + {"_id": ..., "genres": ["Comedy", "Romance"], "title": "Love at First Bite", "year": 1979} + {"_id": ..., "genres": ["Comedy", "Drama"], "title": "Love Affair", "year": 1994} + +Additional Information +---------------------- + +To view a full list of expression operators, see :manual:`Aggregation +Operators </reference/operator/aggregation/>` in the {+mdb-server+} manual. + +To learn about assembling an aggregation pipeline and view examples, see +:manual:`Aggregation Pipeline </core/aggregation-pipeline/>` in the {+mdb-server+} manual. + +To learn more about creating pipeline stages, see :manual:`Aggregation +Stages </reference/operator/aggregation-pipeline/>` in the {+mdb-server+} manual. + +To learn more about explaining MongoDB operations, see +:manual:`Explain Output </reference/explain-results/>` and +:manual:`Query Plans </core/query-plans/>` in the {+mdb-server+} manual. + +API Documentation +~~~~~~~~~~~~~~~~~ - .. replacement:: searchoperator-interface-api-docs +To learn more about the classes and methods mentioned in this guide, see +the following API documentation: - the `SearchOperator Interface API documentation <{+core-api+}/client/model/search/SearchOperator.html>`__ +- `aggregate() <{+driver-api+}/MongoCollection.html#aggregate(java.util.List)>`__ +- `Aggregates <{+core-api+}/client/model/Aggregates.html>`__ +- `AggregatePublisher <{+driver-api+}/AggregatePublisher.html>`__ +- `search() <{+core-api+}/client/model/Aggregates#search(com.mongodb.client.model.search.SearchOperator)>`__ +- `project() <{+core-api+}/client/model/Aggregates#project(org.bson.conversions.Bson)>`__ diff --git a/source/includes/aggregation/atlas-search-examples.java b/source/includes/aggregation/atlas-search-examples.java index e1ffaa37..8e094ebd 100644 --- a/source/includes/aggregation/atlas-search-examples.java +++ b/source/includes/aggregation/atlas-search-examples.java @@ -1,35 +1,30 @@ package org.example; + import com.mongodb.*; import com.mongodb.client.model.Projections; import com.mongodb.reactivestreams.client.*; import org.bson.Document; import reactor.core.publisher.Mono; + import java.util.List; + import org.bson.conversions.Bson; import com.mongodb.client.model.Aggregates; import com.mongodb.client.model.search.SearchOperator; + import static com.mongodb.client.model.search.SearchPath.fieldPath; + import org.reactivestreams.Publisher; -public class Main { +public class SearchHelpers { public static void main(String[] args) { // Replace the placeholder with your Atlas connection string - String uri = "mongodb+srv://admin:APap8822@cluster0.guj3b.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"; - - // Construct a ServerApi instance using the ServerApi.builder() method - ServerApi serverApi = ServerApi.builder() - .version(ServerApiVersion.V1) - .build(); - - MongoClientSettings settings = MongoClientSettings.builder() - .applyConnectionString(new ConnectionString(uri)) - .serverApi(serverApi) - .build(); + String uri = "<connection string>"; // Create a new client and connect to the server - try (MongoClient mongoClient = MongoClients.create(settings)) { + try (MongoClient mongoClient = MongoClients.create(uri)) { MongoDatabase database = mongoClient.getDatabase("sample_mflix"); MongoCollection<Document> movies = database.getCollection("movies"); @@ -38,17 +33,17 @@ public static void main(String[] args) { SearchOperator.compound() .filter( List.of( - SearchOperator.text(fieldPath("genres"), "Drama"), - SearchOperator.phrase(fieldPath("cast"), "sylvester stallone"), - SearchOperator.numberRange(fieldPath("year")).gtLt(1980, 1989), - SearchOperator.wildcard(fieldPath("title"),"Rocky *") + SearchOperator.in(fieldPath("genres"), List.of("Comedy")), + SearchOperator.phrase(fieldPath("fullplot"), "new york"), + SearchOperator.numberRange(fieldPath("year")).gtLt(1950, 2000), + SearchOperator.wildcard(fieldPath("title"), "Love *") ))); Bson projection = Aggregates.project(Projections.fields( - Projections.include("title", "year", "genres", "cast") + Projections.include("title", "year", "genres") )); - List<Bson> aggregateStages = List.of(searchStageFilters,projection); + List<Bson> aggregateStages = List.of(searchStageFilters, projection); Publisher<Document> publisher = movies.aggregate(aggregateStages); publisher.subscribe(new SubscriberHelpers.PrintDocumentSubscriber()); diff --git a/source/indexes.txt b/source/indexes.txt index dda9ca86..dd7f2e82 100644 --- a/source/indexes.txt +++ b/source/indexes.txt @@ -177,6 +177,8 @@ field: .. TODO: To learn more about wildcard indexes, see the :ref:`java-rs-clustered-index` .. guide. +.. _java-rs-atlas-search-idx-mgmt: + Atlas Search Index Management ----------------------------- @@ -275,4 +277,4 @@ The following example deletes an index with the specified name: :dedent: .. TODO: To learn more about removing indexes, see :ref:`java-rs-indexes-remove` -.. in the Work with Indexes guide. \ No newline at end of file +.. in the Work with Indexes guide. diff --git a/source/whats-new.txt b/source/whats-new.txt index ee04ab7b..fd1b73ef 100644 --- a/source/whats-new.txt +++ b/source/whats-new.txt @@ -57,8 +57,8 @@ and features: .. replacement:: atlas-query-operators - the :ref:`Pipelines Stages for Atlas Search <java-rs-atlas-search-stage>` - section of the Aggregation page + the :ref:`java-rs-atlas-search-stage` section of the Aggregation + guide .. _javars-version-5.3: