The Popularity API returns a normalized popularity score for each document. Use this score in your scorer to factor item popularity into result ranking. Before using the API, create and deploy a popularity model in OpenSearch.
Functions
| Function | Description |
|---|---|
Popularity create(OpsScorerInitParams params, CString modelName) | Creates a popularity object. |
Popularity create(OpsScorerInitParams params, CString modelName, float defaultValue) | Creates a popularity object with a default popularity score for new documents. |
double evaluate(OpsScoreParams params) | Obtains the popularity score of a document. |
Function details
Popularity create(OpsScorerInitParams params, CString modelName)
Creates a popularity object. Call this function in init() to initialize the popularity object before calling evaluate() during scoring.
| Parameter | Type | Required | Description |
|---|---|---|---|
params | OpsScorerInitParams | Yes | Parameters used for score calculation. See OpsScoreParams. |
modelName | CString | Yes | Name of the popularity model created and deployed in the OpenSearch console. Must be a constant. |
Popularity create(OpsScorerInitParams params, CString modelName, float defaultValue)
Creates a popularity object with a default popularity score. Use this overload when your index includes new documents that have no popularity data yet — without a default score, new documents may not appear in search results.
| Parameter | Type | Required | Description |
|---|---|---|---|
params | OpsScorerInitParams | Yes | Parameters used for score calculation. See OpsScoreParams. |
modelName | CString | Yes | Name of the popularity model created and deployed in the OpenSearch console. Must be a constant. |
defaultValue | float | Yes | Default popularity score assigned to documents with no popularity data. Valid values: [0,1]. Must be a constant. |
ThedefaultValuerange [0,1] matches the range returned byevaluate(), so scores from both sources are directly comparable.
double evaluate(OpsScoreParams params)
Calculates the popularity score of a document. Call this function inside score() to get the popularity score, then combine it with other scoring signals as needed.
| Parameter | Type | Required | Description |
|---|---|---|---|
params | OpsScoreParams | Yes | Parameters used for score calculation. See OpsScoreParams. |
Return value: The popularity score of a document. Valid values: [0,1].
Sample code
The following example creates a popularity object in init() using a named popularity model and a default score for new documents, then returns the popularity score from score().
package users.scorer;
import com.aliyun.opensearch.cava.framework.OpsScoreParams;
import com.aliyun.opensearch.cava.framework.OpsScorerInitParams;
import com.aliyun.opensearch.cava.framework.OpsRequest;
import com.aliyun.opensearch.cava.framework.OpsDoc;
import com.aliyun.opensearch.cava.features.algo.Popularity;
class IntelligenceAlgorithmScorer {
Popularity _pop;
boolean init(OpsScorerInitParams params) {
_pop = Popularity.create(params, "pop1", 0.123F); // pop1 is the name of the popularity model.
return true;
}
double score(OpsScoreParams params) {
return _pop.evaluate(params);
}
}What's next
Popularity models — Create and deploy the popularity model required by the Popularity API.