Skip to content

Commit 512e3bb

Browse files
committed
Make whereHasNull and orWhereHasNull accept sub query as second parameter
1 parent 84c26d0 commit 512e3bb

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,8 @@ The package is developed and tested under Elasticsearch ``v6.*``. It should be a
404404
405405
| Name | Required | Type | Default | Description |
406406
|:--------:|:--------:|:-----------------------:|:---------:|:-----------------------------------------------------:|
407-
| relation | Y | ``string``, ``callable``| | Must be capitalized |
407+
| relation | Y | ``string`` | | Must be capitalized |
408+
| closure | | ``null``, ``callable`` |``null`` | |
408409
409410
* Output
410411
@@ -416,12 +417,20 @@ The package is developed and tested under Elasticsearch ``v6.*``. It should be a
416417
// find all users with no someRelation
417418
User::es()->whereHasNull('someRelations')->first();
418419
```
420+
2. with sub query
421+
```php
422+
// find all users with no someRelation with id = 1
423+
User::es()->whereHasNull('someRelations', function($q) {
424+
$q->where('id', 1);
425+
})->first();
426+
```
419427
#### orWhereHasNull
420428
* Parameters
421429
422430
| Name | Required | Type | Default | Description |
423431
|:--------:|:--------:|:-----------------------:|:---------:|:-----------------------------------------------------:|
424-
| relation | Y | ``string``, ``callable``| | Must be capitalized |
432+
| relation | Y | ``string`` | | Must be capitalized |
433+
| closure | | ``null``, ``callable`` |``null`` | |
425434
426435
* Output
427436

src/LaravelElasticsearchQueryBuilder.php

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -378,35 +378,51 @@ public function whereHas($column, $closure = null, $or = false, $boost = false)
378378

379379
/**
380380
* @param $column
381+
* @param null $closure
381382
* @param bool $or
382383
* @return $this
383384
*/
384-
public function whereHasNull($column, $or = false) {
385-
$this->query['bool'][$or ? 'should' : 'filter'][] = [
386-
'bool' => [
387-
'must_not' => [
388-
[
389-
'nested' => [
390-
'path' => strtolower($column),
391-
'query' => [
392-
'exists' => [
393-
'field' => strtolower($column)
385+
public function whereHasNull($column, $closure = null, $or = false) {
386+
if($closure === null) {
387+
$this->query['bool'][$or ? 'should' : 'filter'][] = [
388+
'bool' => [
389+
'must_not' => [
390+
[
391+
'nested' => [
392+
'path' => strtolower($column),
393+
'query' => [
394+
'exists' => [
395+
'field' => strtolower($column)
396+
]
394397
]
395398
]
396399
]
397400
]
398401
]
399-
]
400-
];
402+
];
403+
} else {
404+
$column_bak = $column;
405+
$this->getMappingProperty($column, true);
406+
$builder = $this->nested_queries[$column_bak] ?? new LaravelElasticsearchQueryBuilder($this->model, $column_bak);
407+
$closure($builder);
408+
$nested_query = $this->createNestedQuery($column_bak, $builder, '');
409+
$this->query['bool'][$or ? 'should' : 'filter'][] = [
410+
'bool' => [
411+
'must_not' => $nested_query
412+
]
413+
];
414+
}
415+
401416
return $this;
402417
}
403418

404419
/**
405420
* @param $column
421+
* @param null $closure
406422
* @return LaravelElasticsearchQueryBuilder
407423
*/
408-
public function orWhereHasNull($column) {
409-
return $this->whereHasNull($column, true);
424+
public function orWhereHasNull($column, $closure = null) {
425+
return $this->whereHasNull($column, $closure, $or = true);
410426
}
411427

412428
/**

0 commit comments

Comments
 (0)