Skip to content

Fix range filter preprocess search #254

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

Merged
merged 6 commits into from
Jul 20, 2018
Merged
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
2 changes: 1 addition & 1 deletion Filter/ViewData.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function setState(FilterState $state)
}

/**
* @return string
* @return array
*/
public function getTags()
{
Expand Down
25 changes: 20 additions & 5 deletions Filter/Widget/Range/Range.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace ONGR\FilterManagerBundle\Filter\Widget\Range;

use ONGR\ElasticsearchDSL\Aggregation\Bucketing\FilterAggregation;
use ONGR\ElasticsearchDSL\Aggregation\Metric\StatsAggregation;
use ONGR\ElasticsearchDSL\Search;
use ONGR\ElasticsearchBundle\Result\DocumentIterator;
Expand Down Expand Up @@ -58,9 +59,17 @@ public function getState(Request $request)
*/
public function preProcessSearch(Search $search, Search $relatedSearch, FilterState $state = null)
{
$stateAgg = new StatsAggregation($state->getName());
$stateAgg->setField($this->getDocumentField());
$search->addAggregation($stateAgg);
$aggregation = new StatsAggregation($state->getName());
$aggregation->setField($this->getDocumentField());

if ($relatedSearch->getPostFilters()) {
$filterAggregation = new FilterAggregation($state->getName() . '-filter');
$filterAggregation->setFilter($relatedSearch->getPostFilters());
$filterAggregation->addAggregation($aggregation);
$aggregation = $filterAggregation;
}

$search->addAggregation($aggregation);
}

/**
Expand All @@ -69,9 +78,15 @@ public function preProcessSearch(Search $search, Search $relatedSearch, FilterSt
public function getViewData(DocumentIterator $result, ViewData $data)
{
$name = $data->getState()->getName();
$aggregation = $result->getAggregation($name);

if (!$aggregation) {
$aggregation = $result->getAggregation($name . '-filter')->getAggregation($name);
}

/** @var $data ViewData\RangeAwareViewData */
$data->setMinBounds($result->getAggregation($name)['min']);
$data->setMaxBounds($result->getAggregation($name)['max']);
$data->setMinBounds($aggregation['min']);
$data->setMaxBounds($aggregation['max']);

return $data;
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/Functional/Filter/Widget/Range/DateRangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function testBoundsFormation()

$result = $manager->handleRequest(new Request(['limit' => 'blue']))->getFilters()['date_range_filter'];

$this->assertEquals('2001-09-11', $result->getMinBounds()->format('Y-m-d'));
$this->assertEquals('2005-10-11', $result->getMaxBounds()->format('Y-m-d'));
$this->assertEquals('2002-09-12', $result->getMinBounds()->format('Y-m-d'));
$this->assertEquals('2004-09-11', $result->getMaxBounds()->format('Y-m-d'));
}
}
3 changes: 1 addition & 2 deletions Tests/Functional/Filter/Widget/Range/RangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ public function getTestResultsData()
*/
public function testFilter($expectedChoices, $query = [])
{

$manager = $this->getContainer()->get(ONGRFilterManagerExtension::getFilterManagerId('range'));
$result = $manager->handleRequest(new Request($query))->getResult();

Expand All @@ -141,6 +140,6 @@ public function testFilterWithRelatedSearch()
$priceViewData = $result->getFilters()['price_range'];

$this->assertEquals(1, floor($priceViewData->getMinBounds()));
$this->assertEquals(5, ceil($priceViewData->getMaxBounds()));
$this->assertEquals(3, ceil($priceViewData->getMaxBounds()));
}
}
2 changes: 1 addition & 1 deletion Tests/Unit/Controller/ManagerControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function testGetResponseAction()
$templating = $this->createMock('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
$templating
->expects($this->once())
->method('renderResponse')
->method('render')
->with(
'template:name.html.twig',
$this->arrayHasKey('filter_manager')
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-master": "2.0-dev"
"dev-master": "2.1-dev"
}
}
}