Notice: Over the next few months, we're reorganizing the App Engine documentation site to make it easier to find content and better align with the rest of Google Cloud products. The same content will be available, but the navigation will now match the rest of the Cloud products. If you have feedback or questions as you navigate the site, click Send Feedback.

Python 2 is no longer supported by the community. We recommend that you migrate Python 2 apps to Python 3.

The SortExpression Class

Class SortExpression allows you to customize how your search results are sorted.

SortExpression is defined in the module.


The constructor for class SortExpression is defined as follows:

class SortExpression(expression=None, direction=DESCENDING, default_value=None)

Sort results by the specified scoring expression.



An expression to be evaluated when sorting results for each matching document. The expression can simply be a field name, or some compound expression such as _score + count(likes) * 0.1 which will add the score from a scorer to a count of the values of a likes field weighted by a factor of 0.1.


The direction to sort the search results, either ASCENDING or DESCENDING.


The default value of the expression, if no field is present and cannot be calculated for a document. A text value must be specified for text sorts. A numeric value must be specified for numeric sorts.

Result value

A new instance of class SortExpression.



If any of the parameters have an invalid type, or an unknown attribute is passed.


If any parameter has an invalid value.


An instance of class SortExpression has the following properties:


Returns the expression used to sort matched results.


Returns the direction of sort expression: ASCENDING or DESCENDING.


Returns a default value for the expression if no other value could be computed.