Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add request method parameter for search and count #1643

Merged
merged 1 commit into from
Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file based on the
* hits.total is now an object in the search response [hits.total](https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking-changes-7.0.html#_literal_hits_total_literal_is_now_an_object_in_the_search_response)
* Elastica\Reindex does not return an Index anymore but a Response.
* Elastica\Reindex->run() does not refresh the new Index after completion anymore. Use `$reindex->setParam(Reindex::REFRESH, 'wait_for')` instead.
* `Elastica\Search->search()` and `Elastica\Search->count()` use request method `POST` by default. Same for `Elastica\Index`, `Elastica\Type\AbstractType`, `Elastica\Type`.

### Bugfixes
* Always set the Guzzle `base_uri` to support connecting to multiple ES hosts. [#1618](https://github.com/ruflin/Elastica/pull/1618)
Expand All @@ -26,6 +27,7 @@ All notable changes to this project will be documented in this file based on the
* Added `ParentAggregation` [#1616](https://github.com/ruflin/Elastica/pull/1616)
* Elastica\Reindex missing options (script, remote, wait_for_completion, scroll...)
* Added `AdjacencyMatrix` aggregation [#1642](https://github.com/ruflin/Elastica/pull/1642)
* Added request method parameter to `Elastica\SearchableInterface->search()` and `Elastica\SearchableInterface->count()`. Same for `Elastica\Search`[#1441](https://github.com/ruflin/Elastica/issues/1441)

### Improvements
* Added `native_function_invocation` CS rule [#1606](https://github.com/ruflin/Elastica/pull/1606)
Expand Down
12 changes: 7 additions & 5 deletions lib/Elastica/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,32 +353,34 @@ public function createSearch($query = '', $options = null, BuilderInterface $bui
*
* @param string|array|\Elastica\Query $query Array with all query data inside or a Elastica\Query object
* @param int|array $options OPTIONAL Limit or associative array of options (option=>value)
* @param string $method OPTIONAL Request method (use const's) (default = Request::POST)
*
* @return \Elastica\ResultSet with all results inside
*
* @see \Elastica\SearchableInterface::search
*/
public function search($query = '', $options = null)
public function search($query = '', $options = null, $method = Request::POST)
{
$search = $this->createSearch($query, $options);

return $search->search();
return $search->search('', null, $method);
}

/**
* Counts results of query.
*
* @param string|array|\Elastica\Query $query Array with all query data inside or a Elastica\Query object
* @param string|array|\Elastica\Query $query Array with all query data inside or a Elastica\Query object
* @param string $method OPTIONAL Request method (use const's) (default = Request::POST)
*
* @return int number of documents matching the query
*
* @see \Elastica\SearchableInterface::count
*/
public function count($query = '')
public function count($query = '', $method = Request::POST)
{
$search = $this->createSearch($query);

return $search->count();
return $search->count('', false, $method);
}

/**
Expand Down
10 changes: 6 additions & 4 deletions lib/Elastica/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,12 +436,13 @@ public function getPath()
*
* @param mixed $query
* @param int|array $options OPTIONAL Limit or associative array of options (option=>value)
* @param string $method OPTIONAL Request method (use const's) (default = Request::POST)
*
* @throws \Elastica\Exception\InvalidException
*
* @return \Elastica\ResultSet
*/
public function search($query = '', $options = null)
public function search($query = '', $options = null, $method = Request::POST)
{
$this->setOptionsAndQuery($options, $query);

Expand All @@ -460,7 +461,7 @@ public function search($query = '', $options = null)

$response = $this->getClient()->request(
$path,
Request::GET,
$method,
$data,
$params
);
Expand All @@ -471,10 +472,11 @@ public function search($query = '', $options = null)
/**
* @param mixed $query
* @param $fullResult (default = false) By default only the total hit count is returned. If set to true, the full ResultSet including aggregations is returned
* @param string $method OPTIONAL Request method (use const's) (default = Request::POST)
*
* @return int|ResultSet
*/
public function count($query = '', $fullResult = false)
public function count($query = '', $fullResult = false, $method = Request::POST)
{
$this->setOptionsAndQuery(null, $query);

Expand All @@ -485,7 +487,7 @@ public function count($query = '', $fullResult = false)

$response = $this->getClient()->request(
$path,
Request::GET,
$method,
$query->toArray(),
[self::OPTION_SEARCH_TYPE => self::OPTION_SEARCH_TYPE_QUERY_THEN_FETCH]
);
Expand Down
8 changes: 5 additions & 3 deletions lib/Elastica/SearchableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,23 @@ interface SearchableInterface
*
* @param string|array|\Elastica\Query $query Array with all query data inside or a Elastica\Query object
* @param null $options
* @param string $method OPTIONAL Request method (use const's) (default = Request::POST)
*
* @return \Elastica\ResultSet with all results inside
*/
public function search($query = '', $options = null);
public function search($query = '', $options = null, $method = Request::POST);

/**
* Counts results for a query.
*
* If no query is set, matchall query is created
*
* @param string|array|\Elastica\Query $query Array with all query data inside or a Elastica\Query object
* @param string|array|\Elastica\Query $query Array with all query data inside or a Elastica\Query object
* @param string $method OPTIONAL Request method (use const's) (default = Request::POST)
*
* @return int number of documents matching the query
*/
public function count($query = '');
public function count($query = '', $method = Request::POST);

/**
* @param \Elastica\Query|string $query
Expand Down
12 changes: 7 additions & 5 deletions lib/Elastica/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,32 +349,34 @@ public function createSearch($query = '', $options = null, BuilderInterface $bui
*
* @param string|array|\Elastica\Query $query Array with all query data inside or a Elastica\Query object
* @param int|array $options OPTIONAL Limit or associative array of options (option=>value)
* @param string $method OPTIONAL Request method (use const's) (default = Request::POST)
*
* @return \Elastica\ResultSet with all results inside
*
* @see \Elastica\SearchableInterface::search
*/
public function search($query = '', $options = null)
public function search($query = '', $options = null, $method = Request::POST)
{
$search = $this->createSearch($query, $options);

return $search->search();
return $search->search('', null, $method);
}

/**
* Count docs by query.
*
* @param string|array|\Elastica\Query $query Array with all query data inside or a Elastica\Query object
* @param string|array|\Elastica\Query $query Array with all query data inside or a Elastica\Query object
* @param string $method OPTIONAL Request method (use const's) (default = Request::POST)
*
* @return int number of documents matching the query
*
* @see \Elastica\SearchableInterface::count
*/
public function count($query = '')
public function count($query = '', $method = Request::POST)
{
$search = $this->createSearch($query);

return $search->count();
return $search->count('', false, $method);
}

/**
Expand Down
13 changes: 8 additions & 5 deletions lib/Elastica/Type/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Elastica\Exception\InvalidException;
use Elastica\Index;
use Elastica\Query;
use Elastica\Request;
use Elastica\ResultSet;
use Elastica\Search;
use Elastica\SearchableInterface;
Expand Down Expand Up @@ -151,28 +152,30 @@ public function createSearch($query = '', $options = null): Search
*
* @param string|array|Query $query Array with all query data inside or a Elastica\Query object
* @param int|array $options
* @param string $method OPTIONAL Request method (use const's) (default = Request::POST)
*
* @return ResultSet with all results inside
*
* @see \Elastica\SearchableInterface::search
*/
public function search($query = '', $options = null): ResultSet
public function search($query = '', $options = null, $method = Request::POST): ResultSet
{
return $this->getType()->search($query, $options = null);
return $this->getType()->search($query, $options = null, $method);
}

/**
* Count docs in the type based on query.
*
* @param string|array|Query $query Array with all query data inside or a Elastica\Query object
* @param string|array|Query $query Array with all query data inside or a Elastica\Query object
* @param string $method OPTIONAL Request method (use const's) (default = Request::POST)
*
* @return int number of documents matching the query
*
* @see \Elastica\SearchableInterface::count
*/
public function count($query = ''): int
public function count($query = '', $method = Request::POST): int
{
return $this->getType()->count($query);
return $this->getType()->count($query, $method);
}

/**
Expand Down
48 changes: 48 additions & 0 deletions test/Elastica/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,33 @@ public function testCount()
$this->assertEquals(1, $index->count($query));
}

/**
* @group functional
*/
public function testCountGet()
{
$index = $this->_createIndex();

// Add document to normal index
$doc1 = new Document(null, ['name' => 'ruflin']);
$doc2 = new Document(null, ['name' => 'nicolas']);

$type = $index->getType('_doc');
$type->addDocument($doc1);
$type->addDocument($doc2);

$index->refresh();

$this->assertEquals(2, $index->count('', Request::GET));

$query = new Term();
$key = 'name';
$value = 'nicolas';
$query->setTerm($key, $value);

$this->assertEquals(1, $index->count($query, Request::GET));
}

/**
* @group functional
*/
Expand Down Expand Up @@ -696,6 +723,27 @@ public function testSearch()
$this->assertEquals(3, $count);
}

/**
* @group functional
*/
public function testSearchGet()
{
$index = $this->_createIndex();

$type = new Type($index, '_doc');

$docs = [];
$docs[] = new Document(1, ['username' => 'hans']);
$type->addDocuments($docs);
$index->refresh();

$resultSet = $index->search('hans', null, Request::GET);
$this->assertEquals(1, $resultSet->count());

$count = $index->count('hans', Request::GET);
$this->assertEquals(1, $count);
}

/**
* @group functional
*/
Expand Down
58 changes: 58 additions & 0 deletions test/Elastica/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Elastica\Query\FunctionScore;
use Elastica\Query\MatchAll;
use Elastica\Query\QueryString;
use Elastica\Request;
use Elastica\Response;
use Elastica\ResultSet;
use Elastica\Script\Script;
Expand Down Expand Up @@ -456,6 +457,18 @@ public function testSearchWithVersionOption()
$this->assertEquals(1, $hit->getParam('_version'));
}

/**
* @group functional
*/
public function testSearchGet()
{
$client = $this->_getClient();
$search1 = new Search($client);

$result = $search1->search([], [], 'GET');
$this->assertFalse($result->getResponse()->hasError());
}

/**
* @group functional
*/
Expand Down Expand Up @@ -501,6 +514,51 @@ public function testCountRequest()
$this->assertEquals(0, $count);
}

/**
* @group functional
*/
public function testCountRequestGet()
{
$client = $this->_getClient();
$search = new Search($client);

$index = $client->getIndex('zero');
$index->create(['settings' => ['index' => ['number_of_shards' => 1, 'number_of_replicas' => 0]]], true);

$type = $index->getType('_doc');
$type->addDocuments([
new Document(1, ['id' => 1, 'email' => '[email protected]', 'username' => 'farrelley']),
new Document(2, ['id' => 1, 'email' => '[email protected]', 'username' => 'farrelley']),
new Document(3, ['id' => 1, 'email' => '[email protected]', 'username' => 'farrelley']),
new Document(4, ['id' => 1, 'email' => '[email protected]', 'username' => 'farrelley']),
new Document(5, ['id' => 1, 'email' => '[email protected]', 'username' => 'farrelley']),
new Document(6, ['id' => 1, 'email' => '[email protected]', 'username' => 'marley']),
new Document(7, ['id' => 1, 'email' => '[email protected]', 'username' => 'marley']),
new Document(8, ['id' => 1, 'email' => '[email protected]', 'username' => 'marley']),
new Document(9, ['id' => 1, 'email' => '[email protected]', 'username' => 'marley']),
new Document(10, ['id' => 1, 'email' => '[email protected]', 'username' => 'marley']),
new Document(11, ['id' => 1, 'email' => '[email protected]', 'username' => 'marley']),
]);
$index->refresh();

$search->addIndex($index)->addType($type);

$count = $search->count('farrelley', false, Request::GET);
$this->assertEquals(5, $count);

$count = $search->count('marley', false, Request::GET);
$this->assertEquals(6, $count);

$count = $search->count('', false, Request::GET);
$this->assertEquals(6, $count, 'Uses previous query set');

$count = $search->count(new MatchAll(), false, Request::GET);
$this->assertEquals(11, $count);

$count = $search->count('bunny', false, Request::GET);
$this->assertEquals(0, $count);
}

/**
* @group functional
*/
Expand Down
Loading