mirror of
https://github.com/Codappix/search_core.git
synced 2024-11-26 08:36:11 +01:00
BUGFIX: Use fresh query for each creation
This prevents issues with modifying an build query.
This commit is contained in:
parent
646285b8b5
commit
c5766f5b12
1 changed files with 33 additions and 29 deletions
|
@ -39,11 +39,6 @@ class QueryFactory
|
||||||
*/
|
*/
|
||||||
protected $configuration;
|
protected $configuration;
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $query = [];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \TYPO3\CMS\Core\Log\LogManager $logManager
|
* @param \TYPO3\CMS\Core\Log\LogManager $logManager
|
||||||
* @param ConfigurationContainerInterface $configuration
|
* @param ConfigurationContainerInterface $configuration
|
||||||
|
@ -77,26 +72,28 @@ class QueryFactory
|
||||||
*/
|
*/
|
||||||
protected function createElasticaQuery(SearchRequestInterface $searchRequest)
|
protected function createElasticaQuery(SearchRequestInterface $searchRequest)
|
||||||
{
|
{
|
||||||
$this->addSize($searchRequest);
|
$query = [];
|
||||||
$this->addSearch($searchRequest);
|
$this->addSize($searchRequest, $query);
|
||||||
$this->addBoosts($searchRequest);
|
$this->addSearch($searchRequest, $query);
|
||||||
$this->addFilter($searchRequest);
|
$this->addBoosts($searchRequest, $query);
|
||||||
$this->addFacets($searchRequest);
|
$this->addFilter($searchRequest, $query);
|
||||||
|
$this->addFacets($searchRequest, $query);
|
||||||
|
|
||||||
// Use last, as it might change structure of query.
|
// 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.
|
// Better approach would be something like DQL to generate query and build result in the end.
|
||||||
$this->addFactorBoost();
|
$this->addFactorBoost($query);
|
||||||
|
|
||||||
$this->logger->debug('Generated elasticsearch query.', [$this->query]);
|
$this->logger->debug('Generated elasticsearch query.', [$query]);
|
||||||
return new \Elastica\Query($this->query);
|
return new \Elastica\Query($query);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param SearchRequestInterface $searchRequest
|
* @param SearchRequestInterface $searchRequest
|
||||||
|
* @param array &$query
|
||||||
*/
|
*/
|
||||||
protected function addSize(SearchRequestInterface $searchRequest)
|
protected function addSize(SearchRequestInterface $searchRequest, array &$query)
|
||||||
{
|
{
|
||||||
$this->query = ArrayUtility::arrayMergeRecursiveOverrule($this->query, [
|
$query = ArrayUtility::arrayMergeRecursiveOverrule($query, [
|
||||||
'from' => 0,
|
'from' => 0,
|
||||||
'size' => $searchRequest->getSize(),
|
'size' => $searchRequest->getSize(),
|
||||||
]);
|
]);
|
||||||
|
@ -104,19 +101,20 @@ class QueryFactory
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param SearchRequestInterface $searchRequest
|
* @param SearchRequestInterface $searchRequest
|
||||||
|
* @param array &$query
|
||||||
*/
|
*/
|
||||||
protected function addSearch(SearchRequestInterface $searchRequest)
|
protected function addSearch(SearchRequestInterface $searchRequest, array &$query)
|
||||||
{
|
{
|
||||||
$this->query = ArrayUtility::setValueByPath(
|
$query = ArrayUtility::setValueByPath(
|
||||||
$this->query,
|
$query,
|
||||||
'query.bool.must.0.match._all.query',
|
'query.bool.must.0.match._all.query',
|
||||||
$searchRequest->getSearchTerm()
|
$searchRequest->getSearchTerm()
|
||||||
);
|
);
|
||||||
|
|
||||||
$minimumShouldMatch = $this->configuration->getIfExists('searching.minimumShouldMatch');
|
$minimumShouldMatch = $this->configuration->getIfExists('searching.minimumShouldMatch');
|
||||||
if ($minimumShouldMatch) {
|
if ($minimumShouldMatch) {
|
||||||
$this->query = ArrayUtility::setValueByPath(
|
$query = ArrayUtility::setValueByPath(
|
||||||
$this->query,
|
$query,
|
||||||
'query.bool.must.0.match._all.minimum_should_match',
|
'query.bool.must.0.match._all.minimum_should_match',
|
||||||
$minimumShouldMatch
|
$minimumShouldMatch
|
||||||
);
|
);
|
||||||
|
@ -125,8 +123,9 @@ class QueryFactory
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param SearchRequestInterface $searchRequest
|
* @param SearchRequestInterface $searchRequest
|
||||||
|
* @param array &$query
|
||||||
*/
|
*/
|
||||||
protected function addBoosts(SearchRequestInterface $searchRequest)
|
protected function addBoosts(SearchRequestInterface $searchRequest, array &$query)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$fields = $this->configuration->get('searching.boost');
|
$fields = $this->configuration->get('searching.boost');
|
||||||
|
@ -147,7 +146,7 @@ class QueryFactory
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->query = ArrayUtility::arrayMergeRecursiveOverrule($this->query, [
|
$query = ArrayUtility::arrayMergeRecursiveOverrule($query, [
|
||||||
'query' => [
|
'query' => [
|
||||||
'bool' => [
|
'bool' => [
|
||||||
'should' => $boostQueryParts,
|
'should' => $boostQueryParts,
|
||||||
|
@ -156,12 +155,15 @@ class QueryFactory
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function addFactorBoost()
|
/**
|
||||||
|
* @param array &$query
|
||||||
|
*/
|
||||||
|
protected function addFactorBoost(array &$query)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$this->query['query'] = [
|
$query['query'] = [
|
||||||
'function_score' => [
|
'function_score' => [
|
||||||
'query' => $this->query['query'],
|
'query' => $query['query'],
|
||||||
'field_value_factor' => $this->configuration->get('searching.fieldValueFactor'),
|
'field_value_factor' => $this->configuration->get('searching.fieldValueFactor'),
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
@ -172,8 +174,9 @@ class QueryFactory
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param SearchRequestInterface $searchRequest
|
* @param SearchRequestInterface $searchRequest
|
||||||
|
* @param array &$query
|
||||||
*/
|
*/
|
||||||
protected function addFilter(SearchRequestInterface $searchRequest)
|
protected function addFilter(SearchRequestInterface $searchRequest, array &$query)
|
||||||
{
|
{
|
||||||
if (! $searchRequest->hasFilter()) {
|
if (! $searchRequest->hasFilter()) {
|
||||||
return;
|
return;
|
||||||
|
@ -188,7 +191,7 @@ class QueryFactory
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->query = ArrayUtility::arrayMergeRecursiveOverrule($this->query, [
|
$query = ArrayUtility::arrayMergeRecursiveOverrule($query, [
|
||||||
'query' => [
|
'query' => [
|
||||||
'bool' => [
|
'bool' => [
|
||||||
'filter' => $terms,
|
'filter' => $terms,
|
||||||
|
@ -199,11 +202,12 @@ class QueryFactory
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param SearchRequestInterface $searchRequest
|
* @param SearchRequestInterface $searchRequest
|
||||||
|
* @param array &$query
|
||||||
*/
|
*/
|
||||||
protected function addFacets(SearchRequestInterface $searchRequest)
|
protected function addFacets(SearchRequestInterface $searchRequest, array &$query)
|
||||||
{
|
{
|
||||||
foreach ($searchRequest->getFacets() as $facet) {
|
foreach ($searchRequest->getFacets() as $facet) {
|
||||||
$this->query = ArrayUtility::arrayMergeRecursiveOverrule($this->query, [
|
$query = ArrayUtility::arrayMergeRecursiveOverrule($query, [
|
||||||
'aggs' => [
|
'aggs' => [
|
||||||
$facet->getIdentifier() => [
|
$facet->getIdentifier() => [
|
||||||
'terms' => [
|
'terms' => [
|
||||||
|
|
Loading…
Reference in a new issue