mirror of
https://github.com/Codappix/search_core.git
synced 2024-11-22 09:56:11 +01:00
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:
parent
e354e740ca
commit
be75248517
3 changed files with 121 additions and 0 deletions
|
@ -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.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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``
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue