Maslosoft Manganel API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
<?php
/**
* This software package is licensed under `AGPL, Commercial` license[s].
*
* @package maslosoft/manganel
* @license AGPL, Commercial
*
* @copyright Copyright (c) Peter Maselkowski <pmaselkowski@gmail.com>
* @link https://maslosoft.com/manganel/
*/
namespace Maslosoft\Manganel;
use Elasticsearch\Common\Exceptions\BadRequest400Exception;
use Elasticsearch\Common\Exceptions\Missing404Exception;
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
use Maslosoft\Mangan\Interfaces\CriteriaAwareInterface;
use Maslosoft\Mangan\Interfaces\CriteriaInterface;
use Maslosoft\Mangan\Traits\CriteriaAwareTrait;
use Maslosoft\Manganel\Helpers\QueryBuilderDecorator;
use Maslosoft\Manganel\Helpers\RecursiveFilter;
use Maslosoft\Manganel\Helpers\TypeNamer;
use Maslosoft\Manganel\Traits\UniqueModelsAwareTrait;
use UnexpectedValueException;
/**
* QueryBuilder
* @method SearchCriteria getCriteria()
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class QueryBuilder implements CriteriaAwareInterface
{
use CriteriaAwareTrait,
UniqueModelsAwareTrait;
/**
* Manganel instance
* @var Manganel
*/
private $manganel = null;
public function __construct($model = null)
{
if (!empty($model))
{
$this->addModel($model);
}
if (!empty($model))
{
$this->manganel = Manganel::create($model);
}
else
{
$this->manganel = Manganel::fly();
}
}
/**
* Add model or array of models
* @param AnnotatedInterface|AnnotatedInterface[] $model
* @return void
*/
public function add($model)
{
if (is_array($model))
{
foreach ($model as $m)
{
$this->addModel($m);
}
return;
}
$this->addModel($model);
}
/**
* Set criteria
* @param CriteriaInterface|array $criteria
* @return static
*/
public function setCriteria($criteria)
{
$this->criteria = $criteria;
assert($criteria instanceof SearchCriteria);
$this->add($criteria->getModels());
return $this;
}
/**
*
* @param string $q
* @return int
*/
public function count($q = null)
{
$params = $this->getParams($q);
try
{
$result = $this->manganel->getClient()->count($params);
}
catch (Missing404Exception $e)
{
$params = ['index' => strtolower($this->manganel->index)];
$this->manganel->getClient()->indices()->create($params);
}
catch (BadRequest400Exception $e)
{
// Throw previous exception,
// as it holds more meaningfull information
$json = json_encode($params, JSON_PRETTY_PRINT);
$prevMsg = '';
$previous = $e->getPrevious();
if(!empty($previous))
{
$prevMsg = '(' . $previous->getMessage() . ') ';
}
$message = sprintf("Exception %swhile querying `%s`: \n%s\n", $prevMsg, $this->manganel->indexId, $json);
throw new BadRequest400Exception($message, 400, $e);
}
if (empty($result) && empty($result['count']))
{
return 0; // @codeCoverageIgnore
}
return $result['count'];
}
/**
* Get search results - hits from elasticsearch response.
* Optionally raw results might be obtained by reference via second param.
*
* @param string $q
* @param array $result
* @return array
*/
public function search($q = null, &$result = [])
{
$params = $this->getParams($q);
try
{
$result = $this->manganel->getClient()->search($params);
}
catch (Missing404Exception $e)
{
$params = ['index' => strtolower($this->manganel->index)];
$this->manganel->getClient()->indices()->create($params);
}
catch (BadRequest400Exception $e)
{
// Throw previous exception,
// as it holds more meaningful information
$json = json_encode($params, JSON_PRETTY_PRINT);
$previous = $e->getPrevious();
$prevMsg = '';
$previous = $e->getPrevious();
if(!empty($previous))
{
$prevMsg = '(' . $previous->getMessage() . ') ';
}
$message = sprintf("Exception %swhile querying `%s`: \n%s\n", $prevMsg, $this->manganel->indexId, $json);
throw new BadRequest400Exception($message, 400, $e);
}
if (empty($result) && empty($result['hits']) && empty($result['hits']['hits']))
{
return [];
}
return $result['hits']['hits'];
}
public function getParams($q = null)
{
$body = [];
// Try to get query from criteria if empty
$criteria = $this->getCriteria();
if (!$criteria instanceof SearchCriteria && $criteria instanceof CriteriaInterface)
{
$criteria = new SearchCriteria($criteria);
}
if (empty($criteria))
{
$criteria = new SearchCriteria;
}
if (!empty($q))
{
$criteria->search($q);
}
$criteria->setModels($this->getModels());
$decorator = new QueryBuilderDecorator($this->manganel);
$decorator->setModels($this->getModels());
$decorator->decorate($body, $criteria);
$models = $this->getModels();
if (empty($models))
{
$type = '_all';
}
else
{
$types = [];
foreach ($models as $model)
{
assert($model instanceof AnnotatedInterface, new UnexpectedValueException(sprintf('Expected `%s` instance, got `%s`', AnnotatedInterface::class, is_object($model) ? get_class($model) : gettype($model))));
$types[] = TypeNamer::nameType($model);
}
$type = implode(',', array_unique($types));
}
$params = [
'index' => strtolower($this->manganel->index),
'type' => $type,
'body' => $body
];
// return $params;
return RecursiveFilter::mongoIdToString($params);
}
}
API documentation generated by ApiGen