Skip to content

Commit

Permalink
Merge pull request #171 from tompoc/match_search_params
Browse files Browse the repository at this point in the history
Added `operator` and `fuzziness` parameters to `MatchSearch`
  • Loading branch information
saimaz committed Jun 13, 2016
2 parents 8386c29 + d6f067d commit ffd2be8
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 3 deletions.
11 changes: 11 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,17 @@ private function buildFilterTree($filterName)
->end()
->end();
break;
case 'match':
$node
->children()
->scalarNode('operator')
->info('The operator flag.')
->end()
->scalarNode('fuzziness')
->info('The maximum edit distance.')
->end()
->end();
break;
case 'fuzzy':
$node
->children()
Expand Down
5 changes: 5 additions & 0 deletions DependencyInjection/Filter/MatchFilterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ protected function configure(Definition $definition, array $configuration)
parent::configure($definition, $configuration);

!isset($configuration['field']) ? : $definition->addMethodCall('setField', [$configuration['field']]);

!isset($configuration['operator']) ? : $definition
->addMethodCall('setOperator', [$configuration['operator']]);
!isset($configuration['fuzziness']) ? : $definition
->addMethodCall('setFuzziness', [$configuration['fuzziness']]);
}

/**
Expand Down
38 changes: 35 additions & 3 deletions Filter/Widget/Search/MatchSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
*/
class MatchSearch extends AbstractSingleValue
{
/**
* @var array
*/
private $parameters = [];

/**
* {@inheritdoc}
*/
Expand All @@ -32,17 +37,44 @@ public function modifySearch(Search $search, FilterState $state = null, SearchRe
$subQuery = new BoolQuery();
foreach (explode(',', $this->getField()) as $field) {
if (strpos($field, '^') === false) {
$subQuery->add(new MatchQuery($field, $state->getValue()), 'should');
$subQuery->add(new MatchQuery($field, $state->getValue(), $this->parameters), 'should');
} else {
list ($field, $boost) = explode('^', $field);

$subQuery->add(new MatchQuery($field, $state->getValue(), ['boost' => $boost]), 'should');
$subQuery->add(
new MatchQuery(
$field,
$state->getValue(),
array_merge($this->parameters, ['boost' => $boost])
),
'should'
);
}
}
$search->addQuery($subQuery, 'must');
} else {
$search->addQuery(new MatchQuery($this->getField(), $state->getValue()), 'must');
$search->addQuery(new MatchQuery($this->getField(), $state->getValue(), $this->parameters), 'must');
}
}
}

/**
* Sets operator
*
* @param string $operator
*/
public function setOperator($operator)
{
$this->parameters['operator'] = $operator;
}

/**
* Sets the maximum edit distance
*
* @param string|int|float $fuzziness
*/
public function setFuzziness($fuzziness)
{
$this->parameters['fuzziness'] = $fuzziness;
}
}
49 changes: 49 additions & 0 deletions Tests/Unit/DependencyInjection/Filter/MatchFilterFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ONGR\FilterManagerBundle\Tests\Unit\DependencyInjection\Filter;

use ONGR\FilterManagerBundle\DependencyInjection\Filter\MatchFilterFactory;
use ONGR\FilterManagerBundle\DependencyInjection\ONGRFilterManagerExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class MatchFilterFactoryTest extends \PHPUnit_Framework_TestCase
{
/**
* Tests configure method
*/
public function testConfigure()
{
$container = new ContainerBuilder();
$extension = new ONGRFilterManagerExtension();
$matchFilterFactory = new MatchFilterFactory();

$config = [
'ongr_filter_manager' => [
'filters' => [
'match' => [
'test' => [
'request_field' => 'test',
'operator' => 'and',
'fuzziness' => 0,
]
]
]
]
];

$extension->addFilterFactory($matchFilterFactory);
$extension->load($config, $container);

$this->assertTrue($container->getDefinition('ongr_filter_manager.filter.test')->hasMethodCall('setOperator'));
$this->assertTrue($container->getDefinition('ongr_filter_manager.filter.test')->hasMethodCall('setFuzziness'));
}
}
2 changes: 2 additions & 0 deletions Tests/app/config/config_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ ongr_filter_manager:
match:
phrase:
request_field: 'q'
operator: 'and'
fuzziness: 0
pager:
pager:
request_field: 'page'
Expand Down

0 comments on commit ffd2be8

Please sign in to comment.