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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
<?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 Closure;
use Elasticsearch\Client;
use Elasticsearch\ClientBuilder;
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
use Maslosoft\EmbeDi\EmbeDi;
use Maslosoft\Mangan\Interfaces\ProfilerInterface;
use Maslosoft\Mangan\Profillers\NullProfiler;
use Maslosoft\Manganel\Decorators\QueryBuilder\ConditionDecorator;
use Maslosoft\Manganel\Decorators\QueryBuilder\ConditionsDecorator;
use Maslosoft\Manganel\Decorators\QueryBuilder\Operators\InDecorator;
use Maslosoft\Manganel\Decorators\QueryBuilder\Operators\NotDecorator;
use Maslosoft\Manganel\Decorators\QueryBuilder\Operators\RangeDecorator;
use Maslosoft\Manganel\Decorators\QueryBuilder\Operators\SimpleTermDecorator;
use Maslosoft\Manganel\Decorators\QueryBuilder\QueryString\BoostDecorator;
use Maslosoft\Manganel\Decorators\QueryBuilder\QueryString\PrefixQueryDecorator;
use Maslosoft\Manganel\Decorators\QueryBuilder\ScrollDecorator;
use Maslosoft\Manganel\Decorators\QueryBuilder\SearchDecorator;
use Maslosoft\Manganel\Decorators\QueryBuilder\TagDecorator;
use Maslosoft\Manganel\Interfaces\ManganelAwareInterface;
use Maslosoft\Manganel\Meta\ManganelMeta;
/**
* Manganel
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class Manganel
{
const DefaultIndexId = 'manganel';
public $decorators = [
SearchCriteria::class => [
ConditionDecorator::class,
ConditionsDecorator::class,
ScrollDecorator::class,
SearchDecorator::class,
InDecorator::class,
NotDecorator::class,
RangeDecorator::class,
SimpleTermDecorator::class,
BoostDecorator::class,
PrefixQueryDecorator::class,
]
];
public $hosts = [
'localhost:9200'
];
public $auth = null;
public $username = '';
public $password = '';
public $params = [];
/**
* TODO Enforce lowercase
*/
public $index = 'my_index';
public $indexId = self::DefaultIndexId;
/**
* Whether to use refresh option when indexing document.
* NOTE: Due to performance reasons, this should be set to `true` only when
* really necessary - so it can be also callback.
*
* Callback function signature:
* ```
* function(AnnotatedInterface $model)
* ```
* @var string|Closure
*/
public $refresh = false;
/**
*
* @var Client
*/
private $client = null;
/**
* Dependency injection container
* @var EmbeDi
*/
private $di = null;
/**
* Version number holder
* @var string
*/
private static $_version = null;
/**
* Instances of manganel
* @var Manganel[]
*/
private static $mnl = [];
/**
* Hash map of class name to id. This is to reduce overhead of Mangan::fromModel()
* @var string[]
*/
private static $classToId = [];
/**
* Profiler instance
* @var ProfilerInterface
*/
private $profiler = null;
/**
* Class constructor
* @codeCoverageIgnore This is implicitly tested
* @param string $indexId
*/
public function __construct($indexId = self::DefaultIndexId)
{
if (empty($indexId))
{
$indexId = self::DefaultIndexId;
}
$decorators = $this->decorators;
$this->indexId = $indexId;
$this->di = new EmbeDi($this->indexId);
$this->di->configure($this);
$this->decorators = array_merge_recursive($this->decorators, $decorators);
if (empty(self::$mnl[$indexId]))
{
self::$mnl[$indexId] = $this;
}
}
/**
* @codeCoverageIgnore This is implicitly tested
* @param AnnotatedInterface $model
* @return static
*/
public static function create(AnnotatedInterface $model)
{
$key = get_class($model);
if (isset(self::$classToId[$key]))
{
$indexId = self::$classToId[$key];
}
else
{
$indexId = ManganelMeta::create($model)->type()->indexId;
self::$classToId[$key] = $indexId;
}
return static::fly($indexId);
}
/**
* Get flyweight instance of Manganel component.
* Only one instance will be created for each `$indexId`.
*
* @codeCoverageIgnore This is implicitly tested
* @new
* @param string $indexId
* @return Manganel
*/
public static function fly($indexId = self::DefaultIndexId)
{
if (empty($indexId))
{
$indexId = self::DefaultIndexId;
}
if (empty(self::$mnl[$indexId]))
{
self::$mnl[$indexId] = new static($indexId);
}
return self::$mnl[$indexId];
}
/**
* @codeCoverageIgnore This is implicitly tested
*/
public function init()
{
$this->di->store($this);
}
/**
* Get mangan version
* @return string
*/
public function getVersion()
{
if (null === self::$_version)
{
self::$_version = require __DIR__ . '/version.php';
}
return self::$_version;
}
/**
* Drop current index
* @return bool
*/
public function drop()
{
$params = [
'index' => strtolower($this->index)
];
$result = $this->getClient()->indices()->delete($params);
if (is_array($result) && array_key_exists('acknowledged', $result) && $result['acknowledged'])
{
return true;
}
return false;
}
/**
* @codeCoverageIgnore This is implicitly tested
* @return Client
*/
public function getClient()
{
if (null === $this->client)
{
$this->params['hosts'] = $this->hosts;
$this->params['connectionParams']['auth'] = [
$this->username,
$this->password,
$this->auth
];
$cb = ClientBuilder::create();
$cb->setHosts($this->hosts);
$this->client = $cb->build();
}
return $this->client;
}
/**
* Get profiler instance. This is guaranted, if not configured will return NullProfiller.
* @see NullProfiler
* @return ProfilerInterface
*/
public function getProfiler()
{
if (null === $this->profiler)
{
$this->profiler = new NullProfiler;
}
if ($this->profiler instanceof ManganelAwareInterface)
{
$this->profiler->setManganel($this);
}
return $this->profiler;
}
/**
* Set profiler instance
* @param ProfilerInterface $profiller
* @return static
*/
public function setProfiler(ProfilerInterface $profiller)
{
$this->profiler = $profiller;
return $this;
}
}
API documentation generated by ApiGen