- switch to new php_solr version (new getArrayResponse method)
- use EDisMax
This commit is contained in:
@@ -27,7 +27,10 @@ class BookSearch
|
||||
);
|
||||
|
||||
$this->client = new \SolrClient($options);
|
||||
$this->query = new \SolrQuery();
|
||||
$this->query = new \SolrDisMaxQuery();
|
||||
|
||||
// use the Extended DisMax Query parser
|
||||
$this->query->useEDisMaxQueryParser();
|
||||
|
||||
// most options like search fields, sorting, etc are already set
|
||||
// as default in the Solr config and thus should be set only on a
|
||||
@@ -98,7 +101,7 @@ class BookSearch
|
||||
$this->query->setRows($count);
|
||||
|
||||
try {
|
||||
$results = $this->client->query($this->query)->getResponse();
|
||||
$results = $this->client->query($this->query)->getArrayResponse();
|
||||
} catch(\SolrClientException $e) {
|
||||
throw new WebException ("SolrError", $e->getMessage(), -700);
|
||||
}
|
||||
@@ -106,7 +109,7 @@ class BookSearch
|
||||
$books = array();
|
||||
if(isset($results['response']['docs']) && is_array($results['response']['docs'])) {
|
||||
foreach($results['response']['docs'] as $r) {
|
||||
$books[''.$r->id] = (array) $r;
|
||||
$books[$r['id']] = $r;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,14 +127,14 @@ class BookSearch
|
||||
$spelling = array();
|
||||
if(isset($results['spellcheck']['suggestions'])) {
|
||||
foreach($results['spellcheck']['suggestions'] as $s) {
|
||||
$spelling[] = (array) $s;
|
||||
$spelling[] = $s;
|
||||
}
|
||||
}
|
||||
|
||||
$facets = array();
|
||||
if(isset($results['facet_counts']['facet_fields'])) {
|
||||
foreach($results['facet_counts']['facet_fields'] as $f => $d) {
|
||||
$facets[$f] = (array) $d;
|
||||
$facets[$f] = $d;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,14 +153,22 @@ class BookSearch
|
||||
* Return a list of suggested titles for the given text
|
||||
* @param string $text
|
||||
* @return array
|
||||
* @throws WebException
|
||||
*/
|
||||
public function suggest($text) {
|
||||
$this->query->setQuery($text);
|
||||
$this->query->setStart(0);
|
||||
$this->query->setRows(0);
|
||||
$this->query->setParam('suggest', 'true');
|
||||
$this->query->setParam('facet', 'false');
|
||||
$this->query->setParam('hl', 'false');
|
||||
$this->query->setParam('spellcheck', 'false');
|
||||
|
||||
$results = $this->client->query($this->query)->getResponse();
|
||||
try {
|
||||
$results = $this->client->query($this->query)->getArrayResponse();
|
||||
} catch(\SolrClientException $e) {
|
||||
throw new WebException ("SolrError", $e->getMessage(), -700);
|
||||
}
|
||||
|
||||
$text = mb_strtolower ($text, 'UTF-8');
|
||||
|
||||
@@ -165,14 +176,14 @@ class BookSearch
|
||||
if(isset($results['suggest'])) {
|
||||
foreach($results['suggest'] as $suggester) {
|
||||
foreach($suggester[$text]['suggestions'] as $s) {
|
||||
$s->term = strip_tags($s->term);
|
||||
$s['term'] = strip_tags($s['term']);
|
||||
|
||||
$pos = strpos(mb_strtolower($s->term, 'UTF-8'), $text);
|
||||
$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);
|
||||
$s['weight'] += (int) ((1 - $pos / strlen($s['term'])) * 100);
|
||||
}
|
||||
$suggestions[$s->term] = (array) $s;
|
||||
$suggestions[$s['term']] = (array) $s;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user