Skip to content

Commit

Permalink
Exception refactoring. Or filter added, Terms query added. Code refac…
Browse files Browse the repository at this point in the history
…toring.
  • Loading branch information
ruflin committed Feb 2, 2011
1 parent 4e0ad94 commit b84e2f2
Show file tree
Hide file tree
Showing 61 changed files with 435 additions and 305 deletions.
Empty file modified README.markdown 100644 → 100755
Empty file.
Empty file modified build.xml 100644 → 100755
Empty file.
6 changes: 3 additions & 3 deletions lib/Elastica/Admin.php 100644 → 100755
Expand Up @@ -13,14 +13,14 @@ class Elastica_Admin
* @param array $args Args * @param array $args Args
*/ */
public function addAliases(array $args) { public function addAliases(array $args) {
throw new Elastica_Exception('Not implemented yet'); throw new Elastica_Exception_NotImplemented();
} }

/** /**
* @link http://www.elasticsearch.com/docs/elasticsearch/rest_api/admin/indices/aliases/ * @link http://www.elasticsearch.com/docs/elasticsearch/rest_api/admin/indices/aliases/
* @param array $args Args * @param array $args Args
*/ */
public function removeAliases(array $args) { public function removeAliases(array $args) {
throw new Elastica_Exception('Not implemented yet'); throw new Elastica_Exception_NotImplemented();
} }
} }
96 changes: 48 additions & 48 deletions lib/Elastica/Client.php 100644 → 100755
Expand Up @@ -17,7 +17,7 @@ class Elastica_Client
* Default host * Default host
*/ */
const DEFAULT_HOST = 'localhost'; const DEFAULT_HOST = 'localhost';

/** /**
* Number of seconds after a timeout occurs for every request * Number of seconds after a timeout occurs for every request
* If using indexing of file large value necessary. * If using indexing of file large value necessary.
Expand Down Expand Up @@ -58,44 +58,44 @@ public function __construct($host = self::DEFAULT_HOST, $port = self::DEFAULT_PO
public function getIndex($indexName) { public function getIndex($indexName) {
return new Elastica_Index($this, $indexName); return new Elastica_Index($this, $indexName);
} }

/** /**
* Returns host the client connects to * Returns host the client connects to
* *
* @return string Host * @return string Host
*/ */
public function getHost() { public function getHost() {
return $this->_host; return $this->_host;
} }

/** /**
* Returns connection port of this client * Returns connection port of this client
* *
* @return int Connection port * @return int Connection port
*/ */
public function getPort() { public function getPort() {
return intval($this->_port); return intval($this->_port);
} }

/** /**
* Uses _bulk to send documents to the server * Uses _bulk to send documents to the server
* *
* Array of Elastica_Document as input. Index and type has to be * Array of Elastica_Document as input. Index and type has to be
* set inside the document, because for bulk settings documents, * set inside the document, because for bulk settings documents,
* documents can belong to any type and index * documents can belong to any type and index
* *
* @link http://www.elasticsearch.com/docs/elasticsearch/rest_api/bulk/ * @link http://www.elasticsearch.com/docs/elasticsearch/rest_api/bulk/
* @param array $docs Array of Elastica_Document * @param array $docs Array of Elastica_Document
* @return Elastica_Response Response object * @return Elastica_Response Response object
* @throws Elastica_Exception If docs is empty * @throws Elastica_Exception_Invalid If docs is empty
*/ */
public function addDocuments(array $docs) { public function addDocuments(array $docs) {

if (empty($docs)) { if (empty($docs)) {
throw new Elastica_Exception('Array has to consist of at least one element'); throw new Elastica_Exception_Invalid('Array has to consist of at least one element');
} }
$path = '_bulk'; $path = '_bulk';

$queryString = ''; $queryString = '';
foreach($docs as $doc) { foreach($docs as $doc) {
$baseArray = array( $baseArray = array(
Expand All @@ -105,29 +105,29 @@ public function addDocuments(array $docs) {
'_id' => $doc->getId() '_id' => $doc->getId()
) )
); );

// Always newline needed // Always newline needed
$queryString .= json_encode($baseArray) . PHP_EOL; $queryString .= json_encode($baseArray) . PHP_EOL;

$docArray = array( $docArray = array(
$doc->getType() => $doc->getData() $doc->getType() => $doc->getData()
); );

$queryString .= json_encode($docArray) . PHP_EOL; $queryString .= json_encode($docArray) . PHP_EOL;
} }

return $this->request($path, Elastica_Request::PUT, $queryString); return $this->request($path, Elastica_Request::PUT, $queryString);
} }

public function deleteDocuments(array $docs) { public function deleteDocuments(array $docs) {
// TODO: similar to delete ids but with type and index inside files // TODO: similar to delete ids but with type and index inside files
throw new Elastica_Exception('not implemented yet'); throw new Elastica_Exception('not implemented yet');
} }


/** /**
* Deletes documents with the given ids, index, type from the index * Deletes documents with the given ids, index, type from the index
* *
* @link http://www.elasticsearch.com/docs/elasticsearch/rest_api/bulk/ * @link http://www.elasticsearch.com/docs/elasticsearch/rest_api/bulk/
* @param array $ids Document ids * @param array $ids Document ids
* @param string $index Index name * @param string $index Index name
Expand All @@ -140,7 +140,7 @@ public function deleteIds(array $ids, $index, $type) {
throw new Elastica_Exception('Array has to consist of at least one id'); throw new Elastica_Exception('Array has to consist of at least one id');
} }
$path = '_bulk'; $path = '_bulk';

$queryString = ''; $queryString = '';
foreach($ids as $id) { foreach($ids as $id) {
$baseArray = array( $baseArray = array(
Expand All @@ -150,26 +150,26 @@ public function deleteIds(array $ids, $index, $type) {
'_id' => $id, '_id' => $id,
) )
); );

// Always newline needed // Always newline needed
$queryString .= json_encode($baseArray) . PHP_EOL; $queryString .= json_encode($baseArray) . PHP_EOL;
} }


return $this->request($path, Elastica_Request::PUT, $queryString); return $this->request($path, Elastica_Request::PUT, $queryString);
} }

/** /**
* Bukl operation * Bukl operation
* *
* Every entry in the params array has to exactly on array * Every entry in the params array has to exactly on array
* of the bulk operation. An example param array would be: * of the bulk operation. An example param array would be:
* *
* array( * array(
* array('index' => array('_index' => 'test', '_type' => 'user', '_id' => '1')), * array('index' => array('_index' => 'test', '_type' => 'user', '_id' => '1')),
* array('user' => array('name' => 'hans')), * array('user' => array('name' => 'hans')),
* array('delete' => array('_index' => 'test', '_type' => 'user', '_id' => '2')) * array('delete' => array('_index' => 'test', '_type' => 'user', '_id' => '2'))
* ); * );
* *
* @todo Test * @todo Test
* @link http://www.elasticsearch.com/docs/elasticsearch/rest_api/bulk/ * @link http://www.elasticsearch.com/docs/elasticsearch/rest_api/bulk/
* @param array $params Parameter array * @param array $params Parameter array
Expand All @@ -181,21 +181,21 @@ public function bulk(array $params) {
} }


$path = '_bulk'; $path = '_bulk';

$queryString = ''; $queryString = '';
foreach($params as $index => $baseArray) { foreach($params as $index => $baseArray) {
// Always newline needed // Always newline needed
$queryString .= json_encode($baseArray) . PHP_EOL; $queryString .= json_encode($baseArray) . PHP_EOL;
} }


return $this->request($path, Elastica_Request::PUT, $queryString); return $this->request($path, Elastica_Request::PUT, $queryString);
} }

/** /**
* Makes calls to the elasticsearch server based on this index * Makes calls to the elasticsearch server based on this index
* *
* It's possible to make any REST query directly over this method * It's possible to make any REST query directly over this method
* *
* @param string $path Path to call * @param string $path Path to call
* @param string $method Rest method to use (GET, POST, DELETE, PUT) * @param string $method Rest method to use (GET, POST, DELETE, PUT)
* @param array $data OPTIONAL Arguments as array * @param array $data OPTIONAL Arguments as array
Expand All @@ -205,22 +205,22 @@ public function request($path, $method, $data = array()) {
$request = new Elastica_Request($path, $method, $data); $request = new Elastica_Request($path, $method, $data);
return $this->_callService($request); return $this->_callService($request);
} }

/** /**
* Optimizes all search indexes * Optimizes all search indexes
* *
* @link http://www.elasticsearch.com/docs/elasticsearch/rest_api/admin/indices/optimize/ * @link http://www.elasticsearch.com/docs/elasticsearch/rest_api/admin/indices/optimize/
* @return Elastica_Response Response object * @return Elastica_Response Response object
*/ */
public function optimizeAll($args = array()) { public function optimizeAll($args = array()) {
return $this->request('_optimize', Elastica_Request::POST, $args); return $this->request('_optimize', Elastica_Request::POST, $args);
} }

/** /**
* Makes calls to the elasticsearch server * Makes calls to the elasticsearch server
* *
* All calls that are made to the server are down over this function * All calls that are made to the server are down over this function
* *
* @param string $path Path to call * @param string $path Path to call
* @param string $method Rest method to use (GET, POST, DELETE, PUT) * @param string $method Rest method to use (GET, POST, DELETE, PUT)
* @param array $data OPTIONAL Arguments as array * @param array $data OPTIONAL Arguments as array
Expand All @@ -229,52 +229,52 @@ public function optimizeAll($args = array()) {
protected function _callService(Elastica_Request $request) { protected function _callService(Elastica_Request $request) {
$conn = curl_init(); $conn = curl_init();
$baseUri = 'http://' . $this->getHost() . ':' . $this->getPort() . '/'; $baseUri = 'http://' . $this->getHost() . ':' . $this->getPort() . '/';

$baseUri .= $request->getPath(); $baseUri .= $request->getPath();

curl_setopt($conn, CURLOPT_URL, $baseUri); curl_setopt($conn, CURLOPT_URL, $baseUri);
curl_setopt($conn, CURLOPT_TIMEOUT, self::TIMEOUT); curl_setopt($conn, CURLOPT_TIMEOUT, self::TIMEOUT);
curl_setopt($conn, CURLOPT_PORT, $this->getPort()); curl_setopt($conn, CURLOPT_PORT, $this->getPort());
curl_setopt($conn, CURLOPT_RETURNTRANSFER, 1) ; curl_setopt($conn, CURLOPT_RETURNTRANSFER, 1) ;
curl_setopt($conn, CURLOPT_CUSTOMREQUEST, $request->getMethod()); curl_setopt($conn, CURLOPT_CUSTOMREQUEST, $request->getMethod());

// TODO: REFACTOR // TODO: REFACTOR
$data = $request->getData(); $data = $request->getData();

if (!empty($data)) { if (!empty($data)) {
if (is_array($data)) { if (is_array($data)) {
$content = json_encode($data); $content = json_encode($data);
} else { } else {
$content = $data; $content = $data;
} }


// Escaping of / not necessary. Causes problems in base64 encoding of files // Escaping of / not necessary. Causes problems in base64 encoding of files
$content = str_replace('\/', '/', $content); $content = str_replace('\/', '/', $content);
curl_setopt($conn, CURLOPT_POSTFIELDS, $content); curl_setopt($conn, CURLOPT_POSTFIELDS, $content);
} }

$start = microtime(true); $start = microtime(true);
$response = curl_exec($conn); $response = curl_exec($conn);
$end = microtime(true); $end = microtime(true);

// Checks if error exists // Checks if error exists
$errorNumber = curl_errno($conn); $errorNumber = curl_errno($conn);


$response = new Elastica_Response($response); $response = new Elastica_Response($response);

if (defined('DEBUG') && DEBUG) { if (defined('DEBUG') && DEBUG) {
$response->setQueryTime($end - $start); $response->setQueryTime($end - $start);
$response->setTransferInfo(curl_getinfo($conn)); $response->setTransferInfo(curl_getinfo($conn));
} }

if ($response->hasError()) { if ($response->hasError()) {
throw new Elastica_Exception_Response($response); throw new Elastica_Exception_Response($response);
} }

if ($errorNumber > 0) { if ($errorNumber > 0) {
throw new Elastica_Exception_Client($errorNumber, $request, $response); throw new Elastica_Exception_Client($errorNumber, $request, $response);
} }

return $response; return $response;
} }
} }
Empty file modified lib/Elastica/Cluster.php 100644 → 100755
Empty file.

0 comments on commit b84e2f2

Please sign in to comment.