Skip to content

Broaden search result for other sorting than "relevance" (resolves #10) #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/Solr/Query/AbstractParamsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
*
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 8 additions & 2 deletions src/Solr/Query/AbstractQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ protected function getEventDispatcher()
{
return $this->eventDispatcher;
}



/**
* @return string
*/
protected function getAttributetoReset()
{
return $this->attributetoReset;
}
}
11 changes: 11 additions & 0 deletions src/Solr/Query/SearchQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a conceptual difference between $broaden and $attributesToReset, that justifies passing one via setter and one via parameter to the build method? It looks confusing.

If no, I think, the setter is the better way, as it fits the builder pattern better:

  1. configure the builder
  2. build the object based on builder configuration

Then it could look like this:

            $this->getParamsBuilder()
                ->setBroaden($this->broaden)
-               ->buildAsArray($this->getAttributetoReset())
+               ->setAttributesToReset($this->getAttributetoReset())
+               ->buildAsArray()

);
}

/**
* @return string
*/
Expand Down
27 changes: 21 additions & 6 deletions src/Solr/Request/SearchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
}
}