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

Upgrade master to 1.0 #18

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
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
6 changes: 6 additions & 0 deletions CHANGELOG-1.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Release Notes for 1.0.x

## [Unreleased]

- Upgrading the library to Elastic Search V6

10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Baka Phalcon Elastic Search
# Baka Phalcon ElasticSearch

Phalcon Elastic Search package to index / query model with relationship easily

Expand All @@ -9,9 +9,13 @@ Phalcon Elastic Search package to index / query model with relationship easily
2. [Search](#markdown-header-QueryParser)
4. [Testing](#markdown-header-QueryParser-Extended)

## ElasticSearch 6.x configuration
- `indices.query.bool.max_clause_count' => 1000000` if you are doing long searches for lots of conditionals
- [Elasticsearch 6.x Cheatsheet](http://elasticsearch-cheatsheet.jolicode.com/)

## Installing
Packages:
- `"elasticsearch/elasticsearch": "~2.0@beta"`
- `"elasticsearch/elasticsearch": "^6.1"`
- `"baka/database": "dev-master"`
- `"phalcon/incubator": "~3.0","`

Expand Down Expand Up @@ -137,4 +141,4 @@ Example

```
codecept run
```
```
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@
],
"minimum-stability": "dev",
"require": {
"php": ">=7.1",
"php": ">=7.2",
"ext-phalcon": ">=3.0.0",
"vlucas/phpdotenv": "^2.0",
"phalcon/incubator": "~3.3",
"monolog/monolog": "^1.16",
"baka/database": "~0.1.0",
"baka/http": "^0.1.1",
"elasticsearch/elasticsearch": "^2.3.1"
"baka/database": "^0.5",
"baka/http": "^0.5",
"elasticsearch/elasticsearch": "^6.1"
},
"require-dev": {
"codeception/verify": "*",
"codeception/codeception": "2.4",
"duncan3dc/fork-helper": "^2.2.0"
},
"autoload": {
Expand Down
57 changes: 57 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0"?>
<ruleset name="Phalcon">
<description>Phalcon Coding Standards</description>
<arg value="-colors" />
<arg value="s" />
<rule ref="PSR2">
<exclude name="PSR2.Methods.MethodDeclaration.Underscore" />
<exclude name="Generic.Files.LineLength.TooLong" />
<exclude name="PSR1.Files.SideEffects.FoundWithSymbols" />
</rule>

<!-- 2.3 Lines -->

<!-- The soft limit on line length MUST be 120 characters; automated style checkers MUST warn but MUST NOT error at the soft limit. -->
<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="120" />
<property name="absoluteLineLimit" value="0" />
</properties>
</rule>

<!-- 2.5 Keywords and True/False/Null -->

<!-- PHP keywords MUST be in lower case. -->
<rule ref="Generic.PHP.LowerCaseKeyword" />

<!-- The PHP constants true, false, and null MUST be in lower case. -->
<rule ref="Generic.PHP.LowerCaseConstant" />

<!-- Visibility MUST be declared on all methods. -->
<rule ref="Squiz.Scope.MethodScope" />
<rule ref="Squiz.WhiteSpace.ScopeKeywordSpacing" />

<!-- 5. Control Structures -->

<!-- The general style rules for control structures are as follows:
There MUST be one space after the control structure keyword
There MUST NOT be a space after the opening parenthesis
There MUST NOT be a space before the closing parenthesis
There MUST be one space between the closing parenthesis and the opening brace
The structure body MUST be indented once
The closing brace MUST be on the next line after the body -->
<rule ref="Squiz.ControlStructures.ControlSignature" />
<rule ref="Squiz.WhiteSpace.ControlStructureSpacing.SpacingAfterOpen" />
<rule ref="Squiz.WhiteSpace.ControlStructureSpacing.SpacingBeforeClose" />
<rule ref="Squiz.WhiteSpace.ScopeClosingBrace" />
<rule ref="Squiz.ControlStructures.ForEachLoopDeclaration" />
<rule ref="Squiz.ControlStructures.ForLoopDeclaration" />
<rule ref="Squiz.ControlStructures.LowercaseDeclaration" />
<!-- checked by PSR2.ControlStructures.ControlStructureSpacing -->

<file>tests/cli</file>
<file>tests/integration</file>
<file>tests/unit</file>
<file>tests/_data/fixtures/Traits</file>
<file>tests/_support/Helper</file>
</ruleset>
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;
}
}
67 changes: 67 additions & 0 deletions src/Contracts/CustomFiltersSchemaTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Baka\Elasticsearch\Contracts;

/**
* Search controller.
*/
trait CustomFiltersSchemaTrait
{
/**
* 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