Skip to content

Commit

Permalink
optimize count() method
Browse files Browse the repository at this point in the history
  • Loading branch information
basemkhirat committed Feb 27, 2017
1 parent 0966576 commit 2285e83
Showing 1 changed file with 41 additions and 10 deletions.
51 changes: 41 additions & 10 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -875,12 +875,25 @@ public function response($scroll_id = NULL)
}

/**
* Get the count of results
* Get the count of result
* @return mixed
*/
public function count()
{
return $this->get()->total;

$query = $this->query();

// Remove unsupported count query keys

unset(
$query["size"],
$query["from"],
$query["body"]["_source"],
$query["body"]["sort"]
);

return $this->connection->count($query)["count"];

}


Expand Down Expand Up @@ -981,12 +994,15 @@ public function insert($data, $_id = NULL)
}

$parameters = [
"index" => $this->getIndex(),
"body" => $data,
'client' => ['ignore' => $this->ignores]
];

if($type = $this->getType()){
if ($index = $this->getIndex()) {
$parameters["index"] = $index;
}

if ($type = $this->getType()) {
$parameters["type"] = $type;
}

Expand Down Expand Up @@ -1054,13 +1070,16 @@ public function update($data, $_id = NULL)
}

$parameters = [
"index" => $this->getIndex(),
"id" => $this->_id,
"body" => ['doc' => $data],
'client' => ['ignore' => $this->ignores]
];

if($type = $this->getType()){
if ($index = $this->getIndex()) {
$parameters["index"] = $index;
}

if ($type = $this->getType()) {
$parameters["type"] = $type;
}

Expand Down Expand Up @@ -1110,8 +1129,6 @@ public function script($script, $params = [])
{

$parameters = [
"index" => $this->getIndex(),
"type" => $this->getType(),
"id" => $this->_id,
"body" => [
"script" => [
Expand All @@ -1122,6 +1139,14 @@ public function script($script, $params = [])
'client' => ['ignore' => $this->ignores]
];

if ($index = $this->getIndex()) {
$parameters["index"] = $index;
}

if ($type = $this->getType()) {
$parameters["type"] = $type;
}

return (object)$this->connection->update($parameters);

}
Expand All @@ -1139,12 +1164,18 @@ public function delete($_id = NULL)
}

$parameters = [
"index" => $this->getIndex(),
"type" => $this->getType(),
"id" => $this->_id,
'client' => ['ignore' => $this->ignores]
];

if ($index = $this->getIndex()) {
$parameters["index"] = $index;
}

if ($type = $this->getType()) {
$parameters["type"] = $type;
}

return (object)$this->connection->delete($parameters);

}
Expand Down

0 comments on commit 2285e83

Please sign in to comment.