Advanced sorting using Sphinx Search Expressions

Advanced sorting using Sphinx Search Expressions

Sphinx Search allows use of custom expressions, where we can have internal attributes, conditions, arithmetic operators, and functions to calculate custom values for each document/record. It then allows sorting these documents/results on the calculated value by using SPH_SORT_EXPR sort mode.

Here we will demonstrate how expressions can be used to implement flexible sorting to accommodate complex business rules for sorting.

The Problem

In reality we don’t always want to sort search results on pure relevance or some fixed criteria. This situation is very common in eCommerce, where attributes like price, popularity, origin, page views etc are also important factors in search results.

Imagine an eCommerce site which wants to promote best selling products and put these before others in search results, but don’t want to ignore relevancy. To make sure that the results are still relevant, it only want to give 20% advantage to products which are best sellers. Be sure that best seller here is not just an on/off bit, but it is actual number of times the product has been sold. This number can vary from 0 to 1000s for same search terms. To achieve this we can’t just sort on relevance and order count, because it will sort on relevance first and than order count.

The Solution

Here the Sphinx Search expressions can help. Assume we have a “int” attribute in our index “sale_count” for each product and it is the number of orders placed for this product.  The expression to sort results 80% on relevance and 20% on sales_count the expression will be like

Note that {max_weight} and {max_sale_count} are place holders, these are not variables of Sphinx Search. “weight” is internal variable and “sale_count” is available in index as an attribute.

It seems pretty simple, but calculating max_weight and max_sale_count is the real trick here. “max_weight” is the maximum weight assigned to product in results for a term. The weight depends on matching mode, ranker, field weights, number of words in query etc, and its calculated internally by Sphinx. If we go for calculating max_weight manually by using formulas like “maxPossibleWeight = wordCount*totalWeight*1000+999”, it becomes too large in ranger and not accurate as well. It practically reduces the weight/{max_weight} factor.

So the best way of getting max_weight which is applicable to given search term, is to do an extra Sphinx query and sort it purely on relevance. Then get the first match of the query and use its weight value for max_weight. This value will be 100% accurate and will make the expression accurate as well.

Same is the case for max_sale_count. We need to find the product with maximum number of sale count which is matched for this particular search term. Once we can get value of max_sale_count and max_weight by doing extra queries use these these values in expression.

Now sorting on expression will give result which are sorted 80% on relevance and 20% on its sale count. See the example php code and results below.

The matches array will look like array below. Here @expr contains final value of expression for each result and results are sorted on custom expression values.

You can notice that the many products have exact same “weight” which is relevance but product with high sale count is placed before the products with lower sale count.

Let me know your thoughts and comments 🙂

By | 2017-02-12T18:33:13+00:00 May 4th, 2013|4 Comments

About the Author:

Waseem is our consultant for Sphinx Search, Elastic and Solr. He is lamp stack backend developer and has 14 years of experience of development. His areas of interest (other than search) are NoSQL and BigData tools.

4 Comments

  1. Craig April 2, 2014 at 7:21 pm - Reply

    Thanks for this example. I have been trying to get it to work. One thing… In this line, what is “page_views.” I don’t see it mentioned anywhere else.

    $expression = ‘((@weight/’ . $maxWeight . ‘) * 80) + ((page_views/’ . $maxSaleCount . ‘) * 20)’;

    Thanks!

    Craig

    • sphinx April 4, 2014 at 2:50 am - Reply

      Thanks Craig for finding this mistake. The “page_views” should have been sale_count so the expression will look like
      $expression = ‘((@weight/’ . $maxWeight . ‘) * 80) + ((sale_count/’ . $maxSaleCount . ‘) * 20)’;
      So the 80% is relevance calculated by Sphinx and 20% is weight for sale_count of that product.

  2. Craig April 25, 2014 at 5:22 am - Reply

    Awesome!! Thanks so much for taking the time to answer and correct this. It is correctly working on my site now which is awesome… If I need any further help, I will contact you for your services. Thanks for freely offering this nugget of wisdom. Well done.

    • Waseem Asif April 27, 2014 at 4:38 am - Reply

      You are welcome.

Leave A Comment