Skip to content

Commit 1c480fd

Browse files
author
Chris Cho
authored
DOCSP-22127: Atlas searchMeta builder (#259)
* DOCSP-22127: Atlas searchMeta builder
1 parent 9019cc6 commit 1c480fd

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

source/fundamentals/builders/aggregates.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,3 +588,30 @@ field for text that contains the word "Future":
588588

589589
Learn more about the builders from the
590590
`search package API documentation <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/search/package-summary.html>`__.
591+
592+
Atlas Search Metadata
593+
---------------------
594+
595+
Use the ``searchMeta()`` method to create a
596+
:manual:`$searchMeta </reference/operator/aggregation/searchMeta/>`
597+
pipeline stage which returns only the metadata part of the results from
598+
Atlas full-text search queries.
599+
600+
.. tip:: Only Available on Atlas for MongoDB v4.4.11 and later
601+
602+
This aggregation pipeline operator is only available
603+
on :atlas:`MongoDB Atlas </>` clusters running v4.4.11 and later. For a
604+
detailed list of version availability, see the MongoDB Atlas documentation
605+
on :atlas:`$searchMeta </atlas-search/query-syntax/#-searchmeta>`.
606+
607+
The following example shows the ``count`` metadata for an Atlas search
608+
aggregation stage:
609+
610+
.. literalinclude:: /includes/fundamentals/code-snippets/builders/AggregateSearchBuilderExample.java
611+
:start-after: // begin atlasSearchMeta
612+
:end-before: // end atlasSearchMeta
613+
:language: java
614+
:dedent:
615+
616+
Learn more about this helper from the
617+
`searchMeta() API documentation <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Aggregates.html#searchMeta(com.mongodb.client.model.search.SearchCollector)>`__.

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,41 @@ private static void runAtlasTextSearch(MongoCollection<Document> collection) {
4343
SearchPath.fieldPath("title"), "Future"));
4444
// end atlasTextSearch
4545

46-
Bson projection = Aggregates.project(Projections.fields(Projections.include("title", "released")));
46+
// To condense result data, add this projection into the pipeline
47+
// Bson projection = Aggregates.project(Projections.fields(Projections.include("title", "released")));
4748

48-
List<Bson> aggregateStages = Arrays.asList(textSearch, projection);
49+
List<Bson> aggregateStages = Arrays.asList(textSearch);
4950
System.out.println("aggregateStages: " + aggregateStages);
5051

5152
System.out.println("explain:\n" + collection.aggregate(aggregateStages).explain());
5253
collection.aggregate(aggregateStages).forEach(result -> System.out.println(result));
5354
}
5455

56+
private static void runAtlasTextSearchMeta(MongoCollection<Document> collection) {
57+
Bson textSearchMeta =
58+
// begin atlasSearchMeta
59+
Aggregates.searchMeta(
60+
SearchOperator.near(2010, 1, SearchPath.fieldPath("year")));
61+
// end atlasSearchMeta
62+
63+
List<Bson> aggregateStages = Arrays.asList(textSearchMeta);
64+
System.out.println("aggregateStages: " + aggregateStages);
65+
66+
collection.aggregate(aggregateStages).forEach(result -> System.out.println(result));
67+
}
68+
5569
public static void main(String[] args) {
5670
String uri = CONNECTION_URI;
5771

5872
try (MongoClient mongoClient = MongoClients.create(uri)) {
5973
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
6074
MongoCollection<Document> collection = database.getCollection("movies");
6175

76+
77+
// Uncomment the methods that correspond to what you're testing
6278
// runMatch(collection);
63-
runAtlasTextSearch(collection);
79+
// runAtlasTextSearch(collection);
80+
runAtlasTextSearchMeta(collection);
6481
}
6582
}
6683
}

0 commit comments

Comments
 (0)