Completed
Push — master ( cc21e8...5c80a5 )
by Simonas
65:42
created

Filter/Widget/Search/MatchSearch.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/*
3
 * This file is part of the ONGR package.
4
 *
5
 * (c) NFQ Technologies UAB <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace ONGR\FilterManagerBundle\Filter\Widget\Search;
11
12
use ONGR\ElasticsearchDSL\BuilderInterface;
13
use ONGR\ElasticsearchDSL\Query\BoolQuery;
14
use ONGR\ElasticsearchDSL\Query\MatchQuery;
15
use ONGR\ElasticsearchDSL\Query\NestedQuery;
16
use ONGR\ElasticsearchDSL\Search;
17
use ONGR\FilterManagerBundle\Filter\FilterState;
18
use ONGR\FilterManagerBundle\Search\SearchRequest;
19
20
/**
21
 * This class runs match search.
22
 */
23
class MatchSearch extends AbstractSingleValue
24
{
25
    /**
26
     * @var array
27
     */
28
    private $parameters = [];
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function modifySearch(Search $search, FilterState $state = null, SearchRequest $request = null)
34
    {
35
        if ($state && $state->isActive()) {
36
            if (strpos($this->getField(), ',') !== false) {
37
                $subQuery = new BoolQuery();
38
                foreach (explode(',', $this->getField()) as $field) {
39
                    $subQuery->add($this->buildMatchPart($field, $state), 'should');
40
                }
41
                $search->addQuery($subQuery, 'must');
42
            } else {
43
                $search->addQuery($this->buildMatchPart($this->getField(), $state), 'must');
44
            }
45
        }
46
    }
47
48
    /**
49
     * Build possibly nested match part.
50
     *
51
     * @param string      $field
52
     * @param FilterState $state
53
     *
54
     * @return BuilderInterface
55
     */
56
    private function buildMatchPart($field, FilterState $state)
57
    {
58 View Code Duplication
        if (strpos($field, '+') !== false) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
            list ($path, $field) = explode('+', $field);
60
        }
61
62 View Code Duplication
        if (strpos($field, '^') !== false) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
            list ($field, $boost) = explode('^', $field);
64
        }
65
66
        $query =  new MatchQuery($field, $state->getValue(), $this->parameters);
67
        !isset($boost) ? : $query->addParameter('boost', $boost);
68
69
        if (isset($path)) {
70
            $query = new NestedQuery($path, $query);
71
        }
72
73
        return $query;
74
    }
75
76
    /**
77
     * Sets operator
78
     *
79
     * @param string $operator
80
     */
81
    public function setOperator($operator)
82
    {
83
        $this->parameters['operator'] = $operator;
84
    }
85
86
    /**
87
     * Sets the maximum edit distance
88
     *
89
     * @param string|int|float $fuzziness
90
     */
91
    public function setFuzziness($fuzziness)
92
    {
93
        $this->parameters['fuzziness'] = $fuzziness;
94
    }
95
}
96