Getting error when using new sort option in $search stage

I’ve been trying to use the new sort option for $search stage with no luck.

I get the following error:
{{fieldName}} is not indexed as sortable.

What should I do in order to index the corresponding field as sortable? I have already rebuilt the index as mentioned in the docs. I’m using MongoDB 6 on an M0 cluster.

1 Like

Did you… ‘For string fields, you must manually index the field as token type. To learn more, see How to Index String Fields for Sorting.’?

1 Like

Hello @German_Medaglia ,

Welcome back to The MongoDB Community Forums! :wave:

@Elle_Shwer is right, a little addition to this from my side.
I got the same error when I missed the step to index the field as token and string type.

As per the Atlas Search Sort

For string fields, you must manually index the field as token type. To learn more, see How to Index String Fields for Sorting.

The example provided in the How to Index String Fields for Sorting for Index for Sort and Query shows how you can manually index the field as type token and string for Querying and Sorting.

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "<field-name>": [{
        "type": "string"
      },
      {
        "type": "token"
      }]
    }
  }
}

Note: I used JSON Editor in the Atlas UI to configure the index.

Let us know if you face any more issues, would be happy to help you! :slightly_smiling_face:

Regards,
Tarun

1 Like

@Elle_Shwer and @Tarun_Gaur thanks for your replies! Yes, I saw that, but I’m trying it on a date field.

Just in case, this is my index definition:

{
  "analyzer": "lucene.standard",
  "mappings": {
    "dynamic": false,
    "fields": {
      "created_at": {
        "type": "date"
      },
      "description": {
        "multi": {
          "keywordAnalyzer": {
            "analyzer": "lucene.keyword",
            "type": "string"
          }
        },
        "type": "string"
      },
      "properties": {
        "dynamic": true,
        "type": "document"
      }
    }
  },
  "storedSource": {
    "include": [
      "description",
      "properties",
      "created_at",
      "ip"
    ]
  }
}

Please share a few additional details such as:

  • Sample documents.
  • Query that you are running
  • Expected result

Here’s a simple $search stage sample.

{
  index: "default",
  compound: {
    filter: [
      {
        range: {
          path: "created_at",
          lte: ISODate(
            "2023-12-01T00:00:00.000Z"
          ),
          gte: ISODate(
            "2022-04-01T00:00:00.000Z"
          ),
        },
      },
    ],
    should: [
      {
        text: {
          query: "{{query}}",
          fuzzy: {
            maxEdits: 1,
            prefixLength: 1,
          },
          path: {
            wildcard: "properties.*",
          },
        },
      },
      {
        text: {
          query: "{{query}}",
          fuzzy: {
            maxEdits: 1,
            prefixLength: 1,
          },
          path: "description",
        },
      },
    ],    
  },
  count: {
    type: "lowerBound",
    threshold: 1000,
  },
  sort: {
    created_at: 1
  },
  returnStoredSource: true,
  highlight: {
    path: ["description"],
    maxCharsToExamine: 500,
    maxNumPassages: 1,
  },
}

And here are some sample documents:

{
  "_id": {
    "$oid": "63adcea85f7c5357a41c68d3"
  },
  "description": "User created",
  "ip": "127.0.0.1",
  "user_agent": "stensul/3.92.0.1 (core)",
  "controller": "",
  "action": "",
  "properties": {
    "user_id": "system",
    "changed_user_id": {
      "$oid": "63adcea75f7c5357a41c68d2"
    },
    "changed_user_name": "Germán",
    "changed_user_last_name": "Medaglia",
    "changed_user_email": "german.medaglia@stensul.com"
  },
  "updated_at": {
    "$date": "2022-12-29T17:30:16.428Z"
  },
  "created_at": {
    "$date": "2022-12-29T17:30:16.428Z"
  }
}
{
  "_id": {
    "$oid": "63adcedc81cdce61285a2334"
  },
  "description": "User logged in",
  "ip": "172.18.0.1",
  "user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36",
  "controller": "BaseLoginController",
  "action": "postLogin",
  "properties": {
    "user_id": {
      "$oid": "63adcea75f7c5357a41c68d2"
    },
    "email": "german.medaglia@stensul.com",
    "method": "default"
  },
  "updated_at": {
    "$date": "2022-12-29T17:31:08.572Z"
  },
  "created_at": {
    "$date": "2022-12-29T17:31:08.572Z"
  }
}

@Tarun_Gaur any idea? Maybe the option is not available on M0 clusters?

Hello @German_Medaglia ,

The documentation on the Atlas Search Sort was recently updated as follows

Limitations
You must have an M10 or higher cluster to sort the results using the sort option. The sort option is not available on free and shared tier clusters.

You can’t sort on fields of embeddedDocuments type.

You can’t use the sort option with the knnBeta operator.

So you won’t be able to currently use this on an M0 cluster. I will update this post if there are any changes to this.

Feel free to open new thread for any more queries or issues, will be happy to help you! :slightly_smiling_face:

Thank you,
Tarun

Thanks @Tarun_Gaur ! Yes, tried it on an M30 and it worked!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.