Completed
Push — devel ( 3908d4...2f2813 )
by Alexey
02:14
created

Where::match()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
3
namespace Bardex\Elastic;
4
5
6
class Where
7
{
8
    protected $field;
9
10
    protected $query;
11
12
13
    public function __construct(SearchQuery $query)
14
    {
15
        $this->query = $query;
16
    }
17
18
    /**
19
     * @param mixed $field
20
     */
21
    public function setField($field)
22
    {
23
        $this->field = $field;
24
    }
25
26
    /**
27
     * @param $value
28
     * @return SearchQuery
29
     */
30
    public function equal($value)
31
    {
32
        $this->query->addFilter('term', [$this->field => $value]);
33
        return $this->query;
34
    }
35
36
    /**
37
     * Добавить фильтр совпадения хотя бы одного значения из набора, этот фильтр не влияет на поле релевантности _score.
38
     *
39
     * @param $values - массив допустимых значений
40
     * @example $query->where('channel')->in([1,2,3])->where('page.categoryId')->in([10,11]);
41
     * @return SearchQuery;
0 ignored issues
show
Documentation introduced by
The doc-type SearchQuery; could not be parsed: Expected "|" or "end of type", but got ";" at position 11. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
42
     */
43
    public function in(array $values)
44
    {
45
        // потому что ES не понимает дырки в ключах
46
        $values = array_values($values);
47
        $this->query->addFilter('terms', [$this->field => $values]);
48
        return $this->query;
49
    }
50
51
    /**
52
     * Добавить фильтр вхождения значение в диапазон (обе границы включительно).
53
     * Можно искать по диапазону дат.
54
     * Этот фильтр не влияет на поле релевантности _score.
55
     *
56
     * @param $min - нижняя граница диапазона (включительно)
57
     * @param $max - верхняя граница диапазона (включительно)
58
     * @param $dateFormat - необязательное поле описание формата даты
59
     * @link https://www.elastic.co/guide/en/elasticsearch/reference/5.0/query-dsl-range-query.html
60
     * @return SearchQuery;
0 ignored issues
show
Documentation introduced by
The doc-type SearchQuery; could not be parsed: Expected "|" or "end of type", but got ";" at position 11. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
61
     */
62 View Code Duplication
    public function between($min, $max, $dateFormat = null)
0 ignored issues
show
Duplication introduced by
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...
63
    {
64
        $params = ['gte' => $min, 'lte' => $max];
65
        if ($dateFormat) {
66
            $params['format'] = $dateFormat;
67
        }
68
        $this->query->addFilter('range', [$this->field => $params]);
69
        return $this->query;
70
    }
71
72
    /**
73
     * Добавить фильтр "больше или равно"
74
     * @param $value - значение
75
     * @param null $dateFormat - необязательный формат даты
76
     * @return SearchQuery
77
     */
78 View Code Duplication
    public function greaterOrEqual($value, $dateFormat = null)
0 ignored issues
show
Duplication introduced by
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...
79
    {
80
        $params = ['gte' => $value];
81
        if ($dateFormat) {
82
            $params['format'] = $dateFormat;
83
        }
84
        $this->query->addFilter('range', [$this->field => $params]);
85
        return $this->query;
86
    }
87
88
    /**
89
     * Добавить фильтр "больше чем"
90
     * @param $value - значение
91
     * @param null $dateFormat - необязательный формат даты
92
     * @return SearchQuery
93
     */
94 View Code Duplication
    public function greater($value, $dateFormat = null)
0 ignored issues
show
Duplication introduced by
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...
95
    {
96
        $params = ['gt' => $value];
97
        if ($dateFormat) {
98
            $params['format'] = $dateFormat;
99
        }
100
        $this->query->addFilter('range', [$this->field => $params]);
101
        return $this->query;
102
    }
103
104
    /**
105
     * Добавить фильтр "меньше или равно"
106
     * @param $value - значение
107
     * @param null $dateFormat - необязательный формат даты
108
     * @return SearchQuery
109
     */
110 View Code Duplication
    public function lessOrEqual($value, $dateFormat = null)
0 ignored issues
show
Duplication introduced by
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...
111
    {
112
        $params = ['lte' => $value];
113
        if ($dateFormat) {
114
            $params['format'] = $dateFormat;
115
        }
116
        $this->query->addFilter('range', [$this->field => $params]);
117
        return $this->query;
118
    }
119
120
121
    /**
122
     * Добавить фильтр "меньше чем"
123
     * @param $value - значение
124
     * @param null $dateFormat - - необязательный формат даты
125
     * @return SearchQuery
126
     */
127 View Code Duplication
    public function less($value, $dateFormat = null)
0 ignored issues
show
Duplication introduced by
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...
128
    {
129
        $params = ['lt' => $value];
130
        if ($dateFormat) {
131
            $params['format'] = $dateFormat;
132
        }
133
        $this->query->addFilter('range', [$this->field => $params]);
134
        return $this->query;
135
    }
136
137
138
    /**
139
     * Добавить фильтр полнотекстового поиска, этот фильтр влияет на поле релевантности _score.
140
     *
141
     * @param $text - поисковая фраза
142
     * @return SearchQuery;
0 ignored issues
show
Documentation introduced by
The doc-type SearchQuery; could not be parsed: Expected "|" or "end of type", but got ";" at position 11. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
143
     */
144
    public function match($text)
145
    {
146
        if (is_array($this->field)) {
147
            $this->query->addFilter('multi_match', [
148
                'query'  => $text,
149
                'fields' => $this->field
150
            ]);
151
        } else {
152
            $this->query->addFilter('match', [$this->field => $text]);
153
        }
154
        return $this->query;
155
    }
156
157
}