Skip to content

Commit 9019cc6

Browse files
author
Chris Cho
authored
DOCSP-21671: Atlas Text Search Builder (#258)
* DOCSP-21671: Atlas Text Search Builder
1 parent 9830df3 commit 9019cc6

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

source/fundamentals/builders/aggregates.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,3 +562,29 @@ and ``temperature`` fields:
562562
:end-before: // end setWindowFields
563563
:language: java
564564
:dedent:
565+
566+
Atlas Full-Text Search
567+
----------------------
568+
569+
Use the ``search()`` method to create a :manual:`$search </reference/operator/aggregation/search/>`
570+
pipeline stage that specifies a full-text search of one or more fields.
571+
572+
.. tip:: Only Available on Atlas for MongoDB v4.2 and later
573+
574+
This aggregation pipeline operator is only available for collections hosted
575+
on :atlas:`MongoDB Atlas </>` clusters running v4.2 or later that are
576+
covered by an :atlas:`Atlas search index </reference/atlas-search/index-definitions/>`.
577+
Learn more about the required setup and the functionality of this operator
578+
from the :ref:`Atlas Search <fts-top-ref>` documentation.
579+
580+
The following example creates a pipeline stage that searches the ``title``
581+
field for text that contains the word "Future":
582+
583+
.. literalinclude:: /includes/fundamentals/code-snippets/builders/AggregateSearchBuilderExample.java
584+
:start-after: // begin atlasTextSearch
585+
:end-before: // end atlasTextSearch
586+
:language: java
587+
:dedent:
588+
589+
Learn more about the builders from the
590+
`search package API documentation <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/search/package-summary.html>`__.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package fundamentals.builders;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import org.bson.Document;
7+
import org.bson.conversions.Bson;
8+
9+
import com.mongodb.client.MongoClient;
10+
import com.mongodb.client.MongoClients;
11+
import com.mongodb.client.MongoCollection;
12+
import com.mongodb.client.MongoDatabase;
13+
import com.mongodb.client.model.Aggregates;
14+
import com.mongodb.client.model.Filters;
15+
import com.mongodb.client.model.Projections;
16+
import com.mongodb.client.model.search.SearchOperator;
17+
import com.mongodb.client.model.search.SearchPath;
18+
public class AggregateSearchBuilderExample {
19+
20+
private static final String CONNECTION_URI = "<connection URI>";
21+
22+
// Match aggregation
23+
private static void runMatch(MongoCollection<Document> collection) {
24+
Bson matchStage = Aggregates.match(Filters.eq("title", "Future"));
25+
Bson projection = Aggregates.project(Projections.fields(Projections.include("title", "released")));
26+
27+
List<Bson> aggregateStages = Arrays.asList(matchStage, projection);
28+
System.out.println("aggregateStages: " + aggregateStages);
29+
collection.aggregate(
30+
aggregateStages
31+
).forEach(result -> System.out.println(result));
32+
}
33+
34+
/*
35+
* Atlas text search aggregation
36+
* Requires Atlas cluster and full text search index
37+
* See https://www.mongodb.com/docs/atlas/atlas-search/tutorial/ for more info on requirements
38+
*/
39+
private static void runAtlasTextSearch(MongoCollection<Document> collection) {
40+
// begin atlasTextSearch
41+
Bson textSearch = Aggregates.search(
42+
SearchOperator.text(
43+
SearchPath.fieldPath("title"), "Future"));
44+
// end atlasTextSearch
45+
46+
Bson projection = Aggregates.project(Projections.fields(Projections.include("title", "released")));
47+
48+
List<Bson> aggregateStages = Arrays.asList(textSearch, projection);
49+
System.out.println("aggregateStages: " + aggregateStages);
50+
51+
System.out.println("explain:\n" + collection.aggregate(aggregateStages).explain());
52+
collection.aggregate(aggregateStages).forEach(result -> System.out.println(result));
53+
}
54+
55+
public static void main(String[] args) {
56+
String uri = CONNECTION_URI;
57+
58+
try (MongoClient mongoClient = MongoClients.create(uri)) {
59+
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
60+
MongoCollection<Document> collection = database.getCollection("movies");
61+
62+
// runMatch(collection);
63+
runAtlasTextSearch(collection);
64+
}
65+
}
66+
}

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

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)