Skip to content

Commit d2a655a

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 1f15585 commit d2a655a

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

@@ -709,3 +702,116 @@ The search returns the following document:
709702

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

277+
public static List<Guitar> SearchAfter()
278+
{
279+
// start-pagination-options
280+
var projection = Builders<Guitar>.Projection
281+
.Include(x => x.Make)
282+
.MetaSearchSequenceToken(x => x.PaginationToken);
283+
284+
var searchDefinition = Builders<Guitar>.Search.Text(g => g.Description, "classic");
285+
var searchOptions = new SearchOptions<Guitar>
286+
{ IndexName = "default", Sort = Builders<Guitar>.Sort.Ascending(g => g.Id) }
287+
288+
// Runs the base search operation
289+
var baseSearchResults = guitarsCollection.Aggregate()
290+
.Search(searchDefinition, searchOptions)
291+
.Project<Guitar>(projection)
292+
.ToList();
293+
294+
// Sets the starting point for the next search
295+
searchOptions.SearchAfter = baseSearchResults[0].PaginationToken;
296+
297+
var result = guitarsCollection.Aggregate()
298+
.Search(searchDefinition, searchOptions)
299+
.Project<Guitar>(projection)
300+
.ToList();
301+
// end-pagination-options
302+
303+
return result;
304+
}
305+
277306
private static void Setup()
278307
{
279308
// This allows automapping of the camelCase database fields to our models.
@@ -292,6 +321,7 @@ public class GuitarSearch
292321
public string Description { get; set; }
293322
}
294323

324+
// start-guitar-class
295325
public class Guitar
296326
{
297327
public int Id { get; set; }
@@ -303,7 +333,10 @@ public class Guitar
303333
[BsonElement("in_stock_location")]
304334
public Location InStockLocation { get; set; }
305335
public int? Rating { get; set; }
336+
[BsonElement("paginationToken")]
337+
public string PaginationToken { get; set; }
306338
}
339+
// end-guitar-class
307340

308341
public class Location
309342
{

0 commit comments

Comments
 (0)