-
Notifications
You must be signed in to change notification settings - Fork 43
DOCSP-47824: Atlas search #625
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
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,84 @@ Atlas Search | |
:depth: 2 | ||
:class: singlecol | ||
|
||
See :atlas:`Atlas Search </atlas-search/>` in the MongoDB Atlas documentation. | ||
Overview | ||
-------- | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [s] Might be good to include some language in the intro that this using the Java driver. |
||
In this guide, you can learn how to use the **Atlas Search** feature on a collection. | ||
Atlas Search enables you to perform full-text searches on collections hosted on MongoDB | ||
Atlas. Atlas Search indexes specify the behavior of the search and which fields to index. | ||
|
||
Sample Data | ||
~~~~~~~~~~~ | ||
|
||
The example in this guide uses the ``movies`` collection in the ``sample_mflix`` | ||
database from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to | ||
create a free MongoDB Atlas cluster and load the sample datasets, see the | ||
:atlas:`Get Started with Atlas </getting-started>` guide. | ||
|
||
Run an Atlas Search Query | ||
------------------------- | ||
|
||
This section shows how to create an aggregation pipeline to run an | ||
stIncMale marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Atlas Search on a collection. You can use the ``Aggregates.search()`` builder | ||
method to create a ``$search`` pipeline stage, which specifies the search | ||
criteria. Then, call the ``aggregate()`` method and pass your pipeline as a parameter. | ||
|
||
.. tip:: | ||
|
||
To learn more about aggregation operations and builders, see the :ref:`java-aggregation` | ||
guide. | ||
|
||
Before running an Atlas Search query, you must create an Atlas Search index | ||
on your collection. To learn how to programmatically create an Atlas Search | ||
index, see the :ref:`java-search-indexes` section in the Indexes guide. | ||
|
||
Atlas Search Example | ||
~~~~~~~~~~~~~~~~~~~~ | ||
|
||
This example performs an Atlas Search query by calling the | ||
``aggregate()`` method and passing the following pipeline stage | ||
stIncMale marked this conversation as resolved.
Show resolved
Hide resolved
|
||
builders: | ||
|
||
- ``Aggregates.search()``: Specifies a search query for documents in which | ||
the ``title`` field contains the word ``"Alabama"`` | ||
stIncMale marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- ``Aggregates.project()``: Projects the ``title`` field of each matching document | ||
|
||
.. io-code-block:: | ||
:copyable: | ||
|
||
.. input:: /includes/AtlasSearch.java | ||
:start-after: begin-atlas-search | ||
:end-before: end-atlas-search | ||
:language: java | ||
:dedent: | ||
|
||
.. output:: | ||
:language: console | ||
:visible: false | ||
|
||
{"_id": {"$oid": "..."}, "title": "Alabama Moon"} | ||
{"_id": {"$oid": "..."}, "title": "Crazy in Alabama"} | ||
{"_id": {"$oid": "..."}, "title": "Sweet Home Alabama"} | ||
|
||
.. tip:: Java Driver Atlas Search Examples | ||
|
||
To view more examples that use the {+driver-short+} to perform Atlas | ||
Search queries, see :atlas:`Atlas Search Tutorials </atlas-search/tutorials/>` | ||
in the Atlas documentation. | ||
|
||
Additional Information | ||
---------------------- | ||
|
||
To learn more about Atlas Search, see :atlas:`Atlas Search </atlas-search/>` | ||
in the Atlas documentation. | ||
|
||
API Documentation | ||
~~~~~~~~~~~~~~~~~ | ||
|
||
To learn more about the methods mentioned in this guide, see | ||
the following API documentation: | ||
|
||
- `MongoCollection.aggregate() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#aggregate(java.util.List)>`__ | ||
- `Aggregates.search() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Aggregates.html#search(com.mongodb.client.model.search.SearchCollector)>`__ | ||
- `Aggregates.project() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Aggregates.html#project(org.bson.conversions.Bson)>`__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Runs an Atlas Search query by using the Java driver | ||
|
||
package org.example; | ||
|
||
import com.mongodb.client.MongoClient; | ||
import com.mongodb.client.MongoClients; | ||
import com.mongodb.client.MongoCollection; | ||
import com.mongodb.client.MongoDatabase; | ||
import com.mongodb.client.model.Aggregates; | ||
import com.mongodb.client.model.Projections; | ||
import com.mongodb.client.model.search.SearchOperator; | ||
import com.mongodb.client.model.search.SearchPath; | ||
import org.bson.Document; | ||
import java.util.Arrays; | ||
|
||
public class AtlasSearch { | ||
public static void main(String[] args) { | ||
String uri = "<connection string>"; | ||
|
||
try (MongoClient mongoClient = MongoClients.create(uri)) { | ||
MongoDatabase database = mongoClient.getDatabase("sample_mflix"); | ||
MongoCollection<Document> collection = database.getCollection("movies"); | ||
|
||
// Queries for documents that have a "title" value containing the word "Alabama" | ||
// begin-atlas-search | ||
collection.aggregate( | ||
Arrays.asList( | ||
Aggregates.search(SearchOperator.text( | ||
SearchPath.fieldPath("title"), "Alabama")), | ||
Aggregates.project(Projections.include("title")) | ||
) | ||
).forEach(doc -> System.out.println(doc.toJson())); | ||
// end-atlas-search | ||
|
||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[s] Maybe we can link to the Atlas Search docs here, similar to the Atlas Vector Search page.