Completed
Push — devel ( f959ce...54aa50 )
by Alexey
02:09
created

Where::exists()   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
    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
    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) $params['format'] = $dateFormat;
121
        $this->query->addFilter('range', [$this->field => $params]);
122
        return $this->query;
123
    }
124
125
126
    /**
127
     * Добавить фильтр полнотекстового поиска, этот фильтр влияет на поле релевантности _score.
128
     *
129
     * @param $text - поисковая фраза
130
     * @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...
131
     */
132
    public function match($text)
133
    {
134
        if (is_array($this->field)) {
135
            $this->query->addFilter('multi_match', [
136
                'query'  => $text,
137
                'fields' => $this->field
138
            ]);
139
        } else {
140
            $this->query->addFilter('match', [$this->field => $text]);
141
        }
142
        return $this->query;
143
    }
144
145
    /**
146
     * Поле существует и имеет не null значение
147
     * @return SearchQuery
148
     */
149
    public function exists()
150
    {
151
        $this->query->addFilter('exists', ["field" => $this->field]);
152
        return $this->query;
153
    }
154
}