Completed
Pull Request — master (#171)
by
unknown
62:33
created

MatchSearch   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 8
c 2
b 0
f 1
lcom 1
cbo 5
dl 0
loc 58
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B modifySearch() 0 27 6
A setOperator() 0 4 1
A setFuzziness() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\FilterManagerBundle\Filter\Widget\Search;
13
14
use ONGR\ElasticsearchDSL\Query\BoolQuery;
15
use ONGR\ElasticsearchDSL\Query\MatchQuery;
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
                    if (strpos($field, '^') === false) {
40
                        $subQuery->add(new MatchQuery($field, $state->getValue(), $this->parameters), 'should');
41
                    } else {
42
                        list ($field, $boost) = explode('^', $field);
43
44
                        $subQuery->add(
45
                            new MatchQuery(
46
                                $field,
47
                                $state->getValue(),
48
                                array_merge($this->parameters, ['boost' => $boost])
49
                            ),
50
                            'should'
51
                        );
52
                    }
53
                }
54
                $search->addQuery($subQuery, 'must');
55
            } else {
56
                $search->addQuery(new MatchQuery($this->getField(), $state->getValue(), $this->parameters), 'must');
57
            }
58
        }
59
    }
60
61
    /**
62
     * Sets operator
63
     *
64
     * @param string $operator
65
     */
66
    public function setOperator($operator)
67
    {
68
        $this->parameters['operator'] = $operator;
69
    }
70
71
    /**
72
     * Sets the maximum edit distance
73
     *
74
     * @param string|int|float $fuzziness
75
     */
76
    public function setFuzziness($fuzziness)
77
    {
78
        $this->parameters['fuzziness'] = $fuzziness;
79
    }
80
}
81