Skip to content

Commit 969d2bc

Browse files
committed
DOCSP-33429: atlas search idx mgmt (#144)
* DOCSP-33429: (wip) atlas search idx mgmt * DOCSP-33429: atlas search idx mgmt * fixes * MW PR fixes 1 * MW PR Small fixes (cherry picked from commit 1784061)
1 parent edf5764 commit 969d2bc

7 files changed

+327
-49
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
2+
import com.mongodb.client.model.SearchIndexModel
3+
import com.mongodb.kotlin.client.coroutine.MongoClient
4+
import config.getConfig
5+
import kotlinx.coroutines.flow.toList
6+
import kotlinx.coroutines.runBlocking
7+
import org.bson.Document
8+
import org.junit.jupiter.api.AfterAll
9+
import org.junit.jupiter.api.Assertions.assertEquals
10+
import org.junit.jupiter.api.Test
11+
import org.junit.jupiter.api.TestInstance
12+
import kotlin.test.Ignore
13+
import kotlin.test.assertFalse
14+
15+
// :replace-start: {
16+
// "terms": {
17+
// "CONNECTION_URI_PLACEHOLDER": "\"<connection string>\""
18+
// }
19+
// }
20+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
21+
class SearchIndexesTest {
22+
23+
companion object {
24+
private val config = getConfig()
25+
private val CONNECTION_URI_PLACEHOLDER = config.connectionUri
26+
27+
val mongoClient = MongoClient.create(CONNECTION_URI_PLACEHOLDER)
28+
val database = mongoClient.getDatabase("sample_mflix")
29+
val moviesCollection = database.getCollection<Document>("movies")
30+
31+
@AfterAll
32+
@JvmStatic
33+
fun afterAll() {
34+
runBlocking {
35+
moviesCollection.drop()
36+
}
37+
mongoClient.close()
38+
}
39+
}
40+
41+
@Ignore
42+
@Test
43+
fun singleSearchIndexTest() = runBlocking {
44+
// :snippet-start: single-search-index-create
45+
val index = Document(
46+
"mappings",
47+
Document("dynamic", true)
48+
)
49+
val resultCreateIndex = moviesCollection.createSearchIndex("myIndex", index)
50+
// :snippet-end:
51+
println("Index created: $resultCreateIndex")
52+
assertEquals("myIndex", resultCreateIndex)
53+
}
54+
55+
@Ignore
56+
@Test
57+
fun multipleSearchIndexTest() = runBlocking {
58+
// :snippet-start: multi-search-index-create
59+
val indexOne = SearchIndexModel(
60+
"myIndex1",
61+
Document("analyzer", "lucene.standard").append(
62+
"mappings", Document("dynamic", true)
63+
)
64+
)
65+
66+
val indexTwo = SearchIndexModel(
67+
"myIndex2",
68+
Document("analyzer", "lucene.simple").append(
69+
"mappings", Document("dynamic", true)
70+
)
71+
)
72+
73+
val resultCreateIndexes = moviesCollection
74+
.createSearchIndexes(listOf(indexOne, indexTwo))
75+
// :snippet-end:
76+
assertEquals(listOf("myIndex1", "myIndex2"), resultCreateIndexes.toList())
77+
}
78+
79+
@Ignore
80+
@Test
81+
fun listSearchIndexTest() = runBlocking {
82+
// :snippet-start: list-search-indexes
83+
val searchIndexesList = moviesCollection.listSearchIndexes().toList()
84+
// :snippet-end:
85+
86+
assertFalse(searchIndexesList.isEmpty())
87+
}
88+
89+
@Ignore
90+
@Test
91+
fun updateSearchIndexTest() = runBlocking {
92+
// :snippet-start: update-search-indexes
93+
moviesCollection.updateSearchIndex(
94+
"myIndex",
95+
Document("analyzer", "lucene.simple").append(
96+
"mappings",
97+
Document("dynamic", false)
98+
.append(
99+
"fields",
100+
Document(
101+
"title",
102+
Document("type", "string")
103+
)
104+
)
105+
)
106+
)
107+
// :snippet-end:
108+
}
109+
110+
@Ignore
111+
@Test
112+
fun dropSearchIndexTest() = runBlocking {
113+
// :snippet-start: drop-search-index
114+
moviesCollection.dropSearchIndex("myIndex");
115+
// :snippet-end:
116+
}
117+
118+
}
119+
// :replace-end:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
moviesCollection.dropSearchIndex("myIndex");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
val searchIndexesList = moviesCollection.listSearchIndexes().toList()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
val indexOne = SearchIndexModel(
2+
"myIndex1",
3+
Document("analyzer", "lucene.standard").append(
4+
"mappings", Document("dynamic", true)
5+
)
6+
)
7+
8+
val indexTwo = SearchIndexModel(
9+
"myIndex2",
10+
Document("analyzer", "lucene.simple").append(
11+
"mappings", Document("dynamic", true)
12+
)
13+
)
14+
15+
val resultCreateIndexes = moviesCollection
16+
.createSearchIndexes(listOf(indexOne, indexTwo))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
val index = Document(
2+
"mappings",
3+
Document("dynamic", true)
4+
)
5+
val resultCreateIndex = moviesCollection.createSearchIndex("myIndex", index)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
moviesCollection.updateSearchIndex(
2+
"myIndex",
3+
Document("analyzer", "lucene.simple").append(
4+
"mappings",
5+
Document("dynamic", false)
6+
.append(
7+
"fields",
8+
Document(
9+
"title",
10+
Document("type", "string")
11+
)
12+
)
13+
)
14+
)

0 commit comments

Comments
 (0)