Skip to content

Commit 755cc16

Browse files
committed
DOCSP-45702: Atlas search pagination (#518)
* DOCSP-45702: Atlas search pagination * edits * code edit * MW feedback (cherry picked from commit bd02e59)
1 parent 1a05f1d commit 755cc16

File tree

2 files changed

+151
-12
lines changed

2 files changed

+151
-12
lines changed

source/fundamentals/atlas-search.txt

Lines changed: 118 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,11 @@ The examples in this guide use the following documents in a collection called
4848

4949
The following ``Guitar`` class models the documents in this collection.
5050

51-
.. code-block:: csharp
52-
53-
public class Guitar
54-
{
55-
public int Id { get; set; }
56-
public string Make { get; set; }
57-
public List<string> Models { get; set; }
58-
public int EstablishedYear { get; set; }
59-
[BsonElement("in_stock")]
60-
public bool InStock { get; set; }
61-
public int? Rating { get; set; }
62-
}
51+
.. literalinclude:: /includes/fundamentals/code-examples/atlas-search/AtlasSearchExamples.cs
52+
:start-after: // start-guitar-class
53+
:end-before: // end-guitar-class
54+
:language: csharp
55+
:dedent:
6356

6457
.. note::
6558

@@ -683,3 +676,116 @@ The search returns the following document:
683676

684677
To learn more about the ``wildcard`` operator, see the :atlas:`wildcard </atlas-search/wildcard>`
685678
Atlas guide.
679+
680+
Modify Atlas Search Behavior
681+
----------------------------
682+
683+
You can modify the behavior of the ``Search()`` method by passing
684+
a ``SearchOptions`` object as a parameter.
685+
686+
The ``SearchOptions`` class contains the following properties:
687+
688+
.. list-table::
689+
:widths: 30 70
690+
:header-rows: 1
691+
692+
* - Property
693+
- Description
694+
695+
* - ``CountOptions``
696+
- | The options for counting the search results.
697+
698+
| **Data type**: `SearchCountOptions <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.Search.SearchCountOptions.html>`__
699+
| **Default**: ``null``
700+
701+
* - ``Highlight``
702+
- | The options for displaying search terms in their original context.
703+
704+
| **Data type**: `SearchHighlightOptions<TDocument> <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.Search.SearchHighlightOptions-1.html>`__
705+
| **Default**: ``null``
706+
707+
* - ``IndexName``
708+
- | The index to use for the search.
709+
710+
| **Data type**: {+string-data-type+}
711+
| **Default**: ``null``
712+
713+
* - ``ReturnStoredSource``
714+
- | A flag that specifies whether to perform a full document lookup on
715+
the database or to return only stored source fields directly from
716+
Atlas Search.
717+
718+
| **Data type**: {+bool-data-type+}
719+
| **Default**: ``false``
720+
721+
* - ``ScoreDetails``
722+
- | A flag that specifies whether to return detailed information about the
723+
score for each document in the results.
724+
725+
| **Data type**: {+bool-data-type+}
726+
| **Default**: ``false``
727+
728+
* - ``SearchAfter``
729+
- | The starting point for pagination. When set, the search retrieves documents
730+
starting immediately after the specified reference point.
731+
732+
| **Data type**: {+string-data-type+}
733+
| **Default**: ``null``
734+
735+
* - ``SearchBefore``
736+
- | The end point for pagination. When set, the search retrieves documents
737+
starting immediately before the specified reference point.
738+
739+
| **Data type**: {+string-data-type+}
740+
| **Default**: ``null``
741+
742+
* - ``Sort``
743+
- | The sorting criteria to apply to the results.
744+
745+
| **Data type**: `SortDefinition<TDocument> <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.SortDefinition-1.html>`__
746+
| **Default**: ``null``
747+
748+
* - ``Tracking``
749+
- | The options for tracking search terms.
750+
751+
| **Data type**: `SearchTrackingOptions <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.Search.SearchTrackingOptions.html>`__
752+
| **Default**: ``null``
753+
754+
SearchAfter Example
755+
~~~~~~~~~~~~~~~~~~~
756+
757+
The following example paginates the results of an Atlas Search
758+
operation by performing the following actions:
759+
760+
- Defines a projection that uses the ``MetaSearchSequenceToken()``
761+
builder method, which specifies a ``PaginationToken`` to contain
762+
the point of reference
763+
764+
- Creates a ``SearchOptions`` instance and sets the index and sort
765+
criteria to use
766+
767+
- Runs an initial search to find documents that have a ``description`` field value containing
768+
the text ``"classic"``, applying the projection and options to the operation
769+
770+
- Sets the ``SearchAfter`` property of the same ``SearchOptions`` instance to
771+
instruct the next search to begin after the base search's first result
772+
773+
- Runs another search operation that has the same matching criteria and applies
774+
the search options to paginate the results
775+
776+
.. literalinclude:: /includes/fundamentals/code-examples/atlas-search/AtlasSearchExamples.cs
777+
:start-after: // start-pagination-options
778+
:end-before: // end-pagination-options
779+
:language: csharp
780+
:dedent:
781+
782+
The search returns the following document:
783+
784+
.. code-block:: json
785+
786+
{ "_id": 2, "make": "Gibson", "description": "Classic guitars known for their rich, full tones.", "establishedYear": 1902, "in_stock": true, "rating": 8 }
787+
788+
.. tip::
789+
790+
To learn more about Atlas Search pagination, see :atlas:`Paginate the Results </atlas-search/paginate-results/>`
791+
in the Atlas documentation.

source/includes/fundamentals/code-examples/atlas-search/AtlasSearchExamples.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,35 @@ public static List<Guitar> WildcardSearch()
263263
return result;
264264
}
265265

266+
public static List<Guitar> SearchAfter()
267+
{
268+
// start-pagination-options
269+
var projection = Builders<Guitar>.Projection
270+
.Include(x => x.Make)
271+
.MetaSearchSequenceToken(x => x.PaginationToken);
272+
273+
var searchDefinition = Builders<Guitar>.Search.Text(g => g.Description, "classic");
274+
var searchOptions = new SearchOptions<Guitar>
275+
{ IndexName = "default", Sort = Builders<Guitar>.Sort.Ascending(g => g.Id) }
276+
277+
// Runs the base search operation
278+
var baseSearchResults = guitarsCollection.Aggregate()
279+
.Search(searchDefinition, searchOptions)
280+
.Project<Guitar>(projection)
281+
.ToList();
282+
283+
// Sets the starting point for the next search
284+
searchOptions.SearchAfter = baseSearchResults[0].PaginationToken;
285+
286+
var result = guitarsCollection.Aggregate()
287+
.Search(searchDefinition, searchOptions)
288+
.Project<Guitar>(projection)
289+
.ToList();
290+
// end-pagination-options
291+
292+
return result;
293+
}
294+
266295
private static void Setup()
267296
{
268297
// This allows automapping of the camelCase database fields to our models.
@@ -281,6 +310,7 @@ public class GuitarSearch
281310
public string Description { get; set; }
282311
}
283312

313+
// start-guitar-class
284314
public class Guitar
285315
{
286316
public int Id { get; set; }
@@ -292,7 +322,10 @@ public class Guitar
292322
[BsonElement("in_stock_location")]
293323
public Location InStockLocation { get; set; }
294324
public int? Rating { get; set; }
325+
[BsonElement("paginationToken")]
326+
public string PaginationToken { get; set; }
295327
}
328+
// end-guitar-class
296329

297330
public class Location
298331
{

0 commit comments

Comments
 (0)