Skip to content
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

DOCSP-33429: atlas search idx mgmt #144

Merged
merged 5 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions examples/src/test/kotlin/SearchIndexesTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@

import com.mongodb.client.model.SearchIndexModel
import com.mongodb.kotlin.client.coroutine.MongoClient
import config.getConfig
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.runBlocking
import org.bson.Document
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import kotlin.test.Ignore
import kotlin.test.assertFalse

// :replace-start: {
// "terms": {
// "CONNECTION_URI_PLACEHOLDER": "\"<connection string>\""
// }
// }
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class SearchIndexesTest {

companion object {
private val config = getConfig()
private val CONNECTION_URI_PLACEHOLDER = config.connectionUri

val mongoClient = MongoClient.create(CONNECTION_URI_PLACEHOLDER)
val database = mongoClient.getDatabase("sample_mflix")
val moviesCollection = database.getCollection<Document>("movies")

@AfterAll
@JvmStatic
fun afterAll() {
runBlocking {
moviesCollection.drop()
}
mongoClient.close()
}
}

@Ignore
@Test
fun singleSearchIndexTest() = runBlocking {
// :snippet-start: single-search-index-create
val index = Document(
"mappings",
Document("dynamic", true)
)
val resultCreateIndex = moviesCollection.createSearchIndex("myIndex", index)
// :snippet-end:
println("Index created: $resultCreateIndex")
assertEquals("myIndex", resultCreateIndex)
}

@Ignore
@Test
fun multipleSearchIndexTest() = runBlocking {
// :snippet-start: multi-search-index-create
val indexOne = SearchIndexModel(
"myIndex1",
Document("analyzer", "lucene.standard").append(
"mappings", Document("dynamic", true)
)
)

val indexTwo = SearchIndexModel(
"myIndex2",
Document("analyzer", "lucene.simple").append(
"mappings", Document("dynamic", true)
)
)

val resultCreateIndexes = moviesCollection
.createSearchIndexes(listOf(indexOne, indexTwo))
// :snippet-end:
assertEquals(listOf("myIndex1", "myIndex2"), resultCreateIndexes.toList())
}

@Ignore
@Test
fun listSearchIndexTest() = runBlocking {
// :snippet-start: list-search-indexes
val searchIndexesList = moviesCollection.listSearchIndexes().toList()
// :snippet-end:

assertFalse(searchIndexesList.isEmpty())
}

@Ignore
@Test
fun updateSearchIndexTest() = runBlocking {
// :snippet-start: update-search-indexes
moviesCollection.updateSearchIndex(
"myIndex",
Document("analyzer", "lucene.simple").append(
"mappings",
Document("dynamic", false)
.append(
"fields",
Document(
"title",
Document("type", "string")
)
)
)
)
// :snippet-end:
}

@Ignore
@Test
fun dropSearchIndexTest() = runBlocking {
// :snippet-start: drop-search-index
moviesCollection.dropSearchIndex("myIndex");
// :snippet-end:
}

}
// :replace-end:
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
moviesCollection.dropSearchIndex("myIndex");
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
val searchIndexesList = moviesCollection.listSearchIndexes().toList()
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
val indexOne = SearchIndexModel(
"myIndex1",
Document("analyzer", "lucene.standard").append(
"mappings", Document("dynamic", true)
)
)

val indexTwo = SearchIndexModel(
"myIndex2",
Document("analyzer", "lucene.simple").append(
"mappings", Document("dynamic", true)
)
)

val resultCreateIndexes = moviesCollection
.createSearchIndexes(listOf(indexOne, indexTwo))
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
val index = Document(
"mappings",
Document("dynamic", true)
)
val resultCreateIndex = moviesCollection.createSearchIndex("myIndex", index)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
moviesCollection.updateSearchIndex(
"myIndex",
Document("analyzer", "lucene.simple").append(
"mappings",
Document("dynamic", false)
.append(
"fields",
Document(
"title",
Document("type", "string")
)
)
)
)
Loading
Loading