Stay organized with collections
Save and categorize content based on your preferences.
publicstaticfinalclassSortOptions.Builder
A builder that constructs SortOptionss.
A SortOptions will evaluate each of the
SortExpressions on each search result and apply a
sort order with priority given to the sort expressions from left to right.
The following code illustrates creating a SortOptions specification
to sort documents based on decreasing product rating and then within
rating showing cheapest products based on price plus tax,
sorting at most 2000 documents.
The following code fragment shows how the score from a MatchScorer
can be used in an expression that combines the score with one thousandth of
an "importance" field. At most 1000 documents are scored and sorted.
[[["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-04 UTC."],[[["\u003cp\u003eThe \u003ccode\u003eSortOptions.Builder\u003c/code\u003e class is used to construct \u003ccode\u003eSortOptions\u003c/code\u003e, which define how search results are sorted based on specified \u003ccode\u003eSortExpressions\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eYou can add multiple \u003ccode\u003eSortExpressions\u003c/code\u003e to prioritize the sorting of documents, with each expression evaluated from left to right.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003esetLimit\u003c/code\u003e method sets a limit on the number of documents to be scored or sorted, which is useful for large datasets.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003esetMatchScorer\u003c/code\u003e method allows for integrating a \u003ccode\u003eMatchScorer\u003c/code\u003e or \u003ccode\u003eRescoringMatchScorer\u003c/code\u003e, enabling sorting based on a score field (accessible as "_score").\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003ebuild()\u003c/code\u003e method is used to finalize and create the \u003ccode\u003eSortOptions\u003c/code\u003e object from the builder.\u003c/p\u003e\n"]]],[],null,["# Class SortOptions.Builder (2.0.0)\n\n public static final class SortOptions.Builder\n\nA builder that constructs [SortOptionss](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.SortOptions).\nA SortOptions will evaluate each of the\n[SortExpressions](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.SortExpression) on each search result and apply a\nsort order with priority given to the sort expressions from left to right.\nThe following code illustrates creating a SortOptions specification\nto sort documents based on decreasing product rating and then within\nrating showing cheapest products based on price plus tax,\nsorting at most 2000 documents.\n\n\n\n SortOptions sortOptions = SortOptions.newBuilder()\n .addSortExpression(SortExpression.newBuilder()\n .setExpression(\"rating\")\n .setDirection(SortExpression.SortDirection.DESCENDING)\n .setDefaultValueNumeric(0))\n .addSortExpression(SortExpression.newBuilder()\n .setExpression(\"price + tax\")\n .setDirection(SortExpression.SortDirection.ASCENDING)\n .setDefaultValueNumeric(99999999.00))\n .setLimit(1000)\n .build();\n \nThe following code fragment shows how the score from a [MatchScorer](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.MatchScorer)\ncan be used in an expression that combines the score with one thousandth of\nan \"importance\" field. At most 1000 documents are scored and sorted. \n\n\n SortOptions sortOptions = SortOptions.newBuilder()\n .setMatchScorer(MatchScorer.newBuilder())\n .addSortExpression(SortExpression.newBuilder()\n .setExpression(String.format(\n \"%s + (importance * .001)\", SortExpression.SCORE_FIELD_NAME))\n .setDirection(SortExpression.SortDirection.DESCENDING)\n .setDefaultValueNumeric(0))\n .setLimit(1000)\n .build();\n \nInheritance\n-----------\n\n[java.lang.Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \\\u003e SortOptions.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### addSortExpression(SortExpression sortExpression)\n\n public SortOptions.Builder addSortExpression(SortExpression sortExpression)\n\nAdds a [SortExpression](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.SortExpression) to the list of sort expressions.\n\n### addSortExpression(SortExpression.Builder builder)\n\n public SortOptions.Builder addSortExpression(SortExpression.Builder builder)\n\nAdds a [SortExpression](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.SortExpression) built from the builder to the list of sort\nexpressions.\n\n### build()\n\n public SortOptions build()\n\nBuilds a [SortOptions](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.SortOptions) from the set values.\n\n### setLimit(int limit)\n\n public SortOptions.Builder setLimit(int limit)\n\nSets the limit on the number of documents to score or sort.\n\n### setMatchScorer(MatchScorer matchScorer)\n\n public SortOptions.Builder setMatchScorer(MatchScorer matchScorer)\n\nSets a [MatchScorer](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.MatchScorer) or [RescoringMatchScorer](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.RescoringMatchScorer) to base some\n[SortExpressions](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.SortExpression) on. The value of the matchScorer can\nbe accessed by using the field name \"_score\".\n\n### setMatchScorer(MatchScorer.Builder builder)\n\n public SortOptions.Builder setMatchScorer(MatchScorer.Builder builder)\n\nSets the matchScorer to the [MatchScorer](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.MatchScorer) built from the builder.\nSee Also: [#setMatchScorer(MatchScorer)](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.SortOptions.Builder#com_google_appengine_api_search_SortOptions_Builder_setMatchScorer_com_google_appengine_api_search_MatchScorer_)\n\n### setMatchScorer(RescoringMatchScorer.Builder builder)\n\n public SortOptions.Builder setMatchScorer(RescoringMatchScorer.Builder builder)\n\nSets the matchScorer to the [RescoringMatchScorer](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.RescoringMatchScorer) built from the\nbuilder.\nSee Also: [#setMatchScorer(MatchScorer)](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.search.SortOptions.Builder#com_google_appengine_api_search_SortOptions_Builder_setMatchScorer_com_google_appengine_api_search_MatchScorer_)"]]