Skip to content

Commit

Permalink
Merge pull request #186 from einorler/issue146
Browse files Browse the repository at this point in the history
Implemented a check if relatedSearch is required
  • Loading branch information
saimaz committed Sep 1, 2016
2 parents 6501329 + 1a7caa5 commit 5e5f708
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 5 deletions.
8 changes: 8 additions & 0 deletions Filter/FilterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,12 @@ public function getViewData(DocumentIterator $result, ViewData $data);
* @return array
*/
public function getTags();

/**
* Defines whether its necessary to build a related search for
* the filters preProcessSearch() method
*
* @return bool
*/
public function isRelated();
}
8 changes: 8 additions & 0 deletions Filter/Widget/Choice/SingleTermChoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ public function getViewData(DocumentIterator $result, ViewData $data)
return $data;
}

/**
* {@inheritdoc}
*/
public function isRelated()
{
return true;
}

/**
* Adds prioritized choices.
*
Expand Down
8 changes: 8 additions & 0 deletions Filter/Widget/Pager/Pager.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,12 @@ public function getViewData(DocumentIterator $result, ViewData $data)

return $data;
}

/**
* {@inheritdoc}
*/
public function isRelated()
{
return false;
}
}
8 changes: 8 additions & 0 deletions Filter/Widget/Range/AbstractRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,12 @@ public function modifySearch(Search $search, FilterState $state = null, SearchRe
$search->addPostFilter($filter);
}
}

/**
* {@inheritdoc}
*/
public function isRelated()
{
return false;
}
}
8 changes: 8 additions & 0 deletions Filter/Widget/Search/AbstractSingleValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,12 @@ public function preProcessSearch(Search $search, Search $relatedSearch, FilterSt
{
// Nothing more to do here.
}

/**
* {@inheritdoc}
*/
public function isRelated()
{
return false;
}
}
8 changes: 8 additions & 0 deletions Filter/Widget/Sort/Sort.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ public function setChoices($choices)
}
}

/**
* {@inheritdoc}
*/
public function isRelated()
{
return false;
}

/**
* @param string $key
* @param ViewData $data
Expand Down
17 changes: 12 additions & 5 deletions Search/FilterManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace ONGR\FilterManagerBundle\Search;

use ONGR\ElasticsearchDSL\Search;
use ONGR\ElasticsearchBundle\Service\Repository;
use ONGR\ElasticsearchBundle\Result\DocumentIterator;
use ONGR\FilterManagerBundle\Event\PreSearchEvent;
Expand Down Expand Up @@ -84,13 +85,19 @@ public function search(SearchRequest $request)

/** @var FilterInterface $filter */
foreach ($this->container->all() as $name => $filter) {
// We simply exclude not related filters and current filter itself.
$relatedFilters = $this->container->getFiltersByRelation(
new AndRelation([$filter->getSearchRelation(), new ExcludeRelation([$name])])
);
$relatedSearch = new Search();

if ($filter->isRelated()) {
// We simply exclude not related filters and current filter itself.
$relatedFilters = $this->container->getFiltersByRelation(
new AndRelation([$filter->getSearchRelation(), new ExcludeRelation([$name])])
);
$relatedSearch = $this->container->buildSearch($request, $relatedFilters);
}

$filter->preProcessSearch(
$search,
$this->container->buildSearch($request, $relatedFilters),
$relatedSearch,
$request->get($name)
);
}
Expand Down
3 changes: 3 additions & 0 deletions Tests/Unit/Search/FilterManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public function testHandleRequest()
$mockFilterInterface = $this->getMock('ONGR\FilterManagerBundle\Filter\FilterInterface');
$mockFilterInterface->expects($this->once())
->method('preProcessSearch');
$mockFilterInterface->expects($this->once())
->method('isRelated')
->will($this->returnValue(true));
$mockFilterInterface->expects($this->once())
->method('getSearchRelation')
->will($this->returnValue(null));
Expand Down
8 changes: 8 additions & 0 deletions Tests/app/fixture/TestBundle/Filter/FooRange/FooRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,12 @@ public function getViewData(DocumentIterator $result, ViewData $data)

return $data;
}

/**
* {@inheritdoc}
*/
public function isRelated()
{
return true;
}
}

0 comments on commit 5e5f708

Please sign in to comment.