-
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
Open
lindseymoore
wants to merge
3
commits into
mongodb:comp-cov
Choose a base branch
from
lindseymoore:DOCSP-49622
base: comp-cov
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 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
56 changes: 56 additions & 0 deletions
56
source/includes/fundamentals/code-snippets/indexes/atlasSearch.go
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,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) | ||
} | ||
// 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.
Uh oh!
There was an error while loading. Please reload this page.
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.
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 comment
The 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":
Uh oh!
There was an error while loading. Please reload this page.
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.
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!