Skip to content
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

Added operator and fuzziness parameters to MatchSearch #171

Merged
merged 1 commit into from
Jun 13, 2016
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
11 changes: 11 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,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 <[email protected]>
*
* 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