Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ class ProjectStageDsl {
addMetaDataKeyword("indexKey", alias)
}

/**
* Specifies an alias that contains the search sequence token for paginating through Atlas Search results.
*
* @param alias The alias for the field.
* @see <a href="https://www.mongodb.com/docs/atlas/atlas-search/paginate-results/#paginate-the-results">Paginate the results</a>
*/
fun searchSequenceToken(alias: String = "paginationToken") {
addMetaDataKeyword("searchSequenceToken", alias)
}

private fun addMetaDataKeyword(keyword: String, alias: String) {
operation = operation.andExpression("{\$meta: \"$keyword\"}").`as`(alias)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,28 @@ class SearchStageDsl : SearchOperator by SearchOperatorDsl(), SearchCollector by
field = value
}

/**
* A token that specifies the point after which to return search results.
*
* @see <a href="https://www.mongodb.com/docs/atlas/atlas-search/paginate-results/#search-after-a-specific-point-of-reference">Search After</a>
*/
var searchAfter: String? = null
set(value) {
value?.let { document["searchAfter"] = it }
field = value
}

/**
* A token that specifies the point before which to return search results.
*
* @see <a href="https://www.mongodb.com/docs/atlas/atlas-search/paginate-results/#search-before-a-specific-point-of-reference">Search Before</a>
*/
var searchBefore: String? = null
set(value) {
value?.let { document["searchBefore"] = it }
field = value
}

/**
* Configures an option to include a lower bound count of the number of documents that match the query.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,31 @@ internal class ProjectStageDslTest : FreeSpec({
}
}

"searchSequenceToken" - {
"should add search sequence token meta with given alias" {
// given
val stage = project {
searchSequenceToken("alias")
}

// when
val result = stage.get()

// then
result.shouldBeJson(
"""
{
"${'$'}project": {
"alias": {
"${'$'}meta": "searchSequenceToken"
}
}
}
""".trimIndent(),
)
}
}

"searchHighlights" - {
"should add search highlights meta with given alias" {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,50 @@ internal class SearchStageDslTest : FreeSpec({
)
}
}

"searchAfter" - {
"should build a searchAfter option" {
// given
val stage = search {
searchAfter = "token"
}

// when
val result = stage.build()

// then
result.shouldBeJson(
"""
{
"${"$"}search": {
"searchAfter": "token"
}
}
""".trimIndent(),
)
}
}

"searchBefore" - {
"should build a searchBefore option" {
// given
val stage = search {
searchBefore = "token"
}

// when
val result = stage.build()

// then
result.shouldBeJson(
"""
{
"${"$"}search": {
"searchBefore": "token"
}
}
""".trimIndent(),
)
}
}
})
Loading