Download the PHP package victord11/scout-elasticsearch-driver without Composer

On this page you can find all versions of the php package victord11/scout-elasticsearch-driver. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package scout-elasticsearch-driver

Scout Elasticsearch Driver

💥 Introducing a new Elasticsearch ecosystem for Laravel. 💥


Packagist Packagist


This package offers advanced functionality for searching and filtering data in Elasticsearch. Check out its features!

💥 with support PHP 8 and Laravel 10 💥

Contents

Features

Requirements

The package has been tested in the following configuration:

Installation

Use composer to install the package:

If you are using Laravel version <= 5.4 or the package discovery is disabled, add the following providers in config/app.php:

Configuration

To configure the package you need to publish settings first:

Then, set the driver setting to elastic in the config/scout.php file and configure the driver itself in the config/scout_elastic.php file. The available options are:

Option Description
client A setting hash to build Elasticsearch client. More information you can find here. By default the host is set to localhost:9200.
update_mapping The option that specifies whether to update a mapping automatically or not. By default it is set to true.
indexer Set to single for the single document indexing and to bulk for the bulk document indexing. By default is set to single.
document_refresh This option controls when updated documents appear in the search results. Can be set to 'true', 'false', 'wait_for' or null. More details about this option you can find here. By default set to null.

Note, that if you use the bulk document indexing you'll probably want to change the chunk size, you can do that in the config/scout.php file.

Index configurator

An index configurator class is used to set up settings for an Elasticsearch index. To create a new index configurator use the following artisan command:

It'll create the file MyIndexConfigurator.php in the app folder of your project. You can specify index name and settings like in the following example:

More about index settings you can find in the index management section of Elasticsearch documentation.

To create an index just run the artisan command:

Note, that every searchable model requires its own index configurator.

Indices created in Elasticsearch 6.0.0 or later may only contain a single mapping type. Indices created in 5.x with multiple mapping types will continue to function as before in Elasticsearch 6.x. Mapping types will be completely removed in Elasticsearch 7.0.0.

You can find more information here.

Searchable model

To create a model with the ability to perform search requests in an Elasticsearch index use the command:

After executing the command you'll find the file MyModel.php in you app folder:

Each searchable model represents an Elasticsearch type. By default a type name is the same as a table name, but you can set any type name you want through the searchableAs method. You can also specify fields which will be indexed by the driver through the toSearchableArray method. More information about these options you will find in the scout official documentation.

The last important option you can set in the MyModel class is the $searchRules property. It allows you to set different search algorithms for a model. We'll take a closer look at it in the search rules section.

After setting up a mapping in your model you can update an Elasticsearch type mapping:

Usage

Once you've created an index configurator, an Elasticsearch index itself and a searchable model, you are ready to go. Now you can index and search data according to the documentation.

Basic search usage example:

If you only need the number of matches for a query, use the count method:

If you need to load relations, use the with method:

In addition to standard functionality the package offers you the possibility to filter data in Elasticsearch without specifying a query string:

Also you can override model search rules:

And use variety of where conditions:

And filter out results with a score less than min_score:

And add more complex sorting (geo_distance eg.)

At last, if you want to send a custom request, you can use the searchRaw method:

This query will return raw response.

Console commands

Available artisan commands are listed below:

Command Arguments Description
make:index-configurator name - The name of the class Creates a new Elasticsearch index configurator.
make:searchable-model name - The name of the class Creates a new searchable model.
make:search-rule name - The name of the class Creates a new search rule.
elastic:create-index index-configurator - The index configurator class Creates an Elasticsearch index.
elastic:update-index index-configurator - The index configurator class Updates settings and mappings of an Elasticsearch index.
elastic:drop-index index-configurator - The index configurator class Drops an Elasticsearch index.
elastic:update-mapping model - The model class Updates a model mapping.
elastic:migrate-model model - The model class, target-index - The index name to migrate Migrates model to another index.

For detailed description and all available options run php artisan help [command] in the command line.

Search rules

A search rule is a class that describes how a search query will be executed. To create a search rule use the command:

In the file app/MySearchRule.php you will find a class definition:

You can read more about bool queries here and about highlighting here.

The default search rule returns the following payload:

This means that by default when you call search method on a model it tries to find the query string in any field.

To determine default search rules for a model just add a property:

You can also set a search rule in a query builder:

To retrieve highlight, use model highlight attribute:

Available filters

You can use different types of filters:

Method Example Description
where($field, $value) where('id', 1) Checks equality to a simple value.
where($field, $operator, $value) where('id', '>=', 1) Filters records according to a given rule. Available operators are: =, <, >, <=, >=, <>.
whereIn($field, $value) where('id', [1, 2, 3]) Checks if a value is in a set of values.
whereNotIn($field, $value) whereNotIn('id', [1, 2, 3]) Checks if a value isn't in a set of values.
whereBetween($field, $value) whereBetween('price', [100, 200]) Checks if a value is in a range.
whereNotBetween($field, $value) whereNotBetween('price', [100, 200]) Checks if a value isn't in a range.
whereExists($field) whereExists('unemployed') Checks if a value is defined.
whereNotExists($field) whereNotExists('unemployed') Checks if a value isn't defined.
whereMatch($field, $value) whereMatch('tags', 'travel') Filters records matching exact value. Here you can find more about syntax.
whereNotMatch($field, $value) whereNotMatch('tags', 'travel') Filters records not matching exact value. Here you can find more about syntax.
whereRegexp($field, $value, $flags = 'ALL') whereRegexp('name.raw', 'A.+') Filters records according to a given regular expression. Here you can find more about syntax.
whereGeoDistance($field, $value, $distance) whereGeoDistance('location', [-70, 40], '1000m') Filters records according to given point and distance from it. Here you can find more about syntax.
whereGeoBoundingBox($field, array $value) whereGeoBoundingBox('location', ['top_left' => [-74.1, 40.73], 'bottom_right' => [-71.12, 40.01]]) Filters records within given boundings. Here you can find more about syntax.
whereGeoPolygon($field, array $points) whereGeoPolygon('location', [[-70, 40],[-80, 30],[-90, 20]]) Filters records within given polygon. Here you can find more about syntax.
whereGeoShape($field, array $shape, $relation = 'INTERSECTS') whereGeoShape('shape', ['type' => 'circle', 'radius' => '1km', 'coordinates' => [4, 52]], 'WITHIN') Filters records within given shape. Here you can find more about syntax.

In most cases it's better to use raw fields to filter records, i.e. not analyzed fields.

Zero downtime migration

As you might know, you can't change the type of already created field in Elasticsearch. The only choice in such case is to create a new index with necessary mapping and import your models into the new index.
A migration can take quite a long time, so to avoid downtime during the process the driver reads from the old index and writes to the new one. As soon as migration is over it starts reading from the new index and removes the old index. This is how the artisan elastic:migrate-model command works.

Before you run the command, make sure that your index configurator uses the ScoutElastic\Migratable trait. If it's not, add the trait and run the artisan elastic:update-index command using your index configurator class name as an argument:

When you are ready, make changes in the model mapping and run the elastic:migrate-model command using the model class as the first argument and desired index name as the second argument:

Note, that if you need just to add new fields in your mapping, use the elastic:update-mapping command.

Debug

There are two methods that can help you to analyze results of a search query:

Both methods return raw data from ES.

Besides, you can get a query payload that will be sent to ES, by calling the buildPayload method.

Note, that this method returns a collection of payloads, because of possibility of using multiple search rules in one query.

Alternatives

Recently I've released a new Elasticsearch ecosystem for Laravel, it includes:

If any of it sounds interesting to you and you want to get more details, please read The Ultimate Guide to Elasticsearch in Laravel Application. The article makes a good overview of the mentioned packages and provides usage examples.

FAQ:


All versions of scout-elasticsearch-driver with dependencies

PHP Build Version
Package Version
Requires php Version ^7.1|^8.0.2
elasticsearch/elasticsearch Version >=7.0 <=7.11.0
laravel/scout Version ^7.0|^8.0|^9.0|^10.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package victord11/scout-elasticsearch-driver contains the following files

Loading the files please wait ....