Skip to content

Commit 36235c6

Browse files
committed
add whereNull and whereNotNull function
1 parent 512e3bb commit 36235c6

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,47 @@ The package is developed and tested under Elasticsearch ``v6.*``. It should be a
258258
$q->orWhere(...)->orWhere(...);
259259
})->get()->toArray()
260260
```
261+
#### whereNull
262+
* Parameters
263+
264+
| Name | Required | Type | Default | Description |
265+
|:--------:|:--------:|:-----------------------:|:---------:|:-----------------------------------------------------:|
266+
| column | Y | ``string`` | | Column or relation |
267+
* Output
268+
269+
``self``
270+
271+
* Examples
272+
1. Find all users with no name
273+
```php
274+
User::es()->whereNull('name')->get()
275+
```
276+
2. Find all users with no address
277+
```php
278+
// Find the user whose name starts with 'Leo'
279+
User::es()->whereNull('Addresses')->get()
280+
```
281+
282+
#### whereNotNull
283+
* Parameters
284+
285+
| Name | Required | Type | Default | Description |
286+
|:--------:|:--------:|:-----------------------:|:---------:|:-----------------------------------------------------:|
287+
| column | Y | ``string`` | | Column or relation |
288+
* Output
289+
290+
``self``
291+
292+
* Examples
293+
1. Find all users with a name
294+
```php
295+
User::es()->whereNotNull('name')->get()
296+
```
297+
2. Find all users with at least one address
298+
```php
299+
// Find the user whose name starts with 'Leo'
300+
User::es()->whereNotNull('Addresses')->get()
301+
```
261302
262303
#### orWhere
263304

src/LaravelElasticsearchQueryBuilder.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,12 @@ public function where($column, $operator = null, $value = null, $or = false, $bo
180180
$this->validateValue($column_bak, $value);
181181
switch($operator) {
182182
case '=':
183-
$this->query['bool'][$or ? 'should' : 'filter'][] = $value !== null ? ['term' => [$column => $value]] : ['missing' => ['field' => $column]];
183+
$this->query['bool'][$or ? 'should' : 'filter'][] = $value !== null ? ['term' => [$column => $value]] :
184+
['bool' => ['must_not' => [
185+
['exists' => [
186+
'field' => $column
187+
]]
188+
]]];
184189
break;
185190
case '<':
186191
$this->query['bool'][$or ? 'should' : 'filter'][] = ['range' => [$column => ['lt' => $value]]];
@@ -229,7 +234,11 @@ public function where($column, $operator = null, $value = null, $or = false, $bo
229234
}
230235

231236
public function whereNull($column) {
237+
return $this->where(snake_case($column), '=', null);
238+
}
232239

240+
public function whereNotNull($column) {
241+
return $this->where(snake_case($column), '!=', null);
233242
}
234243

235244
/**
@@ -389,10 +398,10 @@ public function whereHasNull($column, $closure = null, $or = false) {
389398
'must_not' => [
390399
[
391400
'nested' => [
392-
'path' => strtolower($column),
401+
'path' => snake_case($column),
393402
'query' => [
394403
'exists' => [
395-
'field' => strtolower($column)
404+
'field' => snake_case($column)
396405
]
397406
]
398407
]

0 commit comments

Comments
 (0)