Laravel、 Lumen 和 原生php 应用程序缺少弹性搜索 ORM
php >= 5.6.6
See Travis CI Builds.
laravel/laravel >= 5.* or laravel/lumen >= 5.* or composer application
See Full Documentation.
$ composer require basemkhirat/elasticsearch
##### 4) Publishing.
```bash
$ php artisan vendor:publish --provider="Basemkhirat\Elasticsearch\ElasticsearchServiceProvider"$ composer require basemkhirat/elasticsearchbootstrap/app.php.$app->register(Basemkhirat\Elasticsearch\ElasticsearchServiceProvider::class);vendor/basemkhirat/elasticsearch/src/config to root folder alongside with app directory.bootstrap/app.php.$app->withFacades();If you don't want to enable working with Lumen facades you can access the query builder using app("es").
app("es")->index("my_index")->type("my_type")->get();
# is similar to
ES::index("my_index")->type("my_type")->get();You can install package with any composer-based applications
$ composer require basemkhirat/elasticsearchrequire "vendor/autoload.php";
use Basemkhirat\Elasticsearch\Connection;
$connection = Connection::create([
'servers' => [
[
"host" => '127.0.0.1',
"port" => 9200,
'user' => '',
'pass' => '',
'scheme' => 'http',
],
],
// Custom handlers
// 'handler' => new MyCustomHandler(),
'index' => 'my_index',
'logging' => [
'enabled' => env('ELASTIC_LOGGING_ENABLED',false),
'level' => env('ELASTIC_LOGGING_LEVEL','all'),
'location' => env('ELASTIC_LOGGING_LOCATION',base_path('storage/logs/elasticsearch.log'))
],
]);
# access the query builder using created connection
$documents = $connection->search("hello")->get();After publishing, two configuration files will be created.
config/es.php where you can add more than one elasticsearch server.# Here you can define the default connection name.
'default' => env('ELASTIC_CONNECTION', 'default'),
# Here you can define your connections.
'connections' => [
'default' => [
'servers' => [
[
"host" => env("ELASTIC_HOST", "127.0.0.1"),
"port" => env("ELASTIC_PORT", 9200),
'user' => env('ELASTIC_USER', ''),
'pass' => env('ELASTIC_PASS', ''),
'scheme' => env('ELASTIC_SCHEME', 'http'),
]
],
// Custom handlers
// 'handler' => new MyCustomHandler(),
'index' => env('ELASTIC_INDEX', 'my_index')
]
],
# Here you can define your indices.
'indices' => [
'my_index_1' => [
"aliases" => [
"my_index"
],
'settings' => [
"number_of_shards" => 1,
"number_of_replicas" => 0,
],
'mappings' => [
'posts' => [
'properties' => [
'title' => [
'type' => 'string'
]
]
]
]
]
]
config/scout.php where you can use package as a laravel scout driver.With some artisan commands you can do some tasks such as creating or updating settings, mappings and aliases.
Note that all commands are running with --connection=default option, you can change it through the command.
These are all available commands:
$ php artisan es:indices:list
+----------------------+--------+--------+----------+------------------------+-----+-----+------------+--------------+------------+----------------+
| configured (es.php) | health | status | index | uuid | pri | rep | docs.count | docs.deleted | store.size | pri.store.size |
+----------------------+--------+--------+----------+------------------------+-----+-----+------------+--------------+------------+----------------+
| yes | green | open | my_index | 5URW60KJQNionAJgL6Q2TQ | 1 | 0 | 0 | 0 | 260b | 260b |
+----------------------+--------+--------+----------+------------------------+-----+-----+------------+--------------+------------+----------------+
es.php config fileNote that creating operation skips the index if exists.
# Create all indices in config file.
$ php artisan es:indices:create
# Create only 'my_index' index in config file
$ php artisan es:indices:create my_index
es.php config fileNote that updating operation updates indices setting, aliases and mapping and doesn't delete the indexed data.
# Update all indices in config file.
$ php artisan es:indices:update
# Update only 'my_index' index in config file
$ php artisan es:indices:update my_index
Be careful when using this command, you will lose your index data!
Running drop command with --force option will skip all confirmation messages.
# Drop all indices in config file.
$ php artisan es:indices:drop
# Drop specific index on sever. Not matter for index to be exist in config file or not.
$ php artisan es:indices:drop my_index
…
php
"aliases" => [
"my_index_alias"
] $ php artisan es:indices:update my_indexmy_new_index with your new mapping in configuration file.$ php artisan es:indices:create my_new_indexmy_index into my_new_index with command:$ php artisan es:indices:reindex my_index my_new_index
# Control bulk size. Adjust it with your server.
$ php artisan es:indices:reindex my_index my_new_index --bulk-size=2000
# Control query scroll value.
$ php artisan es:indices:reindex my_index my_new_index --bulk-size=2000 --scroll=2m
# Skip reindexing errors such as mapper parsing exceptions.
$ php artisan es:indices:reindex my_index my_new_index --bulk-size=2000 --skip-errors
# Hide all reindexing errors and show the progres bar only.
$ php artisan es:indices:reindex my_index my_new_index --bulk-size=2000 --skip-errors --hide-errorsmy_index_alias alias from my_index and add it to my_new_index in configuration file and update with command:$ php artisan es:indices:updateFirst, follow Laravel Scout installation.
All you have to do is updating these lines in config/scout.php configuration file.
# change the default driver to 'es'
'driver' => env('SCOUT_DRIVER', 'es'),
# link `es` driver with default elasticsearch connection in config/es.php
'es' => [
'connection' => env('ELASTIC_CONNECTION', 'default'),
],Have a look at laravel Scout documentation.
Each index type has a corresponding "Model" which is used to interact with that type. Models allow you to query for data in your types or indices, as well as insert new documents into the type.
title;
}
The all method will return all of the results in the model's type. Each elasticsearch model serves as a query builder, you may also add constraints to queries, and then use the get() method to retrieve the results:
$posts = App\Post::where('status', 1)
->orderBy('created_at', 'desc')
->take(10)
->get();
// Retrieve a model by document key...
$posts = App\Post::find("AVp_tCaAoV7YQD3Esfmp");To create a new document, simply create a new model instance, set attributes on the model, then call the save() method:
title = $request->title;
$post->save();
}
}The save() method may also be used to update models that already exist. To update a model, you should retrieve it, set any attributes you wish to update, and then call the save method.
$post = App\Post::find(1);
$post->title = 'New Post Title';
$post->save();To delete a model, call the delete() method on a model instance:
$post = App\Post::find(1);
$post->delete();Scopes allow you to define common sets of constraints that you may easily re-use throughout your application. For example, you may need to frequently retrieve all posts that are considered "popular". To define a scope, simply prefix an Eloquent model method with scope.
Scopes should always return a Query instance.
where('votes', '>', $votes);
}
/**
* Scope a query to only include active posts.
*
* @param \Basemkhirat\Elasticsearch\Query $query
* @return \Basemkhirat\Elasticsearch\Query
*/
public function scopeActive($query)
{
return $query->where('active', 1);
}
}Once the scope has been defined, you may call the scope methods when querying the model. However, you do not need to include the scope prefix when calling the method. You can even chain calls to various scopes, for example:
$posts = App\Post::popular(100)->active()->orderBy('created_at')->get();To define an accessor, create a getFooAttribute method on your model where Foo is the "studly" cased name of the column you wish to access. In this example, we'll define an accessor for the title attribute. The accessor will automatically be called by model when attempting to retrieve the value of the title attribute:
title;Occasionally, you may need to add array attributes that do not have a corresponding field in your index. To do so, simply define an accessor for the value:
public function getIsPublishedAttribute()
{
return $this->attributes['status'] == 1;
}Once you have created the accessor, just add the value to the appends property on the model:
protected $appends = ['is_published'];Once the attribute has been added to the appends list, it will be included in model's array.
To define a mutator, define a setFooAttribute method on your model where Foo is the "studly" cased name of the column you wish to access. So, again, let's define a mutator for the `ti
暂无开放 Issues,或尚未同步最近议题。