Skip to content

Commit 64dc49f

Browse files
committed
feat: add search pagination operation
1 parent 2e276b8 commit 64dc49f

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

core/src/main/kotlin/com/github/inflab/spring/data/mongodb/core/aggregation/ProjectStageDsl.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ class ProjectStageDsl {
139139
addMetaDataKeyword("indexKey", alias)
140140
}
141141

142+
/**
143+
* Specifies an alias that contains the search sequence token for paginating through Atlas Search results.
144+
*
145+
* @param alias The alias for the field.
146+
* @see <a href="https://www.mongodb.com/docs/atlas/atlas-search/paginate-results/#paginate-the-results">Paginate the results</a>
147+
*/
148+
fun searchSequenceToken(alias: String = "paginationToken") {
149+
addMetaDataKeyword("searchSequenceToken", alias)
150+
}
151+
142152
private fun addMetaDataKeyword(keyword: String, alias: String) {
143153
operation = operation.andExpression("{\$meta: \"$keyword\"}").`as`(alias)
144154
}

core/src/main/kotlin/com/github/inflab/spring/data/mongodb/core/aggregation/search/SearchStageDsl.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,28 @@ class SearchStageDsl : SearchOperator by SearchOperatorDsl(), SearchCollector by
5959
field = value
6060
}
6161

62+
/**
63+
* A token that specifies the point after which to return search results.
64+
*
65+
* @see <a href="https://www.mongodb.com/docs/atlas/atlas-search/paginate-results/#search-after-a-specific-point-of-reference">Search After</a>
66+
*/
67+
var searchAfter: String? = null
68+
set(value) {
69+
value?.let { document["searchAfter"] = it }
70+
field = value
71+
}
72+
73+
/**
74+
* A token that specifies the point before which to return search results.
75+
*
76+
* @see <a href="https://www.mongodb.com/docs/atlas/atlas-search/paginate-results/#search-before-a-specific-point-of-reference">Search Before</a>
77+
*/
78+
var searchBefore: String? = null
79+
set(value) {
80+
value?.let { document["searchBefore"] = it }
81+
field = value
82+
}
83+
6284
/**
6385
* Configures an option to include a lower bound count of the number of documents that match the query.
6486
*

core/src/test/kotlin/com/github/inflab/spring/data/mongodb/core/aggregation/ProjectStageDslTest.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,31 @@ internal class ProjectStageDslTest : FreeSpec({
245245
}
246246
}
247247

248+
"searchSequenceToken" - {
249+
"should add search sequence token meta with given alias" {
250+
// given
251+
val stage = project {
252+
searchSequenceToken("alias")
253+
}
254+
255+
// when
256+
val result = stage.get()
257+
258+
// then
259+
result.shouldBeJson(
260+
"""
261+
{
262+
"${'$'}project": {
263+
"alias": {
264+
"${'$'}meta": "searchSequenceToken"
265+
}
266+
}
267+
}
268+
""".trimIndent(),
269+
)
270+
}
271+
}
272+
248273
"searchHighlights" - {
249274
"should add search highlights meta with given alias" {
250275
// given

core/src/test/kotlin/com/github/inflab/spring/data/mongodb/core/aggregation/search/SearchStageDslTest.kt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,4 +257,50 @@ internal class SearchStageDslTest : FreeSpec({
257257
)
258258
}
259259
}
260+
261+
"searchAfter" - {
262+
"should build a searchAfter option" {
263+
// given
264+
val stage = search {
265+
searchAfter = "token"
266+
}
267+
268+
// when
269+
val result = stage.build()
270+
271+
// then
272+
result.shouldBeJson(
273+
"""
274+
{
275+
"${"$"}search": {
276+
"searchAfter": "token"
277+
}
278+
}
279+
""".trimIndent(),
280+
)
281+
}
282+
}
283+
284+
"searchBefore" - {
285+
"should build a searchBefore option" {
286+
// given
287+
val stage = search {
288+
searchBefore = "token"
289+
}
290+
291+
// when
292+
val result = stage.build()
293+
294+
// then
295+
result.shouldBeJson(
296+
"""
297+
{
298+
"${"$"}search": {
299+
"searchBefore": "token"
300+
}
301+
}
302+
""".trimIndent(),
303+
)
304+
}
305+
}
260306
})

0 commit comments

Comments
 (0)