From 9bcdee9ed05e84e36710ebc07b5c4d57f3578241 Mon Sep 17 00:00:00 2001 From: Andreas von Studnitz Date: Tue, 24 Oct 2017 15:28:16 +0200 Subject: [PATCH 1/2] Broaden search result for other sorting than "relevance" This is relevant for search queries containing two words or more where the words are found in different product attributes. In this case, there is a mechanism in the search result which searches for the single words if no results are found otherwise. The mechanism was only used for search by relevance which is extended to other sort orders with this commit. Additionally, there was a bug in this mechanism - the parameter "mm=0%" has to be set for these additional requests. --- src/Solr/Query/AbstractParamsBuilder.php | 16 +++++++++++++- src/Solr/Query/AbstractQueryBuilder.php | 10 +++++++-- src/Solr/Query/SearchQueryBuilder.php | 11 ++++++++++ src/Solr/Request/SearchRequest.php | 27 ++++++++++++++++++------ 4 files changed, 55 insertions(+), 9 deletions(-) diff --git a/src/Solr/Query/AbstractParamsBuilder.php b/src/Solr/Query/AbstractParamsBuilder.php index 5f75498..255253f 100755 --- a/src/Solr/Query/AbstractParamsBuilder.php +++ b/src/Solr/Query/AbstractParamsBuilder.php @@ -49,6 +49,10 @@ abstract class AbstractParamsBuilder implements ParamsBuilder, HasFilter, HasPag * @var $eventDispatcher EventDispatcher */ protected $eventDispatcher; + /** + * @var bool + */ + private $broaden = false; public function __construct(AttributeRepository $attributeRepository, FilterQueryBuilder $filterQueryBuilder, Pagination $pagination, ResultsConfig $resultsConfig, FuzzyConfig $fuzzyConfig, $storeId, $eventDispatcher) @@ -62,6 +66,16 @@ public function __construct(AttributeRepository $attributeRepository, FilterQuer $this->eventDispatcher = $eventDispatcher; } + /** + * @param boolean $broaden + * @return AbstractParamsBuilder + */ + public function setBroaden($broaden) + { + $this->broaden = $broaden; + return $this; + } + /** * Return filter query builder used to build the filter query paramter * @@ -103,7 +117,7 @@ public function buildAsArray($attributeToReset = '') $params = $this->addFacetParams($params); - if (!$this->fuzzyConfig->isActive()) { + if (!$this->fuzzyConfig->isActive() || $this->broaden) { $params['mm'] = '0%'; } return $params; diff --git a/src/Solr/Query/AbstractQueryBuilder.php b/src/Solr/Query/AbstractQueryBuilder.php index 8b7cfe2..960b2e9 100755 --- a/src/Solr/Query/AbstractQueryBuilder.php +++ b/src/Solr/Query/AbstractQueryBuilder.php @@ -116,6 +116,12 @@ protected function getEventDispatcher() { return $this->eventDispatcher; } - - + + /** + * @return string + */ + protected function getAttributetoReset() + { + return $this->attributetoReset; + } } \ No newline at end of file diff --git a/src/Solr/Query/SearchQueryBuilder.php b/src/Solr/Query/SearchQueryBuilder.php index 03dd17a..d40bd77 100755 --- a/src/Solr/Query/SearchQueryBuilder.php +++ b/src/Solr/Query/SearchQueryBuilder.php @@ -114,6 +114,17 @@ protected function getResultsConfig() return $this->resultsConfig; } + public function build() + { + return new Query( + $this->getStoreId(), + $this->getQueryText(), + 0, + $this->getPagination()->getPageSize() * $this->getPagination()->getCurrentPage(), + $this->getParamsBuilder()->setBroaden($this->broaden)->buildAsArray($this->getAttributetoReset()) + ); + } + /** * @return string */ diff --git a/src/Solr/Request/SearchRequest.php b/src/Solr/Request/SearchRequest.php index da2ca30..22f405c 100755 --- a/src/Solr/Request/SearchRequest.php +++ b/src/Solr/Request/SearchRequest.php @@ -106,6 +106,9 @@ public function doRequest($activeFilterAttributeCodes = array()) $minimumResults = $this->fuzzyConfig->getMinimumResults(); if ($this->getCurrentSort() !== 'position') { $result = $this->getResultFromRequest($pageSize, $isFuzzyActive, $activeFilterAttributeCodes); + if ($result->documents()->count() === 0) { + $result = $this->getBroaderResult($activeFilterAttributeCodes, $pageSize, $result); + } return $this->sliceResult($result); } else { $result = $this->getResultFromRequest($isFuzzyActive ? self::PAGESIZE_ALL : $pageSize, false, $activeFilterAttributeCodes); @@ -116,12 +119,7 @@ public function doRequest($activeFilterAttributeCodes = array()) $result = $result->merge($fuzzyResult, $pageSize); } if ($result->documents()->count() === 0) { - $this->foundNoResults = true; - $check = explode(' ', $this->queryBuilder->getSearchString()->getRawString()); - if (count($check) > 1) { - $result = $this->getResultFromRequest($pageSize, false, $activeFilterAttributeCodes); - } - $this->foundNoResults = false; + $result = $this->getBroaderResult($activeFilterAttributeCodes, $pageSize, $result); } return $this->sliceResult($result); } @@ -242,4 +240,21 @@ private function getResource() { return $this->resource; } + + /** + * @param string[] $activeFilterAttributeCodes + * @param int $pageSize + * @param SolrResponse + * @return SolrResponse + */ + private function getBroaderResult($activeFilterAttributeCodes, $pageSize, $result) + { + $this->foundNoResults = true; + $check = explode(' ', $this->queryBuilder->getSearchString()->getRawString()); + if (count($check) > 1) { + $result = $this->getResultFromRequest($pageSize, false, $activeFilterAttributeCodes); + } + $this->foundNoResults = false; + return $result; + } } \ No newline at end of file From 5ffbfec916925e35ada7996afc1932e795e92a01 Mon Sep 17 00:00:00 2001 From: Andreas von Studnitz Date: Tue, 14 Nov 2017 13:32:41 +0100 Subject: [PATCH 2/2] Refactor ParamsBuilder to use setter instead of parameter This makes the interface more consistent as "setBroaden" is also implemented as a setter. --- src/Solr/Query/AbstractParamsBuilder.php | 27 +++++++++++++++++------- src/Solr/Query/AbstractQueryBuilder.php | 4 +++- src/Solr/Query/ParamsBuilder.php | 3 +-- src/Solr/Query/SearchQueryBuilder.php | 17 ++++++++++++--- 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/Solr/Query/AbstractParamsBuilder.php b/src/Solr/Query/AbstractParamsBuilder.php index 255253f..7b8281d 100755 --- a/src/Solr/Query/AbstractParamsBuilder.php +++ b/src/Solr/Query/AbstractParamsBuilder.php @@ -20,6 +20,7 @@ abstract class AbstractParamsBuilder implements ParamsBuilder, HasFilter, HasPagination { + private $attributeToReset = ''; /** * @var $attributeRepository AttributeRepository */ @@ -87,25 +88,24 @@ public function getFilterQueryBuilder() } /** - * @param string $attributeToReset * @return array */ - public function buildAsArray($attributeToReset = '') + public function buildAsArray() { - if ($attributeToReset) { - switch($attributeToReset) { + if ($this->attributeToReset) { + switch($this->attributeToReset) { case 'category'; break; case 'price': - $attributeToReset .= '_f'; + $this->attributeToReset .= '_f'; break; default: - $attributeToReset .= '_facet'; + $this->attributeToReset .= '_facet'; } } $params = array( 'q.op' => $this->resultsConfig->getSearchOperator(), - 'fq' => $this->getFilterQuery($attributeToReset), + 'fq' => $this->getFilterQuery($this->attributeToReset), 'fl' => 'result_html_autosuggest_nonindex,score,sku_s,name_s,product_id', 'sort' => $this->getSortParam(), 'facet' => 'true', @@ -263,7 +263,18 @@ protected function addFacetParams($params) } $params['f.price_f.facet.interval.set'][] = sprintf('(%f,%s]', $lowerBorder, '*'); return $params; - }return $params; + } + + return $params; } + /** + * @param string $attributeToReset + * @return $this + */ + public function setAttributeToReset($attributeToReset) + { + $this->attributeToReset = $attributeToReset; + return $this; + } } \ No newline at end of file diff --git a/src/Solr/Query/AbstractQueryBuilder.php b/src/Solr/Query/AbstractQueryBuilder.php index 960b2e9..10f9873 100755 --- a/src/Solr/Query/AbstractQueryBuilder.php +++ b/src/Solr/Query/AbstractQueryBuilder.php @@ -70,7 +70,9 @@ public function build() $this->getQueryText(), 0, $this->pagination->getPageSize() * $this->pagination->getCurrentPage(), - $this->paramsBuilder->buildAsArray($this->attributetoReset) + $this->paramsBuilder + ->setAttributeToReset($this->attributetoReset) + ->buildAsArray() ); } diff --git a/src/Solr/Query/ParamsBuilder.php b/src/Solr/Query/ParamsBuilder.php index 3f2b9c7..0197b8a 100755 --- a/src/Solr/Query/ParamsBuilder.php +++ b/src/Solr/Query/ParamsBuilder.php @@ -21,10 +21,9 @@ interface ParamsBuilder /** * Return parameters as array as expected by solr service * - * @param string $attributeToReset * @return mixed[] */ - public function buildAsArray($attributeToReset = ''); + public function buildAsArray(); /** * Return store id diff --git a/src/Solr/Query/SearchQueryBuilder.php b/src/Solr/Query/SearchQueryBuilder.php index d40bd77..c8e6d4e 100755 --- a/src/Solr/Query/SearchQueryBuilder.php +++ b/src/Solr/Query/SearchQueryBuilder.php @@ -59,8 +59,16 @@ final class SearchQueryBuilder extends AbstractQueryBuilder * @param int $storeId * @param EventDispatcher $eventDispatcher */ - public function __construct(SearchString $searchString, FuzzyConfig $fuzzyConfig, ResultsConfig $resultsConfig, AttributeRepository $attributeRepository, Pagination $pagination, ParamsBuilder $paramsBuilder, $storeId, EventDispatcher $eventDispatcher) - { + public function __construct( + SearchString $searchString, + FuzzyConfig $fuzzyConfig, + ResultsConfig $resultsConfig, + AttributeRepository $attributeRepository, + Pagination $pagination, + ParamsBuilder $paramsBuilder, + $storeId, + EventDispatcher $eventDispatcher + ) { parent::__construct($attributeRepository, $pagination, $paramsBuilder, $storeId, $eventDispatcher); $this->fuzzyConfig = $fuzzyConfig; $this->resultsConfig = $resultsConfig; @@ -121,7 +129,10 @@ public function build() $this->getQueryText(), 0, $this->getPagination()->getPageSize() * $this->getPagination()->getCurrentPage(), - $this->getParamsBuilder()->setBroaden($this->broaden)->buildAsArray($this->getAttributetoReset()) + $this->getParamsBuilder() + ->setBroaden($this->broaden) + ->setAttributeToReset($this->getAttributetoReset()) + ->buildAsArray() ); }