Search code examples
javascriptcontentful

Contentful.com API order / sort query


For anyone who has experience using the contentful.com API, I'm trying to query and sort by field name and currently getting a "ServerError". An example of the query that's being generated against their example API (with "fields.name" as the parameter):

https://cdn.contentful.com/spaces/cfexampleapi/entries?order=fields.name&access_token=b4c0n73n7fu1

Note that if "sys.createdAt" is used it works fine...

https://cdn.contentful.com/spaces/cfexampleapi/entries?order=sys.createdAt&access_token=b4c0n73n7fu1

The documentation is pretty vague (https://www.contentful.com/developers/documentation/content-delivery-api/javascript/#search-order) and I've searched long and hard for examples / samples but to no avail.

Thanks in advance for any thoughts / ideas!


Solution

  • I'm a frontend engineer at Contentful.
    If you want to order entries by a certain field, you have to restrict your search to a content type by passing the content_type query parameter. This is because the field you want to sort by might not exist in all your entries. Example:

    https://cdn.contentful.com/spaces/cfexampleapi/entries?order=fields.name&content_type=cat&access_token=b4c0n73n7fu1
    

    Please note that this example will still not work, because the name field is of type Text (Fulltext). Fulltext fields support fulltext-search but no ordering. Instead you could use a Symbol field. Symbols support ordering but no fulltext-search.

    This, for example, would work since color is a symbol field:

    https://cdn.contentful.com/spaces/cfexampleapi/entries?order=fields.color&content_type=cat&access_token=b4c0n73n7fu1