Commit 96a273ab authored by Bas de Nooijer's avatar Bas de Nooijer

Merge pull request #248 from adrienbrault/double-suggestion

Fix suggester parser with duplicates
parents 6cdc4983 c271e78a
...@@ -60,6 +60,7 @@ class ResponseParser extends ResponseParserAbstract implements ResponseParserInt ...@@ -60,6 +60,7 @@ class ResponseParser extends ResponseParserAbstract implements ResponseParserInt
$query = $result->getQuery(); $query = $result->getQuery();
$suggestions = array(); $suggestions = array();
$allSuggestions = array();
$collation = null; $collation = null;
if (isset($data['spellcheck']['suggestions']) && is_array($data['spellcheck']['suggestions'])) { if (isset($data['spellcheck']['suggestions']) && is_array($data['spellcheck']['suggestions'])) {
...@@ -74,12 +75,17 @@ class ResponseParser extends ResponseParserAbstract implements ResponseParserInt ...@@ -74,12 +75,17 @@ class ResponseParser extends ResponseParserAbstract implements ResponseParserInt
if ($term == 'collation') { if ($term == 'collation') {
$collation = $termData; $collation = $termData;
} else { } else {
$suggestions[$term] = new $termClass( if (!array_key_exists(0, $termData)) {
$termData['numFound'], $termData = array($termData);
$termData['startOffset'], }
$termData['endOffset'],
$termData['suggestion'] foreach ($termData as $currentTermData) {
); $allSuggestions[] = $this->createTerm($termClass, $currentTermData);
if (!array_key_exists($term, $suggestions)) {
$suggestions[$term] = $this->createTerm($termClass, $currentTermData);
}
}
} }
} }
} }
...@@ -88,8 +94,19 @@ class ResponseParser extends ResponseParserAbstract implements ResponseParserInt ...@@ -88,8 +94,19 @@ class ResponseParser extends ResponseParserAbstract implements ResponseParserInt
$data, $data,
array( array(
'results' => $suggestions, 'results' => $suggestions,
'all' => $allSuggestions,
'collation' => $collation, 'collation' => $collation,
) )
); );
} }
private function createTerm($termClass, array $termData)
{
return new $termClass(
$termData['numFound'],
$termData['startOffset'],
$termData['endOffset'],
$termData['suggestion']
);
}
} }
...@@ -69,6 +69,13 @@ class Result extends BaseResult implements \IteratorAggregate, \Countable ...@@ -69,6 +69,13 @@ class Result extends BaseResult implements \IteratorAggregate, \Countable
*/ */
protected $results; protected $results;
/**
* Suggester flat results
*
* @var array
*/
protected $all;
/** /**
* Collation result * Collation result
* *
...@@ -119,6 +126,18 @@ class Result extends BaseResult implements \IteratorAggregate, \Countable ...@@ -119,6 +126,18 @@ class Result extends BaseResult implements \IteratorAggregate, \Countable
return $this->results; return $this->results;
} }
/**
* Get flat results
*
* @return array
*/
public function getAll()
{
$this->parseResponse();
return $this->all;
}
/** /**
* Get results for a specific term * Get results for a specific term
* *
......
...@@ -65,6 +65,15 @@ class ResponseParserTest extends \PHPUnit_Framework_TestCase ...@@ -65,6 +65,15 @@ class ResponseParserTest extends \PHPUnit_Framework_TestCase
'video', 'video',
) )
), ),
'vid',
array(
'numFound' => 1,
'startOffset' => 6,
'endOffset' => 9,
'suggestion' => array(
'video',
)
),
'collation', 'collation',
'disk video' 'disk video'
), ),
...@@ -88,8 +97,14 @@ class ResponseParserTest extends \PHPUnit_Framework_TestCase ...@@ -88,8 +97,14 @@ class ResponseParserTest extends \PHPUnit_Framework_TestCase
'd' => new Term(2, 3, 7, array('disk', 'ddr')), 'd' => new Term(2, 3, 7, array('disk', 'ddr')),
'vid' => new Term(1, 2, 5, array('video')) 'vid' => new Term(1, 2, 5, array('video'))
); );
$allExpected = array(
new Term(2, 3, 7, array('disk', 'ddr')),
new Term(1, 2, 5, array('video')),
new Term(1, 6, 9, array('video')),
);
$this->assertEquals($expected, $result['results']); $this->assertEquals($expected, $result['results']);
$this->assertEquals($allExpected, $result['all']);
$this->assertEquals('disk video', $result['collation']); $this->assertEquals('disk video', $result['collation']);
} }
} }
...@@ -45,6 +45,11 @@ class ResultTest extends \PHPUnit_Framework_TestCase ...@@ -45,6 +45,11 @@ class ResultTest extends \PHPUnit_Framework_TestCase
*/ */
protected $data; protected $data;
/**
* @var array
*/
protected $allData;
/** /**
* @var string * @var string
*/ */
...@@ -56,8 +61,9 @@ class ResultTest extends \PHPUnit_Framework_TestCase ...@@ -56,8 +61,9 @@ class ResultTest extends \PHPUnit_Framework_TestCase
'term1' => 'data1', 'term1' => 'data1',
'term2' => 'data2', 'term2' => 'data2',
); );
$this->allData = array_values($this->data);
$this->collation = 'collation result'; $this->collation = 'collation result';
$this->result = new SuggesterDummy($this->data, $this->collation); $this->result = new SuggesterDummy($this->data, $this->allData, $this->collation);
} }
public function testGetStatus() public function testGetStatus()
...@@ -81,6 +87,11 @@ class ResultTest extends \PHPUnit_Framework_TestCase ...@@ -81,6 +87,11 @@ class ResultTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($this->data, $this->result->getResults()); $this->assertEquals($this->data, $this->result->getResults());
} }
public function testGetAll()
{
$this->assertEquals($this->allData, $this->result->getAll());
}
public function testGetTerm() public function testGetTerm()
{ {
$this->assertEquals($this->data['term1'], $this->result->getTerm('term1')); $this->assertEquals($this->data['term1'], $this->result->getTerm('term1'));
...@@ -116,9 +127,10 @@ class SuggesterDummy extends Result ...@@ -116,9 +127,10 @@ class SuggesterDummy extends Result
{ {
protected $parsed = true; protected $parsed = true;
public function __construct($results, $collation) public function __construct($results, $all, $collation)
{ {
$this->results = $results; $this->results = $results;
$this->all = $all;
$this->collation = $collation; $this->collation = $collation;
$this->status = 1; $this->status = 1;
$this->queryTime = 12; $this->queryTime = 12;
......
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