Popularity ranking is one of the most useful merchandising features for a WooCommerce store. Instead of sorting products alphabetically or by price, you can rank matching products by sales activity, recent demand, or a blended popularity score calculated in Vespa.
A practical implementation keeps product content in Vespa and continuously updates popularity attributes from WooCommerce order data. Search queries can then use a dedicated ranking profile without changing the product documents or application code that renders the results.
Define what “popular” means
WooCommerce provides several signals that can contribute to popularity:
- Total quantity sold
- Quantity sold during the last 7, 30, or 90 days
- Revenue during a selected period
- Number of orders containing the product
- Add-to-cart or product-view events
- Inventory availability and product status
For most stores, quantity sold over a recent period is a better default than lifetime sales. Lifetime totals permanently favor old products, even when demand has shifted. A common starting point is a weighted score using recent sales windows:
popularity_score = (sales_7d * 0.50) + (sales_30d * 0.30) + (sales_90d * 0.15) + (lifetime_sales * 0.05)
The exact weights should reflect the store’s buying cycle. A fashion store may need a strong 7-day signal, while a business selling replacement parts may need a longer period.
Store popularity attributes in Vespa
Popularity values are ranking inputs, so they should usually be stored as document attributes. Attributes are available to ranking expressions without requiring a full text match operation.
schema product {<br> document product {<br> field product_id type int {<br> indexing: attribute | summary<br> }<br> field title type string {<br> indexing: index | summary<br> }<br> field description type string {<br> indexing: index | summary<br> }<br> field category_ids type array<int> {<br> indexing: attribute | summary<br> }<br> field product_url type string {<br> indexing: summary<br> }<br> field in_stock type bool {<br> indexing: attribute | summary<br> }<br> field sales_7d type int {<br> indexing: attribute | summary<br> }<br> field sales_30d type int {<br> indexing: attribute | summary<br> }<br> field sales_90d type int {<br> indexing: attribute | summary<br> }<br> field lifetime_sales type int {<br> indexing: attribute | summary<br> }<br> field popularity_score type double {<br> indexing: attribute | summary<br> }<br> }<br><br> rank-profile popularity inherits default {<br> first-phase {<br> expression: if(attribute(in_stock), attribute(popularity_score), -1)<br> }<br> }<br>}
The in_stock check is optional. Many stores want out-of-stock products to remain searchable, but rank them below products that can be purchased immediately. If out-of-stock products should be excluded entirely, apply a query filter instead of using the ranking expression.
Calculate sales from WooCommerce orders
Use completed or processing orders according to the store’s fulfillment rules. Do not count cancelled, failed, or refunded orders as normal sales. A WooCommerce integration can retrieve orders through the REST API, consume order webhooks, or read from an internal reporting pipeline.
For an incremental integration, process each qualifying order and update the affected product counters. A refund should subtract the refunded quantity. The integration should be idempotent: recording the same order event twice must not increase the product total twice.
A typical order line contains a product identifier and quantity:
{<br> "order_id": 18452,<br> "status": "completed",<br> "line_items": [<br> { "product_id": 731, "quantity": 2 },<br> { "product_id": 912, "quantity": 1 }<br> ]<br>}
For variable products, decide whether popularity belongs to the variation, the parent product, or both. Ranking variations separately is useful when size or color has independent inventory. Ranking only the parent product is simpler when the storefront displays one product card with selectable variations.
Update the Vespa document
After recalculating the counters, send a partial update for the product rather than reindexing all product content. The exact HTTP request depends on the Vespa deployment and document namespace, but the update body can contain only the changed fields.
curl --request PUT \
--header "Content-Type: application/json" \
--data '{<br> "fields": {<br> "sales_7d": 18,<br> "sales_30d": 64,<br> "sales_90d": 191,<br> "lifetime_sales": 1240,<br> "popularity_score": 74.85<br> }<br> }' \
"https://vespa.example.com/document/v1/shop/product/docid/731"
For high-volume stores, keep the authoritative sales aggregates in a reporting database or stream processor and periodically publish the calculated values to Vespa. This prevents every order event from becoming a separate expensive aggregation operation.
Search with a popularity ranking profile
Popularity should normally be a tie-breaker or a second-stage signal rather than the only ranking factor. A shopper searching for a specific product should receive textually relevant products first, with popularity helping order similarly relevant matches.
GET /search/?yql=select * from sources product where userInput(@query)&query=running+shoes&ranking=popularity&hits=24
The simple profile above uses popularity as the first-phase expression. For a search experience that combines textual relevance and popularity, define a profile that preserves the default match score and adds a normalized popularity component:
rank-profile text_and_popularity inherits default {<br> first-phase {<br> expression: nativeRank * 0.80 + log(1 + attribute(popularity_score)) * 0.20<br> }<br>}
Use a normalization strategy that prevents very large lifetime sales values from overwhelming text relevance. The logarithm reduces the difference between products with 1,000 and 2,000 sales compared with a raw linear score.
Support a “Popular” catalog sort
A catalog page can expose popularity as an explicit sort option. In that case, the query can use a ranking profile that orders products primarily by the popularity attribute. Always include a deterministic secondary sort, such as product ID, so pagination remains stable when products have equal scores.
rank-profile catalog_popular {<br> first-phase {<br> expression: attribute(popularity_score)<br> }<br> second-phase {<br> expression: attribute(product_id)<br> }<br>}
For category pages, restrict the query to the requested WooCommerce category before ranking. Store category IDs as attributes or another filterable representation so category filtering does not depend on tokenized text matching.
GET /search/?yql=select * from sources product where attribute(category_ids contains 42) and in_stock=true&ranking=catalog_popular&hits=24
Prevent stale or misleading rankings
Popularity data should be refreshed frequently enough to match the store’s traffic. An hourly update may be adequate for a smaller catalog, while a large store with fast-moving inventory may need event-driven updates.
- Recompute rolling windows on a schedule so products naturally decay when sales stop.
- Exclude test orders, manually created administrative orders, and failed payments.
- Subtract refunded quantities where the business treats refunds as returned sales.
- Keep discontinued products searchable only if the merchandising team wants them visible.
- Apply inventory or purchasability rules separately from popularity whenever possible.
- Monitor the number of products with missing or zero popularity values after each import.
A rolling window cannot be maintained correctly by only incrementing counters. The integration must either retain order-line events until they age out of the window or periodically rebuild the 7-day, 30-day, and 90-day aggregates from order data.
Use popularity without hiding relevant products
Popularity is a merchandising signal, not a substitute for relevance. A highly purchased product should not rank above an exact title match merely because it has a larger sales count. For WooCommerce search, a blended ranking profile generally provides a better result than a popularity-only profile.
Test the ranking with real queries from the store’s search logs. Compare click-through rate, add-to-cart rate, conversion rate, and the percentage of searches that produce a purchase. Segment the results by category because the ideal balance between textual relevance and sales popularity can differ substantially between product groups.