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
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ class Elastica_Admin
* @param array $args 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/
* @param array $args 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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Elastica_Client
* Default host
*/
const DEFAULT_HOST = 'localhost';

/**
* Number of seconds after a timeout occurs for every request
* 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) {
return new Elastica_Index($this, $indexName);
}

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

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

/**
* Uses _bulk to send documents to the server
*
*
* Array of Elastica_Document as input. Index and type has to be
* set inside the document, because for bulk settings documents,
* documents can belong to any type and index
*
*
* @link http://www.elasticsearch.com/docs/elasticsearch/rest_api/bulk/
* @param array $docs Array of Elastica_Document
* @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) {

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';

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

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

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

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

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

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


/**
* Deletes documents with the given ids, index, type from the index
*
*
* @link http://www.elasticsearch.com/docs/elasticsearch/rest_api/bulk/
* @param array $ids Document ids
* @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');
}
$path = '_bulk';

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

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

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

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

$path = '_bulk';

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

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

/**
* Makes calls to the elasticsearch server based on this index
*
*
* It's possible to make any REST query directly over this method
*
*
* @param string $path Path to call
* @param string $method Rest method to use (GET, POST, DELETE, PUT)
* @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);
return $this->_callService($request);
}

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

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

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

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

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

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

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

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

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

$response = new Elastica_Response($response);
if (defined('DEBUG') && DEBUG) {

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

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

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

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

0 comments on commit b84e2f2

Please sign in to comment.