Giter Site home page Giter Site logo

laravel-cloudsearch's Introduction

Laravel CloudSearch

Latest Stable Version Total Downloads Patreon donate button Donate weekly to this project using Gratipay Donate to this project using Flattr Donate to this project using Paypal

Index and search Laravel models on Amazon's CloudSearch. To get started, you should have a basic knowledge of how CloudSearch works.

Installation

Composer

From the command line run:

$ composer require torann/laravel-cloudsearch

Laravel

Once installed you need to register the service provider with the application. Open up config/app.php and find the providers key.

'providers' => [

    LaravelCloudSearch\LaravelCloudSearchServiceProvider::class,

]

Lumen

For Lumen register the service provider in bootstrap/app.php.

$app->register(LaravelCloudSearch\LaravelCloudSearchServiceProvider::class);

Publish the configurations

Run this on the command line from the root of your project:

$ php artisan vendor:publish --provider="LaravelCloudSearch\LaravelCloudSearchServiceProvider" --tag=config

A configuration file will be publish to config/cloud-search.php.

Migration

The package uses a batch queue system for updating the documents on AWS. This is done to help reduce the number of calls made to the API (will save money in the long run).

php artisan vendor:publish --provider="LaravelCloudSearch\LaravelCloudSearchServiceProvider" --tag=migrations

Run this on the command line from the root of your project to generate the table for storing currencies:

$ php artisan migrate

Fields

The better help manage fields, the package ships with a simple field management command. This is completely optional, as you can manage them in the AWS console.

NOTE: If you choose not to use this command to manage or setup your fields, you will still need to add the field searchable_type as a literal. This is used to store the model type.

They can be found in the config/cloud-search.php file under the fields property:

'fields' => [
    'title' => 'text',
    'status' => 'literal',
],

Artisan Commands

search:fields

Initialize an Eloquent model map.

search:index <model>

Name or comma separated names of the model(s) to index.

Arguments:

 model               Name or comma separated names of the model(s) to index

search:flush <model>

Flush all of the model documents from the index.

Arguments:

 model               Name or comma separated names of the model(s) to index

search:queue

Reduces the number of calls made to the CloudSearch server by queueing the updates and deletes.

Indexing

Once you have added the LaravelCloudSearch\Eloquent\Searchable trait to a model, all you need to do is save a model instance and it will automatically be added to your index when the search:queue command is ran.

$post = new App\Post;

// ...

$post->save();

Note: if the model document has already been indexed, then it will simply be updated. If it does not exist, it will be added.

Updating Documents

To update an index model, you only need to update the model instance's properties and `save`` the model to your database. The package will automatically persist the changes to your search index:

$post = App\Post::find(1);

// Update the post...

$post->save();

Removing Documents

To remove a document from your index, simply delete the model from the database. This form of removal is even compatible with soft deleted models:

$post = App\Post::find(1);

$post->delete();

Searching

You may begin searching a model using the search method. The search method accepts a single string that will be used to search your models. You should then chain the get method onto the search query to retrieve the Eloquent models that match the given search query:

$posts = App\Post::search('Kitten fluff')->get();

Since package searches return a collection of Eloquent models, you may even return the results directly from a route or controller and they will automatically be converted to JSON:

use Illuminate\Http\Request;

Route::get('/search', function (Request $request) {
    return App\Post::search($request->search)->get();
});

Pagination

In addition to retrieving a collection of models, you may paginate your search results using the paginate method. This method will return a Paginator instance just as if you had paginated a traditional Eloquent query:

$posts = App\Post::search('Kitten fluff')->paginate();

You may specify how many models to retrieve per page by passing the amount as the first argument to the paginate method:

$posts = App\Post::search('Kitten fluff')->paginate(15);

Once you have retrieved the results, you may display the results and render the page links using Blade just as if you had paginated a traditional Eloquent query:

<div class="container">
    @foreach ($posts as $post)
        {{ $post->title }}
    @endforeach
</div>

{{ $posts->links() }}

Basic Builder Usage

Initialize a builder instance:

$query = app(\LaravelCloudSearch\CloudSearcher::class)->newQuery();

You can chain query methods like so:

$query->phrase('ford')
    ->term('National Equipment', 'seller')
    ->range('year', '2010');

use the get() or paginate() methods to submit query and retrieve results from AWS.

$results = $query->get();

In the example above we did not set the search type, so this means the results that are returned will match any document on CloudSearch domain. To refine you search to certain model, either use the model like shown in the example previously or use the searchableType() method to set the class name of the model (this is done automatically in the model instance call):

$query = app(\LaravelCloudSearch\CloudSearcher::class)->newQuery();

$results = $query->searchableType(\App\LawnMower::class)
    ->term('honda', 'name')
    ->get();

Search Query Operators and Nested Queries

You can use the and, or, and not operators to build compound and nested queries. The corresponding and(), or(), and not() methods expect a closure as their argument. You can chain all available methods as well nest more sub-queries inside of closures.

$query->or(function($builder) {
    $builder->phrase('ford')
        ->phrase('truck');
});

Queue

The help reduce the number of bulk requests made to the CloudSearch endpoint (because they cost) a queue system is used. This can be set in Laravel Task Scheduling. You can decide how often it is ran using the scheduled task frequency options. Please note this uses the DB to function.

Example of the task added to /app/Console/Kernel.php:

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule $schedule
     *
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('search:queue')->everyTenMinutes();
    }

Multilingual

This feature is experimental

Laravel CloudSearch can support multiple languages by appending the language code to the index type, so when the system performs a search it will only look for data that is on in the current system locale suffixed index type. For this to work the model needs to use the LaravelCloudSearch\Eloquent\Localized trait or something similar to it.

laravel-cloudsearch's People

Contributors

torann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

laravel-cloudsearch's Issues

400 Bad Request

Hello
I'm facing issue with queue every two days with response 400 from amazon

Error executing "UploadDocuments" on "http://SEARCHURL/2013-01-01/documents/batch?format=sdk"; AWS HTTP error: Client error: POST http://SEARCH-URL.com/2013-01-01/documents/batch?format=sdk resulted in a 400 Bad Request response:
{"status": "error", "errors": [{"message": "[Deprecated: Use the outer message field] Validation error for field 'body (truncated...)

I set batching_size to 10 and still face same issue

Please help

search:import abstract method error for getSearchDocument

Hey Torann, Just want to say great work you have done with this package, Ive just started to learn cloud search so everything is quite new and looking forward to working with a much more laravel friendly way by using your package. I appreciate the time and effort you have gone to with making this package it is very clean.

I have an issue i face when using search:import. I have tried with and without the Searchable triat being added to the model, The first error i get without the trait is:


php artisan search:index MasterTest 
Model [\App\Api\MasterTest ] does not support searching.

With the searchable trait:

php artisan search:index MasterTest 
PHP Fatal error:  Class App\Api\MasterTest contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (App\Api\MasterTest::getSearchDocument) in /www/app/Api/MasterTest.php on line 8

[Symfony\Component\Debug\Exception\FatalErrorException]                                                                                                               
  Class App\Api\MasterTest contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (App\Api\MasterTest::getSearchDocument)  

I have attempted to add a getSearchDocument method but unsure what the method should return or if it should be present on the model as it does not mention this in the docs?

Any help would be greatly appreciated, Thansk.

Console command search:index not working

When i execute artisan search:index i get the following error:

[Symfony\Component\Debug\Exception\FatalThrowableError] Type error: Argument 1 passed to LaravelCloudSearch\Console\IndexCommand::index() must be an instance of Illuminate\Database\Eloquent\Builder, instance of A pp\Models\Vintage given, called in /home/vagrant/sites/vinamicus-server/vendor/torann/laravel-cloudsearch/src/Console/AbstractCommand.php on line 76

enable or disable the saved event of Observer

Hi, thank you for developing this package, it is great!
But there is a suggestion for use.
Because the models will always trigger the saved event and then add the queue when save.
However, sometimes updated data may not be related to index.
Hope that new parameters can be added to the config to enable/disable the saved event of Observer.
Let us decide when we want to add a queue.

Thanks again.

command search:import is not defined

I ran through all of the steps, but stuck on adding my Model.

[tracker@vps project]$ php artisan search:import Product


  [Symfony\Component\Console\Exception\CommandNotFoundException]
  Command "search:import" is not defined.
  Did you mean one of these?
      scout:import
      search:fields
      search:flush
      search:index
      search:queue
      tntsearch:import


[tracker@vps project]$ php artisan search:import App\Product


  [Symfony\Component\Console\Exception\CommandNotFoundException]
  Command "search:import" is not defined.
  Did you mean one of these?
      scout:import
      search:fields
      search:flush
      search:index
      search:queue
      tntsearch:import

Any help is greatly appreciated.

cursor cannot be set, error message is displayed directly when an exception occurs

Hi, I found two issues when used:

  1. cursor cannot be set
    Because "string" == 0 will always returns true in PHP.

    $this->cursor = $cursor == 0 ? 'initial' : $cursor;

    After setting $cursor will only be "initial".

  2. error message is displayed directly when an exception occurs

    dd($e->getAwsErrorMessage() ?: $e->getMessage());

Please help confirm, thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.