-
Notifications
You must be signed in to change notification settings - Fork 10
DOCSP-51348-atlas-search-page #122
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 all 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.example | ||
|
||
import com.mongodb.ConnectionString | ||
import com.mongodb.kotlin.client.MongoClient | ||
import com.mongodb.MongoClientSettings | ||
import com.mongodb.client.model.Aggregates.searchMeta | ||
import com.mongodb.client.model.search.SearchOperator | ||
import com.mongodb.client.model.search.SearchPath.fieldPath | ||
import com.mongodb.kotlin.client.MongoCollection | ||
import org.bson.Document | ||
|
||
fun runAtlasTextSearchMeta(collection: MongoCollection<Document>) { | ||
val textSearchMeta = | ||
// begin atlasSearchMeta | ||
searchMeta( | ||
SearchOperator.near(2010, 1, fieldPath("year")) | ||
) | ||
// end atlasSearchMeta | ||
|
||
val aggregateStages = listOf(textSearchMeta) | ||
println("aggregateStages: $aggregateStages") | ||
|
||
collection.aggregate(aggregateStages).forEach { result -> | ||
println(result) | ||
} | ||
} | ||
|
||
fun main() { | ||
val uri = "<connection string>" | ||
|
||
val settings = MongoClientSettings.builder() | ||
.applyConnectionString(ConnectionString(uri)) | ||
.retryWrites(true) | ||
.build() | ||
|
||
MongoClient.create(settings).use { mongoClient -> | ||
val database = mongoClient.getDatabase("sample_mflix") | ||
val collection = database.getCollection<Document>("movies") | ||
|
||
runAtlasTextSearchMeta(collection) | ||
} | ||
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. do we need the above comment? For one thing, it is not methods to be uncommented for the statement below is a method invocation, not the method declaration per se; so But given we have provided this method as example, I think simply using it directly makes more sense:
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. true, this is a remnant of old code where there were multiple methods that could be tested 😅 good catch! |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Runs an Atlas Search query by using the Kotlin sync driver | ||
|
||
package org.example | ||
|
||
import com.mongodb.ConnectionString | ||
import com.mongodb.kotlin.client.MongoClient | ||
import com.mongodb.MongoClientSettings | ||
import com.mongodb.client.model.Aggregates.project | ||
import com.mongodb.client.model.Aggregates.search | ||
import com.mongodb.client.model.Projections | ||
import com.mongodb.client.model.search.SearchOperator | ||
import com.mongodb.client.model.search.SearchPath.fieldPath | ||
import org.bson.Document | ||
import org.bson.conversions.Bson | ||
|
||
fun main() { | ||
val uri = "<connection string>" | ||
|
||
val settings = MongoClientSettings.builder() | ||
.applyConnectionString(ConnectionString(uri)) | ||
.retryWrites(true) | ||
.build() | ||
|
||
val mongoClient = MongoClient.create(settings) | ||
val database = mongoClient.getDatabase("sample_mflix") | ||
val collection = database.getCollection<Document>("movies") | ||
|
||
// begin-atlas-search | ||
val pipeline: List<Bson> = listOf( | ||
search(SearchOperator.text( | ||
fieldPath("title"), "Alabama")), | ||
project(Projections.include("title")) | ||
) | ||
|
||
val results = collection.aggregate(pipeline) | ||
|
||
results.forEach { doc -> | ||
println(doc.toJson()) | ||
} | ||
// 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.
seems it will align better with existing pattern to connection uri placeholder in code directly without indirection of a constant field: