Commit 43dae657 authored by Bas de Nooijer's avatar Bas de Nooijer

Fixed examples and a bug in spellcheck response parsing, added a unittest

parent 5d9b6d5e
...@@ -3,7 +3,7 @@ require(__DIR__.'/init.php'); ...@@ -3,7 +3,7 @@ require(__DIR__.'/init.php');
// this very simple plugin shows a timing for each event and display some request debug info // this very simple plugin shows a timing for each event and display some request debug info
class basicDebug extends Solarium\Core\Plugin class basicDebug extends Solarium\Core\Plugin\Plugin
{ {
protected $start; protected $start;
......
<?php <?php
require(__DIR__.'/init.php'); require(__DIR__.'/init.php');
use Solarium\Client; use Solarium\Client;
use Solarium\Core\Plugin; use Solarium\Core\Plugin\Plugin;
use Solarium\QueryType\Select\Query\Query as Select; use Solarium\QueryType\Select\Query\Query as Select;
// This is a custom query class that could have some customized logic // This is a custom query class that could have some customized logic
......
...@@ -79,18 +79,13 @@ class Spellcheck extends ResponseParserAbstract implements ComponentParserInterf ...@@ -79,18 +79,13 @@ class Spellcheck extends ResponseParserAbstract implements ComponentParserInterf
$collations = array(); $collations = array();
foreach ($spellcheckResults as $key => $value) { foreach ($spellcheckResults as $key => $value) {
switch ($key) { switch ($key) {
case 'correctlySpelled': case 'correctlySpelled':
$correctlySpelled = $value; $correctlySpelled = $value;
break; break;
case 'collation': case 'collation':
if (!array_key_exists('collation', $value)) { $collations = $this->parseCollation($query, $value);
foreach ($value as $collationValue) {
$collations[] = $this->parseCollation($query, $collationValue);
}
} else {
$collations[] = $this->parseCollation($query, $value);
}
break; break;
default: default:
$suggestions[] = $this->parseSuggestion($key, $value); $suggestions[] = $this->parseSuggestion($key, $value);
...@@ -108,51 +103,69 @@ class Spellcheck extends ResponseParserAbstract implements ComponentParserInterf ...@@ -108,51 +103,69 @@ class Spellcheck extends ResponseParserAbstract implements ComponentParserInterf
* *
* @param Query $queryObject * @param Query $queryObject
* @param array $values * @param array $values
* @return Collation * @return Collation[]
*/ */
protected function parseCollation($queryObject, $values) protected function parseCollation($queryObject, $values)
{ {
$collations = array();
if (is_string($values)) { if (is_string($values)) {
return new Collation($values, null, array());
} else { $collations[] = new Collation($values, null, array());
$query = null; } else if (is_array($values) && isset($values[0]) && is_string($values[0]) && $values[0] !== 'collationQuery') {
$hits = null;
$correctionResult = null;
if ($queryObject->getResponseWriter() == $queryObject::WT_JSON) { foreach ($values as $value) {
$values = $this->convertToKeyValueArray($values); $collations[] = new Collation($value, null, array());
} }
foreach ($values as $key => $value) { } else {
switch ($key) {
case 'collationQuery': if ($queryObject->getResponseWriter() == $queryObject::WT_JSON) {
$query = $value; if(is_array(current($values))){
break; foreach($values as $key => $value) {
case 'hits': $values[$key] = $this->convertToKeyValueArray($value);
$hits = $value; }
break; } else {
case 'misspellingsAndCorrections': $values = array($this->convertToKeyValueArray($values));
$correctionResult = $value;
break;
} }
} }
$corrections = array(); foreach($values as $collationValue) {
if ($correctionResult !== null) { $query = null;
$hits = null;
if ($queryObject->getResponseWriter() == $queryObject::WT_JSON) { $correctionResult = null;
$correctionResult = $this->convertToKeyValueArray($correctionResult);
foreach ($collationValue as $key => $value) {
switch ($key) {
case 'collationQuery':
$query = $value;
break;
case 'hits':
$hits = $value;
break;
case 'misspellingsAndCorrections':
$correctionResult = $value;
break;
}
} }
foreach ($correctionResult as $input => $correction) { $corrections = array();
$corrections[$input] = $correction; if ($correctionResult !== null) {
if ($queryObject->getResponseWriter() == $queryObject::WT_JSON) {
$correctionResult = $this->convertToKeyValueArray($correctionResult);
}
foreach ($correctionResult as $input => $correction) {
$corrections[$input] = $correction;
}
} }
}
return new Collation($query, $hits, $corrections); $collations[] = new Collation($query, $hits, $corrections);
}
} }
return $collations;
} }
/** /**
......
...@@ -170,6 +170,47 @@ class SpellcheckTest extends \PHPUnit_Framework_TestCase ...@@ -170,6 +170,47 @@ class SpellcheckTest extends \PHPUnit_Framework_TestCase
} }
public function testParseSingleCollation()
{
$data = array(
'spellcheck' => array(
'suggestions' => array(
0 => 'delll',
1 => array (
'numFound' => 1,
'startOffset' => 0,
'endOffset' => 5,
'origFreq' => 0,
'suggestion' => array (
0 => 'dell',
),
),
2 => 'ultrashar',
3 => array (
'numFound' => 1,
'startOffset' => 6,
'endOffset' => 15,
'origFreq' => 0,
'suggestion' => array (
0 => array (
'word' => 'ultrasharp',
'freq' => 1
),
),
),
4 => 'correctlySpelled',
5 => false,
6 => 'collation',
7 => 'dell ultrasharp',
)
)
);
$result = $this->parser->parse($this->query, null, $data);
$collations = $result->getCollations();
$this->assertEquals('dell ultrasharp', $collations[0]->getQuery());
}
public function testParseNoData() public function testParseNoData()
{ {
$result = $this->parser->parse($this->query, null, array()); $result = $this->parser->parse($this->query, null, array());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment