Commit c271e78a authored by Adrien Brault's avatar Adrien Brault

Fix suggester parser with duplicates

When asking for a suggestion of something like "blac blac", solr will return the blac suggestion 2 times.
parent 29b2be3a
......@@ -60,6 +60,7 @@ class ResponseParser extends ResponseParserAbstract implements ResponseParserInt
$query = $result->getQuery();
$suggestions = array();
$allSuggestions = array();
$collation = null;
if (isset($data['spellcheck']['suggestions']) && is_array($data['spellcheck']['suggestions'])) {
......@@ -74,12 +75,17 @@ class ResponseParser extends ResponseParserAbstract implements ResponseParserInt
if ($term == 'collation') {
$collation = $termData;
} else {
$suggestions[$term] = new $termClass(
$termData['numFound'],
$termData['startOffset'],
$termData['endOffset'],
$termData['suggestion']
);
if (!array_key_exists(0, $termData)) {
$termData = array($termData);
}
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
$data,
array(
'results' => $suggestions,
'all' => $allSuggestions,
'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
*/
protected $results;
/**
* Suggester flat results
*
* @var array
*/
protected $all;
/**
* Collation result
*
......@@ -119,6 +126,18 @@ class Result extends BaseResult implements \IteratorAggregate, \Countable
return $this->results;
}
/**
* Get flat results
*
* @return array
*/
public function getAll()
{
$this->parseResponse();
return $this->all;
}
/**
* Get results for a specific term
*
......
......@@ -65,6 +65,15 @@ class ResponseParserTest extends \PHPUnit_Framework_TestCase
'video',
)
),
'vid',
array(
'numFound' => 1,
'startOffset' => 6,
'endOffset' => 9,
'suggestion' => array(
'video',
)
),
'collation',
'disk video'
),
......@@ -88,8 +97,14 @@ class ResponseParserTest extends \PHPUnit_Framework_TestCase
'd' => new Term(2, 3, 7, array('disk', 'ddr')),
'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($allExpected, $result['all']);
$this->assertEquals('disk video', $result['collation']);
}
}
......@@ -45,6 +45,11 @@ class ResultTest extends \PHPUnit_Framework_TestCase
*/
protected $data;
/**
* @var array
*/
protected $allData;
/**
* @var string
*/
......@@ -56,8 +61,9 @@ class ResultTest extends \PHPUnit_Framework_TestCase
'term1' => 'data1',
'term2' => 'data2',
);
$this->allData = array_values($this->data);
$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()
......@@ -81,6 +87,11 @@ class ResultTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($this->data, $this->result->getResults());
}
public function testGetAll()
{
$this->assertEquals($this->allData, $this->result->getAll());
}
public function testGetTerm()
{
$this->assertEquals($this->data['term1'], $this->result->getTerm('term1'));
......@@ -116,9 +127,10 @@ class SuggesterDummy extends Result
{
protected $parsed = true;
public function __construct($results, $collation)
public function __construct($results, $all, $collation)
{
$this->results = $results;
$this->all = $all;
$this->collation = $collation;
$this->status = 1;
$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