Use the category prediction feature

Updated at:

Category prediction uses a trained model to match a search query to a product category, then ranks results from that category higher. For example, a query like "running shoes" maps to "Sports > Footwear," surfacing athletic shoes above unrelated results. A query like "denim" maps to "Clothing > Pants," ranking jeans above denim-themed home goods.

To enable category prediction, complete three configurations: create and train a model, configure a query analysis rule that references the model, and add a sort expression that calls category_score.

image

Prerequisites

Before you begin, make sure that you have:

  • Category ID, commodity title, and category name fields configured as attribute fields in your application schema

  • (If using Commodity Data) Document data that contains category ID, commodity title, and category name fields

  • (If using Behavior Data) Click behavior data collected via data collection

Create and train a category prediction model

  1. In the left-side navigation pane, choose Search Algorithm Center > Sort Configuration. In the navigation tree, click Category Prediction Model, then click Create.

    image

  2. On the Create Category Prediction page, fill in the model details.

    Field

    Description

    Required

    Model name

    A name for the model

    Yes

    Data Type

    Commodity Data: document data from your application. Requires category ID, commodity title, and category name fields. Behavior Data: collected click behavior data.

    Yes

    Category ID

    The field representing the category ID in your application documents. Supports the INT data type. Must be a positive integer—a non-positive value causes a server error.

    Yes

    Commodity Title

    The field representing the commodity title. Supports TEXT and LITERAL data types.

    Yes

    Category Name

    The field representing the category name. Supports TEXT and LITERAL data types.

    Yes

    Filter Condition

    A condition to filter training data. Field names must match those in the application schema. Supports: <, >, <=, >=, =, !=. Separate multiple conditions with commas (,)—all conditions use AND logic. OR logic is not supported. Example: is_onsale > 0

    No

    image

  3. Click Completed to go to the model details page.

    image

  4. On the Category Prediction Model page, the model status is Pending Training. Click Train in the Actions column to start training.

    image

    The model moves through the following states:

    State

    Meaning

    Pending Training

    Model created, training not started

    Training

    Model is being trained

    Training Failed

    Training failed due to a data error

    Not Applied

    Training complete, not yet referenced for retrieval or sorting

    Trained and Not Passed

    Training complete, but the model failed to pass the training

    If you have questions, submit a ticket.

Configure query analysis and sort expressions

Create a query analysis rule

  1. In the left-side navigation pane, choose Search Algorithm Center > Retrieval Configuration. In the navigation tree, click Query Analysis Rule Configuration, then click Create.

    image

  2. Configure the query analysis rule and save it.

    image

Apply the model to rough sorting

Rough sorting is the first round of ranking.

  1. In the left-side navigation pane, choose Search Algorithm Center > Sort Configuration. On the Policy Management page, click Create.

    image

  2. On the Create Policy page, enter a policy name. Set Scope to Rough Sort and Type to Expression. Only expressions are supported in the console. Custom plug-ins will be supported later.

    image

  3. In the Sort Configuration step, select category_score() from the Scoring Characteristics drop-down list.

    image

  4. Select a field, set the weight, and save the configuration.

    image

Apply the model to fine sorting

Fine sorting is the second round of ranking.

  1. On the Create Policy page, enter a policy name. Set Scope to Fine Sort and Type to Expression. Only expressions are supported in the console. Custom plug-ins will be supported later.

    image

  2. In the Sort Configuration step, select category_score(cate_id_field) from the Built-in Functions drop-down list.

    image

  3. Replace cate_id_field with the category ID of your model and save the configuration.

    image

  4. Verify the model status after it is referenced by the sort policy.

  5. Run a search test on the application. Set the raw_query parameter to a search query to test the category prediction results.

    image

Activate category prediction in API and SDK calls

Category prediction sorts search results only when the raw_query parameter is present and its value matches the search query in the index.

Set raw_query to the search query entered by the user:

raw_query=<search_query>
  • When calling via API: URL-encode the value using URL_ENCODE.

  • When calling via SDK: no encoding needed.

When category prediction takes effect

The table below shows when category prediction applies, based on the index configuration and raw_query value.

Index configuration

raw_query in request

raw_query equals search query

Category prediction applies?

Disabled

Yes

No

Disabled

No

No

Enabled

No

No

Enabled

Yes

No

No

Enabled

Yes

Yes

Yes

Multi-index behavior: If a request covers multiple indexes, category prediction applies per-index based on the rules above.

# index3 prediction takes effect (query matches raw_query for index3 only)
query=index2:'index_query' AND index3:'search_query'&raw_query=search_query

# index2 prediction takes effect preferentially;
# if index2 config doesn't apply, index3 takes effect
query=index2:'search_query' AND index3:'search_query'&raw_query=search_query

Limits

  • Up to five category prediction models can be created per application.

  • The data fields used to train the model must be included in the indexes to which the query analysis rule is applied.

  • Before creating a model, configure category ID and related fields as attribute fields.

  • If a query analysis rule references a category prediction model and the request contains the category_prediction parameter, an error occurs.

  • For category prediction to take effect, all three of the following must be configured: a query analysis rule referencing the model, a sort expression containing the category_score function, and the raw_query parameter in the request.

  • Related API operations: Algorithms

Troubleshooting model training failures

If your category prediction model training fails, follow these steps to troubleshoot:

  1. Check the anomaly report: On the model details page, view the Latest Version Status. If the status shows a data error, open the Anomaly Report to identify the cause of the failure.

  2. Check the completeness level: View the Completeness Level and the corresponding Upgrade Conditions. Adjust your data according to the upgrade conditions to improve the completeness level.

  3. T-1 data logic: Model training uses statistical data from the previous day (T-1). When a newly created model is trained for the first time, the training may fail with a "Training field does not exist" error because no data from the previous day is available. Wait until the next day when data is ready, then retrain the model.

  4. Data volume requirement: Training requires the number of unique queries from the most recent day to exceed 100. The unique query count refers to the number of successful search requests that include the raw_query parameter.

  5. Filter condition format: If filter conditions are configured, field names must match fields in the application schema. Multiple conditions use AND logic and must be separated by commas (,). An incorrect format may cause training failures. Example: is_onsale > 0.

Advanced usage

Combine with Named Entity Recognition (NER)

If you configure NER alongside category prediction in a query analysis rule, more documents are retrieved. The NER results are also used for retrieval. If no documents match the predicted categories, words in the search query are selectively ignored to broaden results—these broader results become the final category prediction output. For more information, see Named entity recognition.

Intervene in prediction results

Intervention lets you override the trained model's predictions, similar to query analysis intervention. For more information, see Intervention dictionaries for category prediction.