Commit fde56db5 authored by Markus Kalkbrenner's avatar Markus Kalkbrenner Committed by GitHub

finished first version of the Terms component and cleaned up some left overs...

finished first version of the Terms component and cleaned up some left overs from the component refactoring (#556)
parent 45f62930
......@@ -37,7 +37,7 @@ class DisMax extends AbstractComponent
*/
public function getType()
{
return SelectQuery::COMPONENT_DISMAX;
return ComponentAwareQueryInterface::COMPONENT_DISMAX;
}
/**
......
......@@ -42,7 +42,7 @@ class DistributedSearch extends AbstractComponent
*/
public function getType()
{
return SelectQuery::COMPONENT_DISTRIBUTEDSEARCH;
return ComponentAwareQueryInterface::COMPONENT_DISTRIBUTEDSEARCH;
}
/**
......
......@@ -28,7 +28,7 @@ class EdisMax extends DisMax
*/
public function getType()
{
return SelectQuery::COMPONENT_EDISMAX;
return ComponentAwareQueryInterface::COMPONENT_EDISMAX;
}
/**
......
......@@ -82,7 +82,7 @@ class FacetSet extends AbstractComponent
*/
public function getType()
{
return SelectQuery::COMPONENT_FACETSET;
return ComponentAwareQueryInterface::COMPONENT_FACETSET;
}
/**
......
......@@ -33,7 +33,7 @@ class Grouping extends AbstractComponent
*
* @var string
*/
protected $type = SelectQuery::COMPONENT_GROUPING;
protected $type = ComponentAwareQueryInterface::COMPONENT_GROUPING;
/**
* Default options.
......@@ -66,7 +66,7 @@ class Grouping extends AbstractComponent
*/
public function getType()
{
return SelectQuery::COMPONENT_GROUPING;
return ComponentAwareQueryInterface::COMPONENT_GROUPING;
}
/**
......
......@@ -2,7 +2,7 @@
namespace Solarium\Component\Highlighting;
use Solarium\QueryType\Select\Query\Query as SelectQuery;
use Solarium\Component\ComponentAwareQueryInterface;
use Solarium\Component\AbstractComponent;
use Solarium\Component\RequestBuilder\Highlighting as RequestBuilder;
use Solarium\Component\ResponseParser\Highlighting as ResponseParser;
......@@ -59,7 +59,7 @@ class Highlighting extends AbstractComponent
*/
public function getType()
{
return SelectQuery::COMPONENT_HIGHLIGHTING;
return ComponentAwareQueryInterface::COMPONENT_HIGHLIGHTING;
}
/**
......
<?php
namespace Solarium\Component\QueryTraits;
use Solarium\Component\ComponentAwareQueryInterface;
/**
* Trait query types supporting components.
*/
trait TermsTrait
{
/**
* Get a terms component instance.
*
* This is a convenience method that maps presets to getComponent
*
* @return \Solarium\Component\Terms
*/
public function getTerms()
{
return $this->getComponent(ComponentAwareQueryInterface::COMPONENT_TERMS, true);
}
}
......@@ -20,7 +20,6 @@ class Terms implements ComponentRequestBuilderInterface
*/
public function buildComponent($component, $request)
{
$request->setHandler($component->getHandler());
$request->addParam('terms', true);
$request->addParam('terms.lower', $component->getLowerbound());
$request->addParam('terms.lower.incl', $component->getLowerboundInclude());
......
......@@ -4,7 +4,6 @@ namespace Solarium\Component\ResponseParser;
use Solarium\Component\Result\Terms\Field;
use Solarium\Component\Result\Terms\Result;
use Solarium\Component\Result\Terms\Terms as ResultTerms;
use Solarium\Component\Terms as TermsComponent;
use Solarium\Core\Query\AbstractQuery;
use Solarium\Core\Query\AbstractResponseParser;
......@@ -30,28 +29,17 @@ class Terms extends AbstractResponseParser implements ComponentParserInterface
if (isset($data['terms']) && is_array($data['terms'])) {
$terms = [];
foreach ($data['terms'] as $field => $termData) {
$allTerms[] = $this->createTerms($termData);
$terms[$field] = $this->createTerms($termData);
if ($query->getResponseWriter() == $query::WT_JSON) {
$termData = $this->convertToKeyValueArray($termData);
}
$allTerms[$field] = $termData;
$terms[$field] = new Field($termData);
}
return new Result($this->createField($terms), $allTerms);
return new Result($terms, $allTerms);
}
return null;
}
private function createField(array $terms)
{
return new Field(
$terms
);
}
private function createTerms(array $termData)
{
return new ResultTerms(
$termData
);
}
}
......@@ -10,14 +10,14 @@ class Field implements \IteratorAggregate, \Countable
/**
* Terms.
*
* @var Terms[]
* @var array
*/
protected $terms;
/**
* Constructor.
*
* @param Terms[] $terms
* @param array $terms
*/
public function __construct(array $terms)
{
......@@ -27,27 +27,11 @@ class Field implements \IteratorAggregate, \Countable
/**
* Get Terms.
*
* @return Terms[]
* @return array
*/
public function getAllTerms()
public function getTerms()
{
return $this->terms;
}
/**
* Get results for a specific term.
*
* @param string $term
*
* @return Terms|null
*/
public function getTerms($field)
{
if (isset($this->terms[$field])) {
return $this->terms[$field];
}
return null;
return array_keys($this->terms);
}
/**
......
<?php
namespace Solarium\Component\Result\Terms;
/**
* Terms.
*/
class Terms implements \IteratorAggregate, \Countable
{
/**
* Terms.
*
* @var array
*/
protected $terms;
/**
* Constructor.
*
* @param array $terms
*/
public function __construct($terms)
{
$this->terms = $terms;
}
/**
* Get suggestions.
*
* @return array
*/
public function getTerms()
{
return $this->terms;
}
/**
* IteratorAggregate implementation.
*
* @return \ArrayIterator
*/
public function getIterator()
{
return new \ArrayIterator($this->terms);
}
/**
* Countable implementation.
*
* @return int
*/
public function count()
{
return count($this->terms);
}
}
......@@ -2,7 +2,7 @@
namespace Solarium\Component\Stats;
use Solarium\QueryType\Select\Query\Query as SelectQuery;
use Solarium\Component\ComponentAwareQueryInterface;
use Solarium\Component\AbstractComponent;
use Solarium\Component\RequestBuilder\Stats as RequestBuilder;
use Solarium\Component\ResponseParser\Stats as ResponseParser;
......@@ -36,7 +36,7 @@ class Stats extends AbstractComponent
*/
public function getType()
{
return SelectQuery::COMPONENT_STATS;
return ComponentAwareQueryInterface::COMPONENT_STATS;
}
/**
......
......@@ -2,9 +2,9 @@
namespace Solarium\Component;
use Solarium\Component\RequestBuilder\Terms as RequestBuilder;
use Solarium\Component\ResponseParser\Terms as ResponseParser;
use Solarium\Component\ComponentTraits\TermsTrait;
use Solarium\Core\Client\Client;
use Solarium\Core\Query\AbstractQuery as BaseQuery;
/**
* Terms component.
......
......@@ -40,7 +40,7 @@
namespace Solarium\QueryType\Select\Result;
use Solarium\QueryType\Select\Query\Query as SelectQuery;
use Solarium\Component\ComponentAwareQueryInterface;
use Solarium\Core\Query\Result\QueryType as BaseResult;
use Solarium\Component\Result\Spellcheck\Result as SpellcheckResult;
use Solarium\Component\Result\Stats\Result as StatsResult;
......@@ -276,7 +276,7 @@ class Result extends BaseResult implements \IteratorAggregate, \Countable
*/
public function getMoreLikeThis()
{
return $this->getComponent(SelectQuery::COMPONENT_MORELIKETHIS);
return $this->getComponent(ComponentAwareQueryInterface::COMPONENT_MORELIKETHIS);
}
/**
......@@ -288,7 +288,7 @@ class Result extends BaseResult implements \IteratorAggregate, \Countable
*/
public function getHighlighting()
{
return $this->getComponent(SelectQuery::COMPONENT_HIGHLIGHTING);
return $this->getComponent(ComponentAwareQueryInterface::COMPONENT_HIGHLIGHTING);
}
/**
......@@ -300,7 +300,7 @@ class Result extends BaseResult implements \IteratorAggregate, \Countable
*/
public function getGrouping()
{
return $this->getComponent(SelectQuery::COMPONENT_GROUPING);
return $this->getComponent(ComponentAwareQueryInterface::COMPONENT_GROUPING);
}
/**
......@@ -312,7 +312,7 @@ class Result extends BaseResult implements \IteratorAggregate, \Countable
*/
public function getFacetSet()
{
return $this->getComponent(SelectQuery::COMPONENT_FACETSET);
return $this->getComponent(ComponentAwareQueryInterface::COMPONENT_FACETSET);
}
/**
......@@ -324,7 +324,7 @@ class Result extends BaseResult implements \IteratorAggregate, \Countable
*/
public function getSpellcheck()
{
return $this->getComponent(SelectQuery::COMPONENT_SPELLCHECK);
return $this->getComponent(ComponentAwareQueryInterface::COMPONENT_SPELLCHECK);
}
/**
......@@ -336,7 +336,7 @@ class Result extends BaseResult implements \IteratorAggregate, \Countable
*/
public function getStats()
{
return $this->getComponent(SelectQuery::COMPONENT_STATS);
return $this->getComponent(ComponentAwareQueryInterface::COMPONENT_STATS);
}
/**
......@@ -348,6 +348,6 @@ class Result extends BaseResult implements \IteratorAggregate, \Countable
*/
public function getDebug()
{
return $this->getComponent(SelectQuery::COMPONENT_DEBUG);
return $this->getComponent(ComponentAwareQueryInterface::COMPONENT_DEBUG);
}
}
......@@ -2,6 +2,9 @@
namespace Solarium\Tests\Integration;
use Solarium\Component\ComponentAwareQueryInterface;
use Solarium\Component\QueryTraits\TermsTrait;
use Solarium\Component\Result\Terms\Result;
use Solarium\Core\Client\ClientInterface;
use Solarium\QueryType\Select\Query\Query as SelectQuery;
......@@ -128,13 +131,89 @@ abstract class AbstractTechproductsTest extends \PHPUnit_Framework_TestCase
$terms = $this->client->createTerms();
$terms->setFields('name');
$result = $this->client->terms($terms);
$phrases = [];
foreach ($result->getTerms('name') as $term => $count) {
$phrases[] = $term;
$this->assertEquals([
'one' => 5,
184 => 3,
'1gb' => 3,
3200 => 3,
400 => 3,
'ddr' => 3,
'gb' => 3,
'ipod' => 3,
'memory' => 3,
'pc' => 3,
], $result->getTerms('name'));
}
public function testTermsComponent()
{
$this->client->registerQueryType('test', '\Solarium\Tests\Integration\TestQuery');
$select = $this->client->createQuery('test');
$terms = $select->getTerms();
$terms->setFields('name');
$result = $this->client->select($select);
/** @var Result $termsComponentResult */
$termsComponentResult = $result->getComponent(ComponentAwareQueryInterface::COMPONENT_TERMS);
$this->assertEquals([
'one',
184,
'1gb',
3200,
400,
'ddr',
'gb',
'ipod',
'memory',
'pc',
], $termsComponentResult->getField('name')->getTerms());
$this->assertEquals([
'one' => 5,
184 => 3,
'1gb' => 3,
3200 => 3,
400 => 3,
'ddr' => 3,
'gb' => 3,
'ipod' => 3,
'memory' => 3,
'pc' => 3,
], $termsComponentResult->getAll()['name']);
$terms = [];
foreach ($termsComponentResult as $field) {
foreach ($field as $term => $count) {
$terms[$term] = $count;
}
}
$this->assertEquals([
'one', 184, '1gb', 3200, 400, 'ddr', 'gb', 'ipod', 'memory', 'pc',
], $phrases);
'one' => 5,
184 => 3,
'1gb' => 3,
3200 => 3,
400 => 3,
'ddr' => 3,
'gb' => 3,
'ipod' => 3,
'memory' => 3,
'pc' => 3,
], $terms);
}
}
class TestQuery extends SelectQuery
{
use TermsTrait;
public function __construct($options = null)
{
parent::__construct($options);
$this->componentTypes[ComponentAwareQueryInterface::COMPONENT_TERMS] = 'Solarium\Component\Terms';
// Unfortunately the terms request Handler is the only one containing a terms component.
$this->setHandler('terms');
}
}
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