当前位置: 首页 > 软件库 > Web应用开发 > Web框架 >

laravel-elasticsearch

授权协议 MIT License
开发语言 PHP
所属分类 Web应用开发、 Web框架
软件类型 开源软件
地区 不详
投 递 者 陶山
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

Laravel-Elasticsearch

An easy way to use the official Elastic Search client in your Laravel or Lumen applications.

Build Status

Installation and Configuration

Install the current version of the cviebrock/laravel-elasticsearch package via composer:

composer require cviebrock/laravel-elasticsearch

If you are using ElasticSearch version 5, then install version 2 of this package:

composer require cviebrock/laravel-elasticsearch:^2

Laravel

The package's service provider will automatically register its service provider.

Publish the configuration file:

php artisan vendor:publish --provider="Cviebrock\LaravelElasticsearch\ServiceProvider"
Alternative configuration method via .env file

After you publish the configuration file as suggested above, you may configure ElasticSearchby adding the following to your application's .env file (with appropriate values):

ELASTICSEARCH_HOST=localhost
ELASTICSEARCH_PORT=9200
ELASTICSEARCH_SCHEME=http
ELASTICSEARCH_USER=
ELASTICSEARCH_PASS=
Connecting to AWS Elasticsearch Service

If you are connecting to ElasticSearch instances on Amazon AWS, then you'll alsoneed to composer require aws/aws-sdk-php:^3.80 and add the following to your.env file:

AWS_ELASTICSEARCH_ENABLED=true
AWS_REGION=...
AWS_ACCESS_KEY_ID=...
AWS_SECRET_ACCESS_KEY=...

If you have to use another authentication method having custom credentials (i.e. instanceProfile()),you have to publish the configuration file and use the aws_credentials:

<?php
// config/elasticsearch.php

$provider = \Aws\Credentials\CredentialProvider::instanceProfile();
$memoizedProvider = \Aws\Credentials\CredentialProvider::memoize($provider);
$credentials = $memoizedProvider()->wait();

...

'hosts' => [
    [
        'host'            => env('ELASTICSEARCH_HOST', 'localhost'),
        'port'            => env('ELASTICSEARCH_PORT', 9200),
        'scheme'          => env('ELASTICSEARCH_SCHEME', null),
        'user'            => env('ELASTICSEARCH_USER', null),
        'pass'            => env('ELASTICSEARCH_PASS', null),

        // If you are connecting to an Elasticsearch instance on AWS, you will need these values as well
        'aws'             => env('AWS_ELASTICSEARCH_ENABLED', false),
        'aws_region'      => env('AWS_REGION', ''),
        'aws_key'         => env('AWS_ACCESS_KEY_ID', ''),
        'aws_secret'      => env('AWS_SECRET_ACCESS_KEY', '')
        'aws_credentials' => $credentials
    ],
],

If you have a job that runs in supervisor, you have to use the Closure.This way the credentials will be renewed at runtime.

<?php
// config/elasticsearch.php

$provider = \Aws\Credentials\CredentialProvider::instanceProfile();
$memoizedProvider = \Aws\Credentials\CredentialProvider::memoize($provider);

...

'hosts' => [
    [
        ...
        'aws_credentials' => $memoizedProvider
    ],
],

If you are using php artisan config:cache, you cannot have the Closure in your config file, call it like this:

<?php
// config/elasticsearch.php

...

'hosts' => [
    [
        ...
        'aws_credentials' => [\Aws\Credentials\CredentialProvider::class, 'defaultProvider'],
    ],
],

Lumen

If you work with Lumen, please register the service provider and configuration in bootstrap/app.php:

$app->register(Cviebrock\LaravelElasticsearch\ServiceProvider::class);
$app->configure('elasticsearch');

Manually copy the configuration file to your application.

Usage

The Elasticsearch facade is just an entry point into the ES client,so previously you might have used:

use Elasticsearch\ClientBuilder;

$data = [
    'body' => [
        'testField' => 'abc'
    ],
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 'my_id',
];

$client = ClientBuilder::create()->build();
$return = $client->index($data);

You can now replace those last two lines with simply:

use Elasticsearch;

$return = Elasticsearch::index($data);

That will run the command on the default connection. You can run a command onany connection (see the defaultConnection setting and connections array inthe configuration file).

$return = Elasticsearch::connection('connectionName')->index($data);

Lumen users who wish to use Facades can do so by editing thebootstrap/app.php file to include the following:

$app->withFacades(true, [
    ...
    Cviebrock\LaravelElasticsearch\Facade::class => 'Elasticsearch',
    ...
]);

Lumen users who aren't using facades will need to use dependency injectionor the application container in order to get the ES service object:

// using injection:
public function handle(\Cviebrock\LaravelElasticsearch\Manager $elasticsearch)
{
    $elasticsearch->ping();
}

// using application container:
$elasticSearch = $this->app('elasticsearch');

Of course, dependency injection and the application container workfor Laravel applications as well.

Advanced Usage

Because the package is a wrapper around the official Elastic client, you cando pretty much anything with this package. Not only can you perform standardCRUD operations, but you can monitor the health of your Elastic cluster programmatically,back it up, or make changes to it. Some of these operations are done through"namespaced" commands, which this package happily supports.

To grab statistics for an index:

$stats = Elasticsearch::indices()->stats(['index' => 'my_index']);
$stats = Elasticsearch::nodes()->stats();
$stats = Elasticsearch::cluster()->stats();

To create and restore snapshots (read the Elastic docs about creating repository paths and plugins first):

$response = Elasticsearch::snapshots()->create($params);
$response = Elasticsearch::snapshots()->restore($params);

To delete whole indices (be careful!):

$response = Elasticsearch::indices()->delete(['index' => 'my_index']);

Please remember that this package is a thin wrapper around a large number of verysophisticated and well-documented Elastic features. Information about those featuresand the methods and parameters used to call them can be found in theElastic documentation.Help with using them is available via the Elastic forumsand on sites like Stack Overflow.

Console commands

This package also provides some useful console commands.

Check if an index exists:

php artisan laravel-elasticsearch:utils:index-exists <your_elasticsearch_index_name>

Create an index:

php artisan laravel-elasticsearch:utils:index-create <your_elasticsearch_index_name>

Delete an index:

php artisan laravel-elasticsearch:utils:index-delete <your_elasticsearch_index_name>

Create or update index mapping:
Note: The index mapping file must contain a valid JSON mapping definition as Elasticsearch expects, for example:

{
    "body": {
        "_source": {
            "enabled": true
        },
        "properties": {
            "id": {
                "type": "keyword"
            },
            "property_1": {
                "type": "text"
            },
            "property_2": {
                "type": "text"
            }
        }
    }
}
php artisan laravel-elasticsearch:utils:index-create-or-update-mapping <your_elasticsearch_index_name> <json_mapping_absolute_file_path>

Creates an alias:

php artisan laravel-elasticsearch:utils:alias-create <your_elasticsearch_index_name> <your_elasticsearch_alias_name>

Remove index from an alias:

php artisan laravel-elasticsearch:utils:alias-remove-index <your_elasticsearch_index_name> <your_elasticsearch_alias_name>

Switch index on alias (useful for zero-downtime release of the new index):

php artisan laravel-elasticsearch:utils:alias-switch-index <your_NEW_elasticsearch_index_name> <your_OLD_elasticsearch_index_name> <your_elasticsearch_alias_name>

Bugs, Suggestions, Contributions and Support

Thanks to everyonewho has contributed to this project!

Special thanks toJetBrains for theirOpen Source License Program ... and the excellent PHPStorm IDE, of course!

Please use Github for reporting bugs,and making comments or suggestions.

See CONTRIBUTING.md for how to contribute changes.

Copyright and License

laravel-elasticsearchwas written by Colin Viebrock and is released under theMIT License.

Copyright (c) 2015 Colin Viebrock

  • 安装 elasticsearch 和 kibana(用来查看管理elasticsearch中的数据) 参照:https://blog.csdn.net/z_ruitao/article/details/105043521 安装 scout composer require laravel/scout # 在config/app.php 的 providers 数组中添加 Laravel\Scout

  • 项目中使用laravel-admin,版本为:5.3.31,是老版本了。但是最近要升级软件环境,将PHP版本由5.6.34升级到7.3.28,升级后引出一系列问题,其他问题逐一解决,基本都是些函数弃用的问题。 但是项目中用到elasticsearch,搜索时报错:{“error”:“Content-Type header [] is not supported”,“status”:406},出现

  • 一 简介 laravel 的组件化使 laravel 的使用更加得心应手。   Laravel Scout 为 Eloquent 模型 全文搜索提供了简单的,基于驱动的解决方案。通过使用模型观察者,Scout 会自动同步 Eloquent 记录的搜索索引。   简单的来说 Scout 是用来全文检索的一个组件(不具有搜索功能),它使用驱动来调用搜索引擎来进行搜索。这里讲解 laravel 使用 e

 相关资料
  • Laravel 是一套简洁、优雅的PHP Web开发框架(PHP Web Framework)。它可以让你从面条一样杂乱的代码中解脱出来;它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁、富于表达力。 功能特点 1、语法更富有表现力 你知道下面这行代码里 “true” 代表什么意思么? $uri = Uri::create(‘some/uri’, array(), array(), tr

  • 我需要空间/Laravel权限的帮助。当我试图分配它给我错误哎呀,看起来像出了问题。 错误 Connection.php第761行中的QueryExcema:SQLSTATE[23000]:完整性约束冲突:1048列role_id不能为空(SQL:插入到(,)值(9,))

  • Laravel 作为现在最流行的 PHP 框架,其中的知识较多,所以单独拿出来写一篇。 简述 Laravel 的生命周期 Laravel 采用了单一入口模式,应用的所有请求入口都是 public/index.php 文件。 注册类文件自动加载器 : Laravel通过 composer 进行依赖管理,无需开发者手动导入各种类文件,而由自动加载器自行导入。 创建服务容器:从 bootstrap/ap

  • 简介 Laravel Scout 为 Eloquent 模型 全文搜索提供了简单的,基于驱动的解决方案。通过使用模型观察者,Scout 会自动同步 Eloquent 记录的搜索索引。 目前,Scout 自带一个 Algolia 驱动;不过,编写自定义驱动很简单, 你可以轻松的通过自己的搜索实现来扩展 Scout。 安装 首先,通过 Composer 包管理器来安装 Scout: composer

  • 简介 Laravel 致力于让整个 PHP 开发体验变得愉快, 包括你的本地开发环境。 Vagrant 提供了一种简单,优雅的方式来管理和配置虚拟机。 Laravel Homestead 是一个官方预封装的 Vagrant box,它为你提供了一个完美的开发环境,而无需在本地机器安装 PHP 、Web 服务器和其他服务器软件。不用担心会搞乱你的操作系统!Vagrant boxes 是一次性的。如果

  • WebStack-Laravel 一个开源的网址导航网站项目,具备完整的前后台,您可以拿来制作自己的网址导航。 部署 克隆代码: git clone https://github.com/hui-ho/WebStack-Laravel.git 安装依赖: composer installphp artisan key:generate 编辑配置: cp .env.example .env ...D