|
8 | 8 | use Elastica\Query\SimpleQueryString;
|
9 | 9 | use Elastica\Query\Term;
|
10 | 10 | use Elastica\Request;
|
| 11 | +use Elastica\Script\Script; |
11 | 12 | use Elastica\Status;
|
12 | 13 | use Elastica\Test\Base as BaseTest;
|
13 | 14 | use Elastica\Type;
|
@@ -290,6 +291,74 @@ public function testDeleteByQueryWithQueryAndOptions()
|
290 | 291 | $this->assertEquals(0, $response->count());
|
291 | 292 | }
|
292 | 293 |
|
| 294 | + /** |
| 295 | + * @group functional |
| 296 | + */ |
| 297 | + public function testUpdateByQueryWithQueryString() |
| 298 | + { |
| 299 | + $index = $this->_createIndex(); |
| 300 | + $type1 = new Type($index, 'test1'); |
| 301 | + $type1->addDocument(new Document(1, ['name' => 'ruflin nicolas'])); |
| 302 | + $type1->addDocument(new Document(2, ['name' => 'ruflin'])); |
| 303 | + $index->refresh(); |
| 304 | + |
| 305 | + $response = $index->search('ruflin*'); |
| 306 | + $this->assertEquals(2, $response->count()); |
| 307 | + |
| 308 | + $response = $index->search('nicolas'); |
| 309 | + $this->assertEquals(1, $response->count()); |
| 310 | + |
| 311 | + // Update the element, searched by specific word. Should match first one |
| 312 | + $response = $index->updateByQuery('nicolas', new Script('ctx._source.name = "marc"')); |
| 313 | + $this->assertTrue($response->isOk()); |
| 314 | + |
| 315 | + $index->refresh(); |
| 316 | + |
| 317 | + // Makes sure first element is updated and renamed to marc. Should match only second |
| 318 | + $response = $index->search('ruflin*'); |
| 319 | + $this->assertEquals(1, $response->count()); |
| 320 | + |
| 321 | + $response = $index->search('marc*'); |
| 322 | + $this->assertEquals(1, $response->count()); |
| 323 | + |
| 324 | + $response = $index->search('nicolas'); |
| 325 | + $this->assertEquals(0, $response->count()); |
| 326 | + } |
| 327 | + |
| 328 | + /** |
| 329 | + * @group functional |
| 330 | + */ |
| 331 | + public function testUpdateByQueryAll() |
| 332 | + { |
| 333 | + $index = $this->_createIndex(); |
| 334 | + $type1 = new Type($index, 'test1'); |
| 335 | + $type1->addDocument(new Document(1, ['name' => 'ruflin nicolas'])); |
| 336 | + $type1->addDocument(new Document(2, ['name' => 'ruflin'])); |
| 337 | + $index->refresh(); |
| 338 | + |
| 339 | + $response = $index->search('ruflin*'); |
| 340 | + $this->assertEquals(2, $response->count()); |
| 341 | + |
| 342 | + $response = $index->search('nicolas'); |
| 343 | + $this->assertEquals(1, $response->count()); |
| 344 | + |
| 345 | + // Update all elements to name "marc" |
| 346 | + $response = $index->updateByQuery('*', new Script('ctx._source.name = "marc"')); |
| 347 | + $this->assertTrue($response->isOk()); |
| 348 | + |
| 349 | + $index->refresh(); |
| 350 | + |
| 351 | + // Because all documents have changed to marc, searching by "ruflin*" should match 0 |
| 352 | + $response = $index->search('ruflin*'); |
| 353 | + $this->assertEquals(0, $response->count()); |
| 354 | + |
| 355 | + $response = $index->search('marc'); |
| 356 | + $this->assertEquals(2, $response->count()); |
| 357 | + |
| 358 | + $response = $index->search('nicolas'); |
| 359 | + $this->assertEquals(0, $response->count()); |
| 360 | + } |
| 361 | + |
293 | 362 | /**
|
294 | 363 | * @group functional
|
295 | 364 | */
|
|
0 commit comments