mirror of
https://github.com/Codappix/search_core.git
synced 2024-11-22 10:16:10 +01:00
FEATURE: Add possibility to boost certain fields
Allow configuration via TS to boost certain fields during searching.
This commit is contained in:
parent
c58e13cdf6
commit
f138cd9034
3 changed files with 120 additions and 4 deletions
|
@ -20,6 +20,7 @@ namespace Codappix\SearchCore\Domain\Search;
|
|||
* 02110-1301, USA.
|
||||
*/
|
||||
|
||||
use Codappix\SearchCore\Configuration\ConfigurationContainerInterface;
|
||||
use Codappix\SearchCore\Connection\ConnectionInterface;
|
||||
use Codappix\SearchCore\Connection\Elasticsearch\Query;
|
||||
use Codappix\SearchCore\Connection\SearchRequestInterface;
|
||||
|
@ -32,6 +33,11 @@ class QueryFactory
|
|||
*/
|
||||
protected $logger;
|
||||
|
||||
/**
|
||||
* @var ConfigurationContainerInterface
|
||||
*/
|
||||
protected $configuration;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
|
@ -39,13 +45,21 @@ class QueryFactory
|
|||
|
||||
/**
|
||||
* @param \TYPO3\CMS\Core\Log\LogManager $logManager
|
||||
* @param ConfigurationContainerInterface $configuration
|
||||
*/
|
||||
public function __construct(\TYPO3\CMS\Core\Log\LogManager $logManager)
|
||||
{
|
||||
public function __construct(
|
||||
\TYPO3\CMS\Core\Log\LogManager $logManager,
|
||||
ConfigurationContainerInterface $configuration
|
||||
) {
|
||||
$this->logger = $logManager->getLogger(__CLASS__);
|
||||
$this->configuration = $configuration;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: This is not in scope Elasticsearch, therefore it should not return
|
||||
* \Elastica\Query, but decide to use a more specific QueryFactory like
|
||||
* ElasticaQueryFactory, once the second query is added?
|
||||
*
|
||||
* @param SearchRequestInterface $searchRequest
|
||||
*
|
||||
* @return \Elastica\Query
|
||||
|
@ -58,12 +72,12 @@ class QueryFactory
|
|||
/**
|
||||
* @param SearchRequestInterface $searchRequest
|
||||
*
|
||||
* TODO: This is not in scope Elasticsearch, therefore should not return elastica.
|
||||
* @return \Elastica\Query
|
||||
*/
|
||||
protected function createElasticaQuery(SearchRequestInterface $searchRequest)
|
||||
{
|
||||
$this->addSearch($searchRequest);
|
||||
$this->addBoosts($searchRequest);
|
||||
$this->addFilter($searchRequest);
|
||||
$this->addFacets($searchRequest);
|
||||
|
||||
|
@ -91,6 +105,42 @@ class QueryFactory
|
|||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param SearchRequestInterface $searchRequest
|
||||
*/
|
||||
protected function addBoosts(SearchRequestInterface $searchRequest)
|
||||
{
|
||||
try {
|
||||
$fields = $this->configuration->get('searching.boost');
|
||||
if (!$fields) {
|
||||
return;
|
||||
}
|
||||
} catch (InvalidArgumentException $e) {
|
||||
return;
|
||||
}
|
||||
|
||||
$boostQueryParts = [];
|
||||
|
||||
foreach ($fields as $fieldName => $boostValue) {
|
||||
$boostQueryParts[] = [
|
||||
'match' => [
|
||||
$fieldName => [
|
||||
'query' => $searchRequest->getSearchTerm(),
|
||||
'boost' => $boostValue,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
$this->query = ArrayUtility::arrayMergeRecursiveOverrule($this->query, [
|
||||
'query' => [
|
||||
'bool' => [
|
||||
'should' => $boostQueryParts,
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param SearchRequestInterface $searchRequest
|
||||
*/
|
||||
|
|
|
@ -209,3 +209,22 @@ Searching
|
|||
|
||||
The above example will provide a facet with options for all found ``CType`` results together
|
||||
with a count.
|
||||
|
||||
.. _boost:
|
||||
|
||||
``boost``
|
||||
"""""""""
|
||||
|
||||
Used by: Elasticsearch connection while building search query.
|
||||
|
||||
Define fields that should boost the score for results.
|
||||
|
||||
Example::
|
||||
|
||||
plugin.tx_searchcore.settings.searching.boost {
|
||||
search_title = 3
|
||||
search_abstract = 1.5
|
||||
}
|
||||
|
||||
For further information take a look at
|
||||
https://www.elastic.co/guide/en/elasticsearch/guide/2.x/_boosting_query_clauses.html
|
||||
|
|
|
@ -20,6 +20,7 @@ namespace Codappix\SearchCore\Tests\Unit\Domain\Search;
|
|||
* 02110-1301, USA.
|
||||
*/
|
||||
|
||||
use Codappix\SearchCore\Configuration\ConfigurationContainerInterface;
|
||||
use Codappix\SearchCore\Domain\Model\FacetRequest;
|
||||
use Codappix\SearchCore\Domain\Model\SearchRequest;
|
||||
use Codappix\SearchCore\Domain\Search\QueryFactory;
|
||||
|
@ -32,11 +33,17 @@ class QueryFactoryTest extends AbstractUnitTestCase
|
|||
*/
|
||||
protected $subject;
|
||||
|
||||
/**
|
||||
* @var ConfigurationContainerInterface
|
||||
*/
|
||||
protected $configuration;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->subject = new QueryFactory($this->getMockedLogger());
|
||||
$this->configuration = $this->getMockBuilder(ConfigurationContainerInterface::class)->getMock();
|
||||
$this->subject = new QueryFactory($this->getMockedLogger(), $this->configuration);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -145,4 +152,44 @@ class QueryFactoryTest extends AbstractUnitTestCase
|
|||
'Facets were not added to query.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function boostsAreAddedToQuery()
|
||||
{
|
||||
$searchRequest = new SearchRequest('SearchWord');
|
||||
|
||||
$this->configuration->expects($this->once())
|
||||
->method('get')
|
||||
->with('searching.boost')
|
||||
->willReturn([
|
||||
'search_title' => 3,
|
||||
'search_abstract' => 1.5,
|
||||
]);
|
||||
|
||||
$query = $this->subject->create($searchRequest);
|
||||
$this->assertSame(
|
||||
[
|
||||
[
|
||||
'match' => [
|
||||
'search_title' => [
|
||||
'query' => 'SearchWord',
|
||||
'boost' => 3,
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
'match' => [
|
||||
'search_abstract' => [
|
||||
'query' => 'SearchWord',
|
||||
'boost' => 1.5,
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
$query->toArray()['query']['bool']['should'],
|
||||
'Boosts were not added to query.'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue