Книга: Apache Solr Search Patterns
Назад: Handling variations in the product
Дальше: Problems and solutions of flash sale searches

Sorting

In addition to search, we also need to provide sorting options on an e-commerce website. By default, the search results are ordered by the relevancy score that has been computed on the basis of the boosting we have provided. However, there would still be requirements to sort the search results by other factors such as price, discount, or latest products. Sorting by already known fields is simple. All we need to do is add the sorting criteria behind our Solr search query:

sort=price asc

Alternatively, add the following sorting code:

sort=price desc

The intelligence that needs to be built into the system here is sorting by relevance after price. This is to take care of scenarios where the ordered results may contain irrelevant results in the top when, say, sorted by price in the ascending order. Therefore, we would be modifying our query to include the score while sorting:

sort=price asc,score desc

Now, the top results would be better. Another intelligence that needs to be taken care of is to exclude or de-boost results that are "out of stock". For this, we will have to include inventory information in our index. We can say that we include the inventory information as the availability of a particular product, so a new Boolean field named 'instock' can be added to the index, which contains the availability of the product:

<field name="instock" type="boolean" indexed="true" stored="true"/>

To exclude the products that are out of stock, we will need to simply add a filter query:

fq=instock:true

This filter query will ensure that the results that we get from our Solr index will contain only those products that are in stock. However, what if the search result contains only "out of stock" products? In that case, the search will return zero results. To fix this, we need to display out of stock products after the products that are in stock. For this, we can run multiple queries. The first query runs with the above filter query such that the results contain only instock products. Then, we run the same query again after replacing the filter query parameter to get products that are "out of stock":

fq=instock:false

De-boosting of "out of stock" products is another way of achieving the same result, but with a single query. Let us understand this with the help of an example. Run the following query on the Solr index. Notice the sort and boost parameters passed in the query:

q=iphone&qf=text%20cat^2%20name^2%20brand^2%20clothes_type^2%20clothes_color^2%20clothes_occassion^2&pf=text%20cat^3%20name^3%20brand^3%20clothes_type^3%20clothes_color^3%20clothes_occassion^3&fl=name,brand,price,instock,score&defType=edismax&facet=true&facet.mincount=1&facet.field=clothes_gender&facet.field=clothes_type&facet.field=clothes_size&facet.field=clothes_color&facet.field=brand&facet.field=mobile_os&facet.field=mobile_screen_size&facet.field=laptop_processor&facet.field=laptop_memory&facet.field=laptop_hard_disk&sort=score%20desc,price%20asc&boost=if(instock,10,0.1) 

We want the cheapest iphone on top, but also want to de-boost all "out of stock" iPhones. The boost parameter over here provides a multiplicative factor to the relevance score. It states that if instock is true, multiply the relevance score by 10. Else, multiply the relevance score by 0.1. Therefore, products that are in stock are boosted, while products that are "not in stock" are de-boosted. Also, here instead of sorting by price directly, we have first sorted by score, and then by price. The results are very impressive:

Sorting

Relevance scoring – results sorted by availability

We can see that the cheapest of all iphones comes at last because it is out of stock. Remaining iphones are sorted by price in the ascending order.

Назад: Handling variations in the product
Дальше: Problems and solutions of flash sale searches

Solr
Testing
dosare
121