FEATURE: Allow configured filters

Add new feature to allow pre configured filters. The filters will be
applied to all requests.
This commit is contained in:
Daniel Siepmann 2017-09-15 21:35:52 +02:00
parent e354e740ca
commit be75248517
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
3 changed files with 121 additions and 0 deletions

View file

@ -72,6 +72,7 @@ class SearchService
$searchRequest->setConnection($this->connection);
$this->addSize($searchRequest);
$this->addConfiguredFacets($searchRequest);
$this->addConfiguredFilters($searchRequest);
return $this->connection->search($searchRequest);
}
@ -113,4 +114,21 @@ class SearchService
));
}
}
/**
* Add filters from configuration, e.g. flexform or TypoScript.
*
* @param SearchRequestInterface $searchRequest
*/
protected function addConfiguredFilters(SearchRequestInterface $searchRequest)
{
try {
$searchRequest->setFilter(array_merge(
$searchRequest->getFilter(),
$this->configuration->get('searching.filter')
));
} catch (InvalidArgumentException $e) {
// Nothing todo, no filter configured.
}
}
}

View file

@ -279,6 +279,23 @@ Searching
The above example will provide a facet with options for all found ``CType`` results together
with a count.
.. _filter:
``filter``
"""""""""""
Used by: While building search request.
Define filter that should be set for all requests.
Example::
plugin.tx_searchcore.settings.searching.filter {
property = value
}
For Elasticsearch the fields have to be filterable, e.g. need a mapping as ``keyword``.
.. _minimumShouldMatch:
``minimumShouldMatch``

View file

@ -21,6 +21,7 @@ namespace Copyright\SearchCore\Tests\Unit\Domain\Search;
*/
use Codappix\SearchCore\Configuration\ConfigurationContainerInterface;
use Codappix\SearchCore\Configuration\InvalidArgumentException;
use Codappix\SearchCore\Connection\ConnectionInterface;
use Codappix\SearchCore\Domain\Model\SearchRequest;
use Codappix\SearchCore\Domain\Search\SearchService;
@ -64,6 +65,10 @@ class SearchServiceTest extends AbstractUnitTestCase
->method('getIfExists')
->withConsecutive(['searching.size'], ['searching.facets'])
->will($this->onConsecutiveCalls(45, null));
$this->configuration->expects($this->exactly(1))
->method('get')
->with('searching.filter')
->will($this->throwException(new InvalidArgumentException));
$this->connection->expects($this->once())
->method('search')
->with($this->callback(function ($searchRequest) {
@ -83,6 +88,10 @@ class SearchServiceTest extends AbstractUnitTestCase
->method('getIfExists')
->withConsecutive(['searching.size'], ['searching.facets'])
->will($this->onConsecutiveCalls(null, null));
$this->configuration->expects($this->exactly(1))
->method('get')
->with('searching.filter')
->will($this->throwException(new InvalidArgumentException));
$this->connection->expects($this->once())
->method('search')
->with($this->callback(function ($searchRequest) {
@ -92,4 +101,81 @@ class SearchServiceTest extends AbstractUnitTestCase
$searchRequest = new SearchRequest('SearchWord');
$this->subject->search($searchRequest);
}
/**
* @test
*/
public function configuredFilterAreAddedToRequestWithoutAnyFilter()
{
$this->configuration->expects($this->exactly(2))
->method('getIfExists')
->withConsecutive(['searching.size'], ['searching.facets'])
->will($this->onConsecutiveCalls(null, null));
$this->configuration->expects($this->exactly(1))
->method('get')
->with('searching.filter')
->willReturn(['property' => 'something']);
$this->connection->expects($this->once())
->method('search')
->with($this->callback(function ($searchRequest) {
return $searchRequest->getFilter() === ['property' => 'something'];
}));
$searchRequest = new SearchRequest('SearchWord');
$this->subject->search($searchRequest);
}
/**
* @test
*/
public function configuredFilterAreAddedToRequestWithExistingFilter()
{
$this->configuration->expects($this->exactly(2))
->method('getIfExists')
->withConsecutive(['searching.size'], ['searching.facets'])
->will($this->onConsecutiveCalls(null, null));
$this->configuration->expects($this->exactly(1))
->method('get')
->with('searching.filter')
->willReturn(['property' => 'something']);
$this->connection->expects($this->once())
->method('search')
->with($this->callback(function ($searchRequest) {
return $searchRequest->getFilter() === [
'anotherProperty' => 'anything',
'property' => 'something',
];
}));
$searchRequest = new SearchRequest('SearchWord');
$searchRequest->setFilter(['anotherProperty' => 'anything']);
$this->subject->search($searchRequest);
}
/**
* @test
*/
public function nonConfiguredFilterIsNotChangingRequestWithExistingFilter()
{
$this->configuration->expects($this->exactly(2))
->method('getIfExists')
->withConsecutive(['searching.size'], ['searching.facets'])
->will($this->onConsecutiveCalls(null, null));
$this->configuration->expects($this->exactly(1))
->method('get')
->with('searching.filter')
->will($this->throwException(new InvalidArgumentException));
$this->connection->expects($this->once())
->method('search')
->with($this->callback(function ($searchRequest) {
return $searchRequest->getFilter() === ['anotherProperty' => 'anything'];
}));
$searchRequest = new SearchRequest('SearchWord');
$searchRequest->setFilter(['anotherProperty' => 'anything']);
$this->subject->search($searchRequest);
}
}