Skip to content
This repository was archived by the owner on Nov 4, 2021. It is now read-only.

Commit 7215621

Browse files
committed
ElasticEngine refactoring
1 parent a78fd3e commit 7215621

File tree

2 files changed

+83
-213
lines changed

2 files changed

+83
-213
lines changed

src/Builders/FilterBuilder.php

Lines changed: 54 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
class FilterBuilder extends Builder
88
{
9+
public $wheres = [
10+
'must' => [],
11+
'must_not' => []
12+
];
13+
914
public function __construct($model, $callback = null)
1015
{
1116
$this->model = $model;
@@ -28,98 +33,103 @@ public function where($field, $value)
2833
$operator = '=';
2934
}
3035

31-
$this->wheres[] = [
32-
'type' => 'basic',
33-
'field' => $field,
34-
'operator' => $operator,
35-
'value' => $value
36-
];
36+
switch ($operator) {
37+
case '=':
38+
$this->wheres['must'][] = ['term' => [$field => $value]];
39+
break;
40+
41+
case '>':
42+
$this->wheres['must'][] = ['range' => [$field => ['gt' => $value]]];
43+
break;
44+
45+
case '<';
46+
$this->wheres['must'][] = ['range' => [$field => ['lt' => $value]]];
47+
break;
48+
49+
case '>=':
50+
$this->wheres['must'][] = ['range' => [$field => ['gte' => $value]]];
51+
break;
52+
53+
case '<=':
54+
$this->wheres['must'][] = ['range' => [$field => ['lte' => $value]]];
55+
break;
56+
57+
case '!=':
58+
case '<>':
59+
$this->wheres['must_not'][] = ['term' => [$field => $value]];
60+
break;
61+
}
3762

3863
return $this;
3964
}
4065

4166
public function whereIn($field, array $value)
4267
{
43-
$this->wheres[] = [
44-
'type' => 'in',
45-
'field' => $field,
46-
'not' => false,
47-
'value' => $value
48-
];
68+
$this->wheres['must'][] = ['terms' => [$field => $value]];
4969

5070
return $this;
5171
}
5272

5373
public function whereNotIn($field, array $value)
5474
{
55-
$this->wheres[] = [
56-
'type' => 'in',
57-
'field' => $field,
58-
'not' => true,
59-
'value' => $value
60-
];
75+
$this->wheres['must_not'][] = ['terms' => [$field => $value]];
6176

6277
return $this;
6378
}
6479

6580
public function whereBetween($field, array $value)
6681
{
67-
$this->wheres[] = [
68-
'type' => 'between',
69-
'field' => $field,
70-
'not' => false,
71-
'value' => $value
72-
];
82+
$this->wheres['must'][] = ['range' => [$field => ['gte' => $value[0], 'lte' => $value[1]]]];
7383

7484
return $this;
7585
}
7686

7787
public function whereNotBetween($field, array $value)
7888
{
79-
$this->wheres[] = [
80-
'type' => 'between',
81-
'field' => $field,
82-
'not' => true,
83-
'value' => $value
84-
];
89+
$this->wheres['must_not'][] = ['range' => [$field => ['gte' => $value[0], 'lte' => $value[1]]]];
8590

8691
return $this;
8792
}
8893

8994
public function whereExists($field)
9095
{
91-
$this->wheres[] = [
92-
'type' => 'exists',
93-
'field' => $field,
94-
'not' => false
95-
];
96+
$this->wheres['must'][] = ['exists' => ['field' => $field]];
9697

9798
return $this;
9899
}
99100

100101
public function whereNotExists($field)
101102
{
102-
$this->wheres[] = [
103-
'type' => 'exists',
104-
'field' => $field,
105-
'not' => true
103+
$this->wheres['must_not'][] = [
104+
'exists' => [
105+
'field' => $field
106+
]
106107
];
107108

108109
return $this;
109110
}
110111

111112
public function whereRegexp($field, $value, $flags = 'ALL')
112113
{
113-
$this->wheres[] = [
114-
'type' => 'regexp',
115-
'field' => $field,
116-
'value' => $value,
117-
'flags' => $flags
114+
$this->wheres['must'][] = [
115+
'regexp' => [
116+
$field => [
117+
'value' => $value,
118+
'flags' => $flags
119+
]
120+
]
118121
];
119122

120123
return $this;
121124
}
122125

126+
public function orderBy($column, $direction = 'asc')
127+
{
128+
$this->orders[] = [$column => strtolower($direction) == 'asc' ? 'asc' : 'desc'];
129+
130+
return $this;
131+
}
132+
123133
public function explain()
124134
{
125135
return $this->engine()->explain($this);

0 commit comments

Comments
 (0)