Permalink
Browse files

Use elastica instead of own implementation.

  • Loading branch information...
1 parent b6e2e8f commit deb33889083265c6e398a7920908307b14260810 @Baachi Baachi committed Mar 20, 2013
View
@@ -11,7 +11,7 @@
"doctrine/common": "2.3.*"
},
"require-dev": {
- "kriswallsmith/buzz": "0.7.*",
+ "ruflin/Elastica": "0.19.*",
"doctrine/orm": "2.3.*",
"doctrine/mongodb": "1.0.*",
"doctrine/mongodb-odm": "1.0.*"
@@ -20,99 +20,62 @@
namespace Doctrine\Search\ElasticSearch;
use Doctrine\Search\SearchClientInterface;
-use Doctrine\Common\Persistence\ObjectManager;
-use Doctrine\Search\Http\ClientInterface as HttpClientInterface;
-
-use Doctrine\Search\Exception\Json\JsonEncodeException;
-use Doctrine\Search\Exception\Json\JsonDecodeException;
-
/**
* SearchManager for ElasticSearch-Backend
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @author Mike Lohmann <mike.h.lohmann@googlemail.com>
+ * @author Markus Bachmann <markus.bachmann@bachi.biz>
*/
class Client implements SearchClientInterface
{
-
+ /**
+ * @var \Elastica_Client
+ */
private $client;
- private $host;
-
- private $port;
-
- /*
- * @param Connection $conn
- * @param Configuration $config
+ /**
+ * @param \Elastica_Client $client
*/
- public function __construct(HttpClientInterface $client = null, $host = 'localhost', $port = '9200')
+ public function __construct(\Elastica_Client $client)
{
- $this->host = (string)$host;
- $this->port = (string)$port;
- $this->client = $client ? : new \Doctrine\Search\Http\Client\BuzzClient();
+ $this->client = $client;
}
/**
- *
- * @see Doctrine\Search.SearchClientInterface::find()
+ * {@inheritDoc}
*/
public function find($index, $type, $query)
{
- $searchUrl = $this->host . ':' . $this->port . '/' . (string)$index . '/' . (string)$type . '/_search?' . (string)$query;
-
- $response = $this->client->sendRequest('GET', $searchUrl);
-
- $content = $response->getContent();
- $decodedJson = json_decode($content);
-
- if ($decodedJson == NULL) {
- throw new JsonDecodeException();
- }
-
- return $decodedJson;
-
+ $index = $this->client->getIndex($index);
+ return iterator_to_array($index->search($query));
}
/**
- * (non-PHPdoc)
- * @see Doctrine\Search.SearchClientInterface::createIndex()
- *
- * @return Doctrine\Search\Http\ResponseInterface
+ * {@inheritDoc}
*/
public function createIndex($index, $type, array $data)
{
- $encodedJson = json_encode($data);
-
- if ($encodedJson == NULL) {
- throw new JsonEncodeException($data);
- }
- $indexUrl = $this->host . ':' . $this->port . '/' . (string)$index . '/' . (string)$type . '/';
-
- return $this->client->sendRequest('PUT', $indexUrl, $encodedJson);
-
- }
-
- public function updateIndex(array $data)
- {
+ $index = $this->client->getIndex($index);
+ $index->create();
+ $index->addDocuments($data);
}
/**
- *
- * @param array $data
+ * {@inheritDoc}
*/
public function deleteIndex($index)
{
- // TODO: Implement deleteIndex() method.
+ $index = $this->client->getIndex($index);
+ $index->delete();
}
/**
- * @param array $data
+ * {@inheritDoc}
*/
public function bulkSearch(array $data)
{
- // TODO: Implement bulkAction() method.
}
-
}
@@ -1,36 +0,0 @@
-<?php
-/*
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * This software consists of voluntary contributions made by many individuals
- * and is licensed under the LGPL. For more information, see
- * <http://www.doctrine-project.org>.
- */
-
-namespace Doctrine\Search\ElasticSearch;
-
-/**
- * Configuration handler for ElasticSearch-Backend
- *
- * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
- * @author Mike Lohmann <mike.h.lohmann@googlemail.com>
- */
-class Configuration
-{
-
-
- public function __construct()
- {
-
- }
-}
@@ -1,52 +0,0 @@
-<?php
-/*
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * This software consists of voluntary contributions made by many individuals
- * and is licensed under the LGPL. For more information, see
- * <http://www.doctrine-project.org>.
- */
-
-namespace Doctrine\Search\ElasticSearch;
-
-use Doctrine\Search\Http\ClientInterface as HttpClientInterface;
-
-/**
- * Connections handler for ElasticSearch-Backend
- *
- * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
- * @author Mike Lohmann <mike.h.lohmann@googlemail.com>
- */
-class Connection
-{
- private $host;
-
- private $port;
-
- private $path;
-
- private $httpClient;
-
- public function __construct($host = null, $port = null, $path = null, HttpClientInterface $httpClient)
- {
- $this->host = $host;
- $this->port = $port;
- $this->path = $path;
- $this->httpClient = $httpClient;
- }
-
- public function initialize()
- {
-
- }
-}
@@ -1,44 +0,0 @@
-<?php
-namespace Doctrine\Search\Http\Client;
-
-use \Doctrine\Search\Http\ClientInterface;
-
-abstract class AbstractClient implements ClientInterface
-{
-
- protected $options = array(
- 'host' => 'localhost',
- 'port' => '80',
- 'timeout' => 10,
- 'keep-alive' => true,
- 'username' => '',
- 'password' => '',
- );
-
- public function __construct($host = 'localhost', $port = 80, $username = '', $password = '')
- {
- $this->setOption('host', rtrim($host, '/'));
- $this->setOption('port', $port);
- $this->setOption('username', $username);
- $this->setOption('password', $password);
- }
-
- public function getOption($key)
- {
- if (!isset($this->options[$key])) {
- throw new Exception(sprintf('The option %s is not available', $key));
- }
-
- return $this->options[$key];
- }
-
- public function setOption($key, $value)
- {
- if (!isset($this->options[$key])) {
- throw new Exception(sprintf('The option %s does not exist', $key));
- }
-
- $this->options[$key] = $value;
- }
-
-}
@@ -1,50 +0,0 @@
-<?php
-namespace Doctrine\Search\Http\Client;
-
-use Doctrine\Search\Http\Response\BuzzResponse as Response;
-use Buzz\Browser;
-
-class BuzzClient extends AbstractClient
-{
- protected $browser;
-
- public function __construct(Browser $browser = null, $host = 'localhost', $port = 80, $username = '', $password = '')
- {
- $this->browser = $browser ? : new Browser();
-
- parent::__construct($host, $port, $username, $password);
- }
-
- /**
- * Send a request
- *
- * @param string $method The request method
- * @param array $headers Some http headers
- * @param string $body POST variables
- * @return ResponseInterface
- */
- public function sendRequest($method = 'get', $path = '/', $data = '')
- {
- $url = $this->getOption('host') . ':' . $this->getOption('port');
- $headers = array();
-
- $username = $this->getOption('username');
- $password = $this->getOption('password');
-
- if (null !== $username && null !== $password) {
- $headers['Authorization'] = sprintf('Basic: %s', base64_encode($username . ':' . $password));
- }
-
- $headers['Connection'] = (true === $this->getOption('keep-alive') ? 'Keep-Alive' : 'Close');
-
- if ($method === 'post') {
- $response = $this->browser->call($url, $method, $headers, $data);
- } else {
- $response = $this->browser->call($url, $method, $headers);
- }
-
- return new Response($response);
- }
-
-
-}
Oops, something went wrong.

0 comments on commit deb3388

Please sign in to comment.