FEATURE: Make number of search results to fetch configurable

This commit is contained in:
Daniel Siepmann 2017-07-18 10:44:39 +02:00
parent c58e13cdf6
commit 1030e8d5cf
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
7 changed files with 178 additions and 0 deletions

View file

@ -41,4 +41,11 @@ interface SearchRequestInterface
* @return array
*/
public function getFilter();
/**
* Defines how many results should be fetched.
*
* @return int
*/
public function getSize();
}

View file

@ -35,6 +35,11 @@ class SearchRequest implements SearchRequestInterface
*/
protected $query = '';
/**
* @var int
*/
protected $size = 10;
/**
* @var array
*/
@ -112,4 +117,20 @@ class SearchRequest implements SearchRequestInterface
{
return $this->facets;
}
/**
* @return int
*/
public function getSize()
{
return $this->size;
}
/**
* @param int $size
*/
public function setSize($size)
{
$this->size = (int) $size;
}
}

View file

@ -63,6 +63,7 @@ class QueryFactory
*/
protected function createElasticaQuery(SearchRequestInterface $searchRequest)
{
$this->addSize($searchRequest);
$this->addSearch($searchRequest);
$this->addFilter($searchRequest);
$this->addFacets($searchRequest);
@ -71,6 +72,17 @@ class QueryFactory
return new \Elastica\Query($this->query);
}
/**
* @param SearchRequestInterface $searchRequest
*/
protected function addSize(SearchRequestInterface $searchRequest)
{
$this->query = ArrayUtility::arrayMergeRecursiveOverrule($this->query, [
'from' => 0,
'size' => $searchRequest->getSize(),
]);
}
/**
* @param SearchRequestInterface $searchRequest
*/

View file

@ -69,11 +69,23 @@ class SearchService
*/
public function search(SearchRequestInterface $searchRequest)
{
$this->addSize($searchRequest);
$this->addConfiguredFacets($searchRequest);
return $this->connection->search($searchRequest);
}
/**
* Add configured size of search result items to request.
*
* @param SearchRequestInterface $searchRequest
*/
protected function addSize(SearchRequestInterface $searchRequest)
{
$size = $this->configuration->getIfExists('searching.size') ?: 10;
$searchRequest->setSize($size);
}
/**
* Add facets from configuration to request.
*

View file

@ -189,6 +189,21 @@ options are available:
Searching
^^^^^^^^^
.. _size:
``size``
""""""""
Used by: Elasticsearch connection while building search query.
Defined how many search results should be fetched to be available in search result.
Example::
plugin.tx_searchcore.settings.searching.size = 50
Default if not configured is 10.
.. _facets:
``facets``

View file

@ -145,4 +145,20 @@ class QueryFactoryTest extends AbstractUnitTestCase
'Facets were not added to query.'
);
}
/**
* @test
*/
public function sizeIsAddedToQuery()
{
$searchRequest = new SearchRequest('SearchWord');
$searchRequest->setSize(45);
$query = $this->subject->create($searchRequest);
$this->assertSame(
45,
$query->toArray()['size'],
'Size was not added to query.'
);
}
}

View file

@ -0,0 +1,95 @@
<?php
namespace Copyright\SearchCore\Tests\Unit\Domain\Search;
/*
* Copyright (C) 2017 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
use Codappix\SearchCore\Configuration\ConfigurationContainerInterface;
use Codappix\SearchCore\Connection\ConnectionInterface;
use Codappix\SearchCore\Domain\Model\SearchRequest;
use Codappix\SearchCore\Domain\Search\SearchService;
use Codappix\SearchCore\Tests\Unit\AbstractUnitTestCase;
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
class SearchServiceTest extends AbstractUnitTestCase
{
/**
* @var SearchService
*/
protected $subject;
public function setUp()
{
parent::setUp();
$this->connection = $this->getMockBuilder(ConnectionInterface::class)
->disableOriginalConstructor()
->getMock();
$this->configuration = $this->getMockBuilder(ConfigurationContainerInterface::class)
->disableOriginalConstructor()
->getMock();
$this->objectManager = $this->getMockBuilder(ObjectManagerInterface::class)
->disableOriginalConstructor()
->getMock();
$this->subject = new SearchService(
$this->connection,
$this->configuration,
$this->objectManager
);
}
/**
* @test
*/
public function sizeIsAddedFromConfiguration()
{
$this->configuration->expects($this->exactly(2))
->method('getIfExists')
->withConsecutive(['searching.size'], ['searching.facets'])
->will($this->onConsecutiveCalls(45, null));
$this->connection->expects($this->once())
->method('search')
->with($this->callback(function ($searchRequest) {
return $searchRequest->getSize() === 45;
}));
$searchRequest = new SearchRequest('SearchWord');
$this->subject->search($searchRequest);
}
/**
* @test
*/
public function defaultSizeIsAddedIfNothingIsConfigured()
{
$this->configuration->expects($this->exactly(2))
->method('getIfExists')
->withConsecutive(['searching.size'], ['searching.facets'])
->will($this->onConsecutiveCalls(null, null));
$this->connection->expects($this->once())
->method('search')
->with($this->callback(function ($searchRequest) {
return $searchRequest->getSize() === 10;
}));
$searchRequest = new SearchRequest('SearchWord');
$this->subject->search($searchRequest);
}
}