mirror of
https://github.com/Codappix/search_core.git
synced 2024-11-22 10:36:12 +01:00
FEATURE: Add field_value_factor support through configuration
This commit is contained in:
parent
f138cd9034
commit
f436a02f55
3 changed files with 110 additions and 9 deletions
|
@ -21,6 +21,7 @@ namespace Codappix\SearchCore\Domain\Search;
|
|||
*/
|
||||
|
||||
use Codappix\SearchCore\Configuration\ConfigurationContainerInterface;
|
||||
use Codappix\SearchCore\Configuration\InvalidArgumentException;
|
||||
use Codappix\SearchCore\Connection\ConnectionInterface;
|
||||
use Codappix\SearchCore\Connection\Elasticsearch\Query;
|
||||
use Codappix\SearchCore\Connection\SearchRequestInterface;
|
||||
|
@ -81,6 +82,10 @@ class QueryFactory
|
|||
$this->addFilter($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]);
|
||||
return new \Elastica\Query($this->query);
|
||||
}
|
||||
|
@ -112,9 +117,6 @@ class QueryFactory
|
|||
{
|
||||
try {
|
||||
$fields = $this->configuration->get('searching.boost');
|
||||
if (!$fields) {
|
||||
return;
|
||||
}
|
||||
} catch (InvalidArgumentException $e) {
|
||||
return;
|
||||
}
|
||||
|
@ -141,6 +143,20 @@ class QueryFactory
|
|||
]);
|
||||
}
|
||||
|
||||
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
|
||||
*/
|
||||
|
|
|
@ -228,3 +228,22 @@ Searching
|
|||
|
||||
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\InvalidArgumentException;
|
||||
use Codappix\SearchCore\Domain\Model\FacetRequest;
|
||||
use Codappix\SearchCore\Domain\Model\SearchRequest;
|
||||
use Codappix\SearchCore\Domain\Search\QueryFactory;
|
||||
|
@ -53,6 +54,10 @@ class QueryFactoryTest extends AbstractUnitTestCase
|
|||
{
|
||||
$searchRequest = new SearchRequest('SearchWord');
|
||||
|
||||
$this->configuration->expects($this->any())
|
||||
->method('get')
|
||||
->will($this->throwException(new InvalidArgumentException));
|
||||
|
||||
$query = $this->subject->create($searchRequest);
|
||||
$this->assertInstanceOf(
|
||||
\Elastica\Query::class,
|
||||
|
@ -66,6 +71,10 @@ class QueryFactoryTest extends AbstractUnitTestCase
|
|||
*/
|
||||
public function filterIsAddedToQuery()
|
||||
{
|
||||
$this->configuration->expects($this->any())
|
||||
->method('get')
|
||||
->will($this->throwException(new InvalidArgumentException));
|
||||
|
||||
$searchRequest = new SearchRequest('SearchWord');
|
||||
$searchRequest->setFilter(['field' => 'content']);
|
||||
|
||||
|
@ -84,6 +93,10 @@ class QueryFactoryTest extends AbstractUnitTestCase
|
|||
*/
|
||||
public function emptyFilterIsNotAddedToQuery()
|
||||
{
|
||||
$this->configuration->expects($this->any())
|
||||
->method('get')
|
||||
->will($this->throwException(new InvalidArgumentException));
|
||||
|
||||
$searchRequest = new SearchRequest('SearchWord');
|
||||
$searchRequest->setFilter([
|
||||
'field' => '',
|
||||
|
@ -109,6 +122,10 @@ class QueryFactoryTest extends AbstractUnitTestCase
|
|||
*/
|
||||
public function userInputIsAlwaysString()
|
||||
{
|
||||
$this->configuration->expects($this->any())
|
||||
->method('get')
|
||||
->will($this->throwException(new InvalidArgumentException));
|
||||
|
||||
$searchRequest = new SearchRequest(10);
|
||||
$searchRequest->setFilter(['field' => 20]);
|
||||
|
||||
|
@ -130,6 +147,9 @@ class QueryFactoryTest extends AbstractUnitTestCase
|
|||
*/
|
||||
public function facetsAreAddedToQuery()
|
||||
{
|
||||
$this->configuration->expects($this->any())
|
||||
->method('get')
|
||||
->will($this->throwException(new InvalidArgumentException));
|
||||
$searchRequest = new SearchRequest('SearchWord');
|
||||
$searchRequest->addFacet(new FacetRequest('Identifier', 'FieldName'));
|
||||
$searchRequest->addFacet(new FacetRequest('Identifier 2', 'FieldName 2'));
|
||||
|
@ -160,13 +180,16 @@ class QueryFactoryTest extends AbstractUnitTestCase
|
|||
{
|
||||
$searchRequest = new SearchRequest('SearchWord');
|
||||
|
||||
$this->configuration->expects($this->once())
|
||||
$this->configuration->expects($this->exactly(2))
|
||||
->method('get')
|
||||
->with('searching.boost')
|
||||
->willReturn([
|
||||
->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(
|
||||
|
@ -192,4 +215,47 @@ class QueryFactoryTest extends AbstractUnitTestCase
|
|||
'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' => 'SearchWord',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
'field_value_factor' => $fieldConfig,
|
||||
],
|
||||
],
|
||||
$query->toArray()['query'],
|
||||
'Boosts were not added to query.'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue