Skip to content

Commit f1e64f7

Browse files
authored
Atlas Search Code examples (#157)
* null check code * finish examples
1 parent 2eb0b1f commit f1e64f7

29 files changed

+1550
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import com.mongodb.client.model.Aggregates.limit
2+
import com.mongodb.client.model.Aggregates.project
3+
import com.mongodb.client.model.Projections.*
4+
import com.mongodb.kotlin.client.coroutine.MongoClient
5+
import kotlinx.coroutines.runBlocking
6+
import org.bson.Document
7+
8+
fun main() {
9+
val uri = "<connection string>"
10+
11+
val mongoClient = MongoClient.create(uri)
12+
val database = mongoClient.getDatabase("sample_mflix")
13+
val collection = database.getCollection<Document>("movies")
14+
15+
runBlocking {
16+
val agg = Document(
17+
"\$search",
18+
Document("index", "autocomplete-tutorial")
19+
.append(
20+
"compound",
21+
Document(
22+
"should", listOf(
23+
Document(
24+
"autocomplete",
25+
Document("query", "pri")
26+
.append("path", "title")
27+
),
28+
Document(
29+
"autocomplete",
30+
Document("query", "pri")
31+
.append("path", "plot")
32+
)
33+
)
34+
)
35+
.append("minimumShouldMatch", 1L)
36+
)
37+
)
38+
39+
val resultsFlow = collection.aggregate<Document>(
40+
listOf(
41+
agg,
42+
limit(5),
43+
project(fields(excludeId(), include("title")))
44+
)
45+
)
46+
47+
resultsFlow.collect { println(it) }
48+
}
49+
50+
mongoClient.close()
51+
}
52+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import com.mongodb.client.model.Aggregates.limit
2+
import com.mongodb.client.model.Aggregates.project
3+
import com.mongodb.client.model.Filters.eq
4+
import com.mongodb.client.model.Projections.*
5+
import com.mongodb.kotlin.client.coroutine.MongoClient
6+
import kotlinx.coroutines.runBlocking
7+
import org.bson.Document
8+
9+
fun main() {
10+
val uri = "<connection string>"
11+
12+
val mongoClient = MongoClient.create(uri)
13+
val database = mongoClient.getDatabase("sample_mflix")
14+
val collection = database.getCollection<Document>("movies")
15+
16+
runBlocking {
17+
val agg = Document(
18+
"\$search",
19+
Document("index", "autocomplete-tutorial")
20+
.append("autocomplete", Document("query", "ger").append("path", "title"))
21+
)
22+
23+
val resultsFlow = collection.aggregate<Document>(
24+
listOf(
25+
agg,
26+
limit(20),
27+
project(fields(excludeId(), include("title")))
28+
)
29+
)
30+
31+
resultsFlow.collect { println(it) }
32+
}
33+
34+
mongoClient.close()
35+
}
36+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import com.mongodb.client.model.Aggregates.limit
2+
import com.mongodb.client.model.Aggregates.project
3+
import com.mongodb.client.model.Projections.*
4+
import com.mongodb.kotlin.client.coroutine.MongoClient
5+
import kotlinx.coroutines.runBlocking
6+
import org.bson.Document
7+
8+
fun main() {
9+
val uri = "<connection string>"
10+
11+
val mongoClient = MongoClient.create(uri)
12+
val database = mongoClient.getDatabase("local_school_district")
13+
val collection = database.getCollection<Document>("schools")
14+
15+
runBlocking {
16+
val mustClauses = listOf(
17+
Document(
18+
"text",
19+
Document("path", "teachers.first").append("query", "John")
20+
)
21+
)
22+
23+
val shouldClauses = listOf(
24+
Document(
25+
"text",
26+
Document("path", "teachers.last")
27+
.append("query", "Smith")
28+
)
29+
)
30+
31+
val agg = Document(
32+
"\$search", Document("index", "embedded-documents-tutorial")
33+
.append(
34+
"embeddedDocument",
35+
Document("path", "teachers")
36+
.append(
37+
"operator",
38+
Document(
39+
"compound",
40+
Document("must", mustClauses)
41+
.append("should", shouldClauses)
42+
)
43+
)
44+
)
45+
.append("highlight", Document("path", "teachers.last"))
46+
)
47+
48+
val resultsFlow = collection.aggregate<Document>(
49+
listOf(
50+
agg,
51+
limit(5),
52+
project(fields(
53+
excludeId(),
54+
include("teachers"),
55+
computed("score", Document("\$meta", "searchScore")),
56+
computed("highlights", Document("\$meta", "searchHighlights"))
57+
))
58+
)
59+
)
60+
61+
resultsFlow.collect { println(it) }
62+
}
63+
64+
mongoClient.close()
65+
}
66+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import com.mongodb.client.model.Aggregates.limit
2+
import com.mongodb.client.model.Aggregates.project
3+
import com.mongodb.client.model.Projections.*
4+
import com.mongodb.kotlin.client.coroutine.MongoClient
5+
import kotlinx.coroutines.runBlocking
6+
import org.bson.Document
7+
8+
fun main() {
9+
val uri = "<connection string>"
10+
11+
val mongoClient = MongoClient.create(uri)
12+
val database = mongoClient.getDatabase("local_school_district")
13+
val collection = database.getCollection<Document>("schools")
14+
15+
runBlocking {
16+
val agg = Document(
17+
"\$search",
18+
Document("index", "embedded-documents-tutorial")
19+
.append("embeddedDocument", Document("path", "clubs.sports")
20+
.append(
21+
"operator",
22+
Document(
23+
"queryString",
24+
Document("defaultPath", "clubs.sports.club_name")
25+
.append("query", "dodgeball OR frisbee")
26+
)
27+
)
28+
)
29+
)
30+
31+
val resultsFlow = collection.aggregate<Document>(
32+
listOf(
33+
agg,
34+
limit(5),
35+
project(
36+
fields(
37+
include("name", "clubs.sports"),
38+
computed("score", Document("\$meta", "searchScore"))
39+
)
40+
)
41+
)
42+
)
43+
44+
resultsFlow.collect { println(it) }
45+
}
46+
47+
mongoClient.close()
48+
}
49+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import com.mongodb.client.model.Aggregates.limit
2+
import com.mongodb.client.model.Aggregates.project
3+
import com.mongodb.client.model.Projections.*
4+
import com.mongodb.kotlin.client.coroutine.MongoClient
5+
import kotlinx.coroutines.runBlocking
6+
import org.bson.Document
7+
8+
fun main() {
9+
val uri = "<connection string>"
10+
11+
val mongoClient = MongoClient.create(uri)
12+
val database = mongoClient.getDatabase("sample_mflix")
13+
val collection = database.getCollection<Document>("movies")
14+
15+
runBlocking {
16+
val mustClauses = listOf(
17+
Document(
18+
"range", Document("path", "year")
19+
.append("gte", 2013)
20+
.append("lte", 2015)
21+
)
22+
)
23+
24+
val shouldClauses = listOf(
25+
Document(
26+
"text",
27+
Document("query", "snow")
28+
.append("path", "title")
29+
.append("score", Document("boost", Document("value", 2)))
30+
)
31+
)
32+
33+
val highlightOption = Document("path", "title")
34+
35+
val agg = Document(
36+
"\$search",
37+
Document("index", "compound-query-custom-score-tutorial")
38+
.append(
39+
"compound",
40+
Document("must", mustClauses).append("should", shouldClauses)
41+
)
42+
.append("highlight", highlightOption)
43+
)
44+
45+
val resultsFlow = collection.aggregate<Document>(
46+
listOf(
47+
agg,
48+
limit(10),
49+
project(fields(
50+
excludeId(),
51+
include("title", "year"),
52+
computed("score", Document("\$meta", "searchScore")),
53+
computed("highlights", Document("\$meta", "searchHighlights"))
54+
))
55+
)
56+
)
57+
58+
resultsFlow.collect { println(it) }
59+
}
60+
61+
mongoClient.close()
62+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import com.mongodb.client.model.Aggregates.limit
2+
import com.mongodb.client.model.Aggregates.project
3+
import com.mongodb.client.model.Projections.*
4+
import com.mongodb.kotlin.client.coroutine.MongoClient
5+
import kotlinx.coroutines.runBlocking
6+
import org.bson.Document
7+
8+
fun main() {
9+
val uri = "<connection string>"
10+
11+
val mongoClient = MongoClient.create(uri)
12+
val database = mongoClient.getDatabase("sample_mflix")
13+
val collection = database.getCollection<Document>("movies")
14+
15+
runBlocking {
16+
val mustClauses = listOf(
17+
Document(
18+
"range", Document("path", "year")
19+
.append("gte", 2013)
20+
.append("lte", 2015)
21+
)
22+
)
23+
24+
val shouldClauses = listOf(
25+
Document(
26+
"text",
27+
Document("query", "snow")
28+
.append("path", "title")
29+
.append("score", Document("constant", Document("value", 5)))
30+
)
31+
)
32+
33+
val highlightOption = Document("path", "title")
34+
35+
val agg = Document(
36+
"\$search",
37+
Document("index", "compound-query-custom-score-tutorial")
38+
.append(
39+
"compound",
40+
Document("must", mustClauses).append("should", shouldClauses)
41+
)
42+
.append("highlight", highlightOption)
43+
)
44+
45+
val resultsFlow = collection.aggregate<Document>(
46+
listOf(
47+
agg,
48+
limit(10),
49+
project(fields(
50+
excludeId(),
51+
include("title", "year"),
52+
computed("score", Document("\$meta", "searchScore")),
53+
computed("highlights", Document("\$meta", "searchHighlights"))
54+
))
55+
)
56+
)
57+
58+
resultsFlow.collect { println(it) }
59+
}
60+
61+
mongoClient.close()
62+
}
63+

0 commit comments

Comments
 (0)