Pagination With Full Text Search In Pymongo

This Is the method which i used.Is there a better solution than this.(accuracy and efficient)

< collections = collection_name.aggregate(
               [
                   {
                       "$search": {
                           "index": 'name_id',
                           "compound": {
                               "should": [
                                   {
                                       "autocomplete": {
                                           "path": "name",
                                           "query": search_key,
                                       }
                                   },
                                   {
                                       "autocomplete": {
                                           "path": "contractId",
                                           "query": search_key,

                                       }
                                   }
                               ],
                               "minimumShouldMatch": 1
                           }
                       }
                   },
                   {
                       '$match': {"_id": {'$gt': ObjectId(start_id)}}
                   },
                   {
                       "$limit": int(70000)
                   },
                   {
                       "$sort": {'_id': 1}
                   },
                   {
                       "$limit": int(limit)
                   },
               ]) />

Hi @LiveRoom,

Welcome to the MongoDB Community forums :sparkles:

To better understand the question, can you share the following details:

  • The MongoDB Version you are using?
  • The sample document from your dataset?
  • The expected result you are seeking?
  • The code snippet where this aggregation pipeline is getting utilized?

Can you please clarify what you mean by “accuracy” and “efficiency” in this context? Also, what are the existing numbers, and what do you expect them to be?

Best,
Kushagra

We recently released support for retrieving $search results sequentially after or before a reference point, which you can specify using a token generated by the $meta keyword searchSequenceToken. To learn more, see Paginate the Results Sequentially.

1 Like