Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New index service #9

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Some small changes on create/remove indexes
  • Loading branch information
poratuk committed Sep 1, 2017
commit c48e427a37d32e2cb20174a150b7b13a42104405
25 changes: 17 additions & 8 deletions src/Pho/Kernel/Services/Index/Adapters/Elasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,35 @@ class Elasticsearch extends Index
private $dbname = 'phonetworks';
private $tablename = 'phoindex';

/**
* Setup function.
* Init elasticsearch connection. Run indexing on runned events
* @param Kernel $kernel Kernel of pho
* @param array $params Sended params to the index.
*/
public function __construct(Kernel $kernel, array $params = [])
{

$this->kernel = $kernel;
$this->dbname = getenv('INDEX_DB')?: $this->dbname;
$this->tablename = getenv('INDEX_TABLE')?: $this->tablename;

$host = [$params['host'] ?: getenv('INDEX_URL') ?: 'http://127.0.0.1:9200/'];
$client = new \Elasticsearch\ClientBuilder($host);
$this->client = $client->build();

$indexParams['index'] = $this->dbname;
if ($this->client->indices()->exists($indexParams)) {
$this->client->indices()->delete($indexParams);
}

if ( ! $this->client->indices()->exists($indexParams)) {
$params = ['index' => $this->dbname];

$this->client->indices()->create($params);
$this->client->indices()->create($indexParams);
$this->kernel->logger()->info("Created Elasticsearch index with name: %s", $this->dbname);
}

$kernel->on('kernel.booted_up', array($this, 'kernelBooted'));
}

public function kernelBooted()
{
var_dump('Kernel booted');
$this->kernel->graph()->on('node.added', array($this, 'index'));
}

/**
Expand Down
11 changes: 8 additions & 3 deletions src/Pho/Kernel/Services/Index/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class Index implements IndexInterface, ServiceInterface
{
private $kernel;

public function __construct(Kernel $kernel, array $params = [])
{
$this->kernel = $kernel;
}

/**
* Indexes an entity.
*
Expand All @@ -30,7 +35,7 @@ class Index implements IndexInterface, ServiceInterface
*
* @return void
*/
public function index(EntityInterface $entity, bool $new=false):void
public function index(EntityInterface $entity, bool $new = false): void
{
$classes = [get_class($entity) => get_class($entity)] + class_parents($entity);
if ($new) {
Expand All @@ -51,14 +56,14 @@ public function index(EntityInterface $entity, bool $new=false):void
*/
public function search(string $value, string $key = null, array $classes = array()): array
{
return $this->searchInIndex($value, $key, $classes);
return $this->searchInIndex($value, $key, $classes);
}

/**
* Searches through the index with given key and its value.
*
* Returns the entity IDs as string
*
*
* @param string $value Value to search
* @param string $key The key to search for. Optional.
* @param array $classes The object classes to search for. Optional.
Expand Down
13 changes: 12 additions & 1 deletion tests/Pho/Kernel/Services/Indexing/IndexingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,24 @@ public function setUp()
putenv('INDEX_TABLE=phoindex');

parent::setUp();

$client = new \Elasticsearch\ClientBuilder();
$this->client = $client->build();

$indexParams = [];
$indexParams['index'] = getenv("INDEX_DB");
if ($this->client->indices()->exists($indexParams)) {
$this->client->indices()->delete($indexParams);
}

$this->client->indices()->create($indexParams);

$this->client = $client->build();
}

public function tearDown()
{
//$this->client->indices()->delete(['index' => getenv('INDEX_DB')]);
$this->client->indices()->delete(['index' => getenv('INDEX_DB')]);
}

public function testCreatedIndex()
Expand Down