PHPackages                             hilsonxhero/elasticvision - 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. hilsonxhero/elasticvision

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

hilsonxhero/elasticvision
=========================

Elasticsearch driver for Laravel Scout.

2.0.1(3y ago)17511PHPPHP 8.0.\*||8.1.\*||8.2.\*

Since Jan 22Pushed 3y ago1 watchersCompare

[ Source](https://github.com/Hilsonxhero/laravel-elastic-vision)[ Packagist](https://packagist.org/packages/hilsonxhero/elasticvision)[ Docs](https://github.com/Hilsonxhero/laravel-elastic-vision)[ RSS](/packages/hilsonxhero-elasticvision/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (8)Dependencies (5)Versions (10)Used By (0)

🔭 Elastic Vision
================

[](#-elastic-vision)

[![Latest Version on Packagist](https://camo.githubusercontent.com/62f01f3357466b1c78ba2817d5f6429a67e19e1ee633696761bd9582cead8761/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f68696c736f6e786865726f2f656c6173746963766973696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/hilsonxhero/laravel-elastic-vision)

Elasticsearch driver for Laravel Scout with the power of Elasticsearch's queries.

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

[](#installation)

Via Composer

```
composer require hilsonxhero/elasticvision
```

You will need the configuration file to define your indexes:

```
php artisan vendor:publish --tag=elasticvision-config
```

Also do not forget to follow the [installation instructions for Laravel Scout](https://laravel.com/docs/scout#installation), and in your Laravel Scout config, set the driver to `elastic`.

### Configuration

[](#configuration)

You may either define the mapping for you index in the elasticvision config file:

```
return [
    'indexes' => [
        \App\Models\Product::class,
    ],
];
```

```
php artisan scout:index products
```

```
php artisan scout:import "\App\Models\Product"
```

Usage
-----

[](#usage)

In the last case you may implement the `Explored` interface and overwrite the mapping with the `mappableAs()` function.

📕 documentation
===============

[](#-documentation)

Essentially this means that it is up to you whether you like having it all together in the model, or separately in the config file.

Term query
==========

[](#term-query)

Returns documents that contain an exact term in a provided field.

You can use the term query to find documents based on a precise value such as a price, a product ID, or a username.

```
use App\Models\Post;

$posts = Post::search('lorem')
    ->filter(new Term('published', true))
    ->get();
```

Terms query
===========

[](#terms-query)

Returns documents that contain one or more exact terms in a provided field.

The terms query is the same as the term query, except you can search for multiple values. A document will match if it contains at least one of the terms. To search for documents that contain more than one matching term.

```
use App\Models\Post;

$posts = Post::search('lorem')
    ->should(new Terms('tags', ['featured'], 2))
    ->get();
```

### Top-level parameters for terms

[](#top-level-parameters-for-terms)

`field`

(Optional, object) Field you wish to search.

`boost`

(Optional, float) Floating point number used to decrease or increase the relevance scores of a query. Defaults to 1.0.

Sorting
=======

[](#sorting)

By default, your search results will be sorted by their score according to Elasticsearch. If you want to step in and influence the sorting you may do so using the default `orderBy()` function from Laravel Scout.

```
use App\Models\Post;

$results = Post::search('Self-steering')
    ->orderBy('published_at', 'desc')
    ->get();
```

Range
=====

[](#range)

Returns documents that contain terms within a provided range.

```
use App\Models\User;

$results = User::search('fugiat')->must(new Range('age',['gte' => 18, 'lte' => 35]))->get();
```

Regexp query
============

[](#regexp-query)

Returns documents that contain terms matching a [regular expression](https://en.wikipedia.org/wiki/Regular_expression).

A regular expression is a way to match patterns in data using placeholder characters, called operators. For a list of operators supported by the regexp query, see [Regular expression syntax](https://www.elastic.co/guide/en/elasticsearch/reference/current/regexp-syntax.html).

```
use App\Models\User;

$results = User::search('fugiat')->must(new RegExp('username', 'k.*y','ALL',false))->get();
```

### Top-level parameters for regexp

[](#top-level-parameters-for-regexp)

`field`

(Optional, object) Field you wish to search.

`value`

(Required, string) Regular expression for terms you wish to find in the provided ``. For a list of supported operators, see [Regular expression syntax](https://www.elastic.co/guide/en/elasticsearch/reference/current/regexp-syntax.html).

`flags`

(Optional, string) Enables optional operators for the regular expression. For valid values and more information, see [Regular expression syntax](https://www.elastic.co/guide/en/elasticsearch/reference/current/regexp-syntax.html#regexp-optional-operators).

`case_insensitive`

(Optional, Boolean) Allows case insensitive matching of the regular expression value with the indexed field values when set to true. Default is false which means the case sensitivity of matching depends on the underlying field’s mapping.

`boost`

(Optional, float) Floating point number used to decrease or increase the relevance scores of a query. Defaults to 1.0.

Wildcard query
==============

[](#wildcard-query)

Returns documents that contain terms matching a wildcard pattern.

A wildcard operator is a placeholder that matches one or more characters. For example, the \* wildcard operator matches zero or more characters. You can combine wildcard operators with other characters to create a wildcard pattern.

```
use App\Models\User;

$users = User::search()
    ->should(new Wildcard('username','ki*y'))
    ->get();
```

Match query
===========

[](#match-query)

Returns documents that match a provided text, number, date or boolean value. The provided text is analyzed before matching.

The match query is the standard query for performing a full-text search, including options for fuzzy matching.

```
use App\Models\Article;

$articles = Article::search()
    ->must(new Matching('title','ipsum'))
    ->get();
```

Match phrase prefix query
=========================

[](#match-phrase-prefix-query)

Returns documents that contain the words of a provided text, in the same order as provided. The last term of the provided text is treated as a prefix, matching any words that begin with that term.

```
use App\Models\User;

$users = User::search()
    ->should(new MatchPhrasePrefix('message','quick brown f'))
    ->get();
```

Match phrase query
==================

[](#match-phrase-query)

The `match_phrase` query analyzes the text and creates a `phrase` query out of the analyzed text. For example:

```
use App\Models\User;

$users = User::search()
    ->should(new MatchPhrase('message','this is a test'))
    ->get();
```

Nested query
============

[](#nested-query)

Wraps another query to search nested fields.

The `nested` query searches nested field objects as if they were indexed as separate documents. If an object matches the search, the `nested` query returns the root parent document.

```
use App\Models\Product;

$products = Product::search()
    ->must(new Nested('category', new Term('category.id', 2)))
    ->get();
```

```
use App\Models\Product;

$search = Product::search("lorem");

// $feature_ids = array([4 => [1,2], 5 => [1,2]])

foreach (request()->feature_id as $key => $value) {
  $query = new BoolQuery();
  $query->must(new Term('features.feature_id', $key));
  $query->must(new Terms('features.feature_value_id', $value));
  $boolQuery->add('must', new Nested('features', $query));
}

 $search->newCompound($boolQuery);

 $products = $search->paginate(15);
```

A phrase query matches terms up to a configurable slop (which defaults to 0) in any order. Transposed terms have a slop of 2.

The analyzer can be set to control which analyzer will perform the analysis process on the text. It defaults to the field explicit mapping definition, or the default search analyzer, for example:

Index settings
==============

[](#index-settings)

Most of the configuration you will be doing through the [mapping](mapping.md) of your index. However, if for example you want to define more advanced Elasticsearch settings such as [analyzers](https://www.elastic.co/guide/en/elasticsearch/reference/current/analyzer.html) or [tokenizers](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-tokenizers.html) you need to do so using index settings.

Be aware that any time you change the index settings, you need to [recreate](commands.md) the index.

To start using index settings, we will expand on the Post model with an `indexSettings` function to set an analyzer.

```
