- 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->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
|
// most options like search fields, sorting, etc are already set
|
||||||
// as default in the Solr config and thus should be set only on a
|
// as default in the Solr config and thus should be set only on a
|
||||||
@@ -98,7 +101,7 @@ class BookSearch
|
|||||||
$this->query->setRows($count);
|
$this->query->setRows($count);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$results = $this->client->query($this->query)->getResponse();
|
$results = $this->client->query($this->query)->getArrayResponse();
|
||||||
} catch(\SolrClientException $e) {
|
} catch(\SolrClientException $e) {
|
||||||
throw new WebException ("SolrError", $e->getMessage(), -700);
|
throw new WebException ("SolrError", $e->getMessage(), -700);
|
||||||
}
|
}
|
||||||
@@ -106,7 +109,7 @@ class BookSearch
|
|||||||
$books = array();
|
$books = array();
|
||||||
if(isset($results['response']['docs']) && is_array($results['response']['docs'])) {
|
if(isset($results['response']['docs']) && is_array($results['response']['docs'])) {
|
||||||
foreach($results['response']['docs'] as $r) {
|
foreach($results['response']['docs'] as $r) {
|
||||||
$books[''.$r->id] = (array) $r;
|
$books[$r['id']] = $r;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,14 +127,14 @@ class BookSearch
|
|||||||
$spelling = array();
|
$spelling = array();
|
||||||
if(isset($results['spellcheck']['suggestions'])) {
|
if(isset($results['spellcheck']['suggestions'])) {
|
||||||
foreach($results['spellcheck']['suggestions'] as $s) {
|
foreach($results['spellcheck']['suggestions'] as $s) {
|
||||||
$spelling[] = (array) $s;
|
$spelling[] = $s;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$facets = array();
|
$facets = array();
|
||||||
if(isset($results['facet_counts']['facet_fields'])) {
|
if(isset($results['facet_counts']['facet_fields'])) {
|
||||||
foreach($results['facet_counts']['facet_fields'] as $f => $d) {
|
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
|
* Return a list of suggested titles for the given text
|
||||||
* @param string $text
|
* @param string $text
|
||||||
* @return array
|
* @return array
|
||||||
|
* @throws WebException
|
||||||
*/
|
*/
|
||||||
public function suggest($text) {
|
public function suggest($text) {
|
||||||
$this->query->setQuery($text);
|
$this->query->setQuery($text);
|
||||||
$this->query->setStart(0);
|
$this->query->setStart(0);
|
||||||
$this->query->setRows(0);
|
$this->query->setRows(0);
|
||||||
$this->query->setParam('suggest', 'true');
|
$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');
|
$text = mb_strtolower ($text, 'UTF-8');
|
||||||
|
|
||||||
@@ -165,14 +176,14 @@ class BookSearch
|
|||||||
if(isset($results['suggest'])) {
|
if(isset($results['suggest'])) {
|
||||||
foreach($results['suggest'] as $suggester) {
|
foreach($results['suggest'] as $suggester) {
|
||||||
foreach($suggester[$text]['suggestions'] as $s) {
|
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) {
|
if($pos !== false) {
|
||||||
// increase weight of proposition that have the words at the beginning
|
// 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