diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/Solr/Response/Json/RecordCollection.php b/module/VuFindSearch/src/VuFindSearch/Backend/Solr/Response/Json/RecordCollection.php index d9a6593ee0e467901ea7a2337b3d7fc75e9b59ca..647729e3ea9235679ee4e2c15aa24ba0fee58364 100644 --- a/module/VuFindSearch/src/VuFindSearch/Backend/Solr/Response/Json/RecordCollection.php +++ b/module/VuFindSearch/src/VuFindSearch/Backend/Solr/Response/Json/RecordCollection.php @@ -99,14 +99,9 @@ class RecordCollection extends AbstractRecordCollection public function getSpellcheck() { if (!$this->spellcheck) { - $params = isset($this->response['responseHeader']['params']) - ? $this->response['responseHeader']['params'] : array(); - $sq = isset($params['spellcheck.q']) - ? $params['spellcheck.q'] - : (isset($params['q']) ? $params['q'] : ''); - $sugg = isset($this->response['spellcheck']['suggestions']) - ? $this->response['spellcheck']['suggestions'] : array(); - $this->spellcheck = new Spellcheck($sugg, $sq); + $this->spellcheck = new Spellcheck( + $this->getRawSpellcheckSuggestions(), $this->getSpellcheckQuery() + ); } return $this->spellcheck; } @@ -155,4 +150,39 @@ class RecordCollection extends AbstractRecordCollection return isset($this->response['highlighting']) ? $this->response['highlighting'] : array(); } + + /** + * Get raw Solr input parameters from the response. + * + * @return array + */ + protected function getSolrParameters() + { + return isset($this->response['responseHeader']['params']) + ? $this->response['responseHeader']['params'] : array(); + } + + /** + * Extract the best matching Spellcheck query from the raw Solr input parameters. + * + * @return string + */ + protected function getSpellcheckQuery() + { + $params = $this->getSolrParameters(); + return isset($params['spellcheck.q']) + ? $params['spellcheck.q'] + : (isset($params['q']) ? $params['q'] : ''); + } + + /** + * Get raw Solr Spellcheck suggestions. + * + * @return array + */ + protected function getRawSpellcheckSuggestions() + { + return isset($this->response['spellcheck']['suggestions']) + ? $this->response['spellcheck']['suggestions'] : array(); + } } \ No newline at end of file diff --git a/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/Solr/Response/Json/RecordCollectionTest.php b/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/Solr/Response/Json/RecordCollectionTest.php index 7c0357d6d78330702d1773ca0a53dc6eb9f0da0e..2f5483a2fcb840e8543f338b87ca0d764eb138c7 100644 --- a/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/Solr/Response/Json/RecordCollectionTest.php +++ b/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/Solr/Response/Json/RecordCollectionTest.php @@ -87,6 +87,66 @@ class RecordCollectionTest extends PHPUnit_Framework_TestCase $this->assertEquals(6, $coll->key()); } + /** + * Test spelling query retrieval. + * + * @return void + */ + public function testSpellingQuery() + { + $input = array( + 'responseHeader' => array( + 'params' => array( + 'spellcheck.q' => 'foo', + 'q' => 'bar', + ) + ) + ); + $coll = new RecordCollection($input); + $this->assertEquals('foo', $coll->getSpellcheck()->getQuery()); + unset($input['responseHeader']['params']['spellcheck.q']); + $coll = new RecordCollection($input); + $this->assertEquals('bar', $coll->getSpellcheck()->getQuery()); + } + + /** + * Test spelling suggestion retrieval. + * + * @return void + */ + public function testSpellingSuggestions() + { + $input = array( + 'spellcheck' => array( + 'suggestions' => array( + array( + 'frunkensteen', + array( + 'numFound' => 6, + 'startOffset' => 0, + 'endOffset' => 12, + 'origFreq' => 0, + 'suggestion' => array( + array( + 'word' => 'frankenstein', + 'freq' => 218, + ), + array( + 'word' => 'funkenstein', + 'freq' => 10, + ), + ), + ), + ), + array('correctlySpelled', false), + ) + ) + ); + $coll = new RecordCollection($input); + $spell = $coll->getSpellcheck(); + $this->assertEquals(1, count($spell)); + } + /** * Test the replace method. *