Skip to content

Commit ef398dd

Browse files
committed
Merge pull request #573 from xyu/delete-by-query-params
Allow for request params in delete by query calls
2 parents 5796de8 + 4c5d40f commit ef398dd

File tree

5 files changed

+54
-8
lines changed

5 files changed

+54
-8
lines changed

changes.txt

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
CHANGES
22

3+
2014-03-20
4+
- Allow for request params in delete by query calls
5+
36
2014-03-17
47
- Added filters: AbstractGeoShape, GeoShapePreIndexed, GeoShapeProvided #568
58

lib/Elastica/Type.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -440,19 +440,21 @@ public function deleteIds(array $ids)
440440
/**
441441
* Deletes entries in the db based on a query
442442
*
443-
* @param \Elastica\Query|string $query Query object
443+
* @param \Elastica\Query|string $query Query object
444+
* @param array $options Optional params
444445
* @return \Elastica\Response
445446
* @link http://www.elasticsearch.org/guide/reference/api/delete-by-query.html
446447
*/
447-
public function deleteByQuery($query)
448+
public function deleteByQuery($query, array $options = array())
448449
{
449450
if (is_string($query)) {
450451
// query_string queries are not supported for delete by query operations
451-
return $this->request('_query', Request::DELETE, array(), array('q' => $query));
452+
$options['q'] = $query;
453+
return $this->request('_query', Request::DELETE, array(), $options);
452454
}
453455
$query = Query::create($query);
454456

455-
return $this->request('_query', Request::DELETE, array('query' => $query->getQuery()));
457+
return $this->request('_query', Request::DELETE, array('query' => $query->getQuery()), $options);
456458
}
457459

458460
/**

test/lib/Elastica/Test/Aggregation/BaseAggregationTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ protected function tearDown()
2121
}
2222
}
2323

24-
protected function _createIndex($name = 'test', $delete = true)
24+
protected function _createIndex($name = 'test', $delete = true, $shards = 1)
2525
{
26-
return parent::_createIndex('test_aggregation_' . $name, $delete);
26+
return parent::_createIndex('test_aggregation_' . $name, $delete, $shards);
2727
}
2828
}

test/lib/Elastica/Test/Base.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ protected function _getClient()
1414
/**
1515
* @param string $name Index name
1616
* @param bool $delete Delete index if it exists
17+
* @param int $shards Number of shards to create
1718
* @return \Elastica\Index
1819
*/
19-
protected function _createIndex($name = 'test', $delete = true)
20+
protected function _createIndex($name = 'test', $delete = true, $shards = 1)
2021
{
2122
$client = $this->_getClient();
2223
$index = $client->getIndex('elastica_' . $name);
23-
$index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), $delete);
24+
$index->create(array('index' => array('number_of_shards' => $shards, 'number_of_replicas' => 0)), $delete);
2425

2526
return $index;
2627
}

test/lib/Elastica/Test/TypeTest.php

+40
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,46 @@ public function testDeleteByQueryWithQuery()
396396
$this->assertEquals(0, $response->count());
397397
}
398398

399+
public function testDeleteByQueryWithQueryAndOptions()
400+
{
401+
$index = $this->_createIndex('test', true, 2);
402+
$type = new Type($index, 'test');
403+
$type->addDocument(new Document(1, array('name' => 'ruflin nicolas')));
404+
$type->addDocument(new Document(2, array('name' => 'ruflin')));
405+
$index->refresh();
406+
407+
$response = $index->search('ruflin*');
408+
$this->assertEquals(2, $response->count());
409+
410+
$response = $index->search('nicolas');
411+
$this->assertEquals(1, $response->count());
412+
413+
// Route to the wrong document id; should not delete
414+
$response = $type->deleteByQuery(new SimpleQueryString('nicolas'), array('routing'=>'2'));
415+
$this->assertTrue($response->isOk());
416+
417+
$index->refresh();
418+
419+
$response = $index->search('ruflin*');
420+
$this->assertEquals(2, $response->count());
421+
422+
$response = $index->search('nicolas');
423+
$this->assertEquals(1, $response->count());
424+
425+
// Delete first document
426+
$response = $type->deleteByQuery(new SimpleQueryString('nicolas'), array('routing'=>'1'));
427+
$this->assertTrue($response->isOk());
428+
429+
$index->refresh();
430+
431+
// Makes sure, document is deleted
432+
$response = $index->search('ruflin*');
433+
$this->assertEquals(1, $response->count());
434+
435+
$response = $index->search('nicolas');
436+
$this->assertEquals(0, $response->count());
437+
}
438+
399439
/**
400440
* Test to see if Elastica_Type::getDocument() is properly using
401441
* the fields array when available instead of _source

0 commit comments

Comments
 (0)