Python 2.7 has reached end of support
and will be deprecated
on January 31, 2026. After deprecation, you won't be able to deploy Python 2.7
applications, even if your organization previously used an organization policy to
re-enable deployments of legacy runtimes. Your existing Python
2.7 applications will continue to run and receive traffic after their deprecation date
. We recommend that
you migrate to the latest supported version of Python
.
The SortExpression Class
Stay organized with collections
Save and categorize content based on your preferences.
Class SortExpression
allows you to customize how your search results are sorted.
SortExpression
is defined in the google.appengine.api.search
module.
Constructor
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.
-
Arguments
- 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.
- direction
-
The direction to sort the search results, either ASCENDING or DESCENDING.
- default_value
-
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
.
Exceptions
- TypeError
-
If any of the parameters have an invalid type, or an unknown attribute is passed.
- ValueError
-
If any parameter has an invalid value.
Properties
An instance of class SortExpression
has the following properties:
- expression
-
Returns the expression used to sort matched results.
- direction
-
Returns the direction of sort expression: ASCENDING
or DESCENDING
.
- default_value
-
Returns a default value for the expression if no other value could be computed.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License
, and code samples are licensed under the Apache 2.0 License
. For details, see the Google Developers Site Policies
. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-29 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-29 UTC."],[[["\u003cp\u003eThe \u003ccode\u003eSortExpression\u003c/code\u003e class enables customization of search result sorting using an expression.\u003c/p\u003e\n"],["\u003cp\u003eIt can be used with first-generation runtimes and is applicable when upgrading to second-generation runtimes.\u003c/p\u003e\n"],["\u003cp\u003eThe class constructor accepts \u003ccode\u003eexpression\u003c/code\u003e, \u003ccode\u003edirection\u003c/code\u003e (\u003ccode\u003eASCENDING\u003c/code\u003e or \u003ccode\u003eDESCENDING\u003c/code\u003e), and \u003ccode\u003edefault_value\u003c/code\u003e to define how results are sorted.\u003c/p\u003e\n"],["\u003cp\u003eProperties \u003ccode\u003eexpression\u003c/code\u003e, \u003ccode\u003edirection\u003c/code\u003e, and \u003ccode\u003edefault_value\u003c/code\u003e can be retrieved from an instance of \u003ccode\u003eSortExpression\u003c/code\u003e.\u003c/p\u003e\n"]]],[],null,["# The SortExpression Class\n\nClass `SortExpression` allows you to customize how your search results are sorted.\n| This API is supported for first-generation runtimes and can be used when [upgrading to corresponding second-generation runtimes](/appengine/docs/standard/\n| python3\n|\n| /services/access). If you are updating to the App Engine Python 3 runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/python-differences) to learn about your migration options for legacy bundled services.\n\n`SortExpression` is defined in the `google.appengine.api.search` module.\n\nConstructor\n-----------\n\nThe constructor for class `SortExpression` is defined as follows:\n\n\nclass SortExpression(expression=None, direction=DESCENDING, default_value=None)\n\n:\n Sort results by the specified scoring expression.\n\n:\n\n Arguments\n\n expression\n\n : 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\n `_score + count(likes) * 0.1`\n which will add the score from a scorer to a count of the values of a\n `likes` field weighted by a factor of 0.1.\n\n direction\n\n : The direction to sort the search results, either ASCENDING or DESCENDING.\n\n default_value\n\n : 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.\n\n Result value\n\n : A new instance of class `SortExpression`.\n\n Exceptions\n\n TypeError\n\n : If any of the parameters have an invalid type, or an unknown attribute is passed.\n\n ValueError\n\n : If any parameter has an invalid value.\n\n \u003cbr /\u003e\n\nProperties\n----------\n\nAn instance of class `SortExpression` has the following properties:\n\nexpression\n\n: Returns the expression used to sort matched results.\n\ndirection\n\n: Returns the direction of sort expression: `ASCENDING` or `DESCENDING`.\n\ndefault_value\n\n: Returns a default value for the expression if no other value could be computed."]]