-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from byng-systems/3.2
Added a raw gateway to allow any query to any index
- Loading branch information
Showing
4 changed files
with
317 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
use Byng\Pimcore\Elasticsearch\PluginConfig\ConfigDistFilePath; | ||
use Byng\Pimcore\Elasticsearch\PluginConfig\ConfigFilePath; | ||
use Byng\Pimcore\Elasticsearch\Gateway\PageGatewayFactory; | ||
use Byng\Pimcore\Elasticsearch\Gateway\RawGatewayFactory; | ||
use Byng\Pimcore\Elasticsearch\PluginConfig\ConfigSchemaPath; | ||
use Elasticsearch\ClientBuilder; | ||
use Pimcore; | ||
|
@@ -35,6 +36,7 @@ | |
* @author Elliot Wright <[email protected]> | ||
* @author Matt Ward <[email protected]> | ||
* @author Michal Maszkiewicz | ||
* @author Asim Liaquat <[email protected]> | ||
*/ | ||
final class ElasticsearchPlugin extends AbstractPlugin implements PluginInterface | ||
{ | ||
|
@@ -44,9 +46,12 @@ final class ElasticsearchPlugin extends AbstractPlugin implements PluginInterfac | |
public function init() | ||
{ | ||
$config = self::loadConfig(); | ||
$hosts = $config->get("hosts"); | ||
|
||
$rawGatewayFactory = new RawGatewayFactory(); | ||
$rawGateway = $rawGatewayFactory->build($hosts); | ||
|
||
if ($types = $config->get("types")) { | ||
$hosts = $config->get("hosts"); | ||
$eventManager = Pimcore::getEventManager(); | ||
|
||
if ($assetConfig = $types->get("asset")) { | ||
|
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,145 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the "byng/pimcore-elasticsearch-plugin" project. | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the LICENSE is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Byng\Pimcore\Elasticsearch\Gateway; | ||
|
||
use Byng\Pimcore\Elasticsearch\Model\ResultsList; | ||
use Elasticsearch\Client; | ||
use Zend_EventManager_EventManager as ZendEventManager; | ||
use Byng\Pimcore\Elasticsearch\Query\QueryBuilder; | ||
|
||
/** | ||
* Raw Gateway | ||
* | ||
* @author Asim Liaquat <[email protected]> | ||
*/ | ||
final class RawGateway extends AbstractGateway | ||
{ | ||
/** | ||
* @var RawGateway | ||
*/ | ||
private static $instance; | ||
|
||
/** | ||
* @var Client | ||
*/ | ||
private $client; | ||
|
||
/** | ||
* @var ZendEventManager | ||
*/ | ||
private $pimcoreEventManager; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param Client $client | ||
* @param ZendEventManager $pimcoreEventManager | ||
*/ | ||
public function __construct( | ||
Client $client, | ||
ZendEventManager $pimcoreEventManager | ||
) { | ||
$this->client = $client; | ||
$this->pimcoreEventManager = $pimcoreEventManager; | ||
|
||
static::$instance = $this; | ||
} | ||
|
||
/** | ||
* Get an instance of this gateway after plugin initialisation | ||
* | ||
* @return RawGateway | ||
*/ | ||
public static function getInstance() | ||
{ | ||
if (null === static::$instance) { | ||
throw new \RuntimeException("No instance of RawGateway available, did the Elasticsearch plugin initialise correctly?"); | ||
} | ||
|
||
return static::$instance; | ||
} | ||
|
||
/** | ||
* Save the given data | ||
* | ||
* @param array $data | ||
* | ||
* @return void | ||
*/ | ||
public function save(array $data) | ||
{ | ||
$this->client->index($data); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function findBy( | ||
array $query, | ||
array $additionalOptions = [], | ||
array $resultOptions = [] | ||
) { | ||
throw new \Exception("Not implemented"); | ||
} | ||
|
||
/** | ||
* Perform a search on the given index and type. | ||
* | ||
* @param string $index | ||
* @param string $type | ||
* @param array $query | ||
* | ||
* @return ResultsList | ||
*/ | ||
public function findByArray($index, $type, array $query) | ||
{ | ||
return $this->doFind($index, $type, $query); | ||
} | ||
|
||
/** | ||
* Perform a search on the given index and type. | ||
* | ||
* @param string $index | ||
* @param string $type | ||
* @param QueryBuilder $query | ||
* | ||
* @return ResultsList | ||
*/ | ||
public function findByQueryBuilder($index, $type, QueryBuilder $query) | ||
{ | ||
return $this->doFind($index, $type, $query->toArray()); | ||
} | ||
|
||
/** | ||
* Perform a search on the given index and type. | ||
* | ||
* @param string $index | ||
* @param string $type | ||
* @param array $query | ||
* | ||
* @return ResultsList | ||
*/ | ||
private function doFind($index, $type, array $query) | ||
{ | ||
$result = $this->doSearch( | ||
$this->client, | ||
$index, | ||
$type, | ||
$query | ||
); | ||
|
||
return new ResultsList($result, $result["hits"]["total"]); | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
lib/Byng/Pimcore/Elasticsearch/Gateway/RawGatewayFactory.php
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,40 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the "byng/pimcore-elasticsearch-plugin" project. | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the LICENSE is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Byng\Pimcore\Elasticsearch\Gateway; | ||
|
||
use Elasticsearch\ClientBuilder; | ||
|
||
/** | ||
* RawGateway Factory | ||
* | ||
* @author Asim Liaquat <[email protected]> | ||
*/ | ||
final class RawGatewayFactory | ||
{ | ||
/** | ||
* Build the RawGateway | ||
* | ||
* @param Zend_Config $hosts | ||
* | ||
* @return PageGateway | ||
*/ | ||
public function build(\Zend_Config $hosts) | ||
{ | ||
$client = ClientBuilder::fromConfig([ | ||
"hosts" => $hosts->toArray() | ||
]); | ||
|
||
return new RawGateway($client, \Pimcore::getEventManager()); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -13,7 +13,10 @@ | |
|
||
namespace Byng\Pimcore\Elasticsearch\Query; | ||
|
||
use Byng\Pimcore\Elasticsearch\Query\QueryInterface; | ||
|
||
/** | ||
* | ||
* QueryBuilder defines the query to send to elasticsearch. | ||
* | ||
* @author Asim Liaquat <[email protected]> | ||
|
@@ -157,4 +160,127 @@ public function setSort(Sort $sort) | |
$this->sort = $sort; | ||
} | ||
|
||
/** | ||
* Converts the query builder into an array | ||
* | ||
* @return array | ||
*/ | ||
public function toArray() | ||
{ | ||
$body = []; | ||
|
||
if ($query = $this->getQuery()) { | ||
$body = $this->processQuery($query); | ||
} | ||
|
||
if ($filter = $this->getFilter()) { | ||
$body = array_merge($body, $this->processQuery($filter)); | ||
} | ||
|
||
if ($from = $this->getFrom()) { | ||
$body["from"] = $from; | ||
} | ||
|
||
if ($size = $this->getSize()) { | ||
$body["size"] = $size; | ||
} | ||
|
||
if ($sort = $this->getSort()) { | ||
$body["sort"] = $this->processQuery($sort); | ||
} | ||
|
||
return $body; | ||
} | ||
|
||
/** | ||
* Process a query into an array usable by Elasticsearch | ||
* | ||
* @param QueryInterface $query | ||
* | ||
* @return array | ||
*/ | ||
protected function processQuery(QueryInterface $query) | ||
{ | ||
switch ($query->getType()) { | ||
|
||
case "query": | ||
$result["query"] = $this->processQuery($query->getBoolQuery()); | ||
break; | ||
|
||
case "filter": | ||
$result["filter"] = $this->processQuery($query->getQuery()); | ||
break; | ||
|
||
case "bool": | ||
$boolResult = []; | ||
|
||
foreach ($query->getMust() as $must) { | ||
$boolResult["must"][] = $this->processQuery($must); | ||
} | ||
|
||
foreach ($query->getShould() as $should) { | ||
$boolResult["should"][] = $this->processQuery($should); | ||
} | ||
|
||
foreach ($query->getMustNot() as $mustNot) { | ||
$boolResult["must_not"][] = $this->processQuery($mustNot); | ||
} | ||
|
||
$result = []; | ||
$result["bool"] = $boolResult; | ||
|
||
break; | ||
|
||
case "match": | ||
$result = []; | ||
|
||
if ($operator = $query->getOperator()) { | ||
$result["match"][$query->getField()] = [ | ||
"query" => $query->getQuery(), | ||
"operator" => $operator | ||
]; | ||
} else { | ||
$result["match"][$query->getField()] = $query->getQuery(); | ||
} | ||
break; | ||
|
||
case "range": | ||
$result = []; | ||
$result["range"][$query->getField()] = $query->getRanges(); | ||
break; | ||
|
||
case "sort": | ||
$result = []; | ||
foreach ($query->getCriteria() as $column => $order) { | ||
$result[$column]["order"] = $order; | ||
} | ||
break; | ||
|
||
case "terms": | ||
$result["terms"] = [ | ||
$query->getField() => $query->getTerms() | ||
]; | ||
break; | ||
|
||
case "constant_score": | ||
$result["constant_score"] = $this->processQuery($query->getFilter()); | ||
break; | ||
|
||
case "nested": | ||
$result["nested"] = [ | ||
"path" => $query->getPath(), | ||
"query" => $this->processQuery($query->getQuery()) | ||
]; | ||
break; | ||
|
||
default: | ||
throw new \InvalidArgumentException(sprintf( | ||
"Unknown query type '%s' given.", | ||
$query->getType() | ||
)); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
} |