├── .gitignore ├── .travis.yml ├── composer.json ├── license.txt ├── phpunit.xml ├── readme.md ├── src ├── ElasticquentClientTrait.php ├── ElasticquentCollection.php ├── ElasticquentCollectionTrait.php ├── ElasticquentConfigTrait.php ├── ElasticquentElasticsearchFacade.php ├── ElasticquentInterface.php ├── ElasticquentPaginator.php ├── ElasticquentResultCollection.php ├── ElasticquentServiceProvider.php ├── ElasticquentSupport.php ├── ElasticquentTrait.php └── config │ └── elasticquent.php └── tests ├── ElasticSearchMethodsTest.php ├── ElasticquentClientTraitTest.php ├── ElasticquentConfigTraitTest.php ├── ElasticquentTraitTest.php ├── models ├── CustomTestModel.php ├── SearchTestModel.php ├── TestModel.php └── TestModelWithCustomTypeName.php └── stubs ├── parameters.php └── results.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store 5 | Thumbs.db 6 | .idea -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 7.1 5 | - 7.3 6 | 7 | include: 8 | - php: 7.1 9 | env: dependencies=lowest 10 | 11 | before_script: 12 | - composer self-update 13 | - composer install --prefer-source --no-interaction --dev 14 | - if [ "$dependencies" = "lowest" ]; then composer update --prefer-lowest --prefer-stable -n; fi; 15 | 16 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "elasticquent/elasticquent", 3 | "type": "library", 4 | "description": "Maps Laravel Eloquent models to Elasticsearch types.", 5 | "keywords": [ 6 | "elasticsearch", 7 | "eloquent", 8 | "laravel" 9 | ], 10 | "homepage": "https://github.com/elasticquent/Elasticquent", 11 | "license": "MIT", 12 | "authors": [ 13 | { 14 | "name": "Adam Fairholm", 15 | "email": "adam.fairholm@gmail.com", 16 | "homepage": "http://adamfairholm.com" 17 | } 18 | ], 19 | "require": { 20 | "php": ">=7.3.0", 21 | "nesbot/carbon": "~1.0|~2", 22 | "elasticsearch/elasticsearch": "~6.1" 23 | }, 24 | "require-dev": { 25 | "phpunit/phpunit": "~4.2|~5.0|~8.0|^9.0", 26 | "mockery/mockery": "^0.9.4|^1.0" 27 | }, 28 | "autoload": { 29 | "psr-4": { 30 | "Elasticquent\\": "src/" 31 | } 32 | }, 33 | "extra": { 34 | "branch-alias": { 35 | "dev-master": "1.0-dev" 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Elasticquent 2 | 3 | _Elasticsearch for Eloquent Laravel Models_ 4 | 5 | Elasticquent makes working with [Elasticsearch](http://www.elasticsearch.org/) and [Eloquent](http://laravel.com/docs/eloquent) models easier by mapping them to Elasticsearch types. You can use the default settings or define how Elasticsearch should index and search your Eloquent models right in the model. 6 | 7 | Elasticquent uses the [official Elasticsearch PHP API](https://github.com/elasticsearch/elasticsearch-php). To get started, you should have a basic knowledge of how Elasticsearch works (indexes, types, mappings, etc). 8 | 9 | # Elasticsearch Requirements 10 | 11 | You must be running _at least_ Elasticsearch 1.0. Elasticsearch 0.9 and below *will not work* and are not supported. 12 | 13 | ## Contents 14 | 15 | * [Overview](#overview) 16 | * [How Elasticquent Works](#how-elasticquent-works) 17 | * [Setup](#setup) 18 | * [Elasticsearch Configuration](#elasticsearch-configuration) 19 | * [Indexes and Mapping](#indexes-and-mapping) 20 | * [Setting a Custom Index Name](#setting-a-custom-index-name) 21 | * [Setting a Custom Type Name](#setting-a-custom-type-name) 22 | * [Indexing Documents](#indexing-documents) 23 | * [Searching](#searching) 24 | * [Search Collections](#search-collections) 25 | * [Search Collection Documents](#search-collection-documents) 26 | * [Chunking results from Elastiquent](#chunking-results-from-elastiquent) 27 | * [Using the Search Collection Outside of Elasticquent](#using-the-search-collection-outside-of-elasticquent) 28 | * [More Options](#more-options) 29 | * [Document Ids](#document-ids) 30 | * [Document Data](#document-data) 31 | * [Using Elasticquent With Custom Collections](#using-elasticquetn-with-custom-collections) 32 | * [Roadmap](#roadmap) 33 | 34 | ## Reporting Issues 35 | 36 | If you do find an issue, please feel free to report it with [GitHub's bug tracker](https://github.com/elasticquent/Elasticquent/issues) for this project. 37 | 38 | Alternatively, fork the project and make a pull request :) 39 | 40 | ## Overview 41 | 42 | Elasticquent allows you take an Eloquent model and easily index and search its contents in Elasticsearch. 43 | 44 | ```php 45 | $books = Book::where('id', '<', 200)->get(); 46 | $books->addToIndex(); 47 | ``` 48 | 49 | When you search, instead of getting a plain array of search results, you instead get an Eloquent collection with some special Elasticsearch functionality. 50 | 51 | ```php 52 | $books = Book::search('Moby Dick'); 53 | echo $books->totalHits(); 54 | ``` 55 | 56 | Plus, you can still use all the Eloquent collection functionality: 57 | 58 | ```php 59 | $books = $books->filter(function ($book) { 60 | return $book->hasISBN(); 61 | }); 62 | ``` 63 | 64 | Check out the rest of the documentation for how to get started using Elasticsearch and Elasticquent! 65 | 66 | ### How Elasticquent Works 67 | 68 | When using a database, Eloquent models are populated from data read from a database table. With Elasticquent, models are populated by data indexed in Elasticsearch. The whole idea behind using Elasticsearch for search is that its fast and light, so you model functionality will be dictated by what data has been indexed for your document. 69 | 70 | ## Setup 71 | 72 | Before you start using Elasticquent, make sure you've installed [Elasticsearch](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/_installation.html). 73 | 74 | To get started, add Elasticquent to you composer.json file: 75 | 76 | "elasticquent/elasticquent": "dev-master" 77 | 78 | Once you've run a `composer update`, you need to register Laravel service provider, in your `config/app.php`: 79 | 80 | ```php 81 | 'providers' => [ 82 | ... 83 | Elasticquent\ElasticquentServiceProvider::class, 84 | ], 85 | ``` 86 | 87 | We also provide a facade for elasticsearch-php client (which has connected using our settings), add following to your `config/app.php` if you need so. 88 | 89 | ```php 90 | 'aliases' => [ 91 | ... 92 | 'Es' => Elasticquent\ElasticquentElasticsearchFacade::class, 93 | ], 94 | ``` 95 | 96 | Then add the Elasticquent trait to any Eloquent model that you want to be able to index in Elasticsearch: 97 | 98 | ```php 99 | use Elasticquent\ElasticquentTrait; 100 | 101 | class Book extends Eloquent 102 | { 103 | use ElasticquentTrait; 104 | } 105 | ``` 106 | 107 | Now your Eloquent model has some extra methods that make it easier to index your model's data using Elasticsearch. 108 | 109 | ### Elasticsearch Configuration 110 | 111 | By default, Elasticquent will connect to `localhost:9200` and use `default` as index name, you can change this and the other settings in the configuration file. You can add the `elasticquent.php` config file at `/app/config/elasticquent.php` for Laravel 4, or use the following Artisan command to publish the configuration file into your config directory for Laravel 5: 112 | 113 | ```shell 114 | $ php artisan vendor:publish --provider="Elasticquent\ElasticquentServiceProvider" 115 | ``` 116 | 117 | ```php 118 | [ 134 | 'hosts' => ['localhost:9200'], 135 | 'retries' => 1, 136 | ], 137 | 138 | /* 139 | |-------------------------------------------------------------------------- 140 | | Default Index Name 141 | |-------------------------------------------------------------------------- 142 | | 143 | | This is the index name that Elastiquent will use for all 144 | | Elastiquent models. 145 | */ 146 | 147 | 'default_index' => 'my_custom_index_name', 148 | 149 | ); 150 | 151 | ``` 152 | 153 | ### Indexes and Mapping 154 | 155 | While you can definitely build your indexes and mapping through the Elasticsearch API, you can also use some helper methods to build indexes and types right from your models. 156 | 157 | If you want a simple way to create indexes, Elasticquent models have a function for that: 158 | 159 | Book::createIndex($shards = null, $replicas = null); 160 | 161 | For custom analyzer, you can set an `indexSettings` property in your model and define the analyzers from there: 162 | 163 | ```php 164 | /** 165 | * The elasticsearch settings. 166 | * 167 | * @var array 168 | */ 169 | protected $indexSettings = [ 170 | 'analysis' => [ 171 | 'char_filter' => [ 172 | 'replace' => [ 173 | 'type' => 'mapping', 174 | 'mappings' => [ 175 | '&=> and ' 176 | ], 177 | ], 178 | ], 179 | 'filter' => [ 180 | 'word_delimiter' => [ 181 | 'type' => 'word_delimiter', 182 | 'split_on_numerics' => false, 183 | 'split_on_case_change' => true, 184 | 'generate_word_parts' => true, 185 | 'generate_number_parts' => true, 186 | 'catenate_all' => true, 187 | 'preserve_original' => true, 188 | 'catenate_numbers' => true, 189 | ] 190 | ], 191 | 'analyzer' => [ 192 | 'default' => [ 193 | 'type' => 'custom', 194 | 'char_filter' => [ 195 | 'html_strip', 196 | 'replace', 197 | ], 198 | 'tokenizer' => 'whitespace', 199 | 'filter' => [ 200 | 'lowercase', 201 | 'word_delimiter', 202 | ], 203 | ], 204 | ], 205 | ], 206 | ]; 207 | 208 | ``` 209 | 210 | For mapping, you can set a `mappingProperties` property in your model and use some mapping functions from there: 211 | 212 | ```php 213 | protected $mappingProperties = array( 214 | 'title' => array( 215 | 'type' => 'string', 216 | 'analyzer' => 'standard' 217 | ) 218 | ); 219 | ``` 220 | 221 | If you'd like to setup a model's type mapping based on your mapping properties, you can use: 222 | 223 | ```php 224 | Book::putMapping($ignoreConflicts = true); 225 | ``` 226 | 227 | To delete a mapping: 228 | 229 | ```php 230 | Book::deleteMapping(); 231 | ``` 232 | 233 | To rebuild (delete and re-add, useful when you make important changes to your mapping) a mapping: 234 | 235 | ```php 236 | Book::rebuildMapping(); 237 | ``` 238 | 239 | You can also get the type mapping and check if it exists. 240 | 241 | ```php 242 | Book::mappingExists(); 243 | Book::getMapping(); 244 | ``` 245 | 246 | ### Setting a Custom Index Name 247 | 248 | By default, Elasticquent will look for the `default_index` key within your configuration file(`config/elasticquent.php`). To set the default value for an index being used, you can edit this file and set the `default_index` key: 249 | 250 | ```php 251 | return array( 252 | 253 | // Other configuration keys ... 254 | 255 | /* 256 | |-------------------------------------------------------------------------- 257 | | Default Index Name 258 | |-------------------------------------------------------------------------- 259 | | 260 | | This is the index name that Elastiquent will use for all 261 | | Elastiquent models. 262 | */ 263 | 264 | 'default_index' => 'my_custom_index_name', 265 | ); 266 | ``` 267 | 268 | If you'd like to have a more dynamic index, you can also override the default configuration with a `getIndexName` method inside your Eloquent model: 269 | 270 | ```php 271 | function getIndexName() 272 | { 273 | return 'custom_index_name'; 274 | } 275 | ``` 276 | 277 | Note: If no index was specified, Elasticquent will use a hardcoded string with the value of `default`. 278 | 279 | ### Setting a Custom Type Name 280 | 281 | By default, Elasticquent will use the table name of your models as the type name for indexing. If you'd like to override it, you can with the `getTypeName` function. 282 | 283 | ```php 284 | function getTypeName() 285 | { 286 | return 'custom_type_name'; 287 | } 288 | ``` 289 | 290 | To check if the type for the Elasticquent model exists yet, use `typeExists`: 291 | 292 | ```php 293 | $typeExists = Book::typeExists(); 294 | ``` 295 | 296 | ## Indexing Documents 297 | 298 | To index all the entries in an Eloquent model, use `addAllToIndex`: 299 | 300 | ```php 301 | Book::addAllToIndex(); 302 | ``` 303 | 304 | You can also index a collection of models: 305 | 306 | ```php 307 | $books = Book::where('id', '<', 200)->get(); 308 | $books->addToIndex(); 309 | ``` 310 | 311 | You can index individual entries as well: 312 | 313 | ```php 314 | $book = Book::find($id); 315 | $book->addToIndex(); 316 | ``` 317 | 318 | You can also reindex an entire model: 319 | 320 | ```php 321 | Book::reindex(); 322 | ``` 323 | 324 | ## Searching 325 | 326 | There are three ways to search in Elasticquent. All three methods return a search collection. 327 | 328 | ### Simple term search 329 | 330 | The first method is a simple term search that searches all fields. 331 | 332 | ```php 333 | $books = Book::search('Moby Dick'); 334 | ``` 335 | 336 | ### Query Based Search 337 | 338 | The second is a query based search for more complex searching needs: 339 | 340 | ```php 341 | public static function searchByQuery($query = null, $aggregations = null, $sourceFields = null, $limit = null, $offset = null, $sort = null) 342 | ``` 343 | 344 | **Example:** 345 | 346 | ```php 347 | $books = Book::searchByQuery(array('match' => array('title' => 'Moby Dick'))); 348 | ``` 349 | Here's the list of available parameters: 350 | 351 | - `query` - Your ElasticSearch Query 352 | - `aggregations` - The Aggregations you wish to return. [See Aggregations for details](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations.html). 353 | - `sourceFields` - Limits returned set to the selected fields only 354 | - `limit` - Number of records to return 355 | - `offset` - Sets the record offset (use for paging results) 356 | - `sort` - Your sort query 357 | 358 | ### Raw queries 359 | 360 | The final method is a raw query that will be sent to Elasticsearch. This method will provide you with the most flexibility 361 | when searching for records inside Elasticsearch: 362 | 363 | ```php 364 | $books = Book::complexSearch(array( 365 | 'body' => array( 366 | 'query' => array( 367 | 'match' => array( 368 | 'title' => 'Moby Dick' 369 | ) 370 | ) 371 | ) 372 | )); 373 | ``` 374 | 375 | This is the equivalent to: 376 | ```php 377 | $books = Book::searchByQuery(array('match' => array('title' => 'Moby Dick'))); 378 | ``` 379 | 380 | ### Search Collections 381 | 382 | When you search on an Elasticquent model, you get a search collection with some special functions. 383 | 384 | You can get total hits: 385 | 386 | ```php 387 | $books->totalHits(); 388 | ``` 389 | 390 | Access the shards array: 391 | 392 | ```php 393 | $books->shards(); 394 | ``` 395 | 396 | Access the max score: 397 | 398 | ```php 399 | $books->maxScore(); 400 | ``` 401 | 402 | Access the timed out boolean property: 403 | 404 | ```php 405 | $books->timedOut(); 406 | ``` 407 | 408 | And access the took property: 409 | 410 | ```php 411 | $books->took(); 412 | ``` 413 | 414 | And access search aggregations - [See Aggregations for details](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations.html): 415 | 416 | ```php 417 | $books->getAggregations(); 418 | ``` 419 | 420 | ### Search Collection Documents 421 | 422 | Items in a search result collection will have some extra data that comes from Elasticsearch. You can always check and see if a model is a document or not by using the `isDocument` function: 423 | 424 | ```php 425 | $book->isDocument(); 426 | ``` 427 | 428 | You can check the document score that Elasticsearch assigned to this document with: 429 | 430 | ```php 431 | $book->documentScore(); 432 | ``` 433 | 434 | ### Chunking results from Elastiquent 435 | 436 | Similar to `Illuminate\Support\Collection`, the `chunk` method breaks the Elasticquent collection into multiple, smaller collections of a given size: 437 | 438 | ```php 439 | $all_books = Book::searchByQuery(array('match' => array('title' => 'Moby Dick'))); 440 | $books = $all_books->chunk(10); 441 | ``` 442 | 443 | 444 | ### Using the Search Collection Outside of Elasticquent 445 | 446 | If you're dealing with raw search data from outside of Elasticquent, you can use the Elasticquent search results collection to turn that data into a collection. 447 | 448 | ```php 449 | $client = new \Elasticsearch\Client(); 450 | 451 | $params = array( 452 | 'index' => 'default', 453 | 'type' => 'books' 454 | ); 455 | 456 | $params['body']['query']['match']['title'] = 'Moby Dick'; 457 | 458 | $collection = Book::hydrateElasticsearchResult($client->search($params)); 459 | 460 | ``` 461 | 462 | ## More Options 463 | 464 | ### Document IDs 465 | 466 | Elasticquent will use whatever is set as the `primaryKey` for your Eloquent models as the id for your Elasticsearch documents. 467 | 468 | ### Document Data 469 | 470 | By default, Elasticquent will use the entire attribute array for your Elasticsearch documents. However, if you want to customize how your search documents are structured, you can set a `getIndexDocumentData` function that returns you own custom document array. 471 | 472 | ```php 473 | function getIndexDocumentData() 474 | { 475 | return array( 476 | 'id' => $this->id, 477 | 'title' => $this->title, 478 | 'custom' => 'variable' 479 | ); 480 | } 481 | ``` 482 | Be careful with this, as Elasticquent reads the document source into the Eloquent model attributes when creating a search result collection, so make sure you are indexing enough data for your the model functionality you want to use. 483 | 484 | ### Using Elasticquent With Custom Collections 485 | 486 | If you are using a custom collection with your Eloquent models, you just need to add the `ElasticquentCollectionTrait` to your collection so you can use `addToIndex`. 487 | 488 | ```php 489 | class MyCollection extends \Illuminate\Database\Eloquent\Collection 490 | { 491 | use ElasticquentCollectionTrait; 492 | } 493 | ``` 494 | 495 | ## Roadmap 496 | 497 | Elasticquent currently needs: 498 | 499 | * Tests that mock ES API calls. 500 | * Support for routes 501 | -------------------------------------------------------------------------------- /src/ElasticquentClientTrait.php: -------------------------------------------------------------------------------- 1 | getElasticConfig(); 17 | 18 | // elasticsearch v2.0 using builder 19 | if (class_exists('\Elasticsearch\ClientBuilder')) { 20 | return \Elasticsearch\ClientBuilder::fromConfig($config); 21 | } 22 | 23 | // elasticsearch v1 24 | return new \Elasticsearch\Client($config); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/ElasticquentCollection.php: -------------------------------------------------------------------------------- 1 | isEmpty()) { 29 | return null; 30 | } 31 | 32 | // Use an stdClass to store result of elasticsearch operation 33 | $result = new \stdClass; 34 | 35 | // Iterate according to the amount configured, and put that iteration's worth of records into elastic search 36 | // This is done so that we do not exceed the maximum request size 37 | $all = $this->all(); 38 | $iteration = 0; 39 | do { 40 | $chunk = array_slice($all, (0 + ($iteration * static::$entriesToSendToElasticSearchInOneGo)), static::$entriesToSendToElasticSearchInOneGo); 41 | 42 | $params = array(); 43 | foreach ($chunk as $item) { 44 | $params['body'][] = array( 45 | 'index' => array( 46 | '_id' => $item->getKey(), 47 | '_type' => $item->getTypeName(), 48 | '_index' => $item->getIndexName(), 49 | ), 50 | ); 51 | 52 | $params['body'][] = $item->getIndexDocumentData(); 53 | } 54 | 55 | $result = $this->getElasticSearchClient()->bulk($params); 56 | 57 | // Check for errors 58 | if ( (array_key_exists('errors', $result) && $result['errors'] != false ) || (array_key_exists('Message', $result) && stristr('Request size exceeded', $result['Message']) !== false)) { 59 | break; 60 | } 61 | 62 | // Remove vars immediately to prevent them hanging around in memory, in case we have a large number of iterations 63 | unset($chunk, $params); 64 | 65 | ++$iteration; 66 | } while (count($all) > ($iteration * static::$entriesToSendToElasticSearchInOneGo) ); 67 | 68 | return $result; 69 | } 70 | 71 | /** 72 | * Delete From Index 73 | * 74 | * @return array 75 | */ 76 | public function deleteFromIndex() 77 | { 78 | $all = $this->all(); 79 | 80 | $params = array(); 81 | 82 | foreach ($all as $item) { 83 | $params['body'][] = array( 84 | 'delete' => array( 85 | '_id' => $item->getKey(), 86 | '_type' => $item->getTypeName(), 87 | '_index' => $item->getIndexName(), 88 | ), 89 | ); 90 | } 91 | 92 | return $this->getElasticSearchClient()->bulk($params); 93 | } 94 | 95 | /** 96 | * Reindex 97 | * 98 | * Delete the items and then re-index them. 99 | * 100 | * @return array 101 | */ 102 | public function reindex() 103 | { 104 | $this->deleteFromIndex(); 105 | return $this->addToIndex(); 106 | } 107 | 108 | } 109 | -------------------------------------------------------------------------------- /src/ElasticquentConfigTrait.php: -------------------------------------------------------------------------------- 1 | getElasticConfig('default_index'); 17 | 18 | if (!empty($index_name)) { 19 | return $index_name; 20 | } 21 | 22 | // Otherwise we will just go with 'default' 23 | return 'default'; 24 | } 25 | 26 | /** 27 | * Get the Elasticquent config 28 | * 29 | * @param string $key the configuration key 30 | * @param string $prefix filename of configuration file 31 | * @return array configuration 32 | */ 33 | public function getElasticConfig($key = 'config', $prefix = 'elasticquent') 34 | { 35 | $key = $prefix . ($key ? '.' : '') . $key; 36 | 37 | if (function_exists('config')) { 38 | // Get config helper for Laravel 5.1+ 39 | $config_helper = config(); 40 | } elseif (function_exists('app')) { 41 | // Get config helper for Laravel 4 & Laravel 5.1 42 | $config_helper = app('config'); 43 | } else { 44 | // Create a config helper when using stand-alone Eloquent 45 | $config_helper = $this->getConfigHelper(); 46 | } 47 | 48 | return $config_helper->get($key); 49 | } 50 | 51 | /** 52 | * Inject given config file into an instance of Laravel's config 53 | * 54 | * @throws \Exception when the configuration file is not found 55 | * @return \Illuminate\Config\Repository configuration repository 56 | */ 57 | protected function getConfigHelper() 58 | { 59 | $config_file = $this->getConfigFile(); 60 | 61 | if (!file_exists($config_file)) { 62 | throw new \Exception('Config file not found.'); 63 | } 64 | 65 | return new \Illuminate\Config\Repository(array('elasticquent' => require($config_file))); 66 | } 67 | 68 | /** 69 | * Get the config path and file name to use when Laravel framework isn't present 70 | * e.g. using Eloquent stand-alone or running unit tests 71 | * 72 | * @return string config file path 73 | */ 74 | protected function getConfigFile() 75 | { 76 | return __DIR__ . '/config/elasticquent.php'; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/ElasticquentElasticsearchFacade.php: -------------------------------------------------------------------------------- 1 | $value) { 21 | $this->{$key} = $value; 22 | } 23 | $this->total = $total; 24 | $this->perPage = $perPage; 25 | $this->lastPage = (int) ceil($total / $perPage); 26 | $this->currentPage = $this->setCurrentPage($currentPage, $this->lastPage); 27 | $this->path = $this->path != '/' ? rtrim($this->path, '/') . '/' : $this->path; 28 | $this->items = $items instanceof Collection ? $items : Collection::make($items); 29 | $this->hits = $hits; 30 | } 31 | 32 | /** 33 | * Get the instance as an array. 34 | * 35 | * @return array 36 | */ 37 | public function toArray() 38 | { 39 | return [ 40 | 'total' => $this->total(), 41 | 'per_page' => $this->perPage(), 42 | 'current_page' => $this->currentPage(), 43 | 'last_page' => $this->lastPage(), 44 | 'next_page_url' => $this->nextPageUrl(), 45 | 'prev_page_url' => $this->previousPageUrl(), 46 | 'from' => $this->firstItem(), 47 | 'to' => $this->lastItem(), 48 | 'hits' => $this->hits, 49 | 'data' => $this->items->toArray(), 50 | ]; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/ElasticquentResultCollection.php: -------------------------------------------------------------------------------- 1 | setMeta($meta); 39 | } 40 | } 41 | 42 | /** 43 | * Set the result meta. 44 | * 45 | * @param array $meta 46 | * @return $this 47 | */ 48 | public function setMeta(array $meta) 49 | { 50 | $this->took = isset($meta['took']) ? $meta['took'] : null; 51 | $this->timed_out = isset($meta['timed_out']) ? $meta['timed_out'] : null; 52 | $this->shards = isset($meta['_shards']) ? $meta['_shards'] : null; 53 | $this->hits = isset($meta['hits']) ? $meta['hits'] : null; 54 | $this->aggregations = isset($meta['aggregations']) ? $meta['aggregations'] : []; 55 | 56 | return $this; 57 | } 58 | 59 | /** 60 | * Total Hits 61 | * 62 | * @return int 63 | */ 64 | public function totalHits() 65 | { 66 | return $this->hits['total']; 67 | } 68 | 69 | /** 70 | * Max Score 71 | * 72 | * @return float 73 | */ 74 | public function maxScore() 75 | { 76 | return $this->hits['max_score']; 77 | } 78 | 79 | /** 80 | * Get Shards 81 | * 82 | * @return array 83 | */ 84 | public function getShards() 85 | { 86 | return $this->shards; 87 | } 88 | 89 | /** 90 | * Took 91 | * 92 | * @return string 93 | */ 94 | public function took() 95 | { 96 | return $this->took; 97 | } 98 | 99 | /** 100 | * Timed Out 101 | * 102 | * @return bool 103 | */ 104 | public function timedOut() 105 | { 106 | return (bool) $this->timed_out; 107 | } 108 | 109 | /** 110 | * Get Hits 111 | * 112 | * Get the raw hits array from 113 | * Elasticsearch results. 114 | * 115 | * @return array 116 | */ 117 | public function getHits() 118 | { 119 | return $this->hits; 120 | } 121 | 122 | /** 123 | * Get aggregations 124 | * 125 | * Get the raw hits array from 126 | * Elasticsearch results. 127 | * 128 | * @return array 129 | */ 130 | public function getAggregations() 131 | { 132 | return $this->aggregations; 133 | } 134 | 135 | /** 136 | * Paginate Collection 137 | * 138 | * @param int $pageLimit 139 | * 140 | * @return Paginator 141 | */ 142 | public function paginate($pageLimit = 25) 143 | { 144 | $page = Paginator::resolveCurrentPage() ?: 1; 145 | 146 | return new Paginator($this->items, $this->hits, $this->totalHits(), $pageLimit, $page, ['path' => Paginator::resolveCurrentPath()]); 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /src/ElasticquentServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 18 | __DIR__.'/config/elasticquent.php' => config_path('elasticquent.php'), 19 | ]); 20 | } 21 | } 22 | 23 | /** 24 | * Register services. 25 | * 26 | * @return void 27 | */ 28 | public function register() 29 | { 30 | // Support class 31 | $this->app->singleton('elasticquent.support', function () { 32 | return new ElasticquentSupport; 33 | }); 34 | 35 | // Elasticsearch client instance 36 | $this->app->singleton('elasticquent.elasticsearch', function ($app) { 37 | return $app->make('elasticquent.support')->getElasticSearchClient(); 38 | }); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/ElasticquentSupport.php: -------------------------------------------------------------------------------- 1 | '); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/ElasticquentTrait.php: -------------------------------------------------------------------------------- 1 | getTable(); 75 | } 76 | 77 | /** 78 | * Uses Timestamps In Index. 79 | */ 80 | public function usesTimestampsInIndex() 81 | { 82 | return $this->usesTimestampsInIndex; 83 | } 84 | 85 | /** 86 | * Use Timestamps In Index. 87 | */ 88 | public function useTimestampsInIndex($shouldUse = true) 89 | { 90 | $this->usesTimestampsInIndex = $shouldUse; 91 | } 92 | 93 | /** 94 | * Don't Use Timestamps In Index. 95 | * 96 | * @deprecated 97 | */ 98 | public function dontUseTimestampsInIndex() 99 | { 100 | $this->useTimestampsInIndex(false); 101 | } 102 | 103 | /** 104 | * Get Mapping Properties 105 | * 106 | * @return array 107 | */ 108 | public function getMappingProperties() 109 | { 110 | return $this->mappingProperties; 111 | } 112 | 113 | /** 114 | * Set Mapping Properties 115 | * 116 | * @param array $mapping 117 | * @internal param array $mapping 118 | */ 119 | public function setMappingProperties(array $mapping = null) 120 | { 121 | $this->mappingProperties = $mapping; 122 | } 123 | 124 | /** 125 | * Get Index Settings 126 | * 127 | * @return array 128 | */ 129 | public function getIndexSettings() 130 | { 131 | return $this->indexSettings; 132 | } 133 | 134 | /** 135 | * Is Elasticsearch Document 136 | * 137 | * Is the data in this module sourced 138 | * from an Elasticsearch document source? 139 | * 140 | * @return bool 141 | */ 142 | public function isDocument() 143 | { 144 | return $this->isDocument; 145 | } 146 | 147 | /** 148 | * Get Document Score 149 | * 150 | * @return null|float 151 | */ 152 | public function documentScore() 153 | { 154 | return $this->documentScore; 155 | } 156 | 157 | /** 158 | * Document Version 159 | * 160 | * @return null|int 161 | */ 162 | public function documentVersion() 163 | { 164 | return $this->documentVersion; 165 | } 166 | 167 | /** 168 | * Get Index Document Data 169 | * 170 | * Get the data that Elasticsearch will 171 | * index for this particular document. 172 | * 173 | * @return array 174 | */ 175 | public function getIndexDocumentData() 176 | { 177 | return $this->toArray(); 178 | } 179 | 180 | /** 181 | * Index Documents 182 | * 183 | * Index all documents in an Eloquent model. 184 | * 185 | * @return array 186 | */ 187 | public static function addAllToIndex() 188 | { 189 | $instance = new static; 190 | 191 | $all = $instance->newQuery()->get(array('*')); 192 | 193 | return $all->addToIndex(); 194 | } 195 | 196 | /** 197 | * Re-Index All Content 198 | * 199 | * @return array 200 | */ 201 | public static function reindex() 202 | { 203 | $instance = new static; 204 | 205 | $all = $instance->newQuery()->get(array('*')); 206 | 207 | return $all->reindex(); 208 | } 209 | 210 | /** 211 | * Search By Query 212 | * 213 | * Search with a query array 214 | * 215 | * @param array $query 216 | * @param array $aggregations 217 | * @param array $sourceFields 218 | * @param int $limit 219 | * @param int $offset 220 | * @param array $sort 221 | * 222 | * @return ElasticquentResultCollection 223 | */ 224 | public static function searchByQuery($query = null, $aggregations = null, $sourceFields = null, $limit = null, $offset = null, $sort = null) 225 | { 226 | $instance = new static; 227 | 228 | $params = $instance->getBasicEsParams(true, $limit, $offset); 229 | 230 | if (!empty($sourceFields)) { 231 | $params['body']['_source']['include'] = $sourceFields; 232 | } 233 | 234 | if (!empty($query)) { 235 | $params['body']['query'] = $query; 236 | } 237 | 238 | if (!empty($aggregations)) { 239 | $params['body']['aggs'] = $aggregations; 240 | } 241 | 242 | if (!empty($sort)) { 243 | $params['body']['sort'] = $sort; 244 | } 245 | 246 | $result = $instance->getElasticSearchClient()->search($params); 247 | 248 | return static::hydrateElasticsearchResult($result); 249 | } 250 | 251 | /** 252 | * Perform a "complex" or custom search. 253 | * 254 | * Using this method, a custom query can be sent to Elasticsearch. 255 | * 256 | * @param $params parameters to be passed directly to Elasticsearch 257 | * @return ElasticquentResultCollection 258 | */ 259 | public static function complexSearch($params) 260 | { 261 | $instance = new static; 262 | 263 | $result = $instance->getElasticSearchClient()->search($params); 264 | 265 | return static::hydrateElasticsearchResult($result); 266 | } 267 | 268 | /** 269 | * Search 270 | * 271 | * Simple search using a match _all query 272 | * 273 | * @param string $term 274 | * 275 | * @return ElasticquentResultCollection 276 | */ 277 | public static function search($term = '') 278 | { 279 | $instance = new static; 280 | 281 | $params = $instance->getBasicEsParams(); 282 | 283 | $params['body']['query']['match']['_all'] = $term; 284 | 285 | $result = $instance->getElasticSearchClient()->search($params); 286 | 287 | return static::hydrateElasticsearchResult($result); 288 | } 289 | 290 | /** 291 | * Add to Search Index 292 | * 293 | * @throws Exception 294 | * @return array 295 | */ 296 | public function addToIndex() 297 | { 298 | if (!$this->exists) { 299 | throw new Exception('Document does not exist.'); 300 | } 301 | 302 | $params = $this->getBasicEsParams(); 303 | 304 | // Get our document body data. 305 | $params['body'] = $this->getIndexDocumentData(); 306 | 307 | // The id for the document must always mirror the 308 | // key for this model, even if it is set to something 309 | // other than an auto-incrementing value. That way we 310 | // can do things like remove the document from 311 | // the index, or get the document from the index. 312 | $params['id'] = $this->getKey(); 313 | 314 | return $this->getElasticSearchClient()->index($params); 315 | } 316 | 317 | /** 318 | * Remove From Search Index 319 | * 320 | * @return array 321 | */ 322 | public function removeFromIndex() 323 | { 324 | return $this->getElasticSearchClient()->delete($this->getBasicEsParams()); 325 | } 326 | 327 | /** 328 | * Partial Update to Indexed Document 329 | * 330 | * @return array 331 | */ 332 | public function updateIndex() 333 | { 334 | $params = $this->getBasicEsParams(); 335 | 336 | // Get our document body data. 337 | $params['body']['doc'] = $this->getIndexDocumentData(); 338 | 339 | return $this->getElasticSearchClient()->update($params); 340 | } 341 | 342 | /** 343 | * Get Search Document 344 | * 345 | * Retrieve an ElasticSearch document 346 | * for this entity. 347 | * 348 | * @return array 349 | */ 350 | public function getIndexedDocument() 351 | { 352 | return $this->getElasticSearchClient()->get($this->getBasicEsParams()); 353 | } 354 | 355 | /** 356 | * Get Basic Elasticsearch Params 357 | * 358 | * Most Elasticsearch API calls need the index and 359 | * type passed in a parameter array. 360 | * 361 | * @param bool $getIdIfPossible 362 | * @param bool $getSourceIfPossible 363 | * @param bool $getTimestampIfPossible 364 | * @param int $limit 365 | * @param int $offset 366 | * 367 | * @return array 368 | */ 369 | public function getBasicEsParams($getIdIfPossible = true, $limit = null, $offset = null) 370 | { 371 | $params = array( 372 | 'index' => $this->getIndexName(), 373 | 'type' => $this->getTypeName(), 374 | ); 375 | 376 | if ($getIdIfPossible && $this->getKey()) { 377 | $params['id'] = $this->getKey(); 378 | } 379 | 380 | if (is_numeric($limit)) { 381 | $params['size'] = $limit; 382 | } 383 | 384 | if (is_numeric($offset)) { 385 | $params['from'] = $offset; 386 | } 387 | 388 | return $params; 389 | } 390 | 391 | /** 392 | * Build the 'fields' parameter depending on given options. 393 | * 394 | * @param bool $getSourceIfPossible 395 | * @param bool $getTimestampIfPossible 396 | * @return array 397 | */ 398 | private function buildFieldsParameter($getSourceIfPossible, $getTimestampIfPossible) 399 | { 400 | $fieldsParam = array(); 401 | 402 | if ($getSourceIfPossible) { 403 | $fieldsParam[] = '_source'; 404 | } 405 | 406 | if ($getTimestampIfPossible) { 407 | $fieldsParam[] = '_timestamp'; 408 | } 409 | 410 | return $fieldsParam; 411 | } 412 | 413 | /** 414 | * Mapping Exists 415 | * 416 | * @return bool 417 | */ 418 | public static function mappingExists() 419 | { 420 | $instance = new static; 421 | 422 | $mapping = $instance->getMapping(); 423 | 424 | return (empty($mapping)) ? false : true; 425 | } 426 | 427 | /** 428 | * Get Mapping 429 | * 430 | * @return void 431 | */ 432 | public static function getMapping() 433 | { 434 | $instance = new static; 435 | 436 | $params = $instance->getBasicEsParams(); 437 | 438 | return $instance->getElasticSearchClient()->indices()->getMapping($params); 439 | } 440 | 441 | /** 442 | * Put Mapping. 443 | * 444 | * @param bool $ignoreConflicts 445 | * 446 | * @return array 447 | */ 448 | public static function putMapping($ignoreConflicts = false) 449 | { 450 | $instance = new static; 451 | 452 | $mapping = $instance->getBasicEsParams(); 453 | 454 | $params = array( 455 | '_source' => array('enabled' => true), 456 | 'properties' => $instance->getMappingProperties(), 457 | ); 458 | 459 | $mapping['body'][$instance->getTypeName()] = $params; 460 | 461 | return $instance->getElasticSearchClient()->indices()->putMapping($mapping); 462 | } 463 | 464 | /** 465 | * Delete Mapping 466 | * 467 | * @return array 468 | */ 469 | public static function deleteMapping() 470 | { 471 | $instance = new static; 472 | 473 | $params = $instance->getBasicEsParams(); 474 | 475 | return $instance->getElasticSearchClient()->indices()->deleteMapping($params); 476 | } 477 | 478 | /** 479 | * Rebuild Mapping 480 | * 481 | * This will delete and then re-add 482 | * the mapping for this model. 483 | * 484 | * @return array 485 | */ 486 | public static function rebuildMapping() 487 | { 488 | $instance = new static; 489 | 490 | // If the mapping exists, let's delete it. 491 | if ($instance->mappingExists()) { 492 | $instance->deleteMapping(); 493 | } 494 | 495 | // Don't need ignore conflicts because if we 496 | // just removed the mapping there shouldn't 497 | // be any conflicts. 498 | return $instance->putMapping(); 499 | } 500 | 501 | /** 502 | * Create Index 503 | * 504 | * @param int $shards 505 | * @param int $replicas 506 | * 507 | * @return array 508 | */ 509 | public static function createIndex($shards = null, $replicas = null) 510 | { 511 | $instance = new static; 512 | 513 | $client = $instance->getElasticSearchClient(); 514 | 515 | $index = array( 516 | 'index' => $instance->getIndexName(), 517 | ); 518 | 519 | $settings = $instance->getIndexSettings(); 520 | if (!is_null($settings)) { 521 | $index['body']['settings'] = $settings; 522 | } 523 | 524 | if (!is_null($shards)) { 525 | $index['body']['settings']['number_of_shards'] = $shards; 526 | } 527 | 528 | if (!is_null($replicas)) { 529 | $index['body']['settings']['number_of_replicas'] = $replicas; 530 | } 531 | 532 | $mappingProperties = $instance->getMappingProperties(); 533 | if (!is_null($mappingProperties)) { 534 | $index['body']['mappings'][$instance->getTypeName()] = [ 535 | '_source' => array('enabled' => true), 536 | 'properties' => $mappingProperties, 537 | ]; 538 | } 539 | 540 | return $client->indices()->create($index); 541 | } 542 | 543 | /** 544 | * Delete Index 545 | * 546 | * @return array 547 | */ 548 | public static function deleteIndex() 549 | { 550 | $instance = new static; 551 | 552 | $client = $instance->getElasticSearchClient(); 553 | 554 | $index = array( 555 | 'index' => $instance->getIndexName(), 556 | ); 557 | 558 | return $client->indices()->delete($index); 559 | } 560 | 561 | /** 562 | * Type Exists. 563 | * 564 | * Does this type exist? 565 | * 566 | * @return bool 567 | */ 568 | public static function typeExists() 569 | { 570 | $instance = new static; 571 | 572 | $params = $instance->getBasicEsParams(); 573 | 574 | return $instance->getElasticSearchClient()->indices()->existsType($params); 575 | } 576 | 577 | /** 578 | * New From Hit Builder 579 | * 580 | * Variation on newFromBuilder. Instead, takes 581 | * 582 | * @param array $hit 583 | * 584 | * @return static 585 | */ 586 | public function newFromHitBuilder($hit = array()) 587 | { 588 | $key_name = $this->getKeyName(); 589 | 590 | $attributes = $hit['_source']; 591 | 592 | if (isset($hit['_id'])) { 593 | $attributes[$key_name] = is_int($hit['_id']) ? intval($hit['_id']) : $hit['_id']; 594 | } 595 | 596 | // Add fields to attributes 597 | if (isset($hit['fields'])) { 598 | foreach ($hit['fields'] as $key => $value) { 599 | $attributes[$key] = $value; 600 | } 601 | } 602 | 603 | $instance = $this::newFromBuilderRecursive($this, $attributes); 604 | 605 | // In addition to setting the attributes 606 | // from the index, we will set the score as well. 607 | $instance->documentScore = $hit['_score']; 608 | 609 | // This is now a model created 610 | // from an Elasticsearch document. 611 | $instance->isDocument = true; 612 | 613 | // Set our document version if it's 614 | if (isset($hit['_version'])) { 615 | $instance->documentVersion = $hit['_version']; 616 | } 617 | 618 | return $instance; 619 | } 620 | 621 | /** 622 | * Create a elacticquent result collection of models from plain elasticsearch result. 623 | * 624 | * @param array $result 625 | * @return \Elasticquent\ElasticquentResultCollection 626 | */ 627 | public static function hydrateElasticsearchResult(array $result) 628 | { 629 | $items = $result['hits']['hits']; 630 | return static::hydrateElasticquentResult($items, $meta = $result); 631 | } 632 | 633 | /** 634 | * Create a elacticquent result collection of models from plain arrays. 635 | * 636 | * @param array $items 637 | * @param array $meta 638 | * @return \Elasticquent\ElasticquentResultCollection 639 | */ 640 | public static function hydrateElasticquentResult(array $items, $meta = null) 641 | { 642 | $instance = new static; 643 | 644 | $items = array_map(function ($item) use ($instance) { 645 | return $instance->newFromHitBuilder($item); 646 | }, $items); 647 | 648 | return $instance->newElasticquentResultCollection($items, $meta); 649 | } 650 | 651 | /** 652 | * Create a new model instance that is existing recursive. 653 | * 654 | * @param \Illuminate\Database\Eloquent\Model $model 655 | * @param array $attributes 656 | * @param \Illuminate\Database\Eloquent\Relations\Relation $parentRelation 657 | * @return static 658 | */ 659 | public static function newFromBuilderRecursive(Model $model, array $attributes = [], Relation $parentRelation = null) 660 | { 661 | $instance = $model->newInstance([], $exists = true); 662 | 663 | $instance->setRawAttributes((array)$attributes, $sync = true); 664 | 665 | // Load relations recursive 666 | static::loadRelationsAttributesRecursive($instance); 667 | // Load pivot 668 | static::loadPivotAttribute($instance, $parentRelation); 669 | 670 | return $instance; 671 | } 672 | 673 | /** 674 | * Create a collection of models from plain arrays recursive. 675 | * 676 | * @param \Illuminate\Database\Eloquent\Model $model 677 | * @param \Illuminate\Database\Eloquent\Relations\Relation $parentRelation 678 | * @param array $items 679 | * @return \Illuminate\Database\Eloquent\Collection 680 | */ 681 | public static function hydrateRecursive(Model $model, array $items, Relation $parentRelation = null) 682 | { 683 | $instance = $model; 684 | 685 | $items = array_map(function ($item) use ($instance, $parentRelation) { 686 | // Convert all null relations into empty arrays 687 | $item = $item ?: []; 688 | 689 | return static::newFromBuilderRecursive($instance, $item, $parentRelation); 690 | }, $items); 691 | 692 | return $instance->newCollection($items); 693 | } 694 | 695 | /** 696 | * Get the relations attributes from a model. 697 | * 698 | * @param \Illuminate\Database\Eloquent\Model $model 699 | */ 700 | public static function loadRelationsAttributesRecursive(Model $model) 701 | { 702 | $attributes = $model->getAttributes(); 703 | 704 | foreach ($attributes as $key => $value) { 705 | if (method_exists($model, $key)) { 706 | $reflection_method = new ReflectionMethod($model, $key); 707 | 708 | // Check if method class has or inherits Illuminate\Database\Eloquent\Model 709 | if (!static::isClassInClass("Illuminate\Database\Eloquent\Model", $reflection_method->class)) { 710 | $relation = $model->$key(); 711 | 712 | if ($relation instanceof Relation) { 713 | // Check if the relation field is single model or collections 714 | if (is_null($value) === true || !static::isMultiLevelArray($value)) { 715 | $value = [$value]; 716 | } 717 | 718 | $models = static::hydrateRecursive($relation->getModel(), $value, $relation); 719 | 720 | // Unset attribute before match relation 721 | unset($model[$key]); 722 | $relation->match([$model], $models, $key); 723 | } 724 | } 725 | } 726 | } 727 | } 728 | 729 | /** 730 | * Get the pivot attribute from a model. 731 | * 732 | * @param \Illuminate\Database\Eloquent\Model $model 733 | * @param \Illuminate\Database\Eloquent\Relations\Relation $parentRelation 734 | */ 735 | public static function loadPivotAttribute(Model $model, Relation $parentRelation = null) 736 | { 737 | $attributes = $model->getAttributes(); 738 | 739 | foreach ($attributes as $key => $value) { 740 | if ($key === 'pivot') { 741 | unset($model[$key]); 742 | $pivot = $parentRelation->newExistingPivot($value); 743 | $model->setRelation($key, $pivot); 744 | } 745 | } 746 | } 747 | 748 | /** 749 | * Create a new Elasticquent Result Collection instance. 750 | * 751 | * @param array $models 752 | * @param array $meta 753 | * @return \Elasticquent\ElasticquentResultCollection 754 | */ 755 | public function newElasticquentResultCollection(array $models = [], $meta = null) 756 | { 757 | return new ElasticquentResultCollection($models, $meta); 758 | } 759 | 760 | /** 761 | * Check if an array is multi-level array like [[id], [id], [id]]. 762 | * 763 | * For detect if a relation field is single model or collections. 764 | * 765 | * @param array $array 766 | * @return boolean 767 | */ 768 | private static function isMultiLevelArray(array $array) 769 | { 770 | foreach ($array as $key => $value) { 771 | if (!is_array($value)) { 772 | return false; 773 | } 774 | } 775 | return true; 776 | } 777 | 778 | /** 779 | * Check the hierarchy of the given class (including the given class itself) 780 | * to find out if the class is part of the other class. 781 | * 782 | * @param string $classNeedle 783 | * @param string $classHaystack 784 | * @return bool 785 | */ 786 | private static function isClassInClass($classNeedle, $classHaystack) 787 | { 788 | // Check for the same 789 | if ($classNeedle == $classHaystack) { 790 | return true; 791 | } 792 | 793 | // Check for parent 794 | $classHaystackReflected = new \ReflectionClass($classHaystack); 795 | while ($parent = $classHaystackReflected->getParentClass()) { 796 | /** 797 | * @var \ReflectionClass $parent 798 | */ 799 | if ($parent->getName() == $classNeedle) { 800 | return true; 801 | } 802 | $classHaystackReflected = $parent; 803 | } 804 | 805 | return false; 806 | } 807 | } 808 | -------------------------------------------------------------------------------- /src/config/elasticquent.php: -------------------------------------------------------------------------------- 1 | [ 17 | 'hosts' => ['localhost:9200'], 18 | 'retries' => 1, 19 | ], 20 | 21 | /* 22 | |-------------------------------------------------------------------------- 23 | | Default Index Name 24 | |-------------------------------------------------------------------------- 25 | | 26 | | This is the index name that Elasticquent will use for all 27 | | Elasticquent models. 28 | */ 29 | 30 | 'default_index' => 'my_custom_index_name', 31 | 32 | ); 33 | -------------------------------------------------------------------------------- /tests/ElasticSearchMethodsTest.php: -------------------------------------------------------------------------------- 1 | 2, 23 | 'max_score' => 0.7768564, 24 | 'hits' => [ 25 | [ 26 | '_index' => 'my_custom_index_name', 27 | '_type' => 'test_table', 28 | '_score' => 0.7768564, 29 | '_source' => [ 30 | 'name' => 'foo', 31 | ] 32 | ], 33 | [ 34 | '_index' => 'my_custom_index_name', 35 | '_type' => 'test_table', 36 | '_score' => 0.5634561, 37 | '_source' => [ 38 | 'name' => 'bar', 39 | ] 40 | ], 41 | ] 42 | ]; 43 | 44 | public function setUp() 45 | { 46 | $this->model = new SearchTestModel; 47 | } 48 | 49 | public function testSuccessfulSearch() 50 | { 51 | $result = $this->model->search('with results'); 52 | 53 | $this->assertInstanceOf('Elasticquent\ElasticquentResultCollection', $result); 54 | $this->assertEquals(2, $result->totalHits()); 55 | $this->assertEquals(0.7768564, $result->maxScore()); 56 | $this->assertEquals(['total' => 5,'successful' => 5,'unsuccessful' => 0], $result->getShards()); 57 | $this->assertEquals(8, $result->took()); 58 | $this->assertFalse($result->timedOut()); 59 | $this->assertEquals($this->expectedHits, $result->getHits()); 60 | $this->assertEmpty($result->getAggregations()); 61 | } 62 | 63 | public function testUnsuccessfulSearch() 64 | { 65 | $result = $this->model->search('with no results'); 66 | 67 | $expectedHits = [ 68 | 'total' => 0, 69 | 'max_score' => null, 70 | 'hits' => [] 71 | ]; 72 | 73 | $this->assertInstanceOf('Elasticquent\ElasticquentResultCollection', $result); 74 | $this->assertEquals(0, $result->totalHits()); 75 | $this->assertNull($result->maxScore()); 76 | $this->assertEquals(['total' => 5,'successful' => 5,'unsuccessful' => 0], $result->getShards()); 77 | $this->assertEquals(4, $result->took()); 78 | $this->assertFalse($result->timedOut()); 79 | $this->assertEquals($expectedHits, $result->getHits()); 80 | $this->assertEmpty($result->getAggregations()); 81 | } 82 | 83 | public function testSearchWithEmptyParamters() 84 | { 85 | $this->model->search(); 86 | $this->model->search(null); 87 | $this->model->search(''); 88 | 89 | $this->addToAssertionCount(3); // does not throw an exception 90 | } 91 | 92 | public function testComplexSearch() 93 | { 94 | $params = complexParameters(); 95 | $result = $this->model->complexSearch($params); 96 | 97 | $this->assertInstanceOf('Elasticquent\ElasticquentResultCollection', $result); 98 | $this->assertEquals($this->expectedHits, $result->getHits()); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /tests/ElasticquentClientTraitTest.php: -------------------------------------------------------------------------------- 1 | model = new TestModel; 8 | } 9 | 10 | public function testClient() 11 | { 12 | $this->assertInstanceOf('ElasticSearch\Client', $this->model->getElasticSearchClient()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/ElasticquentConfigTraitTest.php: -------------------------------------------------------------------------------- 1 | model = new TestModel; 8 | } 9 | 10 | public function testAccesssToConfig() 11 | { 12 | $this->assertEquals(['localhost:9200'], $this->model->getElasticConfig('config.hosts')); 13 | $this->assertEquals(1, $this->model->getElasticConfig('config.retries')); 14 | $this->assertEquals('my_custom_index_name', $this->model->getElasticConfig('default_index')); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/ElasticquentTraitTest.php: -------------------------------------------------------------------------------- 1 | 'Test Name'); 6 | 7 | /** 8 | * Testing Model 9 | * 10 | * @return void 11 | */ 12 | public function setup() 13 | { 14 | $this->model = new TestModel; 15 | $this->model->fill($this->modelData); 16 | } 17 | 18 | /** 19 | * Test type name inferred from table name 20 | */ 21 | public function testTypeNameInferredFromTableName() 22 | { 23 | $this->assertEquals('test_table', $this->model->getTypeName()); 24 | } 25 | 26 | /** 27 | * Test type name overrides table name 28 | */ 29 | public function testTypeNameOverridesTableName() 30 | { 31 | $model = new TestModelWithCustomTypeName; 32 | $this->assertEquals('test_type_name', $model->getTypeName()); 33 | } 34 | 35 | /** 36 | * Test Basic Properties Getters 37 | */ 38 | public function testBasicPropertiesGetters() 39 | { 40 | $this->model->useTimestampsInIndex(); 41 | $this->assertTrue($this->model->usesTimestampsInIndex()); 42 | 43 | $this->model->dontUseTimestampsInIndex(); 44 | $this->assertFalse($this->model->usesTimestampsInIndex()); 45 | } 46 | 47 | /** 48 | * Testing Mapping Setup 49 | */ 50 | public function testMappingSetup() 51 | { 52 | $mapping = array('foo' => 'bar'); 53 | 54 | $this->model->setMappingProperties($mapping); 55 | $this->assertEquals($mapping, $this->model->getMappingProperties()); 56 | } 57 | 58 | /** 59 | * Test Index Document Data 60 | */ 61 | public function testIndexDocumentData() 62 | { 63 | // Basic 64 | $this->assertEquals($this->modelData, $this->model->getIndexDocumentData()); 65 | 66 | // Custom 67 | $custom = new CustomTestModel(); 68 | $custom->fill($this->modelData); 69 | 70 | $this->assertEquals( 71 | array('foo' => 'bar'), $custom->getIndexDocumentData()); 72 | } 73 | 74 | /** 75 | * Test Document Null States 76 | */ 77 | public function testDocumentNullStates() 78 | { 79 | $this->assertFalse($this->model->isDocument()); 80 | $this->assertNull($this->model->documentScore()); 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /tests/models/CustomTestModel.php: -------------------------------------------------------------------------------- 1 | 'bar'); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/models/SearchTestModel.php: -------------------------------------------------------------------------------- 1 | shouldReceive('search') 20 | ->with(searchParams('with results')) 21 | ->andReturn(successfulResults()); 22 | 23 | $elasticClient 24 | ->shouldReceive('search') 25 | ->with(searchParams('with no results')) 26 | ->andReturn(unsuccessfulResults()); 27 | 28 | $elasticClient 29 | ->shouldReceive('search') 30 | ->with(searchParams('')) 31 | ->andReturn(unsuccessfulResults()); 32 | 33 | $elasticClient 34 | ->shouldReceive('search') 35 | ->with(complexParameters()) 36 | ->andReturn(successfulResults()); 37 | 38 | return $elasticClient; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /tests/models/TestModel.php: -------------------------------------------------------------------------------- 1 | 'my_custom_index_name', 11 | 'type' => 'test_table', 12 | ]; 13 | } 14 | 15 | function searchParams($searchTerm) 16 | { 17 | $params = basicParameters(); 18 | $params['body'] = ['query' => ['match' => ['_all' => $searchTerm]]]; 19 | return $params; 20 | } 21 | 22 | function complexParameters() 23 | { 24 | $params = basicParameters(); 25 | $params['body'] = [ 26 | 'query' => [ 27 | 'filtered' => [ 28 | 'filter' => [ 29 | 'term' => [ 'my_field' => 'abc' ] 30 | ], 31 | 'query' => [ 32 | 'match' => [ 'my_other_field' => 'xyz' ] 33 | ] 34 | ] 35 | ] 36 | ]; 37 | return $params; 38 | } 39 | -------------------------------------------------------------------------------- /tests/stubs/results.php: -------------------------------------------------------------------------------- 1 | 8, 11 | 'timed_out' => false, 12 | '_shards' => [ 13 | 'total' => 5, 14 | 'successful' => 5, 15 | 'unsuccessful' => 0, 16 | ], 17 | 'hits' => [ 18 | 'total' => 2, 19 | 'max_score' => 0.7768564, 20 | 'hits' => [ 21 | [ 22 | '_index' => 'my_custom_index_name', 23 | '_type' => 'test_table', 24 | '_score' => 0.7768564, 25 | '_source' => [ 26 | 'name' => 'foo', 27 | ] 28 | ], 29 | [ 30 | '_index' => 'my_custom_index_name', 31 | '_type' => 'test_table', 32 | '_score' => 0.5634561, 33 | '_source' => [ 34 | 'name' => 'bar', 35 | ] 36 | ], 37 | ], 38 | ], 39 | 'aggregations' => [], 40 | ]; 41 | } 42 | 43 | function unsuccessfulResults() 44 | { 45 | return [ 46 | 'took' => 4, 47 | 'timed_out' => false, 48 | '_shards' => [ 49 | 'total' => 5, 50 | 'successful' => 5, 51 | 'unsuccessful' => 0, 52 | ], 53 | 'hits' => [ 54 | 'total' => 0, 55 | 'max_score' => null, 56 | 'hits' => [], 57 | ], 58 | 'aggregations' => [], 59 | ]; 60 | } --------------------------------------------------------------------------------