Skip to content

Commit

Permalink
Add Complex Filter for Canvas (#13)
Browse files Browse the repository at this point in the history
Add Complex Filter for Canvas
  • Loading branch information
kaioken authored May 10, 2019
2 parents 6c1e98d + ef95a6f commit 4dc7e35
Show file tree
Hide file tree
Showing 17 changed files with 606 additions and 493 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ELASTIC_HOST=127.0.0.1:9200
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"vlucas/phpdotenv": "^2.0",
"phalcon/incubator": "~3.3",
"monolog/monolog": "^1.16",
"baka/database": "^0.1",
"baka/http": "^0.1",
"baka/database": "^0.5",
"baka/http": "^0.5",
"elasticsearch/elasticsearch": "^6.1"
},
"require-dev": {
Expand Down
69 changes: 69 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Baka\Elasticsearch;

use Exception;
use GuzzleHttp\Client as GuzzleClient;

class Client
{
private $host;

/**
* Set the host.
*
* @param string $host
* @return void
*/
public function __construct(string $host)
{
$this->host = $host;
}

/**
* Given a SQL search the elastic indices.
*
* @param string $sql
* @return void
*/
public function findBySql(string $sql): array
{
$client = new GuzzleClient([
'base_uri' => $this->host,
]);

// since 6.x we need to use POST
$response = $client->post('/_sql', [
'body' => trim($sql),
'headers' => [
'content-type' => 'application/json',
'Accept' => 'application/json'
],
]);

//get the response in a array
$results = json_decode($response->getBody()->getContents(), true);

if ($results['hits']['total'] == 0) {
return [];
}

return $this->getResults($results);
}

/**
* Given the elastic results, return only the data.
*
* @param array $resonse
* @return array
*/
private function getResults(array $results): array
{
$data = [];
foreach ($results['hits']['hits'] as $result) {
$data[] = $result['_source'];
}

return $data;
}
}
70 changes: 70 additions & 0 deletions src/Contracts/CustomFiltresSchemaTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Baka\Elasticsearch\Contracts;

use Phalcon\Http\Response;
use stdClass;

/**
* Search controller.
*/
trait CustomFiltresSchemaTrait
{
/**
* Given the indice get the Schema configuration for the filter.
*
* @param string $indice
* @return array
*/
public function getSchema(string $index): array
{
$mapping = $this->elastic->indices()->getMapping([
'index' => $index,
]);

$mapping = array_shift($mapping);

//if we dont find mapping return empty
if (!isset($mapping['mappings'])) {
return [];
}

$mapping = array_shift($mapping);

//we only need the infro fromt he properto onward
//we want the result to be in a linear array so we pass it by reference
$result = [];
$results = $this->mappingToArray(array_shift($mapping)['properties'], null, $result);
rsort($results); //rever order?
return $results;
}

/**
* Generate the array map fromt he elastic search mapping.
*
* @param array $mappings
* @param string $parent
* @param array $result
* @return array
*/
protected function mappingToArray(array $mappings, string $parent = null, array &$result): array
{
foreach ($mappings as $key => $mapping) {
if (isset($mapping['type']) && $mapping['type'] != 'nested') {
$result[] = $parent . $key;
} elseif (isset($mapping['type']) && $mapping['type'] == 'nested' && is_array($mapping)) {
//setup key
$parent .= $key . '.';

//look for more records
$this->mappingToArray($mapping['properties'], $parent, $result);

//so we finisht with a child , we need to change the parent to one back
$parentExploded = explode('.', $parent);
$parent = count($parentExploded) > 2 ? $parentExploded[0] . '.' : null;
}
}

return $result;
}
}
Loading

0 comments on commit 4dc7e35

Please sign in to comment.