Completed
Push — develop ( bdcd74...c796db )
by Sam
13s
created

src/Search/Query/FullText/MultiMatchQuery.php (1 issue)

1
<?php namespace Nord\Lumen\Elasticsearch\Search\Query\FullText;
2
3
use Nord\Lumen\Elasticsearch\Exceptions\InvalidArgument;
4
use Nord\Lumen\Elasticsearch\Search\Query\Traits\HasFields;
5
6
/**
7
 * The multi_match query builds on the match query to allow multi-field queries.
8
 *
9
 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html
10
 */
11
class MultiMatchQuery extends MatchQuery
12
{
13
    use HasFields;
14
    
15
    const TYPE_BEST_FIELDS   = 'best_fields';
16
    const TYPE_MOST_FIELDS   = 'most_fields';
17
    const TYPE_CROSS_FIELDS  = 'cross_fields';
18
19
    /**
20
     * @var float By default, each per-term blended query will use the best score returned by any field in a group,
21
     * then these scores are added together to give the final score. The tie_breaker parameter can change the default
22
     * behaviour of the per-term blended queries.
23
     */
24
    private $tieBreaker;
25
26
27
    /**
28
     * @inheritdoc
29
     */
30
    public function toArray()
31
    {
32
        $multiMatch = [
33
            'query'  => $this->getValue(),
34
            'fields' => $this->getFields()
35
        ];
36
37
        $multiMatch = $this->applyOptions($multiMatch);
38
39
        return ['multi_match' => $multiMatch];
40
    }
41
42
43
    /**
44
     * @param float $tieBreaker
45
     * @return MultiMatchQuery
46
     */
47
    public function setTieBreaker($tieBreaker)
48
    {
49
        $this->assertTieBreaker($tieBreaker);
50
        $this->tieBreaker = $tieBreaker;
51
        return $this;
52
    }
53
54
55
    /**
56
     * @return float
57
     */
58
    public function getTieBreaker()
59
    {
60
        return $this->tieBreaker;
61
    }
62
63
64
    /**
65
     * @param array $match
66
     * @return array
67
     */
68
    protected function applyOptions(array $match)
69
    {
70
        $multiMatch = parent::applyOptions($match);
71
72
        $tieBreaker = $this->getTieBreaker();
73
        if (!is_null($tieBreaker)) {
74
            $multiMatch['tie_breaker'] = $tieBreaker;
75
        }
76
77
        return $multiMatch;
78
    }
79
80
81
    /**
82
     * @param string $type
83
     * @throws InvalidArgument
84
     */
85 View Code Duplication
    protected function assertType($type)
0 ignored issues
show
This method seems to be duplicated in 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...
86
    {
87
        $validTypes = [
88
            self::TYPE_BEST_FIELDS,
89
            self::TYPE_MOST_FIELDS,
90
            self::TYPE_CROSS_FIELDS,
91
            self::TYPE_PHRASE,
92
            self::TYPE_PHRASE_PREFIX
93
        ];
94
        if (!in_array($type, $validTypes)) {
95
            throw new InvalidArgument(sprintf(
96
                'MultiMatch Query `type` must be one of "%s", "%s" given.',
97
                implode(', ', $validTypes),
98
                $type
99
            ));
100
        }
101
    }
102
103
104
    /**
105
     * @param float $tieBreaker
106
     * @throws InvalidArgument
107
     */
108
    protected function assertTieBreaker($tieBreaker)
109
    {
110
        if (!is_float($tieBreaker)) {
111
            throw new InvalidArgument(sprintf(
112
                'MultiMatch Query `tie_breaker` must be a float value, "%s" given.',
113
                gettype($tieBreaker)
114
            ));
115
        }
116
    }
117
}
118