HomeDev guideRecipesAPI ReferenceGraphQL
Dev guideUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

OrderBy

Describes the OrderBy parameter, part of the GraphQL API used for the Optimizely querying service, when retrieving content in Optimizely solutions.

The orderBy parameter enables the ordering (sorting) of the result list. You can order by:

  • Relevance – Scores with _ranking using RELEVANCE, which uses BM25 for best matching, and always sorted in descending order (most relevant top). If you only sort by RELEVANCE and the scores are the same, then the index order is used as a secondary sort.
  • Boost Only – Scores with _ranking using BOOST_ONLY. Sort by only using the query clauses that have a boost, and other clauses will only match but not affect the scores. This is useful to take full control over the ranking using only boosts. Non-boosted clauses will be ordered by index order, but it is possible to override this with a secondary (and more) sorting criteria like lexographical sorting of a field in ascending direction.
  • Index order – Sort by index order using DOC (the order in which the items are synchronized with _ranking. Recommended in combination with cursor.
  • Field values – Sorted in DESC (descending) or ASC (ascending) order.

The orderBy can have one or more sorting criteria. If there is a tie-breaker, the next sorting criteria is used, and so on. If there are null values, these are ranked after the non-null values.

If you have multi-valued (list) fields, then Optimizely Graph sorts by the highest value in the list when in DESC order and by the lowest value in the list when in ASC order. For string values, the lexicographic order determines the highest and lowest value, such as B is a higher value than A.

Examples

To sort by relevance, and when the scores are the same, then by a field called created in descending order (newest in top):

orderBy: { 
    _ranking: RELEVANCE 
    created: DESC 
}

You can order nested fields like this:

{
  BiographyPage(
    orderBy: {
      _ranking: RELEVANCE
      ContentLink: {
        GuidValue: ASC
      }
    }) {
    items {
      ContentLink {
        GuidValue
      }
    }
  }
}

As explained above, when you have a field like MetaKeywords that is multi-valued (list of strings), this query sorts the items by the lowest value of the field in each item because the sort direction is ASC.

{
  BiographyPage(
    orderBy: {
      MetaKeywords: ASC
    }) {
    items {
      MetaKeywords
    }
  }
}