Example #1
0
 /**
  * Test deleteIds method using string parameters.
  *
  * This test ensures that the deleteIds method of
  * the \Elastica\Client can properly accept and use
  * an $index parameter and $type parameter that are
  * strings
  *
  * This test is a bit more verbose than just sending the
  * values to deleteIds and checking for exceptions or
  * warnings.
  *
  * It will add a document, search for it, then delete it
  * using the parameter types we are interested in, and then
  * re-search to verify that they have been deleted
  *
  * @group functional
  */
 public function testDeleteIdsIdxStringTypeString()
 {
     $data = array('username' => 'hans');
     $userSearch = 'username:hans';
     $index = $this->_createIndex(null, true, 2);
     // Create the index, deleting it first if it already exists
     $index->create(array(), true);
     $type = $index->getType('user');
     // Adds 1 document to the index
     $doc = new Document(null, $data);
     $doc->setRouting('first_routing');
     $result = $type->addDocument($doc);
     // Refresh index
     $index->refresh();
     $resultData = $result->getData();
     $ids = array($resultData['_id']);
     // Check to make sure the document is in the index
     $resultSet = $type->search($userSearch);
     $totalHits = $resultSet->getTotalHits();
     $this->assertEquals(1, $totalHits);
     // And verify that the variables we are doing to send to
     // deleteIds are the type we are testing for
     $idxString = $index->getName();
     $typeString = $type->getName();
     $this->assertTrue(is_string($idxString));
     $this->assertTrue(is_string($typeString));
     // Try to delete doc with a routing value which hashes to
     // a different shard then the id.
     $resp = $index->getClient()->deleteIds($ids, $index, $type, 'second_routing');
     // Refresh the index
     $index->refresh();
     // Research the index to verify that the items are still there
     $resultSet = $type->search($userSearch);
     $totalHits = $resultSet->getTotalHits();
     $this->assertEquals(1, $totalHits);
     // Using the existing $index and $type variables which
     // are \Elastica\Index and \Elastica\Type objects respectively
     $resp = $index->getClient()->deleteIds($ids, $index, $type, 'first_routing');
     // Refresh the index to clear out deleted ID information
     $index->refresh();
     // Research the index to verify that the items have been deleted
     $resultSet = $type->search($userSearch);
     $totalHits = $resultSet->getTotalHits();
     $this->assertEquals(0, $totalHits);
 }
Example #2
0
 /**
  * @group functional
  */
 public function testDeleteByQueryWithQueryAndOptions()
 {
     $this->_checkPlugin('delete-by-query');
     $index = $this->_createIndex(null, true, 2);
     $type = new Type($index, 'test');
     $doc = new Document(1, array('name' => 'ruflin nicolas'));
     $doc->setRouting('first_routing');
     $type->addDocument($doc);
     $doc = new Document(2, array('name' => 'ruflin'));
     $doc->setRouting('second_routing');
     $type->addDocument($doc);
     $index->refresh();
     $response = $index->search('ruflin*');
     $this->assertEquals(2, $response->count());
     $response = $index->search('ruflin*', array('routing' => 'first_routing'));
     $this->assertEquals(1, $response->count());
     $response = $index->search('nicolas');
     $this->assertEquals(1, $response->count());
     // Route to the wrong document id; should not delete
     $response = $type->deleteByQuery(new SimpleQueryString('nicolas'), array('routing' => 'second_routing'));
     $this->assertTrue($response->isOk());
     $index->refresh();
     $response = $index->search('ruflin*');
     $this->assertEquals(2, $response->count());
     $response = $index->search('nicolas');
     $this->assertEquals(1, $response->count());
     // Delete first document
     $response = $type->deleteByQuery(new SimpleQueryString('nicolas'), array('routing' => 'first_routing'));
     $this->assertTrue($response->isOk());
     $index->refresh();
     // Makes sure, document is deleted
     $response = $index->search('ruflin*');
     $this->assertEquals(1, $response->count());
     $response = $index->search('nicolas');
     $this->assertEquals(0, $response->count());
 }