From 1a48d8581353f48f00aea423f4bf9eae7f8cc173 Mon Sep 17 00:00:00 2001 From: mehedi hasan Date: Thu, 22 Mar 2018 09:22:51 +0600 Subject: [PATCH 1/6] fixing issue with elasticSearch 6+ version. Content-Type : [] "elasticsearch/elasticsearch": "~6.0" used instead old one --- composer.json | 2 +- src/ElasticquentCollectionTrait.php | 22 +++++++++++++++++++--- src/ElasticquentTrait.php | 19 ++++++++++++++++--- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index a5840c7..2c0ad92 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ "illuminate/database": "~4.2|^5", "illuminate/config": "~4.2|^5", "nesbot/carbon": "~1.0", - "elasticsearch/elasticsearch": ">1.0 <2.2" + "elasticsearch/elasticsearch": "~6.0" }, "require-dev": { "phpunit/phpunit": "~4.2|~5.0", diff --git a/src/ElasticquentCollectionTrait.php b/src/ElasticquentCollectionTrait.php index ae51d51..62c62b6 100644 --- a/src/ElasticquentCollectionTrait.php +++ b/src/ElasticquentCollectionTrait.php @@ -24,7 +24,15 @@ public function addToIndex() } $params = array(); - + $params = [ + 'client' => [ + 'curl' => [ + CURLOPT_HTTPHEADER => [ + 'Content-type: application/json', + ] + ] + ] + ]; foreach ($this->all() as $item) { $params['body'][] = array( 'index' => array( @@ -36,7 +44,7 @@ public function addToIndex() $params['body'][] = $item->getIndexDocumentData(); } - +// dd($params); return $this->getElasticSearchClient()->bulk($params); } @@ -50,7 +58,15 @@ public function deleteFromIndex() $all = $this->all(); $params = array(); - + $params = [ + 'client' => [ + 'curl' => [ + CURLOPT_HTTPHEADER => [ + 'Content-type: application/json', + ] + ] + ] + ]; foreach ($all as $item) { $params['body'][] = array( 'delete' => array( diff --git a/src/ElasticquentTrait.php b/src/ElasticquentTrait.php index 6b1e10d..3a2b2d1 100644 --- a/src/ElasticquentTrait.php +++ b/src/ElasticquentTrait.php @@ -242,6 +242,9 @@ public static function searchByQuery($query = null, $aggregations = null, $sourc if (!empty($sort)) { $params['body']['sort'] = $sort; } + //getting error on search becuase of fields _source and _timestamp so, remove it. + unset($params['fields']); +// dd($params); $result = $instance->getElasticSearchClient()->search($params); @@ -282,6 +285,9 @@ public static function search($term = '') $params['body']['query']['match']['_all'] = $term; + //getting error on search becuase of fields _source and _timestamp so, remove it. + unset($params['fields']); + $result = $instance->getElasticSearchClient()->search($params); return static::hydrateElasticsearchResult($result); @@ -371,6 +377,13 @@ public function getBasicEsParams($getIdIfPossible = true, $getSourceIfPossible = $params = array( 'index' => $this->getIndexName(), 'type' => $this->getTypeName(), + 'client' => [ + 'curl' => [ + CURLOPT_HTTPHEADER => [ + 'Content-type: application/json', + ] + ] + ] ); if ($getIdIfPossible && $this->getKey()) { @@ -591,13 +604,13 @@ public static function typeExists() public function newFromHitBuilder($hit = array()) { $key_name = $this->getKeyName(); - + $attributes = $hit['_source']; if (isset($hit['_id'])) { $attributes[$key_name] = is_numeric($hit['_id']) ? intval($hit['_id']) : $hit['_id']; } - + // Add fields to attributes if (isset($hit['fields'])) { foreach ($hit['fields'] as $key => $value) { @@ -690,7 +703,7 @@ public static function hydrateRecursive(Model $model, array $items, Relation $pa $items = array_map(function ($item) use ($instance, $parentRelation) { // Convert all null relations into empty arrays $item = $item ?: []; - + return static::newFromBuilderRecursive($instance, $item, $parentRelation); }, $items); From 81e6fb97e705eb8bf83a956eb5241a057a6cb719 Mon Sep 17 00:00:00 2001 From: mehedi hasan Date: Thu, 22 Mar 2018 09:47:58 +0600 Subject: [PATCH 2/6] package name updated --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 2c0ad92..c1dc55f 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "elasticquent/elasticquent", + "name": "mehdi89/elasticquent", "type": "library", "description": "Maps Laravel Eloquent models to Elasticsearch types.", "keywords": [ From a69ef319ea9cba182b08c2873aa3f452492e542e Mon Sep 17 00:00:00 2001 From: mehedi hasan Date: Mon, 11 Jun 2018 01:08:53 +0600 Subject: [PATCH 3/6] Allow passing raw json query --- readme.md | 510 ++------------------------------------ src/ElasticquentTrait.php | 6 +- 2 files changed, 20 insertions(+), 496 deletions(-) diff --git a/readme.md b/readme.md index 00c3d4c..5b6cb52 100644 --- a/readme.md +++ b/readme.md @@ -1,500 +1,20 @@ -# Elasticquent +# Elasticquent +This fork fixed two issue of main [repository](https://github.com/elasticquent/Elasticquent) . check main repo for more details. -_Elasticsearch for Eloquent Laravel Models_ - -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. - -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). - -# Elasticsearch Requirements - -You must be running _at least_ Elasticsearch 1.0. Elasticsearch 0.9 and below *will not work* and are not supported. - -## Contents - -* [Overview](#overview) - * [How Elasticquent Works](#how-elasticquent-works) -* [Setup](#setup) - * [Elasticsearch Configuration](#elasticsearch-configuration) - * [Indexes and Mapping](#indexes-and-mapping) - * [Setting a Custom Index Name](#setting-a-custom-index-name) - * [Setting a Custom Type Name](#setting-a-custom-type-name) -* [Indexing Documents](#indexing-documents) -* [Searching](#searching) - * [Search Collections](#search-collections) - * [Search Collection Documents](#search-collection-documents) - * [Chunking results from Elastiquent](#chunking-results-from-elastiquent) - * [Using the Search Collection Outside of Elasticquent](#using-the-search-collection-outside-of-elasticquent) -* [More Options](#more-options) - * [Document Ids](#document-ids) - * [Document Data](#document-data) - * [Using Elasticquent With Custom Collections](#using-elasticquetn-with-custom-collections) -* [Roadmap](#roadmap) - -## Reporting Issues - -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. - -Alternatively, fork the project and make a pull request :) - -## Overview - -Elasticquent allows you take an Eloquent model and easily index and search its contents in Elasticsearch. - -```php - $books = Book::where('id', '<', 200)->get(); - $books->addToIndex(); -``` - -When you search, instead of getting a plain array of search results, you instead get an Eloquent collection with some special Elasticsearch functionality. - -```php - $books = Book::search('Moby Dick'); - echo $books->totalHits(); -``` - -Plus, you can still use all the Eloquent collection functionality: - -```php - $books = $books->filter(function ($book) { - return $book->hasISBN(); - }); -``` - -Check out the rest of the documentation for how to get started using Elasticsearch and Elasticquent! - -### How Elasticquent Works - -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. - -## Setup - -Before you start using Elasticquent, make sure you've installed [Elasticsearch](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/_installation.html). - -To get started, add Elasticquent to you composer.json file: - - "elasticquent/elasticquent": "dev-master" - -Once you've run a `composer update`, you need to register Laravel service provider, in your `config/app.php`: - -```php -'providers' => [ - ... - Elasticquent\ElasticquentServiceProvider::class, -], -``` - -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. - -```php -'aliases' => [ - ... - 'Es' => Elasticquent\ElasticquentElasticsearchFacade::class, -], -``` - -Then add the Elasticquent trait to any Eloquent model that you want to be able to index in Elasticsearch: - -```php -use Elasticquent\ElasticquentTrait; - -class Book extends Eloquent -{ - use ElasticquentTrait; -} -``` - -Now your Eloquent model has some extra methods that make it easier to index your model's data using Elasticsearch. - -### Elasticsearch Configuration - -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: - -```shell -$ php artisan vendor:publish --provider="Elasticquent\ElasticquentServiceProvider" -``` - -```php - [ - 'hosts' => ['localhost:9200'], - 'retries' => 1, - ], - - /* - |-------------------------------------------------------------------------- - | Default Index Name - |-------------------------------------------------------------------------- - | - | This is the index name that Elastiquent will use for all - | Elastiquent models. - */ - - 'default_index' => 'my_custom_index_name', - -); - -``` - -### Indexes and Mapping - -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. - -If you want a simple way to create indexes, Elasticquent models have a function for that: - - Book::createIndex($shards = null, $replicas = null); - -For custom analyzer, you can set an `indexSettings` property in your model and define the analyzers from there: - -```php - /** - * The elasticsearch settings. - * - * @var array - */ - protected $indexSettings = [ - 'analysis' => [ - 'char_filter' => [ - 'replace' => [ - 'type' => 'mapping', - 'mappings' => [ - '&=> and ' - ], - ], - ], - 'filter' => [ - 'word_delimiter' => [ - 'type' => 'word_delimiter', - 'split_on_numerics' => false, - 'split_on_case_change' => true, - 'generate_word_parts' => true, - 'generate_number_parts' => true, - 'catenate_all' => true, - 'preserve_original' => true, - 'catenate_numbers' => true, - ] - ], - 'analyzer' => [ - 'default' => [ - 'type' => 'custom', - 'char_filter' => [ - 'html_strip', - 'replace', - ], - 'tokenizer' => 'whitespace', - 'filter' => [ - 'lowercase', - 'word_delimiter', - ], - ], - ], - ], - ]; - -``` - -For mapping, you can set a `mappingProperties` property in your model and use some mapping functions from there: - -```php -protected $mappingProperties = array( - 'title' => array( - 'type' => 'string', - 'analyzer' => 'standard' - ) -); -``` - -If you'd like to setup a model's type mapping based on your mapping properties, you can use: - -```php - Book::putMapping($ignoreConflicts = true); -``` - -To delete a mapping: - -```php - Book::deleteMapping(); -``` - -To rebuild (delete and re-add, useful when you make important changes to your mapping) a mapping: - -```php - Book::rebuildMapping(); -``` - -You can also get the type mapping and check if it exists. - -```php - Book::mappingExists(); - Book::getMapping(); -``` - -### Setting a Custom Index Name - -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: - -```php -return array( - - // Other configuration keys ... - - /* - |-------------------------------------------------------------------------- - | Default Index Name - |-------------------------------------------------------------------------- - | - | This is the index name that Elastiquent will use for all - | Elastiquent models. - */ - - 'default_index' => 'my_custom_index_name', -); -``` - -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: - -```php -function getIndexName() -{ - return 'custom_index_name'; -} -``` - -Note: If no index was specified, Elasticquent will use a hardcoded string with the value of `default`. - -### Setting a Custom Type Name - -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. - -```php -function getTypeName() -{ - return 'custom_type_name'; -} -``` - -To check if the type for the Elasticquent model exists yet, use `typeExists`: - -```php - $typeExists = Book::typeExists(); -``` - -## Indexing Documents - -To index all the entries in an Eloquent model, use `addAllToIndex`: - -```php - Book::addAllToIndex(); -``` - -You can also index a collection of models: - -```php - $books = Book::where('id', '<', 200)->get(); - $books->addToIndex(); -``` - -You can index individual entries as well: - -```php - $book = Book::find($id); - $book->addToIndex(); -``` - -You can also reindex an entire model: - -```php - Book::reindex(); -``` - -## Searching - -There are three ways to search in Elasticquent. All three methods return a search collection. - -### Simple term search - -The first method is a simple term search that searches all fields. - -```php - $books = Book::search('Moby Dick'); -``` - -### Query Based Search - -The second is a query based search for more complex searching needs: +1. issue with elasticSearch 6+ version. Content-Type : [] +2. Allow to pass raw json query. example: ```php - public static function searchByQuery($query = null, $aggregations = null, $sourceFields = null, $limit = null, $offset = null, $sort = null) +$jsonQuery = '{ + "query": { + "bool": { + "must": [ + { "match": { "transcription":"'.$keyword.'" }}, + { "match": { "userID": '.$userid.' }} + ] + } + } +}'; +$audios = Audio::searchByQuery($jsonQuery); ``` -**Example:** - -```php - $books = Book::searchByQuery(array('match' => array('title' => 'Moby Dick'))); -``` -Here's the list of available parameters: - -- `query` - Your ElasticSearch Query -- `aggregations` - The Aggregations you wish to return. [See Aggregations for details](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations.html). -- `sourceFields` - Limits returned set to the selected fields only -- `limit` - Number of records to return -- `offset` - Sets the record offset (use for paging results) -- `sort` - Your sort query - -### Raw queries - -The final method is a raw query that will be sent to Elasticsearch. This method will provide you with the most flexibility -when searching for records inside Elasticsearch: - -```php - $books = Book::complexSearch(array( - 'body' => array( - 'query' => array( - 'match' => array( - 'title' => 'Moby Dick' - ) - ) - ) - )); -``` - -This is the equivalent to: -```php - $books = Book::searchByQuery(array('match' => array('title' => 'Moby Dick'))); -``` - -### Search Collections - -When you search on an Elasticquent model, you get a search collection with some special functions. - -You can get total hits: - -```php - $books->totalHits(); -``` - -Access the shards array: - -```php - $books->shards(); -``` - -Access the max score: - -```php - $books->maxScore(); -``` - -Access the timed out boolean property: - -```php - $books->timedOut(); -``` - -And access the took property: - -```php - $books->took(); -``` - -And access search aggregations - [See Aggregations for details](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations.html): - -```php - $books->getAggregations(); -``` - -### Search Collection Documents - -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: - -```php - $book->isDocument(); -``` - -You can check the document score that Elasticsearch assigned to this document with: - -```php - $book->documentScore(); -``` - -### Chunking results from Elastiquent - -Similar to `Illuminate\Support\Collection`, the `chunk` method breaks the Elasticquent collection into multiple, smaller collections of a given size: - -```php - $all_books = Book::searchByQuery(array('match' => array('title' => 'Moby Dick'))); - $books = $all_books->chunk(10); -``` - - -### Using the Search Collection Outside of Elasticquent - -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. - -```php -$client = new \Elasticsearch\Client(); - -$params = array( - 'index' => 'default', - 'type' => 'books' -); - -$params['body']['query']['match']['title'] = 'Moby Dick'; - -$collection = Book::hydrateElasticsearchResult($client->search($params)); - -``` - -## More Options - -### Document IDs - -Elasticquent will use whatever is set as the `primaryKey` for your Eloquent models as the id for your Elasticsearch documents. - -### Document Data - -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. - -```php -function getIndexDocumentData() -{ - return array( - 'id' => $this->id, - 'title' => $this->title, - 'custom' => 'variable' - ); -} -``` -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. - -### Using Elasticquent With Custom Collections - -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`. - -```php -class MyCollection extends \Illuminate\Database\Eloquent\Collection -{ - use ElasticquentCollectionTrait; -} -``` - -## Roadmap - -Elasticquent currently needs: - -* Tests that mock ES API calls. -* Support for routes diff --git a/src/ElasticquentTrait.php b/src/ElasticquentTrait.php index 3a2b2d1..8e88143 100644 --- a/src/ElasticquentTrait.php +++ b/src/ElasticquentTrait.php @@ -232,7 +232,11 @@ public static function searchByQuery($query = null, $aggregations = null, $sourc } if (!empty($query)) { - $params['body']['query'] = $query; + if (is_array($query)) { + $params['body']['query'] = $query; + } else { + $params['body'] = $query; + } } if (!empty($aggregations)) { From 7bb0c91463f0dba03ec09994b297b1ee77268a74 Mon Sep 17 00:00:00 2001 From: mehedi hasan Date: Thu, 30 Apr 2020 20:13:16 +0600 Subject: [PATCH 4/6] conflict solved --- composer.json | 2 +- src/ElasticquentCollectionTrait.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index eff291e..711873f 100644 --- a/composer.json +++ b/composer.json @@ -37,4 +37,4 @@ "dev-master": "1.0-dev" } } -} +} \ No newline at end of file diff --git a/src/ElasticquentCollectionTrait.php b/src/ElasticquentCollectionTrait.php index 0ac4e41..c08cfc7 100644 --- a/src/ElasticquentCollectionTrait.php +++ b/src/ElasticquentCollectionTrait.php @@ -105,4 +105,4 @@ public function reindex() return $this->addToIndex(); } -} +} \ No newline at end of file From 8c370250ddfa3e2f5305a66f0a4b12a9f609a40b Mon Sep 17 00:00:00 2001 From: mehedi hasan Date: Thu, 30 Apr 2020 20:23:03 +0600 Subject: [PATCH 5/6] elasticquentTrait updated --- src/ElasticquentTrait.php | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/src/ElasticquentTrait.php b/src/ElasticquentTrait.php index fa1f3d5..435f956 100644 --- a/src/ElasticquentTrait.php +++ b/src/ElasticquentTrait.php @@ -232,11 +232,7 @@ public static function searchByQuery($query = null, $aggregations = null, $sourc } if (!empty($query)) { - if (is_array($query)) { - $params['body']['query'] = $query; - } else { - $params['body'] = $query; - } + $params['body']['query'] = $query; } if (!empty($aggregations)) { @@ -246,9 +242,6 @@ public static function searchByQuery($query = null, $aggregations = null, $sourc if (!empty($sort)) { $params['body']['sort'] = $sort; } - //getting error on search becuase of fields _source and _timestamp so, remove it. - unset($params['fields']); -// dd($params); $result = $instance->getElasticSearchClient()->search($params); @@ -289,9 +282,6 @@ public static function search($term = '') $params['body']['query']['match']['_all'] = $term; - //getting error on search becuase of fields _source and _timestamp so, remove it. - unset($params['fields']); - $result = $instance->getElasticSearchClient()->search($params); return static::hydrateElasticsearchResult($result); @@ -381,13 +371,6 @@ public function getBasicEsParams($getIdIfPossible = true, $limit = null, $offset $params = array( 'index' => $this->getIndexName(), 'type' => $this->getTypeName(), - 'client' => [ - 'curl' => [ - CURLOPT_HTTPHEADER => [ - 'Content-type: application/json', - ] - ] - ] ); if ($getIdIfPossible && $this->getKey()) { @@ -822,4 +805,4 @@ private static function isClassInClass($classNeedle, $classHaystack) return false; } -} +} \ No newline at end of file From d72031bdb718498c65d3a1fbc63af6df3331bd16 Mon Sep 17 00:00:00 2001 From: Mehedi Hasan Date: Thu, 4 Mar 2021 16:12:49 +0600 Subject: [PATCH 6/6] compatibility with laravel 8 --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 3bdc249..821b191 100644 --- a/composer.json +++ b/composer.json @@ -18,8 +18,8 @@ ], "require": { "php": ">=5.4.0", - "illuminate/database": "~4.2|^5|^6|^7", - "illuminate/config": "~4.2|^5|^6|^7", + "illuminate/database": "~4.2|^5|^6|^7|^8", + "illuminate/config": "~4.2|^5|^6|^7|^8", "nesbot/carbon": "~1.0|~2", "elasticsearch/elasticsearch": "~6.1" }, @@ -37,4 +37,4 @@ "dev-master": "1.0-dev" } } -} \ No newline at end of file +}