Attribute ranking
This page covers attribute ranking queries for ranking attribute values instead of items. For query fundamentals, see Query Basics.
An attribute ranking query returns the best attribute values for a given user. Instead of ranking items, this ranks attribute values like categories, genres, brands, or tags.
Common applications include:
- Personalized category carousels ("Top categories for you")
- Navigation that adapts to user preferences
- Surfacing relevant filters or facets
Attribute ranking is particularly useful for building category pages and faceted filtering interfaces. Once you've identified the best categories for a user, you can use those categories in filtered queries to show personalized items within each category. See Faceted filtering and category pages for examples of combining attribute ranking with item ranking.
Rank attributes for user
Prerequisites
- An engine with item data containing the attribute you want to rank
- A
user_idto personalize for
Query example
Use the rank_attributes query type to find the best attribute values for a
user. Note: ShapedQL support for attribute ranking is not currently available,
so JSON format is required.
- Python SDK
- TypeScript SDK
- JSON
- ShapedQL
from shaped import RankAttributesQueryBuilder
# Create an attribute ranking query
query = (
RankAttributesQueryBuilder()
.for_attribute('category') # The attribute to rank (e.g., 'brand', 'category', 'tag')
.for_user('$user_id') # User ID to personalize for
.with_embedding('content_embedding') # Embedding to use for ranking
.with_limit(5) # Number of top attributes to return
.build()
)
# Example usage with client
# response = client.rank_attributes(
# query=query,
# parameters={
# 'user_id': '12345'
# }
# )
#
# # Access top categories
# top_categories = [attr['value'] for attr in response['attributes']]
import { RankAttributesQueryBuilder } from '@shaped-ai/api';
// Create an attribute ranking query
const query = new RankAttributesQueryBuilder()
.forAttribute('category') // The attribute to rank
.forUser('$user_id') // User ID to personalize for
.withEmbedding('content_embedding') // Embedding to use for ranking
.withLimit(5) // Number of top attributes to return
.build();
// Example usage with client
// const response = await client.rankAttributes({
// query,
// parameters: {
// user_id: '12345'
// }
// });
//
// // Access top categories
// const topCategories = response.attributes.map(attr => attr.value);
{
"query": {
"type": "rank_attributes",
"input_attribute": "category",
"input_user_id" : "$user_id",
"embeddings": "content_embedding",
"limit": 5
},
"parameters": {
"user_id": "12345"
}
}
-- Note: rank_attributes is currently only available via JSON format.
-- ShapedQL support for attribute ranking is coming soon.
Using attribute ranking with category pages
After ranking attributes (e.g., categories) for a user, you can use those ranked attributes to build personalized category pages. The attribute ranking tells you which categories are most relevant to the user, and then you can query items within those categories using value models for fine-grained ranking.
For example, if attribute ranking returns "Electronics" as the top category for a user, you can then query items in that category with personalized scoring:
- Python SDK
- TypeScript SDK
- ShapedQL
- JSON
from shaped import RankQueryBuilder, ColumnOrder
# After getting top_category from attribute ranking
top_category = "Electronics" # From attribute ranking response
# Create a query for items in the top category
query = (
RankQueryBuilder()
.from_entity('item')
.retrieve(
ColumnOrder(
columns=['_derived_popular_rank ASC'],
limit=1000,
where=f"category = '{top_category}'"
)
)
.score(
value_model='0.6 * click_through_rate + 0.4 * conversion_rate',
input_user_id='$user_id',
input_interactions_item_ids='$interaction_item_ids'
)
.limit(20)
.build()
)
# Example usage with client
# response = client.rank(
# query=query,
# parameters={
# 'user_id': 'user123'
# }
# )
import { RankQueryBuilder } from '@shaped-ai/api';
// After getting topCategory from attribute ranking
const topCategory = "Electronics"; // From attribute ranking response
// Create a query for items in the top category
const query = new RankQueryBuilder()
.from('item')
.retrieve(step =>
step.columnOrder({
columns: [{ name: '_derived_popular_rank', ascending: true }],
limit: 1000,
where: `category = '${topCategory}'`
})
)
.score({
valueModel: '0.6 * click_through_rate + 0.4 * conversion_rate',
inputUserId: '$user_id',
inputInteractionsItemIds: '$interaction_item_ids'
})
.limit(20)
.build();
// Example usage with client
// const response = await client.rank({
// query,
// parameters: {
// user_id: 'user123'
// }
// });
SELECT *
FROM column_order(columns='_derived_popular_rank ASC', limit=1000)
WHERE category = '$top_category'
ORDER BY score(expression='0.6 * click_through_rate + 0.4 * conversion_rate', input_user_id='$user_id', input_interactions_item_ids='$interaction_item_ids')
LIMIT 20
{
"query": {
"type": "rank",
"from": "item",
"retrieve": [
{
"type": "column_order",
"columns": [{ "name": "_derived_popular_rank", "ascending": true }],
"limit": 1000,
"where": "category = '$parameters.top_category'"
}
],
"score": {
"value_model": "0.6 * click_through_rate + 0.4 * conversion_rate",
"input_user_id": "$parameters.user_id",
"input_interactions_item_ids": "$parameters.interaction_item_ids"
},
"limit": 20
},
"parameters": {
"top_category": "Electronics",
"user_id": "user123",
"interaction_item_ids": ["item789", "item012"]
}
}
This two-step approach (attribute ranking → filtered item ranking) enables you to build personalized navigation and category pages that adapt to each user's preferences. Value models allow you to fine-tune the ranking within each category using ensembles, item attributes, and other signals.