|
253 | 253 | ->and($mustNot['nested']['query'])->toBe(['exists' => ['field' => 'comments.country']]); |
254 | 254 | }); |
255 | 255 |
|
| 256 | +it('whereNestedFieldExists emits nested+exists for the path itself', function () { |
| 257 | + $dsl = Post::whereNestedFieldExists('comments')->toDsl(); |
| 258 | + |
| 259 | + $clause = $dsl['body']['query']; |
| 260 | + expect($clause)->toHaveKey('nested') |
| 261 | + ->and($clause['nested']['path'])->toBe('comments') |
| 262 | + ->and($clause['nested']['query'])->toBe(['exists' => ['field' => 'comments']]); |
| 263 | +}); |
| 264 | + |
| 265 | +it('whereNestedFieldExists matches posts that have nested entries', function () { |
| 266 | + expect(Post::whereNestedFieldExists('comments')->count())->toBe(4); |
| 267 | +}); |
| 268 | + |
| 269 | +it('whereNestedFieldDoesntExist wraps nested inside must_not', function () { |
| 270 | + $dsl = Post::whereNestedFieldDoesntExist('comments')->toDsl(); |
| 271 | + |
| 272 | + $mustNot = $dsl['body']['query']['bool']['must_not'][0]; |
| 273 | + expect($mustNot)->toHaveKey('nested') |
| 274 | + ->and($mustNot['nested']['path'])->toBe('comments') |
| 275 | + ->and($mustNot['nested']['query'])->toBe(['exists' => ['field' => 'comments']]); |
| 276 | +}); |
| 277 | + |
| 278 | +it('orWhereNestedFieldExists composes with other clauses', function () { |
| 279 | + $dsl = Post::where('status', 99)->orWhereNestedFieldExists('comments')->toDsl(); |
| 280 | + |
| 281 | + $should = $dsl['body']['query']['bool']['should'] ?? null; |
| 282 | + expect($should)->not->toBeNull(); |
| 283 | +}); |
| 284 | + |
256 | 285 | it('can search with field boosting', function () { |
257 | 286 | $users = User::search('John', 'best_fields', ['name' => 5, 'description' => 1])->get(); |
258 | 287 | expect($users)->toHaveCount(2) |
|
0 commit comments