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 all commits
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
43 changes: 34 additions & 9 deletions src/Solr/Query/AbstractParamsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

abstract class AbstractParamsBuilder implements ParamsBuilder, HasFilter, HasPagination
{
private $attributeToReset = '';
/**
* @var $attributeRepository AttributeRepository
*/
Expand Down Expand Up @@ -49,6 +50,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 +67,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 All @@ -73,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',
Expand All @@ -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 Expand Up @@ -249,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;
}
}
14 changes: 11 additions & 3 deletions src/Solr/Query/AbstractQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

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

If I understood it correctly, setAttributesToReset is only needed in one implementation. I think it should not be implemented in the abstract params builder and probably not even used in the abstract query builder then.

Here we can easily remove the line and move them to setAttributeToReset() above:

     public function setAttributeToReset($attributeToReset)
     {
-        $this->attributetoReset = $attributeToReset;
+        $this->paramsBuilder->setAttributeToReset($attributeToReset);
         return $this;
     }

Then move that method down to the class(es) where it is actually needed and we know that the type of paramsBuilder supports it: SearchQueryBuilder and CategoryQueryBuilder(?)

->buildAsArray()
);
}

Expand Down Expand Up @@ -116,6 +118,12 @@ protected function getEventDispatcher()
{
return $this->eventDispatcher;
}



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

Choose a reason for hiding this comment

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

should be SearchParamsBuilder now, because methods not in the interface are used (which is okay in this case, as discussed)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I changed this. See also integer-net/solr-pro#7 as I had to do changes to the pro library too due to changed interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can't change it to SearchParamsBuilder easily. It crashes with the Autosuggest.

$storeId,
EventDispatcher $eventDispatcher
) {
parent::__construct($attributeRepository, $pagination, $paramsBuilder, $storeId, $eventDispatcher);
$this->fuzzyConfig = $fuzzyConfig;
$this->resultsConfig = $resultsConfig;
Expand Down Expand Up @@ -114,6 +122,20 @@ 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)
->setAttributeToReset($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;
}
}