suggestions

This commit is contained in:
Gilles Crettenand
2015-06-12 14:48:27 +02:00
parent 80e139e018
commit b1eb07f3d4
2 changed files with 48 additions and 0 deletions

View File

@@ -146,6 +146,43 @@ class BookSearch
);
}
/**
* Return a list of suggested titles for the given text
* @param string $text
* @return array
*/
public function suggest($text) {
$this->query->setQuery($text);
$this->query->setStart(0);
$this->query->setRows(0);
$this->query->setParam('suggest', 'true');
$results = $this->client->query($this->query)->getResponse();
$text = mb_strtolower ($text, 'UTF-8');
$suggestions = array();
if(isset($results['suggest'])) {
foreach($results['suggest'] as $suggester) {
foreach($suggester[$text]['suggestions'] as $s) {
$s->term = strip_tags($s->term);
$pos = strpos(mb_strtolower($s->term, 'UTF-8'), $text);
if($pos !== false) {
// increase weight of proposition that have the words at the beginning
$s->weight += (int) ((1 - $pos / strlen($s->term)) * 100);
}
$suggestions[$s->term] = (array) $s;
}
}
}
usort($suggestions, function($a, $b) {
return $b['weight'] - $a['weight'];
});
return $suggestions;
}
/**
* Retrieve books from Solr based on their code (NoticeNr).