Skip to content

Commit

Permalink
Merge pull request #1 from OxCom/develop
Browse files Browse the repository at this point in the history
Add weight based on ratings and votes count
  • Loading branch information
OxCom authored Oct 5, 2018
2 parents a70692f + 2a095b5 commit 4181b39
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions Model/ProductRepositoryModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,20 +252,46 @@ public function getRatedProducts(ProductSearchCriteriaInterface $searchCriteria)
$id = $rating->getData('rating_id');
$joinCond['rating_id'] = ['eq' => $id];

// $ra - alias for rating table
$ra = 'r';
/**
* To create more reliable order based on number of votes and quality of votes we have to introduce product
* weight, because product with 1 vote and 5 stars cannot be at first position. Please see link to get more
* information about this.
*
* At current realisation
* q - 'vote_count' column
* P = 0.5
* Q = 5 - becase we have 5 star system
* p - 'vote_value_sum' / 'vote_count'
*
* @link https://math.stackexchange.com/questions/942738/algorithm-to-calculate-rating-based-on-multiple-reviews-using-both-review-score
*/
$q = "`{$ra}`.vote_count";
$P = 0.5;
$Q = 5;
$p = "(`{$ra}`.vote_value_sum / {$q})";
$weight = "ROUND(({$P} * {$p} + 10 * (1 - {$P}) * (1 - EXP( -({$q}) / {$Q} ))), 2)";
$this->productCollection
->getSelect()
->columns([
'score' => new \Zend_Db_Expr($weight)
]);

$this->productCollection->joinTable(
['r' => $this->ratingAggregated->getMainTable()],
[$ra => $this->ratingAggregated->getMainTable()],
'entity_pk_value = entity_id',
[
'rating_id' => 'rating_id',
'percent' => 'percent',
'vote_count' => 'vote_count',
'vote_value_sum' => 'vote_value_sum',
],
$joinCond
);

$this->productCollection
->addOrder('vote_value_sum', Collection::SORT_ORDER_DESC)
->addOrder('percent', Collection::SORT_ORDER_DESC);
->getSelect()
->order('score ' . Collection::SORT_ORDER_DESC);

$result = $this->processProductCollection($searchCriteria);

Expand Down

0 comments on commit 4181b39

Please sign in to comment.