Skip to content

Commit 340b924

Browse files
rachel-mackrustagir
andcommitted
[Java Sync] Add additional SearchOperator helper methods for the rest of the Atlas Search operators (mongodb#658)
* atlas search operators * aggregation page * formatting * table format * references * JS feedback * use shared doc * fix for new core-api variable * eg updates * rebase * eg format * format * release reference * WIP * use List.of() --------- Co-authored-by: rustagir <[email protected]>
1 parent 5834bc1 commit 340b924

File tree

3 files changed

+134
-20
lines changed

3 files changed

+134
-20
lines changed

source/atlas-search.txt

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,28 @@ search and which fields to index.
3030
Sample Data
3131
~~~~~~~~~~~
3232

33-
The example in this guide uses the ``movies`` collection in the ``sample_mflix``
33+
The examples in this guide use the ``movies`` collection in the ``sample_mflix``
3434
database from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to
3535
create a free MongoDB Atlas cluster and load the sample datasets, see the
36-
:atlas:`Get Started with Atlas </getting-started>` guide.
36+
:atlas:`Get Started with Atlas </getting-started>` guide. To learn more about
37+
aggregation operations and builders, see the :ref:`java-aggregation` guide.
3738

3839
Run an Atlas Search Query
3940
-------------------------
4041

4142
This section shows how to create an aggregation pipeline to run an
4243
Atlas Search query on a collection. You can use the ``Aggregates.search()`` builder
4344
method to create a ``$search`` pipeline stage, which specifies the search
44-
criteria. Then, call the ``aggregate()`` method and pass your pipeline as a parameter.
45+
criteria. Then, call the ``aggregate()`` method and pass your pipeline as a
46+
parameter.
4547

46-
.. tip::
48+
.. note:: Only Available on Atlas for MongoDB v4.2 and later
4749

48-
To learn more about aggregation operations and builders, see the :ref:`java-aggregation`
49-
guide.
50+
This aggregation pipeline operator is only available for collections hosted
51+
on :atlas:`MongoDB Atlas </>` clusters running v4.2 or later that are
52+
covered by an :atlas:`Atlas search index </reference/atlas-search/index-definitions/>`.
53+
Learn more about the required setup and the functionality of this operator
54+
from the :ref:`Atlas Search <java-atlas-search>` documentation.
5055

5156
Before running an Atlas Search query, you must create an Atlas Search index
5257
on your collection. To learn how to programmatically create an Atlas Search
@@ -90,6 +95,60 @@ following actions:
9095
Search queries, see :atlas:`Atlas Search Tutorials </atlas-search/tutorials/>`
9196
in the Atlas documentation.
9297

98+
Atlas Search Metadata
99+
---------------------
100+
101+
Use the ``searchMeta()`` method to create a :manual:`$searchMeta
102+
</reference/operator/aggregation/searchMeta/>` pipeline stage, which returns
103+
only the metadata from of the Atlas full-text search results.
104+
105+
.. tip:: Only Available on Atlas for MongoDB v4.4.11 and later
106+
107+
This aggregation pipeline operator is available only
108+
on :atlas:`MongoDB Atlas </>` clusters running v4.4.11 and later. For a
109+
detailed list of version availability, see the MongoDB Atlas documentation
110+
on :atlas:`$searchMeta </atlas-search/query-syntax/#-searchmeta>`.
111+
112+
The following example shows the ``near`` metadata for an Atlas search
113+
aggregation stage:
114+
115+
.. literalinclude:: /includes/fundamentals/code-snippets/builders/AggregateSearchBuilderExample.java
116+
:start-after: // begin atlasSearchMeta
117+
:end-before: // end atlasSearchMeta
118+
:language: java
119+
:dedent:
120+
121+
Learn more about this helper from the
122+
`searchMeta() API documentation <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Aggregates.html#searchMeta(com.mongodb.client.model.search.SearchCollector)>`__.
123+
124+
.. _java-atlas-search-helpers:
125+
126+
Create Pipeline Search Stages
127+
-----------------------------
128+
129+
.. sharedinclude:: dbx/jvm/atlas-search-operator-helpers.rst
130+
131+
.. replacement:: as-idx-link
132+
133+
the :ref:`java-search-indexes` section of the Indexes guide
134+
135+
.. replacement:: atlas-query-operators-example
136+
137+
.. io-code-block::
138+
139+
.. input:: /includes/fundamentals/code-snippets/builders/AggregateSearchBuilderExample.java
140+
:language: java
141+
:start-after: // begin atlasHelperMethods
142+
:end-before: // end atlasHelperMethods
143+
:dedent:
144+
145+
.. output::
146+
:language: console
147+
:visible: false
148+
149+
{"_id": ..., "genres": ["Comedy", "Romance"], "title": "Love at First Bite", "year": 1979}
150+
{"_id": ..., "genres": ["Comedy", "Drama"], "title": "Love Affair", "year": 1994}
151+
93152
Additional Information
94153
----------------------
95154

@@ -105,3 +164,4 @@ the following API documentation:
105164
- `MongoCollection.aggregate() <{+driver-api+}/MongoCollection.html#aggregate(java.util.List)>`__
106165
- `Aggregates.search() <{+core-api+}/client/model/Aggregates.html#search(com.mongodb.client.model.search.SearchCollector)>`__
107166
- `Aggregates.project() <{+core-api+}/client/model/Aggregates.html#project(org.bson.conversions.Bson)>`__
167+
- `SearchOperator <{+core-api+}/client/model/search/SearchOperator.html>`__

source/includes/fundamentals/code-snippets/builders/AggregateSearchBuilderExample.java

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
package fundamentals.builders;
1+
package org.example;
22

3-
import java.util.Arrays;
43
import java.util.List;
54

65
import org.bson.Document;
@@ -14,7 +13,8 @@
1413
import com.mongodb.client.model.Filters;
1514
import com.mongodb.client.model.Projections;
1615
import com.mongodb.client.model.search.SearchOperator;
17-
import com.mongodb.client.model.search.SearchPath;
16+
import static com.mongodb.client.model.search.SearchPath.fieldPath;
17+
1818
public class AggregateSearchBuilderExample {
1919

2020
private static final String CONNECTION_URI = "<connection URI>";
@@ -24,14 +24,14 @@ private static void runMatch(MongoCollection<Document> collection) {
2424
Bson matchStage = Aggregates.match(Filters.eq("title", "Future"));
2525
Bson projection = Aggregates.project(Projections.fields(Projections.include("title", "released")));
2626

27-
List<Bson> aggregateStages = Arrays.asList(matchStage, projection);
27+
List<Bson> aggregateStages = List.of(matchStage, projection);
2828
System.out.println("aggregateStages: " + aggregateStages);
2929
collection.aggregate(
3030
aggregateStages
31-
).forEach(result -> System.out.println(result));
31+
).forEach(result -> System.out.println(result));
3232
}
3333

34-
/*
34+
/*
3535
* Atlas text search aggregation
3636
* Requires Atlas cluster and full text search index
3737
* See https://www.mongodb.com/docs/atlas/atlas-search/tutorial/ for more info on requirements
@@ -40,13 +40,65 @@ private static void runAtlasTextSearch(MongoCollection<Document> collection) {
4040
// begin atlasTextSearch
4141
Bson textSearch = Aggregates.search(
4242
SearchOperator.text(
43-
SearchPath.fieldPath("title"), "Future"));
43+
fieldPath("title"), "Future"));
4444
// end atlasTextSearch
4545

4646
// To condense result data, add this projection into the pipeline
4747
// Bson projection = Aggregates.project(Projections.fields(Projections.include("title", "released")));
4848

49-
List<Bson> aggregateStages = Arrays.asList(textSearch);
49+
List<Bson> aggregateStages = List.of(textSearch);
50+
System.out.println("aggregateStages: " + aggregateStages);
51+
52+
System.out.println("explain:\n" + collection.aggregate(aggregateStages).explain());
53+
collection.aggregate(aggregateStages).forEach(result -> System.out.println(result));
54+
}
55+
56+
/*
57+
* Atlas search aggregation
58+
* Requires Atlas cluster and full text search index
59+
* See https://www.mongodb.com/docs/atlas/atlas-search/tutorial/ for more info on requirements
60+
*/
61+
private static void runAtlasSearchWithSearchHelperMethods(MongoCollection<Document> collection) {
62+
// begin atlasHelperMethods
63+
List<Bson> pipeline = new ArrayList<>();
64+
65+
pipeline.add(Aggregates.search(
66+
SearchOperator.compound()
67+
.filter(
68+
List.of(
69+
SearchOperator.in(fieldPath("genres"), "Comedy"),
70+
SearchOperator.phrase(fieldPath("fullplot"), "new york"),
71+
SearchOperator.numberRange(fieldPath("year")).gtLt(1950, 2000),
72+
SearchOperator.wildcard(fieldPath("title"), "Love *")
73+
))));
74+
75+
pipeline.add(Aggregates.project(
76+
Projections.include("title", "year", "genres")
77+
));
78+
79+
AggregateIterable<Document> results = collection.aggregate(pipeline);
80+
results.forEach(doc -> System.out.println(doc.toJson()));
81+
// end atlasHelperMethods
82+
}
83+
84+
/*
85+
* Atlas search aggregation
86+
* Requires Atlas cluster and full text search index
87+
* See https://www.mongodb.com/docs/atlas/atlas-search/tutorial/ for more info on requirements
88+
*/
89+
private static void runAtlasSearch(MongoCollection<Document> collection) {
90+
// begin atlasSearch
91+
Bson search_stage = Aggregates.search(
92+
SearchOperator.compound()
93+
.filter(List.of(SearchOperator.text(fieldPath("genres"), "Drama")))
94+
.must(List.of(SearchOperator.phrase(fieldPath("cast"), "keanu reeves")))
95+
);
96+
// end atlasSearch
97+
98+
// To condense result data, add this projection into the pipeline
99+
// Bson projection = Aggregates.project(Projections.fields(Projections.include("title", "released")));
100+
101+
List<Bson> aggregateStages = List.of(search_stage);
50102
System.out.println("aggregateStages: " + aggregateStages);
51103

52104
System.out.println("explain:\n" + collection.aggregate(aggregateStages).explain());
@@ -55,12 +107,12 @@ private static void runAtlasTextSearch(MongoCollection<Document> collection) {
55107

56108
private static void runAtlasTextSearchMeta(MongoCollection<Document> collection) {
57109
Bson textSearchMeta =
58-
// begin atlasSearchMeta
59-
Aggregates.searchMeta(
60-
SearchOperator.near(2010, 1, SearchPath.fieldPath("year")));
110+
// begin atlasSearchMeta
111+
Aggregates.searchMeta(
112+
SearchOperator.near(2010, 1, fieldPath("year")));
61113
// end atlasSearchMeta
62114

63-
List<Bson> aggregateStages = Arrays.asList(textSearchMeta);
115+
List<Bson> aggregateStages = List.of(textSearchMeta);
64116
System.out.println("aggregateStages: " + aggregateStages);
65117

66118
collection.aggregate(aggregateStages).forEach(result -> System.out.println(result));
@@ -77,7 +129,9 @@ public static void main(String[] args) {
77129
// Uncomment the methods that correspond to what you're testing
78130
// runMatch(collection);
79131
// runAtlasTextSearch(collection);
80-
runAtlasTextSearchMeta(collection);
132+
// runAtlasSearch(collection);
133+
// runAtlasTextSearchMeta(collection);
134+
// runAtlasSearchWithSearchHelperMethods(collection);
81135
}
82136
}
83137
}

source/references/whats-new.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ and features:
5454

5555
.. replacement:: atlas-query-operators
5656

57-
the :ref:`java-atlas-search` guide
57+
the :ref:`java-atlas-search-helpers` section of the Atlas Search guide
5858

5959
.. _java-version-5.3:
6060

0 commit comments

Comments
 (0)