Commit 01ed9f94 authored by Bas de Nooijer's avatar Bas de Nooijer

Added support for tagging the main query

parent 724c482a
......@@ -200,7 +200,6 @@ class FilterQuery extends Configurable
public function clearTags()
{
$this->tags = array();
return $this;
}
......@@ -212,11 +211,10 @@ class FilterQuery extends Configurable
* @param array $filterQueries
* @return self Provides fluent interface
*/
public function setTags($filterQueries)
public function setTags($tags)
{
$this->clearTags();
return $this->addTags($filterQueries);
return $this->addTags($tags);
}
}
......@@ -140,6 +140,13 @@ class Query extends BaseQuery
'omitheader' => true,
);
/**
* Tags for this query
*
* @var array
*/
protected $tags = array();
/**
* Default select query component types
*
......@@ -249,6 +256,12 @@ class Query extends BaseQuery
case 'component':
$this->createComponents($value);
break;
case 'tag':
if (!is_array($value)) {
$value = array($value);
}
$this->addTags($value);
break;
}
}
}
......@@ -987,4 +1000,83 @@ class Query extends BaseQuery
return $this->getComponent(self::COMPONENT_DEBUG, true);
}
/**
* Add a tag
*
* @param string $tag
* @return self Provides fluent interface
*/
public function addTag($tag)
{
$this->tags[$tag] = true;
return $this;
}
/**
* Add tags
*
* @param array $tags
* @return self Provides fluent interface
*/
public function addTags($tags)
{
foreach ($tags as $tag) {
$this->addTag($tag);
}
return $this;
}
/**
* Get all tagss
*
* @return array
*/
public function getTags()
{
return array_keys($this->tags);
}
/**
* Remove a tag
*
* @param string $tag
* @return self Provides fluent interface
*/
public function removeTag($tag)
{
if (isset($this->tags[$tag])) {
unset($this->tags[$tag]);
}
return $this;
}
/**
* Remove all tags
*
* @return self Provides fluent interface
*/
public function clearTags()
{
$this->tags = array();
return $this;
}
/**
* Set multiple tags
*
* This overwrites any existing tags
*
* @param array $tags
* @return self Provides fluent interface
*/
public function setTags($tags)
{
$this->clearTags();
return $this->addTags($tags);
}
}
......@@ -59,7 +59,10 @@ class RequestBuilder extends BaseRequestBuilder
$request = parent::build($query);
// add basic params to request
$request->addParam('q', $query->getQuery());
$request->addParam('q', $this->renderLocalParams(
$query->getQuery(),
array('tag' => $query->getTags())
));
$request->addParam('start', $query->getStart());
$request->addParam('rows', $query->getRows());
$request->addParam('fl', implode(',', $query->getFields()));
......
......@@ -444,6 +444,7 @@ class QueryTest extends \PHPUnit_Framework_TestCase
),
'resultclass' => 'MyResultClass',
'documentclass' => 'MyDocumentClass',
'tag' => array('t1','t2'),
);
$query = new Query($config);
......@@ -470,6 +471,13 @@ class QueryTest extends \PHPUnit_Framework_TestCase
$components = $query->getComponents();
$this->assertEquals(1, count($components));
$this->assertThat(array_pop($components), $this->isInstanceOf('Solarium\QueryType\Select\Query\Component\FacetSet'));
$this->assertEquals(array('t1','t2'), $query->getTags());
}
public function testConfigModeWithSingleValueTag()
{
$query = $query = new Query(array('tag' => 't1'));
$this->assertEquals(array('t1'), $query->getTags());
}
public function testSetAndGetComponents()
......@@ -663,4 +671,38 @@ class QueryTest extends \PHPUnit_Framework_TestCase
get_class($stats)
);
}
public function testAddTag()
{
$this->query->addTag('testtag');
$this->assertEquals(array('testtag'), $this->query->getTags());
}
public function testAddTags()
{
$this->query->addTags(array('t1','t2'));
$this->assertEquals(array('t1','t2'), $this->query->getTags());
}
public function testRemoveTag()
{
$this->query->addTags(array('t1','t2'));
$this->query->removeTag('t1');
$this->assertEquals(array('t2'), $this->query->getTags());
}
public function testClearTags()
{
$this->query->addTags(array('t1','t2'));
$this->query->clearTags();
$this->assertEquals(array(), $this->query->getTags());
}
public function testSetTags()
{
$this->query->addTags(array('t1','t2'));
$this->query->setTags(array('t3','t4'));
$this->assertEquals(array('t3','t4'), $this->query->getTags());
}
}
......@@ -169,6 +169,18 @@ class RequestBuilderTest extends \PHPUnit_Framework_TestCase
);
}
public function testWithTags()
{
$this->query->setTags(array('t1','t2'));
$this->query->setQuery('cat:1');
$request = $this->builder->build($this->query);
$this->assertEquals(
'{!tag=t1,t2}cat:1',
$request->getParam('query')
);
}
}
class TestDummyComponent extends Component
......
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