-
Notifications
You must be signed in to change notification settings - Fork 34
DOCSP-49622 Atlas Search #508
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,104 @@ | |
Run an Atlas Search Query | ||
========================= | ||
|
||
.. TODO | ||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: full text, text analyzer, meta, pipeline, scoring, Lucene | ||
:description: Learn about how to use Atlas Search in the {+driver-long+}. | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
Overview | ||
-------- | ||
|
||
In this guide, you can learn how to use the {+driver-short+} to | ||
run :atlas:`Atlas Search </atlas-search/>` queries 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 | ||
Atlas Search query on a collection. In your array of pipeline stages, | ||
add the ``$search`` stage to specify the search criteria. Then, call | ||
the ``Aggregate()`` method and pass your pipeline array as a parameter. | ||
|
||
.. tip:: | ||
|
||
To learn more about aggregation operations, see the :ref:`golang-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:`golang-atlas-search-indexes` section of the Indexes guide. | ||
|
||
Atlas Search Example | ||
~~~~~~~~~~~~~~~~~~~~ | ||
|
||
This example runs an Atlas Search query by performing the | ||
following actions: | ||
|
||
- Creates a ``$search`` stage that instructs the driver | ||
to query for documents in which the ``title`` field contains | ||
the word ``"Alabama"`` | ||
|
||
- Creates a ``$project`` stage that instructs the driver to | ||
include the ``title`` field in the query results | ||
|
||
- Passes the pipeline stages to the ``Aggregate()`` method and | ||
prints the results | ||
|
||
.. io-code-block:: | ||
:copyable: | ||
|
||
.. input:: /includes/fundamentals/code-snippets/indexes/atlasSearch.go | ||
:start-after: begin-atlas-search | ||
:end-before: end-atlas-search | ||
:language: go | ||
:dedent: | ||
|
||
.. output:: | ||
:language: console | ||
:visible: false | ||
|
||
{ | ||
_id: new ObjectId('...'), | ||
title: 'Alabama Moon' | ||
} | ||
{ | ||
_id: new ObjectId('...'), | ||
title: 'Crazy in Alabama' | ||
} | ||
{ | ||
_id: new ObjectId('...'), | ||
title: 'Sweet Home Alabama' | ||
} | ||
|
||
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 ``Aggregate()`` method, see the | ||
`API documentation <{+api+}/mongo#Collection.Aggregate>`__. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"go.mongodb.org/mongo-driver/v2/bson" | ||
"go.mongodb.org/mongo-driver/v2/mongo" | ||
"go.mongodb.org/mongo-driver/v2/mongo/options" | ||
) | ||
|
||
func main() { | ||
var err error | ||
// connect to the Atlas cluster | ||
ctx := context.Background() | ||
// Retrieves your Atlas connection string | ||
uri := os.Getenv("MONGODB_ATLAS_URI") | ||
if uri == "" { | ||
log.Fatal("MONGODB_ATLAS_URI environment variable is not set") | ||
} | ||
client, err := mongo.Connect(options.Client().SetTimeout(5 * time.Second).ApplyURI(uri)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer client.Disconnect(ctx) | ||
|
||
// set namespace | ||
collection := client.Database("sample_mflix").Collection("movies") | ||
|
||
// begin-atlas-search | ||
// Defines the pipeline | ||
searchStage := bson.D{{"$search", bson.D{ | ||
{"text", bson.D{ | ||
{"path", "title"}, | ||
{"query", "Alabama"}, | ||
}}, | ||
}}} | ||
projectStage := bson.D{{"$project", bson.D{{"title", 1}}}} | ||
|
||
// Runs the pipeline | ||
cursor, err := collection.Aggregate(ctx, mongo.Pipeline{searchStage, projectStage}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Prints the results | ||
var results []bson.D | ||
if err = cursor.All(ctx, &results); err != nil { | ||
panic(err) | ||
} | ||
for _, result := range results { | ||
fmt.Println(result) | ||
} | ||
Comment on lines
+47
to
+54
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. For technical reviewer: My results are not printing, and I'm not sure why. Is this correct? 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. The code looks good to me. I guess you have not set the search index. Just as mentioned in the "atlas-search.txt":
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. It's strange that it's not printing because I do have an Atlas Search index on the title field! If nothing is wrong with this code, maybe there's something weird with how I'm creating the index on my end. I'll keep looking, thanks for checking! |
||
// end-atlas-search | ||
} |
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.
I think the first instruction after your intro line should be about creating indexes, rather than talking about the search stage.
You might want to massage this second paragraph a bit, but basically this: