-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Complex Filter for Canvas
- Loading branch information
Showing
17 changed files
with
606 additions
and
493 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ELASTIC_HOST=127.0.0.1:9200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.