/** * remove the eav deleted products from elasticsearch index * * @param array $productIds */ public function cleanElasticSearchIndex(array $productIds) { foreach ($productIds as $productId) { try { $this->_type->deleteById($productId); } catch (Exception $e) { //continue if $productId not found //TODO Logging continue; } } }
/** * @param integer $id * @param Type $type */ public function removeIndexById($id, Type $type) { try { $type->deleteById($id); } catch (\InvalidArgumentException $e) { } catch (NotFoundException $e) { } }
/** * @group functional */ public function testOptimize() { $index = $this->_createIndex(); $type = new Type($index, 'optimize'); $docs = array(); $docs[] = new Document(1, array('foo' => 'bar')); $docs[] = new Document(2, array('foo' => 'bar')); $type->addDocuments($docs); $index->refresh(); $stats = $index->getStats()->getData(); $this->assertEquals(0, $stats['_all']['primaries']['docs']['deleted']); $type->deleteById(1); $index->refresh(); $stats = $index->getStats()->getData(); $this->assertEquals(1, $stats['_all']['primaries']['docs']['deleted']); $index->optimize(array('max_num_segments' => 1)); $stats = $index->getStats()->getData(); $this->assertEquals(0, $stats['_all']['primaries']['docs']['deleted']); }
/** * @group functional */ public function testDeleteById() { $index = $this->_createIndex(); $type = new Type($index, 'user'); // Adds hans, john and rolf to the index $docs = array(new Document(1, array('username' => 'hans', 'test' => array('2', '3', '5'))), new Document(2, array('username' => 'john', 'test' => array('1', '3', '6'))), new Document(3, array('username' => 'rolf', 'test' => array('2', '3', '7'))), new Document('foo/bar', array('username' => 'georg', 'test' => array('4', '2', '5')))); $type->addDocuments($docs); $index->refresh(); // sanity check for rolf $resultSet = $type->search('rolf'); $this->assertEquals(1, $resultSet->count()); $data = $resultSet->current()->getData(); $this->assertEquals('rolf', $data['username']); // delete rolf $type->deleteById(3); $index->refresh(); // rolf should no longer be there $resultSet = $type->search('rolf'); $this->assertEquals(0, $resultSet->count()); // sanity check for id with slash $resultSet = $type->search('georg'); $this->assertEquals(1, $resultSet->count()); // delete georg $type->deleteById('foo/bar'); $index->refresh(); // georg should no longer be there $resultSet = $type->search('georg'); $this->assertEquals(0, $resultSet->count()); // it should not be possible to delete the entire type with this method try { $type->deleteById(''); $this->fail('Delete with empty string id should fail'); } catch (\InvalidArgumentException $e) { $this->assertTrue(true); } try { $type->deleteById(' '); $this->fail('Delete with one space string id should fail'); } catch (\InvalidArgumentException $e) { $this->assertTrue(true); } try { $type->deleteById(null); $this->fail('Delete with null id should fail'); } catch (\InvalidArgumentException $e) { $this->assertTrue(true); } try { $type->deleteById(array()); $this->fail('Delete with empty array id should fail'); } catch (\InvalidArgumentException $e) { $this->assertTrue(true); } try { $type->deleteById('*'); $this->fail('Delete request should fail because of invalid id: *'); } catch (NotFoundException $e) { $this->assertTrue(true); } try { $type->deleteById('*:*'); $this->fail('Delete request should fail because document with id *.* does not exist'); } catch (NotFoundException $e) { $this->assertTrue(true); } try { $type->deleteById('!'); $this->fail('Delete request should fail because document with id ! does not exist'); } catch (NotFoundException $e) { $this->assertTrue(true); } $index->refresh(); // rolf should no longer be there $resultSet = $type->search('john'); $this->assertEquals(1, $resultSet->count()); }
/** * @test */ public function doDeleteReturnsFalseWhenNotFound() { $id = 1; $this->type->deleteById($this->getCacheId($id))->shouldBeCalled()->willThrow('\\Elastica\\Exception\\NotFoundException'); self::assertFalse($this->cache->delete($id)); }
/** * @param Hotel $hotel * * @return $this */ public function removeHotel($hotel) { $this->hotelType->deleteById($hotel->getId()); }