Atlas Search
Helpers for managing Atlas Search indexes
and building Bson aggregate stages from the driver's SearchOperator API. Index management uses
Awaitility to poll the index status.
Constants
const val DEFAULT_MONGO_SEARCH_INDEX = "default"
Index lifecycle
collection.createDefaultSearchIndex(definitionJson)
collection.recreateDefaultSearchIndex(definitionJson)
collection.recreateSearchIndex("my-index", definitionJson)
collection.waitForDefaultSearchIndexReady(seconds = 60)
collection.waitForSearchIndexReady("my-index", seconds = 60)
collection.searchIndexReady("my-index"): Boolean
collection.searchIndexExists("my-index"): Boolean
searchIndexReady is true only for a READY index, while searchIndexExists is true for a listed index in any status (PENDING,
BUILDING, DELETING, ...). recreateSearchIndex drops an existing index whatever its status, waits up to 60 seconds for the name to
leave the listing, then creates the index from the new definition.
Waiting for documents
Useful in integration tests to give Atlas Search time to index newly-written documents.
collection.waitForDocumentWithIdInDefaultSearchIndex(id)
collection.waitForDocumentInDefaultSearchIndex(fieldName = "title", fieldValue = "Invirt")
collection.waitForDocumentsInDefaultSearchIndex(searchOperator, documentCount = 10)
collection.waitForDocumentsInSearchIndex(indexName, searchOperator, documentCount = 10)
The _id variant assumes the index is configured with "_id": { "type": "string", "analyzer": "lucene.keyword" }.
Building aggregate stages
val byText = Product::title.textSearch("invirt") // SearchOperator.text on "title"
val byPrefix = Product::name.autocomplete("invi") // SearchOperator.autocomplete on "name"
val stage: Bson = byText.toAggregate() // default index name
val stage2: Bson = listOf(byText, byPrefix).toAggregate("my-index") // compound MUST
Product::title.fieldPath() // SearchPath.fieldPath("title")