Completed
Push — devel ( ca37cc...7592e3 )
by Alexey
02:06
created

Where::notExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
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 View Code Duplication
    public function in(array $values)
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...
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
    public function between($min, $max, $dateFormat = null)
63
    {
64
        $this->range(['gte' => $min, 'lte' => $max], $dateFormat);
65
        return $this->query;
66
    }
67
68
    /**
69
     * Добавить фильтр "больше или равно"
70
     * @param $value - значение
71
     * @param null $dateFormat - необязательный формат даты
72
     * @return SearchQuery
73
     */
74
    public function greaterOrEqual($value, $dateFormat = null)
75
    {
76
        $this->range(['gte' => $value], $dateFormat);
77
        return $this->query;
78
    }
79
80
    /**
81
     * Добавить фильтр "больше чем"
82
     * @param $value - значение
83
     * @param null $dateFormat - необязательный формат даты
84
     * @return SearchQuery
85
     */
86
    public function greater($value, $dateFormat = null)
87
    {
88
        $this->range(['gt' => $value], $dateFormat);
89
        return $this->query;
90
    }
91
92
    /**
93
     * Добавить фильтр "меньше или равно"
94
     * @param $value - значение
95
     * @param null $dateFormat - необязательный формат даты
96
     * @return SearchQuery
97
     */
98
    public function lessOrEqual($value, $dateFormat = null)
99
    {
100
        $this->range(['lte' => $value], $dateFormat);
101
        return $this->query;
102
    }
103
104
105
    /**
106
     * Добавить фильтр "меньше чем"
107
     * @param $value - значение
108
     * @param null $dateFormat - - необязательный формат даты
109
     * @return SearchQuery
110
     */
111
    public function less($value, $dateFormat = null)
112
    {
113
        $this->range(['lt' => $value], $dateFormat);
114
        return $this->query;
115
    }
116
117
118
    protected function range($params, $dateFormat=null)
119
    {
120
        if ($dateFormat) {
121
            $params['format'] = $dateFormat;
122
        }
123
        $this->query->addFilter('range', [$this->field => $params]);
124
        return $this->query;
125
    }
126
127
128
    /**
129
     * Добавить фильтр полнотекстового поиска, этот фильтр влияет на поле релевантности _score.
130
     *
131
     * @param $text - поисковая фраза
132
     * @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...
133
     */
134 View Code Duplication
    public function match($text)
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...
135
    {
136
        if (is_array($this->field)) {
137
            $this->query->addFilter('multi_match', [
138
                'query'  => $text,
139
                'fields' => $this->field
140
            ]);
141
        } else {
142
            $this->query->addFilter('match', [$this->field => $text]);
143
        }
144
        return $this->query;
145
    }
146
147
    /**
148
     * Поле существует и имеет не null значение
149
     * @return SearchQuery
150
     */
151
    public function exists()
152
    {
153
        $this->query->addFilter('exists', ["field" => $this->field]);
154
        return $this->query;
155
    }
156
157
    /**
158
     * @param $value
159
     * @return SearchQuery
160
     */
161
    public function not($value)
162
    {
163
        $this->query->addNotFilter('term', [$this->field => $value]);
164
        return $this->query;
165
    }
166
167
168
    /**
169
     * @param $values - массив допустимых значений
170
     * @example $query->where('channel')->notIn([1,2,3]);
171
     * @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...
172
     */
173 View Code Duplication
    public function notIn(array $values)
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...
174
    {
175
        // потому что ES не понимает дырки в ключах
176
        $values = array_values($values);
177
        $this->query->addNotFilter('terms', [$this->field => $values]);
178
        return $this->query;
179
    }
180
181
182
    /**
183
     * @param $min
184
     * @param $max
185
     * @param null $dateFormat
186
     * @return SearchQuery
187
     */
188
    public function notBetween($min, $max, $dateFormat = null)
189
    {
190
        $params = ['gte' => $min, 'lte' => $max];
191
        if ($dateFormat) {
192
            $params['format'] = $dateFormat;
193
        }
194
        $this->query->addNotFilter('range', [$this->field => $params]);
195
        return $this->query;
196
    }
197
198
    /**
199
     * @param $text
200
     * @return SearchQuery
201
     */
202 View Code Duplication
    public function notMatch($text)
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...
203
    {
204
        if (is_array($this->field)) {
205
            $this->query->addNotFilter('multi_match', [
206
                'query'  => $text,
207
                'fields' => $this->field
208
            ]);
209
        } else {
210
            $this->query->addNotFilter('match', [$this->field => $text]);
211
        }
212
        return $this->query;
213
    }
214
215
    /**
216
     * @return SearchQuery
217
     */
218
    public function notExists()
219
    {
220
        $this->query->addNotFilter('exists', ["field" => $this->field]);
221
        return $this->query;
222
    }
223
}