mirror of
https://github.com/Codappix/search_core.git
synced 2024-11-24 00:56:10 +01:00
Merge pull request #72 from Codappix/feature/boosting
FEATURE: Add possibility to boost certain fields
This commit is contained in:
commit
b1b5485b47
3 changed files with 205 additions and 1 deletions
|
@ -21,6 +21,7 @@ namespace Codappix\SearchCore\Domain\Search;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use Codappix\SearchCore\Configuration\ConfigurationContainerInterface;
|
use Codappix\SearchCore\Configuration\ConfigurationContainerInterface;
|
||||||
|
use Codappix\SearchCore\Configuration\InvalidArgumentException;
|
||||||
use Codappix\SearchCore\Connection\ConnectionInterface;
|
use Codappix\SearchCore\Connection\ConnectionInterface;
|
||||||
use Codappix\SearchCore\Connection\Elasticsearch\Query;
|
use Codappix\SearchCore\Connection\Elasticsearch\Query;
|
||||||
use Codappix\SearchCore\Connection\SearchRequestInterface;
|
use Codappix\SearchCore\Connection\SearchRequestInterface;
|
||||||
|
@ -56,6 +57,10 @@ class QueryFactory
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* 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
|
* @param SearchRequestInterface $searchRequest
|
||||||
*
|
*
|
||||||
* @return \Elastica\Query
|
* @return \Elastica\Query
|
||||||
|
@ -68,16 +73,20 @@ class QueryFactory
|
||||||
/**
|
/**
|
||||||
* @param SearchRequestInterface $searchRequest
|
* @param SearchRequestInterface $searchRequest
|
||||||
*
|
*
|
||||||
* TODO: This is not in scope Elasticsearch, therefore should not return elastica.
|
|
||||||
* @return \Elastica\Query
|
* @return \Elastica\Query
|
||||||
*/
|
*/
|
||||||
protected function createElasticaQuery(SearchRequestInterface $searchRequest)
|
protected function createElasticaQuery(SearchRequestInterface $searchRequest)
|
||||||
{
|
{
|
||||||
$this->addSize($searchRequest);
|
$this->addSize($searchRequest);
|
||||||
$this->addSearch($searchRequest);
|
$this->addSearch($searchRequest);
|
||||||
|
$this->addBoosts($searchRequest);
|
||||||
$this->addFilter($searchRequest);
|
$this->addFilter($searchRequest);
|
||||||
$this->addFacets($searchRequest);
|
$this->addFacets($searchRequest);
|
||||||
|
|
||||||
|
// Use last, as it might change structure of query.
|
||||||
|
// Better approach would be something like DQL to generate query and build result in the end.
|
||||||
|
$this->addFactorBoost();
|
||||||
|
|
||||||
$this->logger->debug('Generated elasticsearch query.', [$this->query]);
|
$this->logger->debug('Generated elasticsearch query.', [$this->query]);
|
||||||
return new \Elastica\Query($this->query);
|
return new \Elastica\Query($this->query);
|
||||||
}
|
}
|
||||||
|
@ -114,6 +123,53 @@ class QueryFactory
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param SearchRequestInterface $searchRequest
|
||||||
|
*/
|
||||||
|
protected function addBoosts(SearchRequestInterface $searchRequest)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$fields = $this->configuration->get('searching.boost');
|
||||||
|
} 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,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function addFactorBoost()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$this->query['query'] = [
|
||||||
|
'function_score' => [
|
||||||
|
'query' => $this->query['query'],
|
||||||
|
'field_value_factor' => $this->configuration->get('searching.fieldValueFactor'),
|
||||||
|
],
|
||||||
|
];
|
||||||
|
} catch (InvalidArgumentException $e) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param SearchRequestInterface $searchRequest
|
* @param SearchRequestInterface $searchRequest
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -271,3 +271,41 @@ Searching
|
||||||
Example::
|
Example::
|
||||||
|
|
||||||
plugin.tx_searchcore.settings.searching.minimumShouldMatch = 50%
|
plugin.tx_searchcore.settings.searching.minimumShouldMatch = 50%
|
||||||
|
|
||||||
|
.. _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
|
||||||
|
|
||||||
|
.. _fieldValueFactor:
|
||||||
|
|
||||||
|
``fieldValueFactor``
|
||||||
|
""""""""""""""""""""
|
||||||
|
|
||||||
|
Used by: Elasticsearch connection while building search query.
|
||||||
|
|
||||||
|
Define a field to use as a factor for scoring. The configuration is passed through to elastic
|
||||||
|
search ``field_value_factor``, see: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-function-score-query.html#function-field-value-factor
|
||||||
|
|
||||||
|
Example::
|
||||||
|
|
||||||
|
plugin.tx_searchcore.settings.searching.field_value_factor {
|
||||||
|
field = rootlineLevel
|
||||||
|
modifier = reciprocal
|
||||||
|
factor = 2
|
||||||
|
missing = 1
|
||||||
|
}
|
||||||
|
|
|
@ -21,6 +21,7 @@ namespace Codappix\SearchCore\Tests\Unit\Domain\Search;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use Codappix\SearchCore\Configuration\ConfigurationContainerInterface;
|
use Codappix\SearchCore\Configuration\ConfigurationContainerInterface;
|
||||||
|
use Codappix\SearchCore\Configuration\InvalidArgumentException;
|
||||||
use Codappix\SearchCore\Domain\Model\FacetRequest;
|
use Codappix\SearchCore\Domain\Model\FacetRequest;
|
||||||
use Codappix\SearchCore\Domain\Model\SearchRequest;
|
use Codappix\SearchCore\Domain\Model\SearchRequest;
|
||||||
use Codappix\SearchCore\Domain\Search\QueryFactory;
|
use Codappix\SearchCore\Domain\Search\QueryFactory;
|
||||||
|
@ -53,6 +54,10 @@ class QueryFactoryTest extends AbstractUnitTestCase
|
||||||
{
|
{
|
||||||
$searchRequest = new SearchRequest('SearchWord');
|
$searchRequest = new SearchRequest('SearchWord');
|
||||||
|
|
||||||
|
$this->configuration->expects($this->any())
|
||||||
|
->method('get')
|
||||||
|
->will($this->throwException(new InvalidArgumentException));
|
||||||
|
|
||||||
$query = $this->subject->create($searchRequest);
|
$query = $this->subject->create($searchRequest);
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
\Elastica\Query::class,
|
\Elastica\Query::class,
|
||||||
|
@ -66,6 +71,10 @@ class QueryFactoryTest extends AbstractUnitTestCase
|
||||||
*/
|
*/
|
||||||
public function filterIsAddedToQuery()
|
public function filterIsAddedToQuery()
|
||||||
{
|
{
|
||||||
|
$this->configuration->expects($this->any())
|
||||||
|
->method('get')
|
||||||
|
->will($this->throwException(new InvalidArgumentException));
|
||||||
|
|
||||||
$searchRequest = new SearchRequest('SearchWord');
|
$searchRequest = new SearchRequest('SearchWord');
|
||||||
$searchRequest->setFilter(['field' => 'content']);
|
$searchRequest->setFilter(['field' => 'content']);
|
||||||
|
|
||||||
|
@ -84,6 +93,10 @@ class QueryFactoryTest extends AbstractUnitTestCase
|
||||||
*/
|
*/
|
||||||
public function emptyFilterIsNotAddedToQuery()
|
public function emptyFilterIsNotAddedToQuery()
|
||||||
{
|
{
|
||||||
|
$this->configuration->expects($this->any())
|
||||||
|
->method('get')
|
||||||
|
->will($this->throwException(new InvalidArgumentException));
|
||||||
|
|
||||||
$searchRequest = new SearchRequest('SearchWord');
|
$searchRequest = new SearchRequest('SearchWord');
|
||||||
$searchRequest->setFilter([
|
$searchRequest->setFilter([
|
||||||
'field' => '',
|
'field' => '',
|
||||||
|
@ -109,6 +122,9 @@ class QueryFactoryTest extends AbstractUnitTestCase
|
||||||
*/
|
*/
|
||||||
public function facetsAreAddedToQuery()
|
public function facetsAreAddedToQuery()
|
||||||
{
|
{
|
||||||
|
$this->configuration->expects($this->any())
|
||||||
|
->method('get')
|
||||||
|
->will($this->throwException(new InvalidArgumentException));
|
||||||
$searchRequest = new SearchRequest('SearchWord');
|
$searchRequest = new SearchRequest('SearchWord');
|
||||||
$searchRequest->addFacet(new FacetRequest('Identifier', 'FieldName'));
|
$searchRequest->addFacet(new FacetRequest('Identifier', 'FieldName'));
|
||||||
$searchRequest->addFacet(new FacetRequest('Identifier 2', 'FieldName 2'));
|
$searchRequest->addFacet(new FacetRequest('Identifier 2', 'FieldName 2'));
|
||||||
|
@ -154,6 +170,9 @@ class QueryFactoryTest extends AbstractUnitTestCase
|
||||||
public function searchTermIsAddedToQuery()
|
public function searchTermIsAddedToQuery()
|
||||||
{
|
{
|
||||||
$searchRequest = new SearchRequest('SearchWord');
|
$searchRequest = new SearchRequest('SearchWord');
|
||||||
|
$this->configuration->expects($this->any())
|
||||||
|
->method('get')
|
||||||
|
->will($this->throwException(new InvalidArgumentException));
|
||||||
$query = $this->subject->create($searchRequest);
|
$query = $this->subject->create($searchRequest);
|
||||||
|
|
||||||
$this->assertSame(
|
$this->assertSame(
|
||||||
|
@ -185,6 +204,9 @@ class QueryFactoryTest extends AbstractUnitTestCase
|
||||||
->method('getIfExists')
|
->method('getIfExists')
|
||||||
->with('searching.minimumShouldMatch')
|
->with('searching.minimumShouldMatch')
|
||||||
->willReturn('50%');
|
->willReturn('50%');
|
||||||
|
$this->configuration->expects($this->any())
|
||||||
|
->method('get')
|
||||||
|
->will($this->throwException(new InvalidArgumentException));
|
||||||
$query = $this->subject->create($searchRequest);
|
$query = $this->subject->create($searchRequest);
|
||||||
|
|
||||||
$this->assertArraySubset(
|
$this->assertArraySubset(
|
||||||
|
@ -205,4 +227,92 @@ class QueryFactoryTest extends AbstractUnitTestCase
|
||||||
'minimum_should_match was not added to query as configured.'
|
'minimum_should_match was not added to query as configured.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function boostsAreAddedToQuery()
|
||||||
|
{
|
||||||
|
$searchRequest = new SearchRequest('SearchWord');
|
||||||
|
|
||||||
|
$this->configuration->expects($this->exactly(2))
|
||||||
|
->method('get')
|
||||||
|
->withConsecutive(['searching.boost'], ['searching.fieldValueFactor'])
|
||||||
|
->will($this->onConsecutiveCalls(
|
||||||
|
[
|
||||||
|
'search_title' => 3,
|
||||||
|
'search_abstract' => 1.5,
|
||||||
|
],
|
||||||
|
$this->throwException(new InvalidArgumentException)
|
||||||
|
));
|
||||||
|
|
||||||
|
$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.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function factorBoostIsAddedToQuery()
|
||||||
|
{
|
||||||
|
$searchRequest = new SearchRequest('SearchWord');
|
||||||
|
$fieldConfig = [
|
||||||
|
'field' => 'rootlineLevel',
|
||||||
|
'modifier' => 'reciprocal',
|
||||||
|
'factor' => '2',
|
||||||
|
'missing' => '1',
|
||||||
|
];
|
||||||
|
$this->configuration->expects($this->exactly(2))
|
||||||
|
->method('get')
|
||||||
|
->withConsecutive(['searching.boost'], ['searching.fieldValueFactor'])
|
||||||
|
->will($this->onConsecutiveCalls(
|
||||||
|
$this->throwException(new InvalidArgumentException),
|
||||||
|
$fieldConfig
|
||||||
|
));
|
||||||
|
|
||||||
|
$query = $this->subject->create($searchRequest);
|
||||||
|
$this->assertSame(
|
||||||
|
[
|
||||||
|
'function_score' => [
|
||||||
|
'query' => [
|
||||||
|
'bool' => [
|
||||||
|
'must' => [
|
||||||
|
[
|
||||||
|
'match' => [
|
||||||
|
'_all' => [
|
||||||
|
'query' => 'SearchWord',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'field_value_factor' => $fieldConfig,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
$query->toArray()['query'],
|
||||||
|
'Boosts were not added to query.'
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue