Skip to content

Commit

Permalink
Merge pull request #20 from hkulekci/feature/search-module
Browse files Browse the repository at this point in the history
#6 Search Module
  • Loading branch information
hkulekci committed Dec 5, 2015
2 parents 85b9356 + f751e8f commit 8f1d84b
Show file tree
Hide file tree
Showing 18 changed files with 597 additions and 3 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"zfcampus/zf-hal": "~1.0",
"zfcampus/zf-mvc-auth": "~1.0",
"zfcampus/zf-rest": "~1.0",
"predis/predis": "1.0.3"
"predis/predis": "1.0.3",
"ruflin/elastica":"2.3.1"
},

"require-dev": {
Expand Down
1 change: 1 addition & 0 deletions config/application.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
'Core',
'DoctrineModule',
'DoctrineORMModule',
'Search',
],
// Load only on www.* requests
APP_URI_FRONTEND => [
Expand Down
21 changes: 21 additions & 0 deletions config/autoload/elastica.php.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Elastica Configuration Override
*
* This configuration override file is for overriding environment-specific and
* security-sensitive configuration information. Copy this file without the
* .dist extension at the end and populate values as needed.
*
* @NOTE: This file is ignored from Git by default with the .gitignore included
* in ZendSkeletonApplication. This is a good practice, as it prevents sensitive
* credentials from accidentally being committed into version control.
*/

return [
'elastica' => [
'servers' => [
'host' => '127.0.0.1',
'port' => 9200,
]
]
];
11 changes: 9 additions & 2 deletions module/Search/config/module.config.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
<?php
return array(
);
return [
'service_manager' => [
'factories' => [
'search.client.elastic' => 'Search\Client\Factory\ElasticClientFactory',
'search.index.user' => 'Search\Index\Service\Factory\UserIndexServiceFactory',
'search.query.user' => 'Search\Query\Service\Factory\UserQueryServiceFactory',
],
],
];
15 changes: 15 additions & 0 deletions module/Search/src/Search/Client/ElasticClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Elasticsearch Client Service
*
* @since Oct 2015
* @author Haydar KULEKCI <[email protected]>
*/
namespace Search\Client;

use Elastica\Client as ElasticaClient;
use Search\Client\Interfaces\SearchClientInterface;

class ElasticClient extends ElasticaClient implements SearchClientInterface
{
}
29 changes: 29 additions & 0 deletions module/Search/src/Search/Client/Factory/ElasticClientFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Elastica Client Factory
*
* @since Nov 2015
* @author Haydar KULEKCI <[email protected]>
*/
namespace Search\Client\Factory;

use Search\Client\ElasticClient;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class ElasticClientFactory implements FactoryInterface
{
/**
* Create elastica client
*
* @param ServiceLocatorInterface $sm
* @return ElasticClient
*/
public function createService(ServiceLocatorInterface $sm)
{
$config = $sm->get('Config');
$clientConfig = isset($config['elastica']) ? $config['elastica'] : array();

return new ElasticClient($clientConfig);
}
}
26 changes: 26 additions & 0 deletions module/Search/src/Search/Client/Factory/SolrClientFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* Solra Client Factory
*
* @since Nov 2015
* @author Haydar KULEKCI <[email protected]>
*/
namespace Search\Client\Factory;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class SolrClientFactory implements FactoryInterface
{
/**
* Create Solr client
*
* @param ServiceLocatorInterface $sm
* @return SolrClient
*/
public function createService(ServiceLocatorInterface $sm)
{
//TODO: Return a new Solr Client here
// return new SolrClient($clientConfig);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Search Client Inteface
*
* @since Nov 2015
* @author Haydar KULEKCI <[email protected]>
*/
namespace Search\Client\Interfaces;

interface SearchClientInterface
{
}
12 changes: 12 additions & 0 deletions module/Search/src/Search/Exception/MissingDataException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Abstract Index Service
*
* @since Oct 2015
* @author Haydar KULEKCI <[email protected]>
*/
namespace Search\Exception;

class MissingDataException extends \InvalidArgumentException
{
}
31 changes: 31 additions & 0 deletions module/Search/src/Search/Index/Service/AbstractIndexService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Abstract Index Service
*
* @since Oct 2015
* @author Haydar KULEKCI <[email protected]>
*/
namespace Search\Index\Service;

use Search\Exception\MissingDataException;
use Search\Service\AbstractSearchService;

class AbstractIndexService extends AbstractSearchService
{
/**
*
* @param array $data
* @param integer $version
* @return null
*/
public function index(array $data, $version = 1)
{
if (!array_key_exists('id', $data)) {
throw new MissingDataException('`id` field is requeired to index data');
}

//TODO: this creation will be moved to a builder
$document = new \Elastica\Document($data['id'], $data);
$this->getType($version)->addDocument($document);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Abstract Index Service Factory
*
* @since Nov 2015
* @author Haydar KULEKCI <[email protected]>
*/
namespace Search\Index\Service\Factory;

use Search\Index\Service\UserIndexService;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class UserIndexServiceFactory implements FactoryInterface
{
/**
* Create service
*
* @param ServiceLocatorInterface $serviceLocator
* @return UserIndexService
*/
public function createService(ServiceLocatorInterface $sm)
{
$userIndexService = new UserIndexService($sm->get('search.client.elastic'));

return $userIndexService;
}
}
25 changes: 25 additions & 0 deletions module/Search/src/Search/Index/Service/UserIndexService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Abstract Index Service
*
* @since Oct 2015
* @author Haydar KULEKCI <[email protected]>
*/
namespace Search\Index\Service;

class UserIndexService extends AbstractIndexService
{
/**
* @var string|array
*/
protected $index = [
1 => 'users'
];

/**
* @var string|array
*/
protected $type = [
1 => 'user'
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Elasticsearch Paginator Adapter
*
* @since Nov 2015
* @author Haydar KULEKCI <[email protected]>
*/
namespace Search\Paginator\Adapter;

use Zend\Paginator\Adapter\ArrayAdapter;

class ElasticsearchAdapter extends ArrayAdapter
{
protected $meta = [];

/**
* Constructor.
*
* @param array $array ArrayAdapter to paginate
*/
public function __construct(array $array = array(), $count=0)
{
$this->array = $array;
$this->count = $count;
}

/**
* Returns an array of items for a page.
*
* @param int $offset Page offset
* @param int $itemCountPerPage Number of items per page
* @return array
*/
public function getItems($offset, $itemCountPerPage)
{
return $this->array;
}

public function getMeta($key)
{
if (isset($this->meta[$key])) {
return $this->meta[$key];
}

return null;
}

public function setMeta($key, $value)
{
$this->meta[$key] = $value;
}
}
Loading

0 comments on commit 8f1d84b

Please sign in to comment.