Index and search Laravel models on Amazon’s CloudSearch. To get started, you should have a basic knowledge of how CloudSearch works.
From the command line run:
Once installed you need to register the service provider with the application. Open up config/app.php and find the providers key.
For Lumen register the service provider in bootstrap/app.php.
Publish the configurations
Run this on the command line from the root of your project:
A configuration file will be publish to config/cloud-search.php.
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).
Run this on the command line from the root of your project to generate the table for storing currencies:
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.
They can be found in the config/cloud-search.php file under the fields property:
Initialize an Eloquent model map.
Name or comma separated names of the model(s) to index.
Flush all of the model documents from the index.
Reduces the number of calls made to the CloudSearch server by queueing the updates and deletes.
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.
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:
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:
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:
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:
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:
You may specify how many models to retrieve per page by passing the amount as the first argument to the paginate method:
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:
Basic Builder Usage
Initialize a builder instance:
You can chain query methods like so:
use the get() or paginate() methods to submit query and retrieve results from AWS.
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):
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.
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:
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.