Stay organized with collections
Save and categorize content based on your preferences.
publicstaticfinalclassSortExpression.Builder
A builder that constructs SortExpressions. The user
must provide an expression. The expression can be as simple as a field
name, or can be some other expression such as "score +
count(likes) * 0.1", which combines a scorer score with a count
of the number of likes values times 0.1. A default value must be specified
for the expression.
[[["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-09-03 UTC."],[[["\u003cp\u003eThe \u003ccode\u003eSortExpression.Builder\u003c/code\u003e class is used to construct \u003ccode\u003eSortExpression\u003c/code\u003e objects, which are crucial for defining how search results are ordered.\u003c/p\u003e\n"],["\u003cp\u003eYou can set a custom expression for sorting, ranging from simple field names to complex calculations, such as combining a score with a weighted count of likes.\u003c/p\u003e\n"],["\u003cp\u003eA default value, which can be numeric, text, or date-based, must be provided for the expression to handle cases where the expression cannot be evaluated for a particular document.\u003c/p\u003e\n"],["\u003cp\u003eThe direction of the sort can be specified using \u003ccode\u003esetDirection\u003c/code\u003e, allowing you to sort in either ascending or descending order, and the \u003ccode\u003ebuild()\u003c/code\u003e method finalizes the \u003ccode\u003eSortExpression\u003c/code\u003e object creation.\u003c/p\u003e\n"],["\u003cp\u003eMethods are provided to set an expression, a sort direction, and default values of string, date, and numeric types for the sort expression.\u003c/p\u003e\n"]]],[],null,["# Class SortExpression.Builder (2.0.0)\n\n public static final class SortExpression.Builder\n\nA builder that constructs [SortExpressions](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.SortExpression). The user\nmust provide an expression. The expression can be as simple as a field\nname, or can be some other expression such as `\"score +\ncount(likes) * 0.1\"`, which combines a scorer score with a count\nof the number of likes values times 0.1. A default value must be specified\nfor the expression.\n\n\n\n SortExpression expr = SortExpression.newBuilder()\n .setExpression(String.format(\n \"%s + count(likes) * 0.1\", SortExpression.SCORE_FIELD_NAME))\n .setDirection(SortExpression.SortDirection.ASCENDING)\n .setDefaultValueNumeric(0.0)\n .build()\n \nInheritance\n-----------\n\n[java.lang.Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \\\u003e SortExpression.Builder \n\nInherited Members\n-----------------\n\n[Object.clone()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--) \n[Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) \n[Object.finalize()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#finalize--) \n[Object.getClass()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--) \n[Object.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--) \n[Object.notify()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--) \n[Object.notifyAll()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notifyAll--) \n[Object.toString()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) \n[Object.wait()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--) \n[Object.wait(long)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-) \n[Object.wait(long,int)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-)\n\nMethods\n-------\n\n### build()\n\n public SortExpression build()\n\nBuilds a [SortExpression](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.SortExpression) from the set values.\n\n### setDefaultValue(String defaultValue)\n\n public SortExpression.Builder setDefaultValue(String defaultValue)\n\nSets the default value for the field for sorting purposes. Must provide\nfor text sorts.\n\n### setDefaultValueDate(Date defaultValue)\n\n public SortExpression.Builder setDefaultValueDate(Date defaultValue)\n\nSets the default value for the field for sorting purposes. Must provide\nfor Date sorts. Typically, you should use\n[com.google.appengine.api.search.checkers.SearchApiLimits#MINIMUM_DATE_VALUE](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.checkers.SearchApiLimits#com_google_appengine_api_search_checkers_SearchApiLimits_MINIMUM_DATE_VALUE) or\n[com.google.appengine.api.search.checkers.SearchApiLimits#MAXIMUM_DATE_VALUE](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.checkers.SearchApiLimits#com_google_appengine_api_search_checkers_SearchApiLimits_MAXIMUM_DATE_VALUE)\nas a default value.\n\n### setDefaultValueNumeric(double defaultValue)\n\n public SortExpression.Builder setDefaultValueNumeric(double defaultValue)\n\nSets the default value for the field for sorting purposes. Must provide\nfor numeric sorts.\n\n### setDirection(SortExpression.SortDirection direction)\n\n public SortExpression.Builder setDirection(SortExpression.SortDirection direction)\n\nSets the direction to sort the search results in.\n\n### setExpression(String expression)\n\n public SortExpression.Builder setExpression(String expression)\n\nSets an expression to be evaluated for each document to sort by.\nA default string value [#setDefaultValue(String)](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.SortExpression.Builder#com_google_appengine_api_search_SortExpression_Builder_setDefaultValue_java_lang_String_) or numeric\n[#setDefaultValueNumeric(double)](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.SortExpression.Builder#com_google_appengine_api_search_SortExpression_Builder_setDefaultValueNumeric_double_) or date\n[#setDefaultValueDate(Date)](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.SortExpression.Builder#com_google_appengine_api_search_SortExpression_Builder_setDefaultValueDate_java_util_Date_) must be specified for the expression."]]