Download the PHP package michaeljennings/laralastica without Composer
On this page you can find all versions of the php package michaeljennings/laralastica. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download michaeljennings/laralastica
More information about michaeljennings/laralastica
Files in michaeljennings/laralastica
Package laralastica
Short Description A laravel package that allows you to search eloquent results using elasticsearch.
License MIT
Informations about the package laralastica
Laralastica
A laravel 5 package that adds the ability to search eloquent models using elasticsearch results, it also handles indexing and removing documents when you save or delete models.
- Installation
- Configuration
- Usage
- Building Your Index
- Searching
- Searching Soft Deleted Records
- Searching Without the Searchable Trait
- Limit Results
- Offsetting Results
- Sorting Results
- Queries
- Filters
- Paginate Results
- The Result Collection
Upgrading from 3.0 to 3.1+
When hitting the search
method on a model the query builder will return an instance of Michaeljennings\Laralastica\Eloquent\ResultCollection
instead of Illuminate\Database\Eloquent\Collection
.
This is useful as you get access to the totalHits
, maxScore
, and totalTime
methods.
However if you want to use the default collection you may override the newCollection
method on your model and return the collection instance you need.
Installation
Check the table below to see which version you will need.
Laralastica | Laravel | Elasticsearch | PHP |
---|---|---|---|
3.x | ^5.1 | 6.x | ^7.0 |
2.x | ^5.1 | 2.x-5.x | ^5.5.9 |
To install through composer either run composer require michaeljennings/laralastica
or add the package to you
composer.json.
For Laravel 5.5 and upwards, the service provider and facade will be loaded automatically.
For older versions of Laravel, you will need to add the laralastica service provider into your providers array in config/app.php
.
The package also comes with a facade, to use it add it to your aliases array in config/app.php
.
Configuration
Finally publish the package config using php artisan vendor:publish
. Once the config has published you can edit the `config/laralastica.php' file to set your elasticsearch connection.
The package comes with 2 drivers: elastica, and null. By default the package will use the elastica driver. The null driver is mainly testing purposes where you don't want to have an elasticsearch instance running.
As of laralastica 3.0 we now use multiple indexes, rather than multiple types in one index. To see why check this post about the removal of types.
If you are using multiple environments (production, staging, testing etc.) you can define a index prefix in the config. This will the be added to each index when searcing, adding documents etc.
Finally you need to configure your elasticsearch connection. Out of the box the package comes ready to support multiple connections.
However you can pass through any of the parameters the elastica client can receive, check the elastica documentation for more information.
To set the connection you wish to use either enter the host and port you want to connect to, or the url to connect with.
Usage
To get started using the package simply add the Searchable
trait to the models you want to index and search.
Once you have added the trait it will use model events to watch when a model is saved, deleted or restored and will add or delete the elasticsearch document as appropriate.
Set Elasticsearch Index
To set the elasticsearch index for the model use the getIndex
method to return the name of the index. By default
this will return the table name the model is using. The index must be lowercase.
Set Elasticsearch Key To Index By
To set the value to index the elasticsearch documents by use the 'getSearchKey' method to return the key. By default this will return the primary key of the model.
Set the Attributes to Index
To set which attributes should be indexed for the model use the getIndexableAttributes
method. The attributes must be
returned as an array of key value pairs. By default all of the models attributes are indexed.
Type Cast Attributes
In elasticsearch each index has a mapping type which will determine how records are indexed, this means that you have to supply each object in the same format. To this we can use the casts
property that is supplied by laravel.
Occasionally you may find yourself wanting to cast values slightly differently for your elasticsearch records. To that you can define the laralasticaCasts
property and this will allow you to override the casts property.
Building Your Index
If you already have data you in your database and you want to index it you can use the laralastica:index
artisan command.
Defining Models To Index
To get started you'll need to set the models you want to index in the indexable section of the laralastica.php
config file.
By default the package is setup to index the standard App\User
class.
To add a new model we need to set a custom key to reference this model by, this will allow us to index just that model if we want to.
Then the value needs to be the fully qualified path of the model class.
For example if we wanted to index a products model we could do:
Occasionally you may want to index relational data, if you have a large database this can take a long time to lazy load the relations. To get around this you can specify the relations to bring when indexing.
Indexing
If you want to index all of the models you have setup then you can run:
To run a specific index run:
If you are indexing a large amount of data you may want to push it to the queue, you can do so by providing the queue option
Searching
To run a search use the search
method. This uses a closure to search the elasticsearch type and then gets the results
and adds a where in query from the results.
The first parameter for the search
method is a Closure which gets passed an instance of the laralastica query builder.
The second parameter is the column for the where in query, this defaults to 'id'.
The third parameter is a boolean indicating if the query should be ordered by the order of the elasticsearch results.
You can also set whether the query must, should or must not match the value you are searching for.
You may also chain any Laravel query builder methods before or after searching.
Searching Soft Deleted Records
By default laralastica will delete the elasticsearch record when a model is deleted.
Occasionally you may want to search against your soft deleted records, to do that you implement the SearchSoftDeletes
trait instead of the Searchable
trait in your model.
This adds adds two new methods - searchWithTrashed
and searchOnlyTrashed
.
Both methods take the same parameters as the search method, but searchWithTrashed
will search both soft deleted and non-soft deleted results, and searchOnlyTrashed
will only get soft deleted results.
To only search for non-soft deleted results just use the search
method as usual.
Searching Without the Searchable Trait
It is also possible to use Laralastica without using the searchable trait. To do so you can either dependency inject
the class via its contract, use the provided Facade, or use the laralastica
helper method.
To run a new query use the search
method. This takes two parameters:
- The index/indices you are searching in
- The query to be run
To search across multiple elasticsearch indices simply pass an array of indices as the first parameter.
To get a paginated list of results hit the paginate
method and the amount to paginate by.
Limit Results
By default the results will be limited to the size set in the laralastica.php
config file. However you can override it by hitting the size
method.
Offsetting Results
It is also possible to provide an offset by hitting the from
method.
Sorting Results
By default we sort the results by their score in descending order. You can override this by hitting the sort method.
Queries
The elasticsearch queries are powered by the great elastica package.
There are some preset queries on the query builder, but it is also possible to create an instance of an elastica query and pass that through.
Available Queries
A list of the available queries can be found below.
Each of the queries can optionally be passed a callback as the final parameter which will allow you to access the raw elastica query.
Bool Query
Common Query
Exists Query
Fuzzy Query
Match Query
Match All Query
Query String Query
Range Query
Regular Expression Query
Term Query
Terms Query
Wildcard Query
Filters
Occasionally you will want to run a query to exclude/include records, but you don't want the query to effect the score.
You can do this using filters.
To add a filter hit the filter
methods and pass it a callback. The callback will be passed an instance of the laralastica builder as the first parameter.
For example if only wanted to search against records that had a due date we could do the following.
Paginate Results
To get a paginated list of results use the paginate
method and pass the amount to paginate the results by.
Raw Elastica Queries
To run a raw elastica query create the query instance and then pass it to the query
method.
The Result Collection
The search method will return an instance of the result collection. This extends the default laravel collection but also adds a couple of laralastica specific methods.
Total Hits
Gets the total amount of hits matched by the query.
Maximum Score
Gets the maximum score matched by the search.
Time Taken
Gets the time taken to execute the elasticsearch query.
All versions of laralastica with dependencies
ruflin/elastica Version ^6.0
illuminate/database Version >=5.0
illuminate/events Version >=5.0
illuminate/http Version >=5.0
illuminate/pagination Version >=5.0
illuminate/console Version >=5.0
illuminate/queue Version >=5.0
illuminate/bus Version >=5.0