PHPackages                             amrsoliman/laralastica - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Search &amp; Filtering](/categories/search)
4. /
5. amrsoliman/laralastica

ActiveLibrary[Search &amp; Filtering](/categories/search)

amrsoliman/laralastica
======================

A laravel elastica package.

v5.1.1(9y ago)11.4k1MITPHPPHP &gt;=5.4.0

Since Jun 10Pushed 9y ago1 watchersCompare

[ Source](https://github.com/AmrSoliman/laralastica)[ Packagist](https://packagist.org/packages/amrsoliman/laralastica)[ RSS](/packages/amrsoliman-laralastica/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (4)Versions (30)Used By (0)

Laralastica [![Build Status](https://camo.githubusercontent.com/e5e0437568cf004df8f54df837518e018537a84260dfd64522caf636a1cf264f/68747470733a2f2f7472617669732d63692e6f72672f616d72736f6c696d616e2f6c6172616c6173746963612e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/michaeljennings/laralastica) [![Latest Stable Version](https://camo.githubusercontent.com/3a1d03ace4ada0b693ba037c005c166958dbb2bbe5b8fa961866233e70b334da/68747470733a2f2f706f7365722e707567782e6f72672f6d69636861656c6a656e6e696e67732f6c6172616c6173746963612f762f737461626c65)](https://packagist.org/packages/michaeljennings/laralastica) [![Latest Unstable Version](https://camo.githubusercontent.com/fbfa8bfb39fee35cb579f3ef10d500a4aac614f5528b05012ffb03bba85287e7/68747470733a2f2f706f7365722e707567782e6f72672f6d69636861656c6a656e6e696e67732f6c6172616c6173746963612f762f756e737461626c65)](https://packagist.org/packages/michaeljennings/laralastica) [![License](https://camo.githubusercontent.com/27fb47289b8b071b12f2c02b373dbdbc7d86f44853fb2359b8cd97b39af50b07/68747470733a2f2f706f7365722e707567782e6f72672f6d69636861656c6a656e6e696e67732f6c6172616c6173746963612f6c6963656e7365)](https://packagist.org/packages/michaeljennings/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](#installation)
- [Usage](#usage)
- [Searching](#searching)
    - [Ids Query](#ids-query)
    - [Exists Query](#exists-query)
    - [Match Query](#match-query)
    - [Multi Match Query](#multi-match-query)
    - [Match All Query](#multi-all-query)
    - [Common Query](#common-query)
    - [Range Query](#range-query)
    - [Regular Expression Query](#regular-expression-query)
    - [Term Query](#term-query)
    - [Terms Query](#terms-query)
    - [Wildcard Query](#wildcard-query)

Installation
------------

[](#installation)

This package requires at least PHP 5.4 and at present only supports Laravel 5.0.

To install through composer either run `composer require michaeljennings/laralastica` or add the package to you composer.json

```
"michaeljennings/laralastica": "1.2.*"
```

Then add the laralastica service provider into your providers array in `config/app.php`.

```
'providers' => array(

	'Michaeljennings\Laralastica\LaralasticaServiceProvider'

);
```

The package also comes with a facade, to use it add it to your aliases array in `config/app.php`.

```
'aliases' => array(

  'Laralastica' => 'Michaeljennings\Laralastica\Facades\Laralastica',

);
```

### Config

[](#config)

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. To set the connection you can either pass the host and port to connect to, or alternatively you can pass a url to connect with.

```
'index' => 'yourindex',
'host' => 'localhost',
'port' => 9200,
'url' => 'https://user:pass@your-search.com/',
```

There is also an empty types array. This allows you to bind an elasticsearch type to a model so that when you search elasticsearch it will return results belonging to that type as the specified model.

```
'types' => [
	'testType' => 'App\TestType'
]
```

Usage
-----

[](#usage)

To get started using the package simply add the `Searchable` trait to the models you want to index and search.

```
use Illuminate\Database\Eloquent\Model;
use Michaeljennings\Laralastica\Searchable;

class Foo extends Model {

	use Searchable;

}
```

Once you have added the trait it will use [model events](http://laravel.com/docs/5.0/eloquent#model-events) to watch when a model is saved, deleted or restored and will add or delete the elasticsearch document as appropriate.

### Set Elasticsearch Type

[](#set-elasticsearch-type)

To set the elasticsearch type for the model use the `getSearchType` method to return the name of the type. By default this will return the table name the model is using.

```
public function getSearchType()
{
	return 'foo';
}
```

### Set Elasticsearch Key To Index By

[](#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.

```
public function getSearchKey()
{
	return $this->key;
}
```

### Set the Attributes to Index

[](#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.

```
public function getIndexableAttributes()
{
	return [
		'foo' => $this->bar,
	];
}
```

### Type Cast Attributes

[](#type-cast-attributes)

When you index attributes you may need to type cast the value, to do this use the `getSearchDataTypes` method. This must return an array with the key as the column being indexed and the value as the data type. The data types supported are:

- int
- string
- float
- bool

```
public function getSearchDataTypes()
{
	return [
		'price' => 'float',
		'active' => 'bool',
		'quantity' => 'int',
		'name' => 'string'
	];
}
```

Searching
---------

[](#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 paramater is the column for the where in query, this defaults to 'id'.

The third parameter is a key to group the elasticsearch results, the grouped results are then passed to the where in query. This also defaults 'id'.

```
Foo::search(function(Builder $query) {

	$query->matchAll();

}, 'foo', 'bar')->get();
```

You can also set whether the query must, should or must not match the value you are searching for.

```
Foo::search(function(Builder $query) {

	$query->match('foo', 'bar')->must();
	$query->term('bar', 'baz')->should();
	$query->wildcard('baz', 'qux*')->mustNot();

})->get();
```

You may also chain any Laravel query builder methods before or after searching.

```
Foo::where('foo', 'bar')->search(function(Builder $query) {

	$query->match('foo', 'bar');

})->orderBy('baz')->get();
```

### Searching Without the Searchable Trait

[](#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 or use the provided Facade.

```
class Foo {
	public function __construct(Michaeljennings\Laralastica\Contracts\Wrapper $laralastica)
	{
		$this->laralastica = $laralastica;
	}

	public function foo()
	{
		$laralastica = Laralastica::search();
		// sort search  from elasticsearch fields
		  $sort = array('created__at' => array('order' => 'asc'));
		  $laralastica->setSortFields($sort);

		$laralastica = app('laralastica');
	}
}
}
```

To run a new query use the `search` method. This takes two parameters:

- The type/types you are searching in
- The query to be run

```
$laralastica->search('foo', function($q)
{
	$q->matchAll();
});
```

To search across multiple elasticsearch types simply pass an array of types as the first parameter.

```
$laralastica->search(['foo', 'bar], function($q)
{
	$q->matchAll();
});
```

### Match Query

[](#match-query)

To run a match query call `match` on the query builder. This takes 4 parameters:

- The column to search
- The query to search for
- The type of search, defaults to phrase
- A flag for if the search should be fuzzy, by default this is false

The two types of search you can run are `phrase` and `phrase_prefix`. The phrase match analyzes the text and creates a phrase query out of the analyzed text. The phrase prefix match is the same as phrase, except that it allows for prefix matches on the last term in the text.

For more information about the search types [click here](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html).

```
Foo::search(function(Builder $query)
{
	$query->match('foo', 'bar');
	$query->match('foo', 'bar', 'phrase', false);
});
```

### Multi Match Query

[](#multi-match-query)

To run a multi match query use the `multiMatch` method on the query builder. This takes 6 parameters:

- An array of columns to search in
- The query string to search for
- The type of search, defaults to phrase
- A flag for if the search should be fuzzy, by default this is false
- The tie breaker value, only used with the best\_fields type, defaults to 0.0
- An operator, only needed for the cross\_fields type, defaults to 'and'

There are 5 different search types for the multi match: best\_fields, most\_fields, cross\_fields, phrase and phrase\_prefix.

best\_fields finds documents which match any field, but uses the \_score from the best field.

most\_fields finds documents which match any field and combines the \_score from each field.

cross\_fields treats fields with the same analyzer as though they were one big field. Looks for each word in any field.

phrase runs a match\_phrase query on each field and combines the \_score from each field.

phrase\_prefix runs a match\_phrase\_prefix query on each field and combines the \_score from each field.

For more information about the search types [click here](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html).

```
Foo::search(function(Builder $query)
{
	$query->multiMatch(['foo', 'bar'], 'The Quick Brown Fox');
	$query->multiMatch(['foo', 'bar'], 'The Quick Brown Fox', 'phrase', true);
	$query->multiMatch(['foo', 'bar'], 'The Quick Brown Fox', 'best_fields', true, 0.5);
	$query->multiMatch(['foo', 'bar'], 'The Quick Brown Fox', 'cross_fields', true, 0.0, 'or');
});
```

### Match All Query

[](#match-all-query)

To use a match all query use the `matchAll` method on the query builder.

```
Foo::search(function(Builder $query)
{
	$query->matchAll();
})
```

### Exists query

[](#exists-query)

To run a exists query use the `exists` method. This takes 1 parameter:

- Field name

For more information about the term query [click here](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html).

```
Foo::search(function(Builder $query)
{
    $query->exists('FieldName');
});
```

### Common Query

[](#common-query)

To run a common query use the `common` method on the query builder. This takes 4 parameters:

- The column to search
- The query string to search for
- The cut off, defaults to 0.001
- A flag stating if a minimum match should be allowed, defaults to false

For more information about the common query [click here](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-common-terms-query.html).

```
Foo::search(function(Builder $query)
{
	$query->common('foo', 'bar');
	$query->common('foo', 'bar', 0.001-, true);
})
```

### Range Query

[](#range-query)

To run a range query use the `range` method on the query builder. This takes 4 parameters:

- The column to search in
- The range to search in
- If you are searching for a date you can specify a timezone
- If you are searching for dates you can specify a date format.

To specify a range you pass an array which gt, gte, lt or lte as keys. So to get any values greater than 3 and less than 10 you would do the following.

```
Foo::search(function(Builder $query)
{
	$range = [
		'gt' => 3,
		'lt' => 10
	];

	$query->range('foo', $range);
});
```

To search for dates between the 1st January 1970 and 31st January 1970 you would do the following.

```
Foo::search(function(Builder $query)
{
	$range => [
		'gte' => '1970-01-01',
		'lte' => '1970-01-31'
	];

	$query->range('foo', $range, '+1:00', 'yyyy-mm-dd');
});
```

### Regular Expression Query

[](#regular-expression-query)

To run a regular expression query use the `regexp` method. This takes 2 parameters:

- The column being searched
- The regular expression to search for

For more information about the term query [click here](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html).

```
Foo::search(function(Builder $query)
{
    $query->regexp('foo', 'b.*r');
});
```

### Term query

[](#term-query)

To run a term query use the `term` method. This takes 3 parameters:

- The column to search
- The term to search for
- Set the boost to search by, defaults to 1.0

For more information about the term query [click here](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html).

```
Foo::search(function(Builder $query)
{
    $query->term('foo', 'bar', 2.0);
});
```

### Terms Query

[](#terms-query)

To run a terms query use the `terms` method. This takes 3 parameters:

- The column to search in
- An array of terms
- The minimum amount of terms to match, defaults to false

For more information about the terms query [click here](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html).

```
Foo::search(function(Builder $query)
{
    $query->terms('foo', ['foo', 'bar', 'baz'], 2);
});
```

### Wildcard Query

[](#wildcard-query)

To run a wildcard query use the `wildcard` method. This takes 2 parameters:

- The column being searched
- The value to search for

For more information about the wildcard query [click here](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html).

```
Foo::search(function(Builder $query)
{
    $query->wildcard('foo', 'ba*');
});
```

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 87.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~22 days

Recently: every ~85 days

Total

29

Last Release

3402d ago

Major Versions

v0.1.1 → v1.02015-06-11

v1.3.4 → 5.1.02016-04-17

v1.3.5 → v5.1.12017-03-07

### Community

Maintainers

![](https://www.gravatar.com/avatar/6a7c17b25848bf9ad789ff5115159cd996cba6d0ed36f5fdbbcebe05c81beddc?d=identicon)[AmrSoliman](/maintainers/AmrSoliman)

---

Top Contributors

[![michaeljennings](https://avatars.githubusercontent.com/u/5189701?v=4)](https://github.com/michaeljennings "michaeljennings (123 commits)")[![AmrSoliman](https://avatars.githubusercontent.com/u/3324468?v=4)](https://github.com/AmrSoliman "AmrSoliman (15 commits)")[![ceesvanegmond](https://avatars.githubusercontent.com/u/883497?v=4)](https://github.com/ceesvanegmond "ceesvanegmond (1 commits)")[![threesquared](https://avatars.githubusercontent.com/u/892142?v=4)](https://github.com/threesquared "threesquared (1 commits)")

---

Tags

laravelelasticsearchelastica

### Embed Badge

![Health badge](/badges/amrsoliman-laralastica/health.svg)

```
[![Health](https://phpackages.com/badges/amrsoliman-laralastica/health.svg)](https://phpackages.com/packages/amrsoliman-laralastica)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3345.1M337](/packages/psalm-plugin-laravel)[watson/validating

Eloquent model validating trait.

9733.4M53](/packages/watson-validating)[jolicode/elastically

Opinionated Elastica based framework to bootstrap PHP and Elasticsearch implementations.

2581.8M1](/packages/jolicode-elastically)[michaeljennings/laralastica

A laravel package that allows you to search eloquent results using elasticsearch.

102.9k](/packages/michaeljennings-laralastica)[baijunyao/laravel-scout-elasticsearch

Elasticsearch Driver for Laravel Scout

8023.7k1](/packages/baijunyao-laravel-scout-elasticsearch)[heyday/silverstripe-elastica

Provides Elastic Search integration for SilverStripe DataObjects using Elastica

1137.9k2](/packages/heyday-silverstripe-elastica)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
