This is an opinionated Laravel package to store and retrieve objects from Elasticsearch. It was tailormade for a project I was working on and only provides the functionality that I needed. If you need full control over elasticsearch via PHP, take a look at the official low-level client.
That being said, if you want an easy and simple syntax to work with elasticsearch, this is the package for you.
Laravel | googlesearch |
---|---|
4.2.x | 1.x |
5.x | 2.x |
To be able to use this package you should have a server with Elasticsearch installed. If you want to install it on your local development machine you can use these instructions from the excellent Vaprobash repo.
This package itself can be installed through Composer.
composer require spatie/searchindex
You must install this service provider.
// Laravel 5: config/app.php
'providers' => [
...
'Spatie\SearchIndex\SearchIndexServiceProvider',
...
];
This package also comes with a facade, which provides an easy way to call the the class.
// Laravel 5: config/app.php
'aliases' => array(
...
'SearchIndex' => 'Spatie\SearchIndex\SearchIndexFacade',
...
)
You can publish the config file of the package using artisan.
php artisan vendor:publish --provider="Spatie\SearchIndex\SearchIndexServiceProvider"
The options in the config file are set with sane default values and they should be self-explanatory.
###Prepare your object
Objects that you want to store in the index should implement the provided Spatie\SearchIndex\Searchable
- interface.
namespace Spatie\SearchIndex;
interface Searchable {
/**
* Returns an array with properties which must be indexed
*
* @return array
*/
public function getSearchableBody();
/**
* Return the type of the searchable subject
*
* @return string
*/
public function getSearchableType();
/**
* Return the id of the searchable subject
*
* @return string
*/
public function getSearchableId();
Here is an example how you could implement it with an Eloquent model:
class Product extends Eloquent implements Searchable
{
...
/**
* Returns an array with properties which must be indexed
*
* @return array
*/
public function getSearchableBody()
{
$searchableProperties = [
'name' => $this->name,
'brand' => $this->brand->name
'category' => $this->category->name
];
return $searchableProperties;
}
/**
* Return the type of the searchable subject
*
* @return string
*/
public function getSearchableType()
{
return 'product';
}
/**
* Return the id of the searchable subject
*
* @return string
*/
public function getSearchableId()
{
return $this->id;
}
}
The searchindex will use the returned searchableType and searchableId to identify an object in the index.
###Add an object to the index If you are using the facade it couldn't be simpler.
//$product is an object that implements the Searchable interface
SearchIndex::upsertToIndex($product)
###Update an object in the index You probably would have guessed it.
//$product is an object that implements the Searchable interface
SearchIndex::upsertToIndex($product)
###Remove an object from the index Yep. Easy.
//$product is an object that implements the Searchable interface
SearchIndex::removeFromIndex($product)
###Clear the entire index If only you could to this with your facebook account.
SearchIndex::clearIndex();
###Perform a search on the index You can retrieve search results with this method:
SearchIndex::getResults($query);
$query
should be an array that adheres to the scheme provided by the elasticsearch documentation.
A query to perform a fuzzy like search that operates all fields of the index could look like this:
$query =
[
'body' =>
[
'from' => 0,
'size' => 500,
'query' =>
[
'fuzzy_like_this' =>
[
'_all' =>
[
'like_text' => $query,
'fuzziness' => 0.5,
],
],
],
]
];
The search results that come back are simply elasticsearch response elements serialized into an array. You can see an example of a response in the official elasticsearch documentation.
##Tests This package comes with a set of unit tests. Every time the package gets updated Travis CI will automatically run them.
You can aslo run them manually. You'll have first run composer install --dev
to install phpspec. After that's out of the way you can run the tests with vendor/bin/phpspec run
.