Language

SearchSpec.Builder.SetRankingStrategy Method

Definition

Overloads

Name Description
SetRankingStrategy(Int32)

Sets ranking strategy for AppSearch results.

SetRankingStrategy(String)

Enables advanced ranking to score based on advancedRankingExpression.

SetRankingStrategy(Int32)

Sets ranking strategy for AppSearch results.

[Android.Runtime.Register("setRankingStrategy", "(I)Landroid/app/appsearch/SearchSpec$Builder;", "", ApiSince=31)]
public Android.App.AppSearch.SearchSpec.Builder SetRankingStrategy(int rankingStrategy);
[<Android.Runtime.Register("setRankingStrategy", "(I)Landroid/app/appsearch/SearchSpec$Builder;", "", ApiSince=31)>]
member this.SetRankingStrategy : int -> Android.App.AppSearch.SearchSpec.Builder

Parameters

rankingStrategy
Int32

Value is one of the following: SearchSpec.RANKING_STRATEGY_NONE SearchSpec.RANKING_STRATEGY_DOCUMENT_SCORE SearchSpec.RANKING_STRATEGY_CREATION_TIMESTAMP SearchSpec.RANKING_STRATEGY_RELEVANCE_SCORE SearchSpec.RANKING_STRATEGY_USAGE_COUNT SearchSpec.RANKING_STRATEGY_USAGE_LAST_USED_TIMESTAMP SearchSpec.RANKING_STRATEGY_SYSTEM_USAGE_COUNT SearchSpec.RANKING_STRATEGY_SYSTEM_USAGE_LAST_USED_TIMESTAMP SearchSpec.RANKING_STRATEGY_JOIN_AGGREGATE_SCORE SearchSpec.RANKING_STRATEGY_ADVANCED_RANKING_EXPRESSION

Returns

Attributes

Remarks

Sets ranking strategy for AppSearch results.

Android reference for android.app.appsearch.SearchSpec.Builder.setRankingStrategy.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

SetRankingStrategy(String)

Enables advanced ranking to score based on advancedRankingExpression.

[Android.Runtime.Register("setRankingStrategy", "(Ljava/lang/String;)Landroid/app/appsearch/SearchSpec$Builder;", "", ApiSince=34)]
public Android.App.AppSearch.SearchSpec.Builder SetRankingStrategy(string advancedRankingExpression);
[<Android.Runtime.Register("setRankingStrategy", "(Ljava/lang/String;)Landroid/app/appsearch/SearchSpec$Builder;", "", ApiSince=34)>]
member this.SetRankingStrategy : string -> Android.App.AppSearch.SearchSpec.Builder

Parameters

advancedRankingExpression
String

a non-empty string representing the ranking expression. This value cannot be null.

Returns

Attributes

Remarks

Enables advanced ranking to score based on advancedRankingExpression. This method will set RankingStrategy to SearchSpec.RANKING_STRATEGY_ADVANCED_RANKING_EXPRESSION. The ranking expression is a mathematical expression that will be evaluated to a floating-point number of double type representing the score of each document. Numeric literals, arithmetic operators, mathematical functions, document-based, and property-value-based functions are supported to build expressions. The following are supported arithmetic operators: Addition(+) Subtraction(-) Multiplication(*) Floating Point Division(/) Operator precedences are compliant with the Java Language, and parentheses are supported. For example, "2.2 + (3 - 4) / 2" evaluates to 1.7. The following are supported basic mathematical functions: log(x) - the natural log of x log(x, y) - the log of y with base x pow(x, y) - x to the power of y sqrt(x) abs(x) sin(x), cos(x), tan(x) Example: "max(abs(-100), 10) + pow(2, 10)" will be evaluated to 1124 The following variadic mathematical functions are supported, with n > 0. They also accept list value parameters. For example, if V is a value of list type, we can call sum(V) to get the sum of all the values in V. List literals are not supported, so a value of list type can only be constructed as a return value of some particular document-based functions. max(v1, v2,..., vn) or max(V) min(v1, v2,..., vn) or min(V) len(v1, v2,..., vn) or len(V) sum(v1, v2,..., vn) or sum(V) avg(v1, v2,..., vn) or avg(V) Document-based functions must be called via "this", which represents the current document being scored. The following are supported document-based functions: this.documentScore() Get the app-provided document score of the current document. This is the same score that is returned for SearchSpec.RANKING_STRATEGY_DOCUMENT_SCORE. this.creationTimestamp() Get the creation timestamp of the current document. This is the same score that is returned for SearchSpec.RANKING_STRATEGY_CREATION_TIMESTAMP. this.relevanceScore() Get the BM25F relevance score of the current document in relation to the query string. This is the same score that is returned for SearchSpec.RANKING_STRATEGY_RELEVANCE_SCORE. this.usageCount(type) and this.usageLastUsedTimestamp(type) Get the number of usages or the timestamp of last usage by type for the current document, where type must be evaluated to an integer from 1 to 2. Type 1 refers to usages reported by AppSearchSession.reportUsage, and type 2 refers to usages reported by GlobalSearchSession.reportSystemUsage. this.childrenRankingSignals() Returns a list of children ranking signals calculated by scoring the joined documents using the ranking strategy specified in the nested SearchSpec. Currently, a document can only be a child of another document in the context of joins. If this function is called without the Join API enabled, a type error will be raised. this.propertyWeights() Returns a list of the normalized weights of the matched properties for the current document being scored. Property weights come from what's specified in SearchSpec. After normalizing, each provided weight will be divided by the maximum weight, so that each of them will be AppSearchSession.search ) based on embedding_index and metric. If metric is omitted, it defaults to the metric specified in SearchSpec.Builder.setDefaultEmbeddingSearchMetricType(int). If no "semanticSearch" is called for embedding_index and metric in the query, this function will return an empty list. If multiple "semanticSearch"s are called for the same embedding_index and metric, this function will return a list of their merged scores. Example: `this.matchedSemanticScores(getEmbeddingParameter(0), "COSINE")` will return a list of matched scores within the range of [0.5, 1], if `semanticSearch(getEmbeddingParameter(0), 0.5, 1, "COSINE")` is called in the query expression. Property-value-based functions can be called via the function of getScorableProperty(schemaType, propertyPath) In order to use this function, ScorablePropertyRanking feature must be enabled via SearchSpec.Builder.setScorablePropertyRankingEnabled(boolean). Param 'schemaType' must be a valid AppSearch SchemaType otherwise an error is returned. Param 'propertyPath' must be valid and scorable otherwise an error is returned. It is considered scorable when: It is to a property that is set to be enabled for scoring, or that It points to a scorable property of nested schema types. This function returns a list double values for the matched documents. If the matched document's schema is different from 'schemaType', or the property under the 'propertyPath' holds no element, an empty list is returned. Some examples below: Suppose that there are two schemas: 'Gmail' and 'Person'. 'Gmail' schema has a property 'recipient' with schema type 'Person'. In the advanced ranking expression, you can have: "sum(getScorableProperty('Gmail', 'viewTimes'))" "maxOrDefault(getScorableProperty('Person', 'income'), 0)" "sum(getScorableProperty('Gmail', 'recipient.income'))" "this.documentScore() + sum(getScorableProperty('Gmail', 'viewTimes'))" The following functions are provided for enhanced list manipulation. minOrDefault(V, default_score) Returns the minimum value in the input list V or the default_score if the list is empty. Example: "minOrDefault(this.matchedSemanticScores(getEmbeddingParameter(0)), 10)" will return the minimum matched semantic scores or 10 if there is no matched score for the current document. This function requires the feature ERROR(/Features#SEARCH_SPEC_RANKING_FUNCTION_MAX_MIN_OR_DEFAULT). maxOrDefault(V, default_score) Returns the maximum value in the input list V or the default_score if the list is empty. Example: "maxOrDefault(this.matchedSemanticScores(getEmbeddingParameter(0)), -10)" will return the maximum matched semantic scores or -10 if there is no matched score for the current document. This function requires the feature ERROR(/Features#SEARCH_SPEC_RANKING_FUNCTION_MAX_MIN_OR_DEFAULT). filterByRange(V, low, high) Returns a sublist of V that only contains the elements that fall within the specified range [low, high]. Example: "filterByRange(this.matchedSemanticScores(getEmbeddingParameter(0)), 0, 1)" will return a list of matched semantic scores that are between 0 and 1, inclusive. This function requires the feature ERROR(/Features#SEARCH_SPEC_RANKING_FUNCTION_FILTER_BY_RANGE). Some errors may occur when using advanced ranking. Syntax Error: the expression violates the syntax of the advanced ranking language. Below are some examples. "1 + " - missing operand "2 * (1 + 2))" - unbalanced parenthesis "2 ^ 3" - unknown operator Type Error: the expression fails a static type check. Below are some examples. "sin(2, 3)" - wrong number of arguments for the sin function "this.childrenRankingSignals() + 1" - cannot add a list with a number "this.propertyWeights()" - the final type of the overall expression cannot be a list, which can be fixed by "max(this.propertyWeights())" "abs(this.propertyWeights())" - the abs function does not support list type arguments "print(2)" - unknown function Evaluation Error: an error occurred while evaluating the value of the expression. Below are some examples. "1 / 0", "log(0)", "1 + sqrt(-1)" - getting a non-finite value in the middle of evaluation "this.usageCount(1 + 0.5)" - expect the argument to be an integer. Note that this is not a type error and "this.usageCount(1.5 + 1/2)" can succeed without any issues "this.documentScore()" - in case of an IO error, this will be an evaluation error Syntax errors and type errors will fail the entire search and will cause SearchResults.getNextPage to throw an AppSearchException with the result code of AppSearchResult.RESULT_INVALID_ARGUMENT. Evaluation errors will result in the offending documents receiving the default score. For SearchSpec.ORDER_DESCENDING, the default score will be 0, for SearchSpec.ORDER_ASCENDING the default score will be infinity.

Android reference for android.app.appsearch.SearchSpec.Builder.setRankingStrategy.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to