Skip to content

Commit

Permalink
Improved "did you mean"
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroenvermeulen committed Jul 23, 2014
1 parent 3697ee2 commit b176ffb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
8 changes: 4 additions & 4 deletions app/code/community/JeroenVermeulen/Solarium/Model/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,15 +535,15 @@ function search(
$doAutoCorrect = ( 1 == $try && $this->getConf( 'results/autocorrect', $storeId ) );
$doDidYouMean = ( 1 == $try && $this->getConf( 'results/did_you_mean', $storeId ) );
if ($doAutoCorrect || $doDidYouMean) {
$numSuggestions = 1 + $this->getConf( 'results/did_you_mean_suggestions', $storeId );
$spellCheck = $query->getSpellcheck();
$spellCheck->setQuery( $queryString );
$spellCheck->setCollate( true );
$spellCheck->setCount( 1 );
$spellCheck->setCount( $numSuggestions );
$spellCheck->setMaxCollations( 1 );
$spellCheck->setExtendedResults( true );
$numSuggestions = 1 + $this->getConf( 'results/did_you_mean_suggestions', $storeId );
$spellCheck->setOnlyMorePopular( true );
$query->addParam( 'spellcheck.maxResultsForSuggest', $numSuggestions );
$query->addParam( 'spellcheck.count', $numSuggestions );
// You need Solr >= 4.0 for this to improve spell correct results.
$query->addParam( 'spellcheck.alternativeTermCount', 1 );
}
Expand All @@ -568,7 +568,7 @@ function search(
$spellCheckResult = $solrResultSet->getSpellcheck();
if ($spellCheckResult && !$spellCheckResult->getCorrectlySpelled()) {
$suggestions = $spellCheckResult->getSuggestions();
foreach ($suggestions as $suggestion) {
foreach ( $suggestions as $suggestion ) {
foreach ($suggestion->getWords() as $word) {
if ($word[ 'freq' ] > $suggestion->getOriginalFrequency()) {
$suggest[ $word[ 'word' ] ] = $word[ 'freq' ];
Expand Down
28 changes: 19 additions & 9 deletions app/code/community/JeroenVermeulen/Solarium/Model/SearchResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,15 @@
* @method JeroenVermeulen_Solarium_Model_SearchResult setSuggestions($data)
* @method array getSuggestions()
*/

/**
* Most of the work is done by Magento's Mage_Core_Model_Abstract.
*
* Class JeroenVermeulen_Solarium_Model_SearchResult
*/
class JeroenVermeulen_Solarium_Model_SearchResult extends Mage_Core_Model_Abstract
{
// Most of the work is done by Magento's Mage_Core_Model_Abstract.
protected $betterSuggestions = null;

/**
* @return bool - True if autocorrect changed the the string to search for.
Expand Down Expand Up @@ -79,16 +85,20 @@ public function getResultCount() {
* @return array - with key = term, value = result count.
*/
public function getBetterSuggestions() {
$betterSuggestions = array();
$suggestions = $this->getSuggestions();
$resultCount = $this->getResultCount();
if ( is_array($suggestions) ) {
foreach ( $suggestions as $term => $termResultCount ) {
if ( $termResultCount > $resultCount ) {
$betterSuggestions[ $term ] = $termResultCount;
if ( is_null($this->betterSuggestions) ) {
$this->betterSuggestions = array();
$suggestions = $this->getSuggestions();
$resultCount = $this->getResultCount();
$engine = Mage::getSingleton( 'jeroenvermeulen_solarium/engine' );
if ( is_array($suggestions) ) {
foreach ( $suggestions as $term => $freq ) {
$termResult = $engine->search( $this->getStoreId(), $term, JeroenVermeulen_Solarium_Model_Engine::SEARCH_TYPE_LITERAL, 2 );
if ( $termResult->getResultCount() > $resultCount ) {
$this->betterSuggestions[ $term ] = $termResult->getResultCount();
}
}
}
}
return $betterSuggestions;
return $this->betterSuggestions;
}
}

0 comments on commit b176ffb

Please sign in to comment.